usb_ecm: Use the current configuration instead of a fixed one.
[haiku.git] / src / preferences / sounds / HEventList.cpp
blob5e7edd04f4b77242d5f4528fe50c3a37a751c5ba
1 /*
2 * Copyright 2003-2008 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Jérôme Duval
7 * Oliver Ruiz Dorantes
8 * Atsushi Takamatsu
9 */
10 #include "HEventList.h"
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>
22 #undef B_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_CONTEXT "HEventList"
26 HEventRow::HEventRow(const char* name, const char* path)
28 BRow(),
29 fName(name)
31 SetField(new BStringField(name), kEventColumn);
32 SetPath(path);
36 HEventRow::~HEventRow()
41 void
42 HEventRow::SetPath(const char* _path)
44 fPath = _path;
45 BPath path(_path);
46 SetField(new BStringField(_path ? path.Leaf() : B_TRANSLATE("<none>")),
47 kSoundColumn);
51 void
52 HEventRow::Remove(const char* type)
54 BMediaFiles().RemoveItem(type, Name());
58 HEventList::HEventList(const char* name)
60 BColumnListView(name, B_NAVIGABLE, B_PLAIN_BORDER, true),
61 fType(NULL)
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);
70 HEventList::~HEventList()
72 RemoveAll();
73 delete fType;
77 void
78 HEventList::SetType(const char* type)
80 RemoveAll();
81 BMediaFiles mfiles;
82 mfiles.RewindRefs(type);
83 delete fType;
84 fType = strdup(type);
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()));
97 ResizeAllColumnsToPreferred();
101 void
102 HEventList::RemoveAll()
104 BRow* row;
105 while ((row = RowAt((int32)0, NULL)) != NULL) {
106 RemoveRow(row);
107 delete row;
112 void
113 HEventList::SelectionChanged()
115 BColumnListView::SelectionChanged();
117 HEventRow* row = (HEventRow*)CurrentSelection();
118 if (row != NULL) {
119 entry_ref ref;
120 BMediaFiles().GetRefFor(fType, row->Name(), &ref);
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;
136 BMessage msg(M_EVENT_CHANGED);
137 msg.AddString("name", row->Name());
138 msg.AddString("path", row->Path());
139 Window()->PostMessage(&msg);
144 void
145 HEventList::SetPath(const char* path)
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);
154 row->SetPath(path);
155 UpdateRow(row);