]> git.sesse.net Git - vlc/blob - modules/gui/beos/InterfaceWindow.cpp
+ src/libvlc.h: use KEY_SPACE instead of ' '
[vlc] / modules / gui / beos / InterfaceWindow.cpp
1 /*****************************************************************************
2  * InterfaceWindow.cpp: beos interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id$
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Tony Castley <tony@castley.net>
10  *          Richard Shepherd <richard@rshepherd.demon.co.uk>
11  *          Stephan Aßmus <superstippi@gmx.de>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
26  *****************************************************************************/
27
28 /* System headers */
29 #include <kernel/OS.h>
30 #include <InterfaceKit.h>
31 #include <AppKit.h>
32 #include <StorageKit.h>
33 #include <SupportKit.h>
34 #include <malloc.h>
35 #include <scsi.h>
36 #include <scsiprobe_driver.h>
37 #include <fs_info.h>
38 #include <string.h>
39
40 /* VLC headers */
41 #include <vlc/vlc.h>
42 #include <vlc/aout.h>
43 #include <vlc/intf.h>
44 #include <vlc/input.h>
45
46 /* BeOS interface headers */
47 #include "MsgVals.h"
48 #include "MediaControlView.h"
49 #include "PlayListWindow.h"
50 #include "PreferencesWindow.h"
51 #include "MessagesWindow.h"
52 #include "InterfaceWindow.h"
53
54 #define INTERFACE_UPDATE_TIMEOUT  80000 // 2 frames if at 25 fps
55 #define INTERFACE_LOCKING_TIMEOUT 5000
56
57 // make_sure_frame_is_on_screen
58 bool
59 make_sure_frame_is_on_screen( BRect& frame )
60 {
61     BScreen screen( B_MAIN_SCREEN_ID );
62     if (frame.IsValid() && screen.IsValid()) {
63         if (!screen.Frame().Contains(frame)) {
64             // make sure frame fits in the screen
65             if (frame.Width() > screen.Frame().Width())
66                 frame.right -= frame.Width() - screen.Frame().Width() + 10.0;
67             if (frame.Height() > screen.Frame().Height())
68                 frame.bottom -= frame.Height() - screen.Frame().Height() + 30.0;
69             // frame is now at the most the size of the screen
70             if (frame.right > screen.Frame().right)
71                 frame.OffsetBy(-(frame.right - screen.Frame().right), 0.0);
72             if (frame.bottom > screen.Frame().bottom)
73                 frame.OffsetBy(0.0, -(frame.bottom - screen.Frame().bottom));
74             if (frame.left < screen.Frame().left)
75                 frame.OffsetBy((screen.Frame().left - frame.left), 0.0);
76             if (frame.top < screen.Frame().top)
77                 frame.OffsetBy(0.0, (screen.Frame().top - frame.top));
78         }
79         return true;
80     }
81     return false;
82 }
83
84 // make_sure_frame_is_within_limits
85 void
86 make_sure_frame_is_within_limits( BRect& frame, float minWidth, float minHeight,
87                                   float maxWidth, float maxHeight )
88 {
89     if ( frame.Width() < minWidth )
90         frame.right = frame.left + minWidth;
91     if ( frame.Height() < minHeight )
92         frame.bottom = frame.top + minHeight;
93     if ( frame.Width() > maxWidth )
94         frame.right = frame.left + maxWidth;
95     if ( frame.Height() > maxHeight )
96         frame.bottom = frame.top + maxHeight;
97 }
98
99 // get_volume_info
100 bool
101 get_volume_info( BVolume& volume, BString& volumeName, bool& isCDROM, BString& deviceName )
102 {
103     bool success = false;
104     isCDROM = false;
105     deviceName = "";
106     volumeName = "";
107     char name[B_FILE_NAME_LENGTH];
108     if ( volume.GetName( name ) >= B_OK )    // disk is currently mounted
109     {
110         volumeName = name;
111         dev_t dev = volume.Device();
112         fs_info info;
113         if ( fs_stat_dev( dev, &info ) == B_OK )
114         {
115             success = true;
116             deviceName = info.device_name;
117             if ( volume.IsReadOnly() )
118             {
119                 int i_dev = open( info.device_name, O_RDONLY );
120                 if ( i_dev >= 0 )
121                 {
122                     device_geometry g;
123                     if ( ioctl( i_dev, B_GET_GEOMETRY, &g, sizeof( g ) ) >= 0 )
124                         isCDROM = ( g.device_type == B_CD );
125                     close( i_dev );
126                 }
127             }
128         }
129      }
130      return success;
131 }
132
133 // collect_folder_contents
134 void
135 collect_folder_contents( BDirectory& dir, BList& list, bool& deep, bool& asked, BEntry& entry )
136 {
137     while ( dir.GetNextEntry( &entry, true ) == B_OK )
138     {
139         if ( !entry.IsDirectory() )
140         {
141             BPath path;
142             // since the directory will give us the entries in reverse order,
143             // we put them each at the same index, effectively reversing the
144             // items while adding them
145             if ( entry.GetPath( &path ) == B_OK )
146             {
147                 BString* string = new BString( path.Path() );
148                 if ( !list.AddItem( string, 0 ) )
149                     delete string;    // at least don't leak
150             }
151         }
152         else
153         {
154             if ( !asked )
155             {
156                 // ask user if we should parse sub-folders as well
157                 BAlert* alert = new BAlert( "sub-folders?",
158                                             _("Open files from all sub-folders as well?"),
159                                             _("Cancel"), _("Open"), NULL, B_WIDTH_AS_USUAL,
160                                             B_IDEA_ALERT );
161                 int32 buttonIndex = alert->Go();
162                 deep = buttonIndex == 1;
163                 asked = true;
164                 // never delete BAlerts!!
165             }
166             if ( deep )
167             {
168                 BDirectory subDir( &entry );
169                 if ( subDir.InitCheck() == B_OK )
170                     collect_folder_contents( subDir, list,
171                                              deep, asked, entry );
172             }
173         }
174     }
175 }
176
177
178 /*****************************************************************************
179  * InterfaceWindow
180  *****************************************************************************/
181
182 InterfaceWindow::InterfaceWindow( intf_thread_t * _p_intf, BRect frame,
183                                   const char * name )
184     : BWindow( frame, name, B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
185                B_NOT_ZOOMABLE | B_WILL_ACCEPT_FIRST_CLICK | B_ASYNCHRONOUS_CONTROLS ),
186       
187       /* Initializations */
188       p_intf( _p_intf ),
189       p_input( NULL ),
190       p_playlist( NULL ),
191       
192       fFilePanel( NULL ),
193       fLastUpdateTime( system_time() ),
194       fSettings( new BMessage( 'sett' ) )
195 {
196     char psz_tmp[1024];
197 #define ADD_ELLIPSIS( a ) \
198     memset( psz_tmp, 0, 1024 ); \
199     snprintf( psz_tmp, 1024, "%s%s", a, B_UTF8_ELLIPSIS );
200
201     BScreen screen;
202     BRect screen_rect = screen.Frame();
203     BRect window_rect;
204     window_rect.Set( ( screen_rect.right - PREFS_WINDOW_WIDTH ) / 2,
205                      ( screen_rect.bottom - PREFS_WINDOW_HEIGHT ) / 2,
206                      ( screen_rect.right + PREFS_WINDOW_WIDTH ) / 2,
207                      ( screen_rect.bottom + PREFS_WINDOW_HEIGHT ) / 2 );
208     fPreferencesWindow = new PreferencesWindow( p_intf, window_rect, _("Preferences") );
209     window_rect.Set( screen_rect.right - 500,
210                      screen_rect.top + 50,
211                      screen_rect.right - 150,
212                      screen_rect.top + 250 );
213     fPlaylistWindow = new PlayListWindow( window_rect, _("Playlist"), this, p_intf );
214     window_rect.Set( screen_rect.right - 550,
215                      screen_rect.top + 300,
216                      screen_rect.right - 150,
217                      screen_rect.top + 500 );
218     fMessagesWindow = new MessagesWindow( p_intf, window_rect, _("Messages") );
219
220     // the media control view
221     p_mediaControl = new MediaControlView( p_intf, BRect( 0.0, 0.0, 250.0, 50.0 ) );
222     p_mediaControl->SetViewColor( ui_color( B_PANEL_BACKGROUND_COLOR ) );
223
224     float width, height;
225     p_mediaControl->GetPreferredSize( &width, &height );
226
227     // set up the main menu
228     fMenuBar = new BMenuBar( BRect(0.0, 0.0, width, 15.0), "main menu",
229                              B_FOLLOW_NONE, B_ITEMS_IN_ROW, false );
230
231     // make menu bar resize to correct height
232     float menuWidth, menuHeight;
233     fMenuBar->GetPreferredSize( &menuWidth, &menuHeight );
234     fMenuBar->ResizeTo( width, menuHeight );    // don't change! it's a workarround!
235     // take care of proper size for ourself
236     height += fMenuBar->Bounds().Height();
237     ResizeTo( width, height );
238
239     p_mediaControl->MoveTo( fMenuBar->Bounds().LeftBottom() + BPoint(0.0, 1.0) );
240     AddChild( fMenuBar );
241
242
243     // Add the file Menu
244     BMenu* fileMenu = new BMenu( _("File") );
245     fMenuBar->AddItem( fileMenu );
246     ADD_ELLIPSIS( _("Open File") );
247     fileMenu->AddItem( new BMenuItem( psz_tmp, new BMessage( OPEN_FILE ), 'O') );
248     fileMenu->AddItem( new CDMenu( _("Open Disc") ) );
249     ADD_ELLIPSIS( _("Open Subtitles") );
250     fileMenu->AddItem( new BMenuItem( psz_tmp, new BMessage( LOAD_SUBFILE ) ) );
251
252     fileMenu->AddSeparatorItem();
253     ADD_ELLIPSIS( _("About") );
254     BMenuItem* item = new BMenuItem( psz_tmp, new BMessage( B_ABOUT_REQUESTED ), 'A');
255     item->SetTarget( be_app );
256     fileMenu->AddItem( item );
257     fileMenu->AddItem( new BMenuItem( _("Quit"), new BMessage( B_QUIT_REQUESTED ), 'Q') );
258
259     fLanguageMenu = new LanguageMenu( p_intf, _("Language"), "audio-es" );
260     fSubtitlesMenu = new LanguageMenu( p_intf, _("Subtitles"), "spu-es" );
261
262     /* Add the Audio menu */
263     fAudioMenu = new BMenu( _("Audio") );
264     fMenuBar->AddItem ( fAudioMenu );
265     fAudioMenu->AddItem( fLanguageMenu );
266     fAudioMenu->AddItem( fSubtitlesMenu );
267
268     fPrevTitleMI = new BMenuItem( _("Prev Title"), new BMessage( PREV_TITLE ) );
269     fNextTitleMI = new BMenuItem( _("Next Title"), new BMessage( NEXT_TITLE ) );
270     fPrevChapterMI = new BMenuItem( _("Previous chapter"), new BMessage( PREV_CHAPTER ) );
271     fNextChapterMI = new BMenuItem( _("Next chapter"), new BMessage( NEXT_CHAPTER ) );
272     fGotoMenuMI = new BMenuItem( _("Goto Menu"), new BMessage( NAVIGATE_MENU ) );
273
274     /* Add the Navigation menu */
275     fNavigationMenu = new BMenu( _("Navigation") );
276     fMenuBar->AddItem( fNavigationMenu );
277     fNavigationMenu->AddItem( fGotoMenuMI );
278     fNavigationMenu->AddSeparatorItem();
279     fNavigationMenu->AddItem( fPrevTitleMI );
280     fNavigationMenu->AddItem( fNextTitleMI );
281     fNavigationMenu->AddItem( fTitleMenu = new TitleMenu( _("Go to Title"), p_intf ) );
282     fNavigationMenu->AddSeparatorItem();
283     fNavigationMenu->AddItem( fPrevChapterMI );
284     fNavigationMenu->AddItem( fNextChapterMI );
285     fNavigationMenu->AddItem( fChapterMenu = new ChapterMenu( _("Go to Chapter"), p_intf ) );
286
287     /* Add the Speed menu */
288     fSpeedMenu = new BMenu( _("Speed") );
289     fSpeedMenu->SetRadioMode( true );
290     fSpeedMenu->AddItem(
291         fHeighthMI = new BMenuItem( "1/8x", new BMessage( HEIGHTH_PLAY ) ) );
292     fSpeedMenu->AddItem(
293         fQuarterMI = new BMenuItem( "1/4x", new BMessage( QUARTER_PLAY ) ) );
294     fSpeedMenu->AddItem(
295         fHalfMI = new BMenuItem( "1/2x", new BMessage( HALF_PLAY ) ) );
296     fSpeedMenu->AddItem(
297         fNormalMI = new BMenuItem( "1x", new BMessage( NORMAL_PLAY ) ) );
298     fSpeedMenu->AddItem(
299         fTwiceMI = new BMenuItem( "2x", new BMessage( TWICE_PLAY ) ) );
300     fSpeedMenu->AddItem(
301         fFourMI = new BMenuItem( "4x", new BMessage( FOUR_PLAY ) ) );
302     fSpeedMenu->AddItem(
303         fHeightMI = new BMenuItem( "8x", new BMessage( HEIGHT_PLAY ) ) );
304     fMenuBar->AddItem( fSpeedMenu );
305
306     /* Add the Show menu */
307     fShowMenu = new BMenu( _("Window") );
308     ADD_ELLIPSIS( _("Playlist") );
309     fShowMenu->AddItem( new BMenuItem( psz_tmp, new BMessage( OPEN_PLAYLIST ), 'P') );
310     ADD_ELLIPSIS( _("Messages") );
311     fShowMenu->AddItem( new BMenuItem( psz_tmp, new BMessage( OPEN_MESSAGES ), 'M' ) );
312     ADD_ELLIPSIS( _("Preferences") );
313     fShowMenu->AddItem( new BMenuItem( psz_tmp, new BMessage( OPEN_PREFERENCES ), 'S' ) );
314     fMenuBar->AddItem( fShowMenu );
315
316     // add the media control view after the menubar is complete
317     // because it will set the window size limits in AttachedToWindow()
318     // and the menubar needs to report the correct PreferredSize()
319     AddChild( p_mediaControl );
320
321     /* Prepare fow showing */
322     _SetMenusEnabled( false );
323     p_mediaControl->SetEnabled( false );
324
325     _RestoreSettings();
326
327     Show();
328 }
329
330 InterfaceWindow::~InterfaceWindow()
331 {
332     if( p_input )
333     {
334         vlc_object_release( p_input );
335     }
336     if( p_playlist )
337     {
338         vlc_object_release( p_playlist );
339     }
340     if( fPlaylistWindow )
341     {
342         fPlaylistWindow->ReallyQuit();
343     }
344     if( fMessagesWindow )
345     {
346         fMessagesWindow->ReallyQuit();
347     }
348     if( fPreferencesWindow )
349     {
350         fPreferencesWindow->ReallyQuit();
351     }
352     delete fFilePanel;
353     delete fSettings;
354 }
355
356 /*****************************************************************************
357  * InterfaceWindow::FrameResized
358  *****************************************************************************/
359 void
360 InterfaceWindow::FrameResized(float width, float height)
361 {
362     BRect r(Bounds());
363     fMenuBar->MoveTo(r.LeftTop());
364     fMenuBar->ResizeTo(r.Width(), fMenuBar->Bounds().Height());
365     r.top += fMenuBar->Bounds().Height() + 1.0;
366     p_mediaControl->MoveTo(r.LeftTop());
367     p_mediaControl->ResizeTo(r.Width(), r.Height());
368 }
369
370 /*****************************************************************************
371  * InterfaceWindow::MessageReceived
372  *****************************************************************************/
373 void InterfaceWindow::MessageReceived( BMessage * p_message )
374 {
375     switch( p_message->what )
376     {
377         case B_ABOUT_REQUESTED:
378         {
379             BAlert * alert;
380             
381             alert = new BAlert( "VLC media player" VERSION,
382                                 "VLC media player" VERSION " (BeOS interface)\n\n"
383                                 "The VideoLAN team <videolan@videolan.org>\n"
384                                 "http://www.videolan.org/", _("OK") );
385             alert->Go();
386             break;
387         }
388         case TOGGLE_ON_TOP:
389             break;
390
391         case OPEN_FILE:
392             _ShowFilePanel( B_REFS_RECEIVED, _("VLC media player: Open Media Files") );
393             break;
394
395         case LOAD_SUBFILE:
396             _ShowFilePanel( SUBFILE_RECEIVED, _("VLC media player: Open Subtitle File") );
397             break;
398
399         case OPEN_PLAYLIST:
400             if (fPlaylistWindow->Lock())
401             {
402                 if (fPlaylistWindow->IsHidden())
403                     fPlaylistWindow->Show();
404                 else
405                     fPlaylistWindow->Activate();
406                 fPlaylistWindow->Unlock();
407             }
408             break;
409
410         case OPEN_DVD:
411             {
412                 const char * psz_device;
413                 if( p_playlist &&
414                     p_message->FindString( "device", &psz_device ) == B_OK )
415                 {
416                     char psz_uri[1024];
417                     memset( psz_uri, 0, 1024 );
418                     snprintf( psz_uri, 1024, "dvdnav:%s", psz_device );
419                     playlist_Add( p_playlist, psz_uri, psz_device,
420                                   PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
421                 }
422                 _UpdatePlaylist();
423             }
424             break;
425
426         case SUBFILE_RECEIVED:
427         {
428             entry_ref ref;
429             if( p_message->FindRef( "refs", 0, &ref ) == B_OK )
430             {
431                 BPath path( &ref );
432                 if ( path.InitCheck() == B_OK )
433                     config_PutPsz( p_intf, "sub-file", path.Path() );
434             }
435             break;
436         }
437
438         case STOP_PLAYBACK:
439             if( p_playlist )
440             {
441                 playlist_Stop( p_playlist );
442             }
443             p_mediaControl->SetStatus(-1, INPUT_RATE_DEFAULT);
444             break;
445     
446         case START_PLAYBACK:
447             /*  starts playing in normal mode */
448     
449         case PAUSE_PLAYBACK:
450             if( p_input )
451             {
452                 if( var_GetInteger( p_input, "state" ) == PAUSE_S )
453                 {
454                     if( p_playlist )
455                     {
456                         playlist_Play( p_playlist );
457                     }
458                 }
459                 else
460                 {
461                     var_SetInteger( p_input, "state", PAUSE_S );
462                 }
463             }
464             break;
465     
466         case HEIGHTH_PLAY:
467             if( p_input )
468             {
469                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT * 8 );
470             }
471             break;
472
473         case QUARTER_PLAY:
474             if( p_input )
475             {
476                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT * 4 );
477             }
478             break;
479
480         case HALF_PLAY:
481             if( p_input )
482             {
483                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT * 2 );
484             }
485             break;
486
487         case NORMAL_PLAY:
488             if( p_input )
489             {
490                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT );
491             }
492             break;
493
494         case TWICE_PLAY:
495             if( p_input )
496             {
497                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT / 2 );
498             }
499             break;
500
501         case FOUR_PLAY:
502             if( p_input )
503             {
504                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT / 4 );
505             }
506             break;
507
508         case HEIGHT_PLAY:
509             if( p_input )
510             {
511                 var_SetInteger( p_input, "rate", INPUT_RATE_DEFAULT / 8 );
512             }
513             break;
514
515         case SEEK_PLAYBACK:
516             /* handled by semaphores */
517             break;
518
519         case VOLUME_CHG:
520             aout_VolumeSet( p_intf, p_mediaControl->GetVolume() );
521             break;
522     
523         case VOLUME_MUTE:
524             aout_VolumeMute( p_intf, NULL );
525             break;
526     
527         case SELECT_CHANNEL:
528         {
529             int32 channel;
530             if( p_input )
531             {
532                 if( p_message->FindInt32( "audio-es", &channel ) == B_OK )
533                 {
534                     var_SetInteger( p_input, "audio-es", channel );
535                 }
536                 else if( p_message->FindInt32( "spu-es", &channel ) == B_OK )
537                 {
538                     var_SetInteger( p_input, "spu-es", channel );
539                 }
540             }
541             break;
542         }
543     
544         case PREV_TITLE:
545             if( p_input )
546             {
547                 var_SetVoid( p_input, "prev-title" );
548             }
549             break;
550
551         case NEXT_TITLE:
552             if( p_input )
553             {
554                 var_SetVoid( p_input, "next-title" );
555             }
556             break;
557
558         case NAVIGATE_MENU:
559             if( p_input )
560             {
561                 var_SetInteger( p_input, "title", 0 );
562             }
563             break;
564
565         case TOGGLE_TITLE:
566         {
567             int32 index;
568             if( p_input &&
569                 p_message->FindInt32( "index", &index ) == B_OK )
570             {
571                 var_SetInteger( p_input, "title", index );
572             }
573             break;
574         }
575
576         case PREV_CHAPTER:
577             if( p_input )
578             {
579                 var_SetVoid( p_input, "prev-chapter" );
580             }
581             break;
582
583         case NEXT_CHAPTER:
584             if( p_input )
585             {
586                 var_SetVoid( p_input, "next-chapter" );
587             }
588             break;
589
590         case TOGGLE_CHAPTER:
591         {
592             int32 index;
593             if( p_input &&
594                 p_message->FindInt32( "index", &index ) == B_OK )
595             {
596                 var_SetInteger( p_input, "chapter", index );
597             }
598             break;
599         }
600
601         case PREV_FILE:
602             if( p_playlist )
603             {
604                 playlist_Prev( p_playlist );
605             }
606             break;
607
608         case NEXT_FILE:
609             if( p_playlist )
610             {
611                 playlist_Next( p_playlist );
612             }
613             break;
614
615         case NAVIGATE_PREV:
616             if( p_input )
617             {
618                 vlc_value_t val;
619
620                 /* First try to go to previous chapter */
621                 if( !var_Get( p_input, "chapter", &val ) )
622                 {
623                     if( val.i_int > 1 )
624                     {
625                         var_SetVoid( p_input, "prev-chapter" );
626                         break;
627                     }
628                 }
629
630                 /* Try to go to previous title */
631                 if( !var_Get( p_input, "title", &val ) )
632                 {
633                     if( val.i_int > 1 )
634                     {
635                         var_SetVoid( p_input, "prev-title" );
636                         break;
637                     }
638                 }
639
640                 /* Try to go to previous file */
641                 if( p_playlist )
642                 {
643                     playlist_Prev( p_playlist );
644                 }
645             }
646             break;
647
648         case NAVIGATE_NEXT:
649             if( p_input )
650             {
651                 vlc_value_t val, val_list;
652
653                 /* First try to go to next chapter */
654                 if( !var_Get( p_input, "chapter", &val ) )
655                 {
656                     var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
657                                 &val_list, NULL );
658                     if( val_list.p_list->i_count > val.i_int )
659                     {
660                         var_Change( p_input, "chapter", VLC_VAR_FREELIST,
661                                     &val_list, NULL );
662                         var_SetVoid( p_input, "next-chapter" );
663                         break;
664                     }
665                     var_Change( p_input, "chapter", VLC_VAR_FREELIST,
666                                 &val_list, NULL );
667                 }
668
669                 /* Try to go to next title */
670                 if( !var_Get( p_input, "title", &val ) )
671                 {
672                     var_Change( p_input, "title", VLC_VAR_GETCHOICES,
673                                 &val_list, NULL );
674                     if( val_list.p_list->i_count > val.i_int )
675                     {
676                         var_Change( p_input, "title", VLC_VAR_FREELIST,
677                                     &val_list, NULL );
678                         var_SetVoid( p_input, "next-title" );
679                         break;
680                     }
681                     var_Change( p_input, "title", VLC_VAR_FREELIST,
682                                 &val_list, NULL );
683                 }
684
685                 /* Try to go to next file */
686                 if( p_playlist )
687                 {
688                     playlist_Next( p_playlist );
689                 }
690             }
691             break;
692
693         // drag'n'drop and system messages
694         case MSG_SOUNDPLAY:
695             // convert soundplay drag'n'drop message (containing paths)
696             // to normal message (containing refs)
697             {
698                 const char* path;
699                 for ( int32 i = 0; p_message->FindString( "path", i, &path ) == B_OK; i++ )
700                 {
701                     entry_ref ref;
702                     if ( get_ref_for_path( path, &ref ) == B_OK )
703                         p_message->AddRef( "refs", &ref );
704                 }
705             }
706             // fall through
707         case B_REFS_RECEIVED:
708         case B_SIMPLE_DATA:
709         {
710             /* file(s) opened by the File menu -> append to the playlist;
711                file(s) opened by drag & drop -> replace playlist;
712                file(s) opened by 'shift' + drag & drop -> append */
713
714             int32 count;
715             type_code dummy;
716             if( p_message->GetInfo( "refs", &dummy, &count ) != B_OK ||
717                 count < 1 )
718             {
719                 break;
720             }
721             
722             vlc_bool_t b_remove = ( p_message->WasDropped() &&
723                                     !( modifiers() & B_SHIFT_KEY ) ); 
724
725             if( b_remove && p_playlist )
726             {
727                 /* Empty playlist */
728                 while( p_playlist->i_size > 0 )
729                 {
730                     playlist_Delete( p_playlist, 0 );
731                 }
732             }
733
734             entry_ref ref;
735             for( int i = 0; p_message->FindRef( "refs", i, &ref ) == B_OK; i++ )
736             {
737                 BPath path( &ref );
738
739                 /* TODO: find out if this is a DVD icon */
740
741                 if( p_playlist )
742                 {
743                     playlist_Add( p_playlist, path.Path(), path.Path(),
744                                   PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
745                 }
746             }
747
748             _UpdatePlaylist();
749             break;
750         }
751
752         case OPEN_PREFERENCES:
753         {
754             if( fPreferencesWindow->Lock() )
755             {
756                 if (fPreferencesWindow->IsHidden())
757                     fPreferencesWindow->Show();
758                 else
759                     fPreferencesWindow->Activate();
760                 fPreferencesWindow->Unlock();
761             }
762             break;
763         }
764
765         case OPEN_MESSAGES:
766         {
767             if( fMessagesWindow->Lock() )
768             {
769                 if (fMessagesWindow->IsHidden())
770                     fMessagesWindow->Show();
771                 else
772                     fMessagesWindow->Activate();
773                 fMessagesWindow->Unlock();
774             }
775             break;
776         }
777         case MSG_UPDATE:
778             UpdateInterface();
779             break;
780         default:
781             BWindow::MessageReceived( p_message );
782             break;
783     }
784 }
785
786 /*****************************************************************************
787  * InterfaceWindow::QuitRequested
788  *****************************************************************************/
789 bool InterfaceWindow::QuitRequested()
790 {
791     if( p_playlist )
792     {
793         playlist_Stop( p_playlist );
794     }
795     p_mediaControl->SetStatus(-1, INPUT_RATE_DEFAULT);
796
797      _StoreSettings();
798    
799     p_intf->b_die = 1;
800
801     return( true );
802 }
803
804 /*****************************************************************************
805  * InterfaceWindow::UpdateInterface
806  *****************************************************************************/
807 void InterfaceWindow::UpdateInterface()
808 {
809     /* Manage the input part */
810     if( !p_playlist )
811     {
812         p_playlist = (playlist_t *)
813             vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
814     }
815     if( !p_input )
816     {
817         p_input = (input_thread_t *)
818             vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
819     }
820     else if( p_input->b_dead )
821     {
822         vlc_object_release( p_input );
823         p_input = NULL;
824     }
825
826     /* Get ready to update the interface */
827     if( LockWithTimeout( INTERFACE_LOCKING_TIMEOUT ) != B_OK )
828     {
829         return;
830     }
831     
832     if( p_input )
833     {
834         vlc_value_t val;
835         p_mediaControl->SetEnabled( true );
836         bool hasTitles   = var_Get( p_input, "title", &val );
837         bool hasChapters = var_Get( p_input, "chapter", &val );
838         p_mediaControl->SetStatus( var_GetInteger( p_input, "state" ), 
839                                    var_GetInteger( p_input, "rate" ) );
840         var_Get( p_input, "position", &val );
841         p_mediaControl->SetProgress( val.f_float );
842         _SetMenusEnabled( true, hasChapters, hasTitles );
843         _UpdateSpeedMenu( var_GetInteger( p_input, "rate" ) );
844
845         // enable/disable skip buttons
846 #if 0
847         bool canSkipPrev;
848         bool canSkipNext;
849         p_wrapper->GetNavCapabilities( &canSkipPrev, &canSkipNext );
850         p_mediaControl->SetSkippable( canSkipPrev, canSkipNext );
851 #endif
852
853         audio_volume_t i_volume;
854         aout_VolumeGet( p_input, &i_volume );
855         p_mediaControl->SetAudioEnabled( true );
856         p_mediaControl->SetMuted( i_volume );
857
858         // update playlist as well
859         if( fPlaylistWindow->LockWithTimeout( INTERFACE_LOCKING_TIMEOUT ) == B_OK )
860         {
861             fPlaylistWindow->UpdatePlaylist();
862             fPlaylistWindow->Unlock();
863         }
864     }
865     else
866     {
867         p_mediaControl->SetAudioEnabled( false );
868
869         _SetMenusEnabled( false );
870         
871         if( !p_playlist || p_playlist->i_size <= 0 )
872         {
873             p_mediaControl->SetProgress( 0 );
874             
875 #if 0
876             // enable/disable skip buttons
877             bool canSkipPrev;
878             bool canSkipNext;
879             p_wrapper->GetNavCapabilities( &canSkipPrev, &canSkipNext );
880             p_mediaControl->SetSkippable( canSkipPrev, canSkipNext );
881 #endif
882         }
883         else
884         {
885             p_mediaControl->SetEnabled( false );
886         }
887     }
888     
889     Unlock();
890     fLastUpdateTime = system_time();
891 }
892
893 /*****************************************************************************
894  * InterfaceWindow::IsStopped
895  *****************************************************************************/
896 bool
897 InterfaceWindow::IsStopped() const
898 {
899     return (system_time() - fLastUpdateTime > INTERFACE_UPDATE_TIMEOUT);
900 }
901
902 /*****************************************************************************
903  * InterfaceWindow::_UpdatePlaylist
904  *****************************************************************************/
905 void
906 InterfaceWindow::_UpdatePlaylist()
907 {
908     if( fPlaylistWindow->Lock() )
909     {
910         fPlaylistWindow->UpdatePlaylist( true );
911         fPlaylistWindow->Unlock();
912     }
913     p_mediaControl->SetEnabled( p_playlist->i_size );
914 }
915
916 /*****************************************************************************
917  * InterfaceWindow::_SetMenusEnabled
918  *****************************************************************************/
919 void
920 InterfaceWindow::_SetMenusEnabled(bool hasFile, bool hasChapters, bool hasTitles)
921 {
922     if (!hasFile)
923     {
924         hasChapters = false;
925         hasTitles = false;
926     }
927     if ( LockWithTimeout( INTERFACE_LOCKING_TIMEOUT ) == B_OK)
928     {
929         if ( fNextChapterMI->IsEnabled() != hasChapters )
930              fNextChapterMI->SetEnabled( hasChapters );
931         if ( fPrevChapterMI->IsEnabled() != hasChapters )
932              fPrevChapterMI->SetEnabled( hasChapters );
933         if ( fChapterMenu->IsEnabled() != hasChapters )
934              fChapterMenu->SetEnabled( hasChapters );
935         if ( fNextTitleMI->IsEnabled() != hasTitles )
936              fNextTitleMI->SetEnabled( hasTitles );
937         if ( fPrevTitleMI->IsEnabled() != hasTitles )
938              fPrevTitleMI->SetEnabled( hasTitles );
939         if ( fTitleMenu->IsEnabled() != hasTitles )
940              fTitleMenu->SetEnabled( hasTitles );
941         if ( fAudioMenu->IsEnabled() != hasFile )
942              fAudioMenu->SetEnabled( hasFile );
943         if ( fNavigationMenu->IsEnabled() != hasFile )
944              fNavigationMenu->SetEnabled( hasFile );
945         if ( fLanguageMenu->IsEnabled() != hasFile )
946              fLanguageMenu->SetEnabled( hasFile );
947         if ( fSubtitlesMenu->IsEnabled() != hasFile )
948              fSubtitlesMenu->SetEnabled( hasFile );
949         if ( fSpeedMenu->IsEnabled() != hasFile )
950              fSpeedMenu->SetEnabled( hasFile );
951         fGotoMenuMI->SetEnabled( true );
952         Unlock();
953     }
954 }
955
956 /*****************************************************************************
957  * InterfaceWindow::_UpdateSpeedMenu
958  *****************************************************************************/
959 void
960 InterfaceWindow::_UpdateSpeedMenu( int rate )
961 {
962     BMenuItem * toMark = NULL;
963     
964     switch( rate )
965     {
966         case ( INPUT_RATE_DEFAULT * 8 ):
967             toMark = fHeighthMI;
968             break;
969             
970         case ( INPUT_RATE_DEFAULT * 4 ):
971             toMark = fQuarterMI;
972             break;
973             
974         case ( INPUT_RATE_DEFAULT * 2 ):
975             toMark = fHalfMI;
976             break;
977             
978         case ( INPUT_RATE_DEFAULT ):
979             toMark = fNormalMI;
980             break;
981             
982         case ( INPUT_RATE_DEFAULT / 2 ):
983             toMark = fTwiceMI;
984             break;
985             
986         case ( INPUT_RATE_DEFAULT / 4 ):
987             toMark = fFourMI;
988             break;
989             
990         case ( INPUT_RATE_DEFAULT / 8 ):
991             toMark = fHeightMI;
992             break;
993     }
994
995     if ( toMark && !toMark->IsMarked() )
996     {
997         toMark->SetMarked( true );
998     }
999 }
1000
1001 /*****************************************************************************
1002  * InterfaceWindow::_ShowFilePanel
1003  *****************************************************************************/
1004 void
1005 InterfaceWindow::_ShowFilePanel( uint32 command, const char* windowTitle )
1006 {
1007     if( !fFilePanel )
1008     {
1009         fFilePanel = new BFilePanel( B_OPEN_PANEL, NULL, NULL,
1010                                      B_FILE_NODE | B_DIRECTORY_NODE );
1011         fFilePanel->SetTarget( this );
1012     }
1013     fFilePanel->Window()->SetTitle( windowTitle );
1014     BMessage message( command );
1015     fFilePanel->SetMessage( &message );
1016     if ( !fFilePanel->IsShowing() )
1017     {
1018         fFilePanel->Refresh();
1019         fFilePanel->Show();
1020     }
1021 }
1022
1023 // set_window_pos
1024 void
1025 set_window_pos( BWindow* window, BRect frame )
1026 {
1027     // sanity checks: make sure window is not too big/small
1028     // and that it's not off-screen
1029     float minWidth, maxWidth, minHeight, maxHeight;
1030     window->GetSizeLimits( &minWidth, &maxWidth, &minHeight, &maxHeight );
1031
1032     make_sure_frame_is_within_limits( frame,
1033                                       minWidth, minHeight, maxWidth, maxHeight );
1034     if ( make_sure_frame_is_on_screen( frame ) )
1035     {
1036         window->MoveTo( frame.LeftTop() );
1037         window->ResizeTo( frame.Width(), frame.Height() );
1038     }
1039 }
1040
1041 // set_window_pos
1042 void
1043 launch_window( BWindow* window, bool showing )
1044 {
1045     if ( window->Lock() )
1046     {
1047         if ( showing )
1048         {
1049             if ( window->IsHidden() )
1050                 window->Show();
1051         }
1052         else
1053         {
1054             if ( !window->IsHidden() )
1055                 window->Hide();
1056         }
1057         window->Unlock();
1058     }
1059 }
1060
1061 /*****************************************************************************
1062  * InterfaceWindow::_RestoreSettings
1063  *****************************************************************************/
1064 void
1065 InterfaceWindow::_RestoreSettings()
1066 {
1067     if ( load_settings( fSettings, "interface_settings", "VideoLAN Client" ) == B_OK )
1068     {
1069         BRect frame;
1070         if ( fSettings->FindRect( "main frame", &frame ) == B_OK )
1071             set_window_pos( this, frame );
1072         if (fSettings->FindRect( "playlist frame", &frame ) == B_OK )
1073             set_window_pos( fPlaylistWindow, frame );
1074         if (fSettings->FindRect( "messages frame", &frame ) == B_OK )
1075             set_window_pos( fMessagesWindow, frame );
1076         if (fSettings->FindRect( "settings frame", &frame ) == B_OK )
1077         {
1078             /* FIXME: Preferences resizing doesn't work correctly yet */
1079             frame.right = frame.left + fPreferencesWindow->Frame().Width();
1080             frame.bottom = frame.top + fPreferencesWindow->Frame().Height();
1081             set_window_pos( fPreferencesWindow, frame );
1082         }
1083         
1084         bool showing;
1085         if ( fSettings->FindBool( "playlist showing", &showing ) == B_OK )
1086             launch_window( fPlaylistWindow, showing );
1087         if ( fSettings->FindBool( "messages showing", &showing ) == B_OK )
1088             launch_window( fMessagesWindow, showing );
1089         if ( fSettings->FindBool( "settings showing", &showing ) == B_OK )
1090             launch_window( fPreferencesWindow, showing );
1091
1092         uint32 displayMode;
1093         if ( fSettings->FindInt32( "playlist display mode", (int32*)&displayMode ) == B_OK )
1094             fPlaylistWindow->SetDisplayMode( displayMode );
1095     }
1096 }
1097
1098 /*****************************************************************************
1099  * InterfaceWindow::_StoreSettings
1100  *****************************************************************************/
1101 void
1102 InterfaceWindow::_StoreSettings()
1103 {
1104     /* Save the volume */
1105     config_PutInt( p_intf, "volume", p_mediaControl->GetVolume() );
1106     config_SaveConfigFile( p_intf, "main" );
1107
1108     /* Save the windows positions */
1109     if ( fSettings->ReplaceRect( "main frame", Frame() ) != B_OK )
1110         fSettings->AddRect( "main frame", Frame() );
1111     if ( fPlaylistWindow->Lock() )
1112     {
1113         if (fSettings->ReplaceRect( "playlist frame", fPlaylistWindow->Frame() ) != B_OK)
1114             fSettings->AddRect( "playlist frame", fPlaylistWindow->Frame() );
1115         if (fSettings->ReplaceBool( "playlist showing", !fPlaylistWindow->IsHidden() ) != B_OK)
1116             fSettings->AddBool( "playlist showing", !fPlaylistWindow->IsHidden() );
1117         fPlaylistWindow->Unlock();
1118     }
1119     if ( fMessagesWindow->Lock() )
1120     {
1121         if (fSettings->ReplaceRect( "messages frame", fMessagesWindow->Frame() ) != B_OK)
1122             fSettings->AddRect( "messages frame", fMessagesWindow->Frame() );
1123         if (fSettings->ReplaceBool( "messages showing", !fMessagesWindow->IsHidden() ) != B_OK)
1124             fSettings->AddBool( "messages showing", !fMessagesWindow->IsHidden() );
1125         fMessagesWindow->Unlock();
1126     }
1127     if ( fPreferencesWindow->Lock() )
1128     {
1129         if (fSettings->ReplaceRect( "settings frame", fPreferencesWindow->Frame() ) != B_OK)
1130             fSettings->AddRect( "settings frame", fPreferencesWindow->Frame() );
1131         if (fSettings->ReplaceBool( "settings showing", !fPreferencesWindow->IsHidden() ) != B_OK)
1132             fSettings->AddBool( "settings showing", !fPreferencesWindow->IsHidden() );
1133         fPreferencesWindow->Unlock();
1134     }
1135     uint32 displayMode = fPlaylistWindow->DisplayMode();
1136     if (fSettings->ReplaceInt32( "playlist display mode", displayMode ) != B_OK )
1137         fSettings->AddInt32( "playlist display mode", displayMode );
1138
1139     save_settings( fSettings, "interface_settings", "VideoLAN Client" );
1140 }
1141
1142
1143 /*****************************************************************************
1144  * CDMenu::CDMenu
1145  *****************************************************************************/
1146 CDMenu::CDMenu(const char *name)
1147       : BMenu(name)
1148 {
1149 }
1150
1151 /*****************************************************************************
1152  * CDMenu::~CDMenu
1153  *****************************************************************************/
1154 CDMenu::~CDMenu()
1155 {
1156 }
1157
1158 /*****************************************************************************
1159  * CDMenu::AttachedToWindow
1160  *****************************************************************************/
1161 void CDMenu::AttachedToWindow(void)
1162 {
1163     // remove all items
1164     while ( BMenuItem* item = RemoveItem( 0L ) )
1165         delete item;
1166     GetCD( "/dev/disk" );
1167     BMenu::AttachedToWindow();
1168 }
1169
1170 /*****************************************************************************
1171  * CDMenu::GetCD
1172  *****************************************************************************/
1173 int CDMenu::GetCD( const char *directory )
1174 {
1175     BVolumeRoster volRoster;
1176     BVolume vol;
1177     BDirectory dir;
1178     status_t status = volRoster.GetNextVolume( &vol );
1179     while ( status ==  B_NO_ERROR )
1180     {
1181         BString deviceName;
1182         BString volumeName;
1183         bool isCDROM;
1184         if ( get_volume_info( vol, volumeName, isCDROM, deviceName )
1185              && isCDROM )
1186         {
1187             BMessage* msg = new BMessage( OPEN_DVD );
1188             msg->AddString( "device", deviceName.String() );
1189             BMenuItem* item = new BMenuItem( volumeName.String(), msg );
1190             AddItem( item );
1191         }
1192          vol.Unset();
1193         status = volRoster.GetNextVolume( &vol );
1194     }
1195     return 0;
1196 }
1197
1198 /*****************************************************************************
1199  * LanguageMenu::LanguageMenu
1200  *****************************************************************************/
1201 LanguageMenu::LanguageMenu( intf_thread_t * _p_intf, const char * psz_name,
1202                             char * _psz_variable )
1203     : BMenu( psz_name )
1204 {
1205     p_intf       = _p_intf;
1206     psz_variable = strdup( _psz_variable );
1207 }
1208
1209 /*****************************************************************************
1210  * LanguageMenu::~LanguageMenu
1211  *****************************************************************************/
1212 LanguageMenu::~LanguageMenu()
1213 {
1214     free( psz_variable );
1215 }
1216
1217 /*****************************************************************************
1218  * LanguageMenu::AttachedToWindow
1219  *****************************************************************************/
1220 void LanguageMenu::AttachedToWindow()
1221 {
1222     BMenuItem * item;
1223
1224     // remove all items
1225     while( ( item = RemoveItem( 0L ) ) )
1226     {
1227         delete item;
1228     }
1229
1230     SetRadioMode( true );
1231
1232     input_thread_t * p_input = (input_thread_t *)
1233             vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1234     if( !p_input )
1235     {
1236         return;
1237     }
1238     
1239     vlc_value_t val_list, text_list;
1240     BMessage * message;
1241     int i_current;
1242     
1243     i_current = var_GetInteger( p_input, psz_variable );
1244     var_Change( p_input, psz_variable, VLC_VAR_GETLIST, &val_list, &text_list );
1245     for( int i = 0; i < val_list.p_list->i_count; i++ )
1246     {
1247         message = new BMessage( SELECT_CHANNEL );
1248         message->AddInt32( psz_variable, val_list.p_list->p_values[i].i_int );
1249         item = new BMenuItem( text_list.p_list->p_values[i].psz_string, message );
1250         if( val_list.p_list->p_values[i].i_int == i_current )
1251         {
1252             item->SetMarked( true );
1253         }
1254         AddItem( item );
1255     }
1256     var_Change( p_input, psz_variable, VLC_VAR_FREELIST, &val_list, &text_list );
1257     
1258     vlc_object_release( p_input );
1259     
1260     BMenu::AttachedToWindow();
1261 }
1262
1263 /*****************************************************************************
1264  * TitleMenu::TitleMenu
1265  *****************************************************************************/
1266 TitleMenu::TitleMenu( const char *name, intf_thread_t  *p_interface )
1267     : BMenu(name),
1268     p_intf( p_interface )
1269 {
1270 }
1271
1272 /*****************************************************************************
1273  * TitleMenu::~TitleMenu
1274  *****************************************************************************/
1275 TitleMenu::~TitleMenu()
1276 {
1277 }
1278
1279 /*****************************************************************************
1280  * TitleMenu::AttachedToWindow
1281  *****************************************************************************/
1282 void TitleMenu::AttachedToWindow()
1283 {
1284     BMenuItem * item;
1285     while( ( item = RemoveItem( 0L ) ) )
1286     {
1287         delete item;
1288     }
1289
1290     input_thread_t * p_input;
1291     p_input = (input_thread_t *)
1292         vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1293     if( !p_input )
1294     {
1295         return;
1296     }
1297
1298     vlc_value_t val;
1299     if( !var_Get( p_input, "title", &val ) )
1300     {
1301         vlc_value_t val_list, text_list;
1302         var_Change( p_input, "title", VLC_VAR_GETCHOICES,
1303                     &val_list, &text_list );
1304         
1305         for( int i = 0; i < val_list.p_list->i_count; i++ )
1306         {
1307             item = new BMenuItem( text_list.p_list->p_values[i].psz_string,
1308                                   NULL );
1309             if( val_list.p_list->p_values[i].i_int == val.i_int )
1310             {
1311                 item->SetMarked( true );
1312             }
1313             AddItem( item );
1314         }
1315
1316         var_Change( p_input, "title", VLC_VAR_FREELIST,
1317                     &val_list, &text_list );
1318     }
1319     BMenu::AttachedToWindow();
1320 }
1321
1322
1323 /*****************************************************************************
1324  * ChapterMenu::ChapterMenu
1325  *****************************************************************************/
1326 ChapterMenu::ChapterMenu( const char *name, intf_thread_t  *p_interface )
1327     : BMenu(name),
1328     p_intf( p_interface )
1329 {
1330 }
1331
1332 /*****************************************************************************
1333  * ChapterMenu::~ChapterMenu
1334  *****************************************************************************/
1335 ChapterMenu::~ChapterMenu()
1336 {
1337 }
1338
1339 /*****************************************************************************
1340  * ChapterMenu::AttachedToWindow
1341  *****************************************************************************/
1342 void ChapterMenu::AttachedToWindow()
1343 {
1344     BMenuItem * item;
1345     while( ( item = RemoveItem( 0L ) ) )
1346     {
1347         delete item;
1348     }
1349
1350     input_thread_t * p_input;
1351     p_input = (input_thread_t *)
1352         vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
1353     if( !p_input )
1354     {
1355         return;
1356     }
1357
1358     vlc_value_t val;
1359     if( !var_Get( p_input, "chapter", &val ) )
1360     {
1361         vlc_value_t val_list, text_list;
1362         var_Change( p_input, "chapter", VLC_VAR_GETCHOICES,
1363                     &val_list, &text_list );
1364         
1365         for( int i = 0; i < val_list.p_list->i_count; i++ )
1366         {
1367             item = new BMenuItem( text_list.p_list->p_values[i].psz_string,
1368                                   NULL );
1369             if( val_list.p_list->p_values[i].i_int == val.i_int )
1370             {
1371                 item->SetMarked( true );
1372             }
1373             AddItem( item );
1374         }
1375
1376         var_Change( p_input, "chapter", VLC_VAR_FREELIST,
1377                     &val_list, &text_list );
1378     }
1379     BMenu::AttachedToWindow();
1380 }
1381
1382
1383 /*****************************************************************************
1384  * load_settings
1385  *****************************************************************************/
1386 status_t
1387 load_settings( BMessage* message, const char* fileName, const char* folder )
1388 {
1389     status_t ret = B_BAD_VALUE;
1390     if ( message )
1391     {
1392         BPath path;
1393         if ( ( ret = find_directory( B_USER_SETTINGS_DIRECTORY, &path ) ) == B_OK )
1394         {
1395             // passing folder is optional
1396             if ( folder )
1397                 ret = path.Append( folder );
1398             if ( ret == B_OK && ( ret = path.Append( fileName ) ) == B_OK )
1399             {
1400                 BFile file( path.Path(), B_READ_ONLY );
1401                 if ( ( ret = file.InitCheck() ) == B_OK )
1402                 {
1403                     ret = message->Unflatten( &file );
1404                     file.Unset();
1405                 }
1406             }
1407         }
1408     }
1409     return ret;
1410 }
1411
1412 /*****************************************************************************
1413  * save_settings
1414  *****************************************************************************/
1415 status_t
1416 save_settings( BMessage* message, const char* fileName, const char* folder )
1417 {
1418     status_t ret = B_BAD_VALUE;
1419     if ( message )
1420     {
1421         BPath path;
1422         if ( ( ret = find_directory( B_USER_SETTINGS_DIRECTORY, &path ) ) == B_OK )
1423         {
1424             // passing folder is optional
1425             if ( folder && ( ret = path.Append( folder ) ) == B_OK )
1426                 ret = create_directory( path.Path(), 0777 );
1427             if ( ret == B_OK && ( ret = path.Append( fileName ) ) == B_OK )
1428             {
1429                 BFile file( path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE );
1430                 if ( ( ret = file.InitCheck() ) == B_OK )
1431                 {
1432                     ret = message->Flatten( &file );
1433                     file.Unset();
1434                 }
1435             }
1436         }
1437     }
1438     return ret;
1439 }