xref: /haiku/src/preferences/sounds/HEventList.cpp (revision 1a3518cf757c2da8006753f83962da5935bbc82b)
1 /*
2  * Copyright 2003-2008 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Jérôme Duval
7  *		Oliver Ruiz Dorantes
8  *		Atsushi Takamatsu
9  */
10 #include "HEventList.h"
11 
12 #include <Alert.h>
13 #include <Catalog.h>
14 #include <ColumnTypes.h>
15 #include <Entry.h>
16 #include <Locale.h>
17 #include <MediaFiles.h>
18 #include <Path.h>
19 #include <stdio.h>
20 
21 
22 #undef B_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_CONTEXT "HEventList"
24 
25 
26 HEventRow::HEventRow(const char* name, const char* path)
27 	:
28 	BRow(),
29 	fName(name)
30 {
31 	SetField(new BStringField(name), kEventColumn);
32 	SetPath(path);
33 }
34 
35 
36 HEventRow::~HEventRow()
37 {
38 }
39 
40 
41 void
42 HEventRow::SetPath(const char* _path)
43 {
44 	fPath = _path;
45 	BPath path(_path);
46 	SetField(new BStringField(_path ? path.Leaf() : B_TRANSLATE("<none>")),
47 		kSoundColumn);
48 }
49 
50 
51 void
52 HEventRow::Remove(const char* type)
53 {
54 	BMediaFiles().RemoveItem(type, Name());
55 }
56 
57 
58 HEventList::HEventList(const char* name)
59 	:
60 	BColumnListView(name, B_NAVIGABLE, B_PLAIN_BORDER, true),
61 	fType(NULL)
62 {
63 	AddColumn(new BStringColumn(B_TRANSLATE("Event"), 180, 50, 500,
64 		B_TRUNCATE_MIDDLE), kEventColumn);
65 	AddColumn(new BStringColumn(B_TRANSLATE("Sound"), 130, 50, 500,
66 		B_TRUNCATE_END), kSoundColumn);
67 }
68 
69 
70 HEventList::~HEventList()
71 {
72 	RemoveAll();
73 	free(fType);
74 }
75 
76 
77 void
78 HEventList::SetType(const char* type)
79 {
80 	RemoveAll();
81 	BMediaFiles mfiles;
82 	mfiles.RewindRefs(type);
83 	free(fType);
84 	fType = strdup(type);
85 
86 	BString name;
87 	entry_ref ref;
88 	while (mfiles.GetNextRef(&name,&ref) == B_OK) {
89 		BPath path(&ref);
90 		if (path.InitCheck() != B_OK || ref.name == NULL
91 			|| strcmp(ref.name, "") == 0)
92 			AddRow(new HEventRow(name.String(), NULL));
93 		else
94 			AddRow(new HEventRow(name.String(), path.Path()));
95 	}
96 
97 	ResizeAllColumnsToPreferred();
98 }
99 
100 
101 void
102 HEventList::RemoveAll()
103 {
104 	BRow* row;
105 	while ((row = RowAt((int32)0, NULL)) != NULL) {
106 		RemoveRow(row);
107 		delete row;
108 	}
109 }
110 
111 
112 void
113 HEventList::SelectionChanged()
114 {
115 	BColumnListView::SelectionChanged();
116 
117 	HEventRow* row = (HEventRow*)CurrentSelection();
118 	if (row != NULL) {
119 		entry_ref ref;
120 		BMediaFiles().GetRefFor(fType, row->Name(), &ref);
121 
122 		BPath path(&ref);
123 		if (path.InitCheck() == B_OK || ref.name == NULL
124 			|| strcmp(ref.name, "") == 0) {
125 			row->SetPath(path.Path());
126 			UpdateRow(row);
127 		} else {
128 			printf("name %s\n", ref.name);
129 			BMediaFiles().RemoveRefFor(fType, row->Name(), ref);
130 			BAlert* alert = new BAlert("alert",
131 				B_TRANSLATE("No such file or directory"), B_TRANSLATE("OK"));
132 			alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
133 			alert->Go();
134 			return;
135 		}
136 		BMessage msg(M_EVENT_CHANGED);
137 		msg.AddString("name", row->Name());
138 		msg.AddString("path", row->Path());
139 		Window()->PostMessage(&msg);
140 	}
141 }
142 
143 
144 void
145 HEventList::SetPath(const char* path)
146 {
147 	HEventRow* row = (HEventRow*)CurrentSelection();
148 	if (row != NULL) {
149 		entry_ref ref;
150 		BEntry entry(path);
151 		entry.GetRef(&ref);
152 		BMediaFiles().SetRefFor(fType, row->Name(), ref);
153 
154 		row->SetPath(path);
155 		UpdateRow(row);
156 	}
157 }
158