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