Patch from Kaoutsis: use find_directory() instead of a fixed path

added some checks, keep the style clean


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25366 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval 2008-05-08 11:12:01 +00:00
parent 52def890da
commit 31442c74a4

View File

@ -1,44 +1,41 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ /*
// * Copyright 2003-2008, Haiku, Inc. All rights reserved.
// Copyright (c) 2003, OpenBeOS * Distributed under the terms of the MIT License.
// *
// This software is part of the OpenBeOS distribution and is covered * Authors:
// by the OpenBeOS license. * Jérôme Duval
// * Oliver Ruiz Dorantes
// * Atsushi Takamatsu
// File: HWindow.cpp */
// Author: Jérôme Duval, Oliver Ruiz Dorantes, Atsushi Takamatsu
// Description: Sounds Preferences
// Created : November 24, 2003
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#include "HWindow.h" #include "HWindow.h"
#include "HEventList.h" #include "HEventList.h"
#include "HEventItem.h" #include "HEventItem.h"
#include <ScrollView.h>
#include <StringView.h> #include <stdio.h>
#include <Alert.h>
#include <Application.h>
#include <Beep.h>
#include <Box.h>
#include <Button.h>
#include <ClassInfo.h> #include <ClassInfo.h>
#include <FindDirectory.h>
#include <fs_attr.h>
#include <MediaFiles.h> #include <MediaFiles.h>
#include <MenuBar.h> #include <MenuBar.h>
#include <Box.h>
#include <MenuItem.h>
#include <MenuField.h> #include <MenuField.h>
#include <Button.h> #include <MenuItem.h>
#include <Beep.h>
#include <Path.h>
#include <Application.h>
#include <Node.h> #include <Node.h>
#include <NodeInfo.h> #include <NodeInfo.h>
#include <Alert.h> #include <Path.h>
#include <Roster.h> #include <Roster.h>
#include <fs_attr.h> #include <ScrollView.h>
#include <stdio.h> #include <StringView.h>
#include <Sound.h> #include <Sound.h>
/***********************************************************
* Constructor
***********************************************************/
HWindow::HWindow(BRect rect, const char *name) HWindow::HWindow(BRect rect, const char *name)
: _inherited(rect, name, B_TITLED_WINDOW, 0), : _inherited(rect, name, B_TITLED_WINDOW, 0),
fFilePanel(NULL), fFilePanel(NULL),
@ -57,18 +54,14 @@ HWindow::HWindow(BRect rect, const char* name)
fEventList->Select(0); fEventList->Select(0);
} }
/***********************************************************
* Destructor
***********************************************************/
HWindow::~HWindow() HWindow::~HWindow()
{ {
delete fFilePanel; delete fFilePanel;
delete fPlayer; delete fPlayer;
} }
/***********************************************************
* Initialize GUIs.
***********************************************************/
void void
HWindow::InitGUI() HWindow::InitGUI()
{ {
@ -97,12 +90,8 @@ HWindow::InitGUI()
fEventList = new HEventList(rect); fEventList = new HEventList(rect);
fEventList->SetType(BMediaFiles::B_SOUNDS); fEventList->SetType(BMediaFiles::B_SOUNDS);
BScrollView *scrollView = new BScrollView("" BScrollView *scrollView = new BScrollView("", fEventList, B_FOLLOW_ALL, 0,
,fEventList false, true);
,B_FOLLOW_ALL
,0
,false
,true);
listView->AddChild(scrollView); listView->AddChild(scrollView);
rect = Bounds(); rect = Bounds();
@ -125,19 +114,13 @@ HWindow::InitGUI()
menu->AddItem(new BMenuItem("<none>", new BMessage(M_NONE_MESSAGE))); menu->AddItem(new BMenuItem("<none>", new BMessage(M_NONE_MESSAGE)));
menu->AddItem(new BMenuItem("Other" B_UTF8_ELLIPSIS, new BMessage(M_OTHER_MESSAGE))); menu->AddItem(new BMenuItem("Other" B_UTF8_ELLIPSIS, new BMessage(M_OTHER_MESSAGE)));
BMenuField *menuField = new BMenuField(rect BMenuField *menuField = new BMenuField(rect, "filemenu", "Sound File:", menu,
,"filemenu" B_FOLLOW_TOP | B_FOLLOW_LEFT);
,"Sound File:"
,menu
,B_FOLLOW_TOP | B_FOLLOW_LEFT);
menuField->SetDivider(menuField->StringWidth("Sound File:") + 10); menuField->SetDivider(menuField->StringWidth("Sound File:") + 10);
box->AddChild(menuField); box->AddChild(menuField);
rect.OffsetBy(-2, menuField->Bounds().Height() + 15); rect.OffsetBy(-2, menuField->Bounds().Height() + 15);
BButton *button = new BButton(rect BButton *button = new BButton(rect, "stop", "Stop", new BMessage(M_STOP_MESSAGE),
,"stop" B_FOLLOW_RIGHT | B_FOLLOW_TOP);
,"Stop"
,new BMessage(M_STOP_MESSAGE)
,B_FOLLOW_RIGHT | B_FOLLOW_TOP);
button->ResizeToPreferred(); button->ResizeToPreferred();
button->SetEnabled(false); button->SetEnabled(false);
button->MoveTo(box->Bounds().right - button->Bounds().Width() - 15, rect.top); button->MoveTo(box->Bounds().right - button->Bounds().Width() - 15, rect.top);
@ -148,11 +131,8 @@ HWindow::InitGUI()
box->ResizeTo(box->Bounds().Width(), rect.bottom + 12); box->ResizeTo(box->Bounds().Width(), rect.bottom + 12);
button->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_TOP); button->SetResizingMode(B_FOLLOW_RIGHT | B_FOLLOW_TOP);
button = new BButton(rect button = new BButton(rect, "play", "Play", new BMessage(M_PLAY_MESSAGE),
,"play" B_FOLLOW_RIGHT | B_FOLLOW_TOP);
,"Play"
,new BMessage(M_PLAY_MESSAGE)
,B_FOLLOW_RIGHT | B_FOLLOW_TOP);
button->ResizeToPreferred(); button->ResizeToPreferred();
button->SetEnabled(false); button->SetEnabled(false);
button->MoveTo(rect.left - button->Bounds().Width() - 15, rect.top); button->MoveTo(rect.left - button->Bounds().Width() - 15, rect.top);
@ -169,9 +149,6 @@ HWindow::InitGUI()
} }
/***********************************************************
* MessageReceived
***********************************************************/
void void
HWindow::MessageReceived(BMessage *message) HWindow::MessageReceived(BMessage *message)
{ {
@ -198,6 +175,7 @@ HWindow::MessageReceived(BMessage *message)
fFilePanel->Show(); fFilePanel->Show();
break; break;
} }
case B_SIMPLE_DATA: case B_SIMPLE_DATA:
case B_REFS_RECEIVED: case B_REFS_RECEIVED:
{ {
@ -206,6 +184,7 @@ HWindow::MessageReceived(BMessage *message)
if (message->FindRef("refs", &ref) == B_OK && sel >= 0) { if (message->FindRef("refs", &ref) == B_OK && sel >= 0) {
BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField); BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField);
BMenu *menu = menufield->Menu(); BMenu *menu = menufield->Menu();
// check audio file // check audio file
BNode node(&ref); BNode node(&ref);
BNodeInfo ninfo(&node); BNodeInfo ninfo(&node);
@ -220,6 +199,7 @@ HWindow::MessageReceived(BMessage *message)
B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go(); B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go();
break; break;
} }
// add file item // add file item
BMessage *msg = new BMessage(M_ITEM_MESSAGE); BMessage *msg = new BMessage(M_ITEM_MESSAGE);
BPath path(&ref); BPath path(&ref);
@ -235,15 +215,14 @@ HWindow::MessageReceived(BMessage *message)
} }
break; break;
} }
case M_PLAY_MESSAGE: case M_PLAY_MESSAGE:
{ {
int32 sel = fEventList->CurrentSelection(); int32 sel = fEventList->CurrentSelection();
if (sel >= 0) if (sel >= 0) {
{
HEventItem *item = cast_as(fEventList->ItemAt(sel), HEventItem); HEventItem *item = cast_as(fEventList->ItemAt(sel), HEventItem);
const char *path = item->Path(); const char *path = item->Path();
if (path) if (path) {
{
entry_ref ref; entry_ref ref;
::get_ref_for_path(path, &ref); ::get_ref_for_path(path, &ref);
delete fPlayer; delete fPlayer;
@ -253,18 +232,19 @@ HWindow::MessageReceived(BMessage *message)
} }
break; break;
} }
case M_STOP_MESSAGE: case M_STOP_MESSAGE:
{ {
if (!fPlayer) if (!fPlayer)
break; break;
if (fPlayer->IsPlaying()) if (fPlayer->IsPlaying()) {
{
fPlayer->StopPlaying(); fPlayer->StopPlaying();
delete fPlayer; delete fPlayer;
fPlayer = NULL; fPlayer = NULL;
} }
break; break;
} }
case M_EVENT_CHANGED: case M_EVENT_CHANGED:
{ {
const char *path; const char *path;
@ -285,27 +265,27 @@ HWindow::MessageReceived(BMessage *message)
} }
break; break;
} }
case M_ITEM_MESSAGE: case M_ITEM_MESSAGE:
{ {
entry_ref ref; entry_ref ref;
if (message->FindRef("refs", &ref) == B_OK) { if (message->FindRef("refs", &ref) == B_OK)
fEventList->SetPath(BPath(&ref).Path()); fEventList->SetPath(BPath(&ref).Path());
}
break; break;
} }
case M_NONE_MESSAGE: case M_NONE_MESSAGE:
{ {
fEventList->SetPath(NULL); fEventList->SetPath(NULL);
break; break;
} }
default: default:
_inherited::MessageReceived(message); _inherited::MessageReceived(message);
} }
} }
/***********************************************************
* Init menu
***********************************************************/
void void
HWindow::SetupMenuField() HWindow::SetupMenuField()
{ {
@ -330,16 +310,19 @@ HWindow::SetupMenuField()
menu->AddItem(new BMenuItem(path.Leaf(), msg), 0); menu->AddItem(new BMenuItem(path.Leaf(), msg), 0);
} }
BPath path("/boot/beos/etc/sounds"); BPath path;
status_t err = B_OK; BDirectory dir;
BDirectory dir( path.Path() );
BEntry entry; BEntry entry;
BPath item_path; BPath item_path;
status_t err = find_directory(B_BEOS_SOUNDS_DIRECTORY, &path);
if (err == B_OK)
err = dir.SetTo(path.Path());
while (err == B_OK) { while (err == B_OK) {
err = dir.GetNextEntry((BEntry*)&entry, true); err = dir.GetNextEntry((BEntry*)&entry, true);
if (entry.InitCheck() != B_NO_ERROR) { if (entry.InitCheck() != B_NO_ERROR)
break; break;
}
entry.GetPath(&item_path); entry.GetPath(&item_path);
if (menu->FindItem(item_path.Leaf())) if (menu->FindItem(item_path.Leaf()))
@ -352,14 +335,14 @@ HWindow::SetupMenuField()
menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0); menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
} }
path.SetTo("/boot/home/config/sounds"); err = find_directory(B_USER_SOUNDS_DIRECTORY, &path);
dir.SetTo(path.Path()); if (err == B_OK)
err = B_OK; err = dir.SetTo(path.Path());
while (err == B_OK) { while (err == B_OK) {
err = dir.GetNextEntry((BEntry*)&entry, true); err = dir.GetNextEntry((BEntry*)&entry, true);
if (entry.InitCheck() != B_NO_ERROR) { if (entry.InitCheck() != B_NO_ERROR)
break; break;
}
entry.GetPath(&item_path); entry.GetPath(&item_path);
if (menu->FindItem(item_path.Leaf())) if (menu->FindItem(item_path.Leaf()))
@ -373,14 +356,14 @@ HWindow::SetupMenuField()
menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0); menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
} }
path.SetTo("/boot/home/media"); err = find_directory(B_COMMON_SOUNDS_DIRECTORY, &path);
dir.SetTo(path.Path()); if (err == B_OK)
err = B_OK; err = dir.SetTo(path.Path());
while (err == B_OK) { while (err == B_OK) {
err = dir.GetNextEntry((BEntry*)&entry, true); err = dir.GetNextEntry((BEntry*)&entry, true);
if (entry.InitCheck() != B_NO_ERROR) { if (entry.InitCheck() != B_NO_ERROR)
break; break;
}
entry.GetPath(&item_path); entry.GetPath(&item_path);
if (menu->FindItem(item_path.Leaf())) if (menu->FindItem(item_path.Leaf()))
@ -397,9 +380,6 @@ HWindow::SetupMenuField()
} }
/***********************************************************
* Pulse
***********************************************************/
void void
HWindow::Pulse() HWindow::Pulse()
{ {
@ -424,6 +404,7 @@ HWindow::Pulse()
menufield->SetEnabled(false); menufield->SetEnabled(false);
button->SetEnabled(false); button->SetEnabled(false);
} }
if (fPlayer) { if (fPlayer) {
if (fPlayer->IsPlaying()) if (fPlayer->IsPlaying())
stop->SetEnabled(true); stop->SetEnabled(true);
@ -434,9 +415,6 @@ HWindow::Pulse()
} }
/***********************************************************
* DispatchMessage
***********************************************************/
void void
HWindow::DispatchMessage(BMessage *message, BHandler *handler) HWindow::DispatchMessage(BMessage *message, BHandler *handler)
{ {
@ -445,9 +423,7 @@ HWindow::DispatchMessage(BMessage *message, BHandler *handler)
BWindow::DispatchMessage(message, handler); BWindow::DispatchMessage(message, handler);
} }
/***********************************************************
* QuitRequested
***********************************************************/
bool bool
HWindow::QuitRequested() HWindow::QuitRequested()
{ {