xref: /haiku/src/preferences/sounds/HEventList.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
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 	{
97 		HEventItem *item = cast_as(ItemAt(sel),HEventItem);
98 		if(!item)
99 			return;
100 
101 		entry_ref ref;
102 		BMediaFiles().GetRefFor(fType, item->Name(), &ref);
103 
104 		BPath path(&ref);
105 		if((path.InitCheck()==B_OK) || (ref.name == NULL) || (strcmp(ref.name, "")==0)) {
106 			item->SetPath(path.Path());
107 			InvalidateItem(sel);
108 		} else {
109 			printf("name %s\n", ref.name);
110 			BMediaFiles().RemoveRefFor(fType, item->Name(), ref);
111 			(new BAlert("alert", "No such file or directory", "Ok"))->Go();
112 			return;
113 		}
114 		BMessage msg(M_EVENT_CHANGED);
115 		msg.AddString("name",item->Name());
116 
117 		msg.AddString("path",item->Path());
118 		Window()->PostMessage(&msg);
119 	}
120 }
121 
122 /***********************************************************
123  * SetPath
124  ***********************************************************/
125 void
126 HEventList::SetPath(const char* path)
127 {
128 	int32 sel = CurrentSelection();
129 	if(sel >= 0) {
130 		HEventItem *item = cast_as(ItemAt(sel),HEventItem);
131 		if(!item)
132 			return;
133 
134 		entry_ref ref;
135 		BEntry entry(path);
136 		entry.GetRef(&ref);
137 		BMediaFiles().SetRefFor(fType, item->Name(), ref);
138 
139 		item->SetPath(path);
140 		InvalidateItem(sel);
141 	}
142 }
143