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,46 +1,43 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ /*
// * 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)
***********************************************************/ : _inherited(rect, name, B_TITLED_WINDOW, 0),
HWindow::HWindow(BRect rect, const char* name)
:_inherited(rect, name, B_TITLED_WINDOW, 0),
fFilePanel(NULL), fFilePanel(NULL),
fPlayer(NULL) fPlayer(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()
{ {
@ -83,26 +76,22 @@ HWindow::InitGUI()
stringView->SetFont(be_bold_font); stringView->SetFont(be_bold_font);
stringView->ResizeToPreferred(); stringView->ResizeToPreferred();
listView->AddChild(stringView); listView->AddChild(stringView);
stringRect.OffsetBy(120, 0); stringRect.OffsetBy(120, 0);
stringView = new BStringView(stringRect, "sound", "Sound"); stringView = new BStringView(stringRect, "sound", "Sound");
stringView->SetFont(be_bold_font); stringView->SetFont(be_bold_font);
stringView->ResizeToPreferred(); stringView->ResizeToPreferred();
listView->AddChild(stringView); listView->AddChild(stringView);
rect.left += 13; rect.left += 13;
rect.right -= B_V_SCROLL_BAR_WIDTH + 13; rect.right -= B_V_SCROLL_BAR_WIDTH + 13;
rect.top += 28; rect.top += 28;
rect.bottom -= 7; rect.bottom -= 7;
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,34 +114,25 @@ 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);
box->AddChild(button); box->AddChild(button);
rect = button->Frame(); rect = button->Frame();
view->ResizeTo(view->Bounds().Width(), 24 + rect.bottom + 12); view->ResizeTo(view->Bounds().Width(), 24 + rect.bottom + 12);
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,18 +149,15 @@ HWindow::InitGUI()
} }
/***********************************************************
* MessageReceived
***********************************************************/
void void
HWindow::MessageReceived(BMessage *message) HWindow::MessageReceived(BMessage *message)
{ {
switch(message->what) { switch (message->what) {
case M_OTHER_MESSAGE: case M_OTHER_MESSAGE:
{ {
BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField); BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField);
BMenu *menu = menufield->Menu(); BMenu *menu = menufield->Menu();
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);
@ -189,7 +166,7 @@ HWindow::MessageReceived(BMessage *message)
BMenuItem *item = menu->FindItem("<none>"); BMenuItem *item = menu->FindItem("<none>");
if (item) if (item)
item->SetMarked(true); item->SetMarked(true);
} else{ } else {
BMenuItem *item = menu->FindItem(path.Leaf()); BMenuItem *item = menu->FindItem(path.Leaf());
if (item) if (item)
item->SetMarked(true); item->SetMarked(true);
@ -198,18 +175,20 @@ HWindow::MessageReceived(BMessage *message)
fFilePanel->Show(); fFilePanel->Show();
break; break;
} }
case B_SIMPLE_DATA:
case B_REFS_RECEIVED: case B_SIMPLE_DATA:
case B_REFS_RECEIVED:
{ {
entry_ref ref; entry_ref ref;
int32 sel = fEventList->CurrentSelection(); int32 sel = fEventList->CurrentSelection();
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);
char type[B_MIME_TYPE_LENGTH+1]; char type[B_MIME_TYPE_LENGTH + 1];
ninfo.GetType(type); ninfo.GetType(type);
BMimeType mtype(type); BMimeType mtype(type);
BMimeType superType; BMimeType superType;
@ -217,9 +196,10 @@ HWindow::MessageReceived(BMessage *message)
if (superType.Type() == NULL || strcmp(superType.Type(), "audio") != 0) { if (superType.Type() == NULL || strcmp(superType.Type(), "audio") != 0) {
beep(); beep();
(new BAlert("", "This is not a audio file.", "OK", NULL, NULL, (new BAlert("", "This is not a audio file.", "OK", NULL, NULL,
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,24 +232,25 @@ 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;
BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField); BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField);
BMenu *menu = menufield->Menu(); BMenu *menu = menufield->Menu();
if (message->FindString("path", &path) == B_OK) { if (message->FindString("path", &path) == B_OK) {
BPath path(path); BPath path(path);
if (path.InitCheck() != B_OK) { if (path.InitCheck() != B_OK) {
@ -285,110 +265,113 @@ 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:
_inherited::MessageReceived(message); default:
_inherited::MessageReceived(message);
} }
} }
/***********************************************************
* Init menu
***********************************************************/
void void
HWindow::SetupMenuField() HWindow::SetupMenuField()
{ {
BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField); BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField);
BMenu *menu = menufield->Menu(); BMenu *menu = menufield->Menu();
int32 count = fEventList->CountItems(); int32 count = fEventList->CountItems();
for(int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
HEventItem *item = cast_as(fEventList->ItemAt(i), HEventItem); HEventItem *item = cast_as(fEventList->ItemAt(i), HEventItem);
if (!item) if (!item)
continue; continue;
BPath path(item->Path()); BPath path(item->Path());
if (path.InitCheck() != B_OK) if (path.InitCheck() != B_OK)
continue; continue;
if (menu->FindItem(path.Leaf())) if (menu->FindItem(path.Leaf()))
continue; continue;
BMessage *msg = new BMessage(M_ITEM_MESSAGE); BMessage *msg = new BMessage(M_ITEM_MESSAGE);
entry_ref ref; entry_ref ref;
::get_ref_for_path(path.Path(), &ref); ::get_ref_for_path(path.Path(), &ref);
msg->AddRef("refs", &ref); msg->AddRef("refs", &ref);
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;
while (err == B_OK){
err = dir.GetNextEntry((BEntry *)&entry, true); status_t err = find_directory(B_BEOS_SOUNDS_DIRECTORY, &path);
if (entry.InitCheck() != B_NO_ERROR) { if (err == B_OK)
err = dir.SetTo(path.Path());
while (err == B_OK) {
err = dir.GetNextEntry((BEntry*)&entry, true);
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()))
continue; continue;
BMessage *msg = new BMessage(M_ITEM_MESSAGE); BMessage *msg = new BMessage(M_ITEM_MESSAGE);
entry_ref ref; entry_ref ref;
::get_ref_for_path(item_path.Path(), &ref); ::get_ref_for_path(item_path.Path(), &ref);
msg->AddRef("refs", &ref); msg->AddRef("refs", &ref);
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()))
continue; continue;
BMessage *msg = new BMessage(M_ITEM_MESSAGE); BMessage *msg = new BMessage(M_ITEM_MESSAGE);
entry_ref ref; entry_ref ref;
::get_ref_for_path(item_path.Path(), &ref); ::get_ref_for_path(item_path.Path(), &ref);
msg->AddRef("refs", &ref); msg->AddRef("refs", &ref);
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()))
continue; continue;
BMessage *msg = new BMessage(M_ITEM_MESSAGE); BMessage *msg = new BMessage(M_ITEM_MESSAGE);
entry_ref ref; entry_ref ref;
::get_ref_for_path(item_path.Path(), &ref); ::get_ref_for_path(item_path.Path(), &ref);
msg->AddRef("refs", &ref); msg->AddRef("refs", &ref);
menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0); menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
@ -397,9 +380,6 @@ HWindow::SetupMenuField()
} }
/***********************************************************
* Pulse
***********************************************************/
void void
HWindow::Pulse() HWindow::Pulse()
{ {
@ -407,15 +387,15 @@ HWindow::Pulse()
BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField); BMenuField *menufield = cast_as(FindView("filemenu"), BMenuField);
BButton *button = cast_as(FindView("play"), BButton); BButton *button = cast_as(FindView("play"), BButton);
BButton *stop = cast_as(FindView("stop"), BButton); BButton *stop = cast_as(FindView("stop"), BButton);
if (!menufield) if (!menufield)
return; return;
if (sel >=0) { if (sel >= 0) {
menufield->SetEnabled(true); menufield->SetEnabled(true);
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 && strcmp(path, "")) if (path && strcmp(path, ""))
button->SetEnabled(true); button->SetEnabled(true);
else else
@ -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()
{ {