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