xref: /haiku/src/preferences/sounds/HWindow.cpp (revision 23d878482ed22e55dad6d1fca1df7bea42eb157c)
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 
11 
12 #include "HWindow.h"
13 #include "HEventList.h"
14 
15 #include <stdio.h>
16 
17 #include <Alert.h>
18 #include <Application.h>
19 #include <Beep.h>
20 #include <Box.h>
21 #include <Button.h>
22 #include <Catalog.h>
23 #include <ControlLook.h>
24 #include <FindDirectory.h>
25 #include <fs_attr.h>
26 #include <GroupLayoutBuilder.h>
27 #include <Locale.h>
28 #include <MediaFiles.h>
29 #include <MenuBar.h>
30 #include <MenuField.h>
31 #include <MenuItem.h>
32 #include <Node.h>
33 #include <NodeInfo.h>
34 #include <Path.h>
35 #include <Roster.h>
36 #include <ScrollView.h>
37 #include <StringView.h>
38 #include <Sound.h>
39 
40 
41 #undef B_TRANSLATION_CONTEXT
42 #define B_TRANSLATION_CONTEXT "HWindow"
43 
44 static const char kSettingsFile[] = "Sounds_Settings";
45 
46 
47 HWindow::HWindow(BRect rect, const char* name)
48 	:
49 	BWindow(rect, name, B_TITLED_WINDOW, B_AUTO_UPDATE_SIZE_LIMITS),
50 	fFilePanel(NULL),
51 	fPlayer(NULL)
52 {
53 	InitGUI();
54 
55 	fFilePanel = new BFilePanel();
56 	fFilePanel->SetTarget(this);
57 
58 	BPath path;
59 	BMessage msg;
60 
61 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
62 		path.Append(kSettingsFile);
63 		BFile file(path.Path(), B_READ_ONLY);
64 
65 		if (file.InitCheck() == B_OK && msg.Unflatten(&file) == B_OK
66 			&& msg.FindRect("frame", &fFrame) == B_OK) {
67 			MoveTo(fFrame.LeftTop());
68 			ResizeTo(fFrame.Width(), fFrame.Height());
69 		}
70 	}
71 }
72 
73 
74 HWindow::~HWindow()
75 {
76 	delete fFilePanel;
77 	delete fPlayer;
78 
79 	BPath path;
80 	BMessage msg;
81 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
82 		path.Append(kSettingsFile);
83 		BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE);
84 
85 		if (file.InitCheck() == B_OK) {
86 			msg.AddRect("frame", fFrame);
87 			msg.Flatten(&file);
88 		}
89 	}
90 }
91 
92 
93 void
94 HWindow::InitGUI()
95 {
96 	fEventList = new HEventList();
97 	fEventList->SetType(BMediaFiles::B_SOUNDS);
98 	fEventList->SetSelectionMode(B_SINGLE_SELECTION_LIST);
99 
100 	BGroupView* view = new BGroupView();
101 	BBox* box = new BBox("", B_WILL_DRAW | B_FRAME_EVENTS
102 		| B_NAVIGABLE_JUMP | B_PULSE_NEEDED);
103 
104 	BMenu* menu = new BMenu("file");
105 	menu->SetRadioMode(true);
106 	menu->SetLabelFromMarked(true);
107 	menu->AddSeparatorItem();
108 	menu->AddItem(new BMenuItem(B_TRANSLATE("<none>"),
109 		new BMessage(M_NONE_MESSAGE)));
110 	menu->AddItem(new BMenuItem(B_TRANSLATE("Other" B_UTF8_ELLIPSIS),
111 		new BMessage(M_OTHER_MESSAGE)));
112 	BMenuField* menuField = new BMenuField("filemenu",
113 		B_TRANSLATE("Sound File:"), menu);
114 	menuField->SetDivider(menuField->StringWidth(B_TRANSLATE("Sound File:"))
115 		+ 10);
116 
117 	BButton* stopbutton = new BButton("stop", B_TRANSLATE("Stop"),
118 		new BMessage(M_STOP_MESSAGE));
119 	stopbutton->SetEnabled(false);
120 
121 	BButton* playbutton = new BButton("play", B_TRANSLATE("Play"),
122 		new BMessage(M_PLAY_MESSAGE));
123 	playbutton->SetEnabled(false);
124 
125 	const float kInset = be_control_look->DefaultItemSpacing();
126 	view->SetLayout(new BGroupLayout(B_HORIZONTAL));
127 	view->AddChild(BGroupLayoutBuilder(B_VERTICAL, kInset)
128 		.AddGroup(B_HORIZONTAL)
129 			.Add(menuField)
130 			.AddGlue()
131 		.End()
132 		.AddGroup(B_HORIZONTAL, kInset)
133 			.AddGlue()
134 			.Add(playbutton)
135 			.Add(stopbutton)
136 		.End()
137 		.SetInsets(kInset, kInset, kInset, kInset)
138 	);
139 
140 	box->AddChild(view);
141 
142 	SetLayout(new BGroupLayout(B_HORIZONTAL));
143 	AddChild(BGroupLayoutBuilder(B_VERTICAL)
144 		.AddGroup(B_VERTICAL, kInset)
145 			.Add(fEventList)
146 			.Add(box)
147 		.End()
148 		.SetInsets(kInset, kInset, kInset, kInset)
149 	);
150 
151 	// setup file menu
152 	SetupMenuField();
153 	BMenuItem* noneItem = menu->FindItem(B_TRANSLATE("<none>"));
154 	if (noneItem != NULL)
155 		noneItem->SetMarked(true);
156 }
157 
158 
159 void
160 HWindow::MessageReceived(BMessage* message)
161 {
162 	switch (message->what) {
163 		case M_OTHER_MESSAGE:
164 		{
165 			BMenuField* menufield
166 				= dynamic_cast<BMenuField*>(FindView("filemenu"));
167 			if (menufield == NULL)
168 				return;
169 			BMenu* menu = menufield->Menu();
170 
171 			HEventRow* row = (HEventRow*)fEventList->CurrentSelection();
172 			if (row != NULL) {
173 				BPath path(row->Path());
174 				if (path.InitCheck() != B_OK) {
175 					BMenuItem* item = menu->FindItem(B_TRANSLATE("<none>"));
176 					if (item != NULL)
177 						item->SetMarked(true);
178 				} else {
179 					BMenuItem* item = menu->FindItem(path.Leaf());
180 					if (item != NULL)
181 						item->SetMarked(true);
182 				}
183 			}
184 			fFilePanel->Show();
185 			break;
186 		}
187 
188 		case B_SIMPLE_DATA:
189 		case B_REFS_RECEIVED:
190 		{
191 			entry_ref ref;
192 			HEventRow* row = (HEventRow*)fEventList->CurrentSelection();
193 			if (message->FindRef("refs", &ref) == B_OK && row != NULL) {
194 				BMenuField* menufield
195 					= dynamic_cast<BMenuField*>(FindView("filemenu"));
196 				if (menufield == NULL)
197 					return;
198 				BMenu* menu = menufield->Menu();
199 
200 				// check audio file
201 				BNode node(&ref);
202 				BNodeInfo ninfo(&node);
203 				char type[B_MIME_TYPE_LENGTH + 1];
204 				ninfo.GetType(type);
205 				BMimeType mtype(type);
206 				BMimeType superType;
207 				mtype.GetSupertype(&superType);
208 				if (superType.Type() == NULL
209 					|| strcmp(superType.Type(), "audio") != 0) {
210 					beep();
211 					BAlert* alert = new BAlert("",
212 						B_TRANSLATE("This is not an audio file."),
213 						B_TRANSLATE("OK"), NULL, NULL,
214 						B_WIDTH_AS_USUAL, B_STOP_ALERT);
215 					alert->Go();
216 					break;
217 				}
218 
219 				// add file item
220 				BMessage* msg = new BMessage(M_ITEM_MESSAGE);
221 				BPath path(&ref);
222 				msg->AddRef("refs", &ref);
223 				BMenuItem* menuitem = menu->FindItem(path.Leaf());
224 				if (menuitem == NULL)
225 					menu->AddItem(menuitem = new BMenuItem(path.Leaf(), msg), 0);
226 				// refresh item
227 				fEventList->SetPath(BPath(&ref).Path());
228 				// check file menu
229 				if (menuitem != NULL)
230 					menuitem->SetMarked(true);
231 			}
232 			break;
233 		}
234 
235 		case M_PLAY_MESSAGE:
236 		{
237 			HEventRow* row = (HEventRow*)fEventList->CurrentSelection();
238 			if (row != NULL) {
239 				const char* path = row->Path();
240 				if (path != NULL) {
241 					entry_ref ref;
242 					::get_ref_for_path(path, &ref);
243 					delete fPlayer;
244 					fPlayer = new BFileGameSound(&ref, false);
245 					fPlayer->StartPlaying();
246 				}
247 			}
248 			break;
249 		}
250 
251 		case M_STOP_MESSAGE:
252 		{
253 			if (fPlayer == NULL)
254 				break;
255 			if (fPlayer->IsPlaying()) {
256 				fPlayer->StopPlaying();
257 				delete fPlayer;
258 				fPlayer = NULL;
259 			}
260 			break;
261 		}
262 
263 		case M_EVENT_CHANGED:
264 		{
265 			const char* path;
266 			BMenuField* menufield
267 				= dynamic_cast<BMenuField*>(FindView("filemenu"));
268 			if (menufield == NULL)
269 				return;
270 			BMenu* menu = menufield->Menu();
271 
272 			if (message->FindString("path", &path) == B_OK) {
273 				BPath path(path);
274 				if (path.InitCheck() != B_OK) {
275 					BMenuItem* item = menu->FindItem(B_TRANSLATE("<none>"));
276 					if (item != NULL)
277 						item->SetMarked(true);
278 				} else {
279 					BMenuItem* item = menu->FindItem(path.Leaf());
280 					if (item != NULL)
281 						item->SetMarked(true);
282 				}
283 			}
284 			break;
285 		}
286 
287 		case M_ITEM_MESSAGE:
288 		{
289 			entry_ref ref;
290 			if (message->FindRef("refs", &ref) == B_OK)
291 				fEventList->SetPath(BPath(&ref).Path());
292 			break;
293 		}
294 
295 		case M_NONE_MESSAGE:
296 		{
297 			fEventList->SetPath(NULL);
298 			break;
299 		}
300 
301 		default:
302 			BWindow::MessageReceived(message);
303 	}
304 }
305 
306 
307 void
308 HWindow::SetupMenuField()
309 {
310 	BMenuField* menufield = dynamic_cast<BMenuField*>(FindView("filemenu"));
311 	if (menufield == NULL)
312 		return;
313 	BMenu* menu = menufield->Menu();
314 	int32 count = fEventList->CountRows();
315 	for (int32 i = 0; i < count; i++) {
316 		HEventRow* row = (HEventRow*)fEventList->RowAt(i);
317 		if (row == NULL)
318 			continue;
319 
320 		BPath path(row->Path());
321 		if (path.InitCheck() != B_OK)
322 			continue;
323 		if (menu->FindItem(path.Leaf()))
324 			continue;
325 
326 		BMessage* msg = new BMessage(M_ITEM_MESSAGE);
327 		entry_ref ref;
328 		::get_ref_for_path(path.Path(), &ref);
329 		msg->AddRef("refs", &ref);
330 		menu->AddItem(new BMenuItem(path.Leaf(), msg), 0);
331 	}
332 
333 	BPath path;
334 	BDirectory dir;
335 	BEntry entry;
336 	BPath item_path;
337 
338 	status_t err = find_directory(B_BEOS_SOUNDS_DIRECTORY, &path);
339 	if (err == B_OK)
340 		err = dir.SetTo(path.Path());
341 	while (err == B_OK) {
342 		err = dir.GetNextEntry(&entry, true);
343 		if (entry.InitCheck() != B_NO_ERROR)
344 			break;
345 
346 		entry.GetPath(&item_path);
347 
348 		if (menu->FindItem(item_path.Leaf()))
349 			continue;
350 
351 		BMessage* msg = new BMessage(M_ITEM_MESSAGE);
352 		entry_ref ref;
353 		::get_ref_for_path(item_path.Path(), &ref);
354 		msg->AddRef("refs", &ref);
355 		menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
356 	}
357 
358 	err = find_directory(B_USER_SOUNDS_DIRECTORY, &path);
359 	if (err == B_OK)
360 		err = dir.SetTo(path.Path());
361 	while (err == B_OK) {
362 		err = dir.GetNextEntry(&entry, true);
363 		if (entry.InitCheck() != B_NO_ERROR)
364 			break;
365 
366 		entry.GetPath(&item_path);
367 
368 		if (menu->FindItem(item_path.Leaf()))
369 			continue;
370 
371 		BMessage* msg = new BMessage(M_ITEM_MESSAGE);
372 		entry_ref ref;
373 
374 		::get_ref_for_path(item_path.Path(), &ref);
375 		msg->AddRef("refs", &ref);
376 		menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
377 	}
378 
379 	err = find_directory(B_COMMON_SOUNDS_DIRECTORY, &path);
380 	if (err == B_OK)
381 		err = dir.SetTo(path.Path());
382 	while (err == B_OK) {
383 		err = dir.GetNextEntry(&entry, true);
384 		if (entry.InitCheck() != B_NO_ERROR)
385 			break;
386 
387 		entry.GetPath(&item_path);
388 
389 		if (menu->FindItem(item_path.Leaf()))
390 			continue;
391 
392 		BMessage* msg = new BMessage(M_ITEM_MESSAGE);
393 		entry_ref ref;
394 
395 		::get_ref_for_path(item_path.Path(), &ref);
396 		msg->AddRef("refs", &ref);
397 		menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
398 	}
399 
400 }
401 
402 
403 void
404 HWindow::Pulse()
405 {
406 	HEventRow* row = (HEventRow*)fEventList->CurrentSelection();
407 	BMenuField* menufield = dynamic_cast<BMenuField*>(FindView("filemenu"));
408 	BButton* button = dynamic_cast<BButton*>(FindView("play"));
409 	BButton* stop = dynamic_cast<BButton*>(FindView("stop"));
410 
411 	if (menufield == NULL || button == NULL || stop == NULL)
412 		return;
413 
414 	if (row != NULL) {
415 		menufield->SetEnabled(true);
416 
417 		const char* path = row->Path();
418 		if (path != NULL && strcmp(path, ""))
419 			button->SetEnabled(true);
420 		else
421 			button->SetEnabled(false);
422 	} else {
423 		menufield->SetEnabled(false);
424 		button->SetEnabled(false);
425 	}
426 
427 	if (fPlayer != NULL) {
428 		if (fPlayer->IsPlaying())
429 			stop->SetEnabled(true);
430 		else
431 			stop->SetEnabled(false);
432 	} else
433 		stop->SetEnabled(false);
434 }
435 
436 
437 void
438 HWindow::DispatchMessage(BMessage* message, BHandler* handler)
439 {
440 	if (message->what == B_PULSE)
441 		Pulse();
442 	BWindow::DispatchMessage(message, handler);
443 }
444 
445 
446 bool
447 HWindow::QuitRequested()
448 {
449 	fFrame = Frame();
450 
451 	fEventList->RemoveAll();
452 	be_app->PostMessage(B_QUIT_REQUESTED);
453 	return true;
454 }
455