xref: /haiku/src/preferences/sounds/HEventList.cpp (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
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_TRANSLATE_CONTEXT
23 #define B_TRANSLATE_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, 0, 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 	delete fType;
74 }
75 
76 
77 void
78 HEventList::SetType(const char* type)
79 {
80 	RemoveAll();
81 	BMediaFiles mfiles;
82 	mfiles.RewindRefs(type);
83 	delete 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 
98 
99 void
100 HEventList::RemoveAll()
101 {
102 	BRow* row;
103 	while ((row = RowAt((int32)0, NULL)) != NULL) {
104 		RemoveRow(row);
105 		delete row;
106 	}
107 }
108 
109 
110 void
111 HEventList::SelectionChanged()
112 {
113 	BColumnListView::SelectionChanged();
114 
115 	HEventRow* row = (HEventRow*)CurrentSelection();
116 	if (row != NULL) {
117 		entry_ref ref;
118 		BMediaFiles().GetRefFor(fType, row->Name(), &ref);
119 
120 		BPath path(&ref);
121 		if (path.InitCheck() == B_OK || ref.name == NULL
122 			|| strcmp(ref.name, "") == 0) {
123 			row->SetPath(path.Path());
124 			UpdateRow(row);
125 		} else {
126 			printf("name %s\n", ref.name);
127 			BMediaFiles().RemoveRefFor(fType, row->Name(), ref);
128 			BAlert* alert = new BAlert("alert",
129 				B_TRANSLATE("No such file or directory"), B_TRANSLATE("OK"));
130 			alert->Go();
131 			return;
132 		}
133 		BMessage msg(M_EVENT_CHANGED);
134 		msg.AddString("name", row->Name());
135 		msg.AddString("path", row->Path());
136 		Window()->PostMessage(&msg);
137 	}
138 }
139 
140 
141 void
142 HEventList::SetPath(const char* path)
143 {
144 	HEventRow* row = (HEventRow*)CurrentSelection();
145 	if (row != NULL) {
146 		entry_ref ref;
147 		BEntry entry(path);
148 		entry.GetRef(&ref);
149 		BMediaFiles().SetRefFor(fType, row->Name(), ref);
150 
151 		row->SetPath(path);
152 		UpdateRow(row);
153 	}
154 }
155