xref: /haiku/src/preferences/sounds/HEventList.cpp (revision 020cbad9d40235a2c50a81a42d69912a5ff8fbc4)
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 //	Copyright (c) 2003, OpenBeOS
4 //
5 //  This software is part of the OpenBeOS distribution and is covered
6 //  by the OpenBeOS license.
7 //
8 //
9 //  File:        HEventList.cpp
10 //  Author:      Jérôme Duval, Oliver Ruiz Dorantes, Atsushi Takamatsu
11 //  Description: Sounds Preferences
12 //  Created :    November 24, 2003
13 //
14 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
15 
16 #include "HEventList.h"
17 #include "HEventItem.h"
18 
19 #include "HWindow.h"
20 #include <Bitmap.h>
21 #include <ClassInfo.h>
22 #include <MediaFiles.h>
23 #include <Path.h>
24 #include <Alert.h>
25 #include <PopUpMenu.h>
26 #include <MenuField.h>
27 #include <MenuItem.h>
28 #include <stdio.h>
29 #include <Mime.h>
30 #include <Roster.h>
31 #include <NodeInfo.h>
32 
33 /***********************************************************
34  * Constructor
35  ***********************************************************/
36 HEventList::HEventList(BRect rect, const char* name)
37 	: BListView(rect, name, B_SINGLE_SELECTION_LIST,
38 			B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE),
39 		fType(NULL)
40 {
41 
42 }
43 
44 /***********************************************************
45  * Destructor
46  ***********************************************************/
47 HEventList::~HEventList()
48 {
49 	RemoveAll();
50 	delete fType;
51 }
52 
53 /***********************************************************
54  * Set type
55  ***********************************************************/
56 void
57 HEventList::SetType(const char* type)
58 {
59 	RemoveAll();
60 	BMediaFiles mfiles;
61 	mfiles.RewindRefs(type);
62 	delete fType;
63 	fType = strdup(type);
64 
65 	BString name;
66 	entry_ref ref;
67 	while (mfiles.GetNextRef(&name,&ref) == B_OK) {
68 		BPath path(&ref);
69 		AddItem(new HEventItem(name.String(), path.Path()));
70 	}
71 }
72 
73 /***********************************************************
74  * Remove all items
75  ***********************************************************/
76 void
77 HEventList::RemoveAll()
78 {
79 	BListItem *item;
80 	while ((item = RemoveItem((int32)0))!=NULL)
81 		delete item;
82 	MakeEmpty();
83 }
84 
85 
86 /***********************************************************
87  * Selection Changed
88  ***********************************************************/
89 void
90 HEventList::SelectionChanged()
91 {
92 	BListView::SelectionChanged();
93 
94 	int32 sel = CurrentSelection();
95 	if (sel >= 0) {
96 		HEventItem *item = cast_as(ItemAt(sel),HEventItem);
97 		if (!item)
98 			return;
99 
100 		entry_ref ref;
101 		BMediaFiles().GetRefFor(fType, item->Name(), &ref);
102 
103 		BPath path(&ref);
104 		if ((path.InitCheck()==B_OK) || (ref.name == NULL) || (strcmp(ref.name, "")==0)) {
105 			item->SetPath(path.Path());
106 			InvalidateItem(sel);
107 		} else {
108 			printf("name %s\n", ref.name);
109 			BMediaFiles().RemoveRefFor(fType, item->Name(), ref);
110 			(new BAlert("alert", "No such file or directory", "Ok"))->Go();
111 			return;
112 		}
113 		BMessage msg(M_EVENT_CHANGED);
114 		msg.AddString("name",item->Name());
115 
116 		msg.AddString("path",item->Path());
117 		Window()->PostMessage(&msg);
118 	}
119 }
120 
121 /***********************************************************
122  * SetPath
123  ***********************************************************/
124 void
125 HEventList::SetPath(const char* path)
126 {
127 	int32 sel = CurrentSelection();
128 	if (sel >= 0) {
129 		HEventItem *item = cast_as(ItemAt(sel),HEventItem);
130 		if (!item)
131 			return;
132 
133 		entry_ref ref;
134 		BEntry entry(path);
135 		entry.GetRef(&ref);
136 		BMediaFiles().SetRefFor(fType, item->Name(), ref);
137 
138 		item->SetPath(path);
139 		InvalidateItem(sel);
140 	}
141 }
142