xref: /haiku/src/preferences/sounds/HEventList.cpp (revision b289aaf66bbf6e173aa90fa194fc256965f1b34d)
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 <ColumnTypes.h>
14 #include <Entry.h>
15 #include <MediaFiles.h>
16 #include <Path.h>
17 #include <stdio.h>
18 
19 
20 HEventRow::HEventRow(const char* name, const char* path)
21 	:
22 	BRow(),
23 	fName(name)
24 {
25 	SetField(new BStringField(name), kEventColumn);
26 	SetPath(path);
27 }
28 
29 
30 HEventRow::~HEventRow()
31 {
32 }
33 
34 
35 void
36 HEventRow::SetPath(const char* _path)
37 {
38 	fPath = _path;
39 	BPath path(_path);
40 	SetField(new BStringField(_path ? path.Leaf() : "<none>"), kSoundColumn);
41 }
42 
43 
44 void
45 HEventRow::Remove(const char* type)
46 {
47 	BMediaFiles().RemoveItem(type, Name());
48 }
49 
50 
51 HEventList::HEventList(BRect rect, const char* name)
52 	:
53 	BColumnListView(rect, name, B_FOLLOW_ALL, 0, B_PLAIN_BORDER, true),
54 	fType(NULL)
55 {
56 	AddColumn(new BStringColumn("Event", 150, 50, 500, B_TRUNCATE_MIDDLE),
57 		kEventColumn);
58 	AddColumn(new BStringColumn("Sound", 150, 50, 500, B_TRUNCATE_END),
59 		kSoundColumn);
60 }
61 
62 
63 HEventList::~HEventList()
64 {
65 	RemoveAll();
66 	delete fType;
67 }
68 
69 
70 void
71 HEventList::SetType(const char* type)
72 {
73 	RemoveAll();
74 	BMediaFiles mfiles;
75 	mfiles.RewindRefs(type);
76 	delete fType;
77 	fType = strdup(type);
78 
79 	BString name;
80 	entry_ref ref;
81 	while (mfiles.GetNextRef(&name,&ref) == B_OK) {
82 		BPath path(&ref);
83 		if (path.InitCheck() != B_OK || ref.name == NULL
84 			|| strcmp(ref.name, "") == 0)
85 			AddRow(new HEventRow(name.String(), NULL));
86 		else
87 			AddRow(new HEventRow(name.String(), path.Path()));
88 	}
89 }
90 
91 
92 void
93 HEventList::RemoveAll()
94 {
95 	BRow* row;
96 	while ((row = RowAt((int32)0, NULL)) != NULL) {
97 		RemoveRow(row);
98 		delete row;
99 	}
100 }
101 
102 
103 void
104 HEventList::SelectionChanged()
105 {
106 	BColumnListView::SelectionChanged();
107 
108 	HEventRow* row = (HEventRow*)CurrentSelection();
109 	if (row != NULL) {
110 		entry_ref ref;
111 		BMediaFiles().GetRefFor(fType, row->Name(), &ref);
112 
113 		BPath path(&ref);
114 		if (path.InitCheck() == B_OK || ref.name == NULL
115 			|| strcmp(ref.name, "") == 0) {
116 			row->SetPath(path.Path());
117 			UpdateRow(row);
118 		} else {
119 			printf("name %s\n", ref.name);
120 			BMediaFiles().RemoveRefFor(fType, row->Name(), ref);
121 			BAlert* alert = new BAlert("alert",
122 				"No such file or directory", "OK");
123 			alert->Go();
124 			return;
125 		}
126 		BMessage msg(M_EVENT_CHANGED);
127 		msg.AddString("name", row->Name());
128 		msg.AddString("path", row->Path());
129 		Window()->PostMessage(&msg);
130 	}
131 }
132 
133 
134 void
135 HEventList::SetPath(const char* path)
136 {
137 	HEventRow* row = (HEventRow*)CurrentSelection();
138 	if (row != NULL) {
139 		entry_ref ref;
140 		BEntry entry(path);
141 		entry.GetRef(&ref);
142 		BMediaFiles().SetRefFor(fType, row->Name(), ref);
143 
144 		row->SetPath(path);
145 		UpdateRow(row);
146 	}
147 }
148