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