]> git.sesse.net Git - vlc/blob - modules/gui/macosx/open.m
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / gui / macosx / open.m
1 /*****************************************************************************
2  * open.m: MacOS X module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Derk-Jan Hartman <thedj@users.sourceforge.net>
10  *          Benjamin Pracht <bigben at videolan dot org>
11  *          Felix Kühne <fkuehne at videolan dot org>
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 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <sys/param.h>                                    /* for MAXPATHLEN */
33 #include <string.h>
34
35 #include <paths.h>
36 #include <IOKit/IOKitLib.h>
37 #include <IOKit/IOBSD.h>
38 #include <IOKit/storage/IOMedia.h>
39 #include <IOKit/storage/IOCDMedia.h>
40 #include <IOKit/storage/IODVDMedia.h>
41
42 #import "intf.h"
43 #import "playlist.h"
44 #import "open.h"
45 #import "output.h"
46 #import "eyetv.h"
47
48 /*****************************************************************************
49  * GetEjectableMediaOfClass
50  *****************************************************************************/
51 NSArray *GetEjectableMediaOfClass( const char *psz_class )
52 {
53     io_object_t next_media;
54     mach_port_t master_port;
55     kern_return_t kern_result;
56     NSArray *o_devices = nil;
57     NSMutableArray *p_list = nil;
58     io_iterator_t media_iterator;
59     CFMutableDictionaryRef classes_to_match;
60
61     kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
62     if( kern_result != KERN_SUCCESS )
63     {
64         return( nil );
65     }
66  
67     classes_to_match = IOServiceMatching( psz_class );
68     if( classes_to_match == NULL )
69     {
70         return( nil );
71     }
72  
73     CFDictionarySetValue( classes_to_match, CFSTR( kIOMediaEjectableKey ),
74                           kCFBooleanTrue );
75  
76     kern_result = IOServiceGetMatchingServices( master_port, classes_to_match,
77                                                 &media_iterator );
78     if( kern_result != KERN_SUCCESS )
79     {
80         return( nil );
81     }
82
83     p_list = [NSMutableArray arrayWithCapacity: 1];
84  
85     next_media = IOIteratorNext( media_iterator );
86     if( next_media )
87     {
88         char psz_buf[0x32];
89         size_t dev_path_length;
90         CFTypeRef str_bsd_path;
91  
92         do
93         {
94             str_bsd_path = IORegistryEntryCreateCFProperty( next_media,
95                                                             CFSTR( kIOBSDNameKey ),
96                                                             kCFAllocatorDefault,
97                                                             0 );
98             if( str_bsd_path == NULL )
99             {
100                 IOObjectRelease( next_media );
101                 continue;
102             }
103  
104             snprintf( psz_buf, sizeof(psz_buf), "%s%c", _PATH_DEV, 'r' );
105             dev_path_length = strlen( psz_buf );
106  
107             if( CFStringGetCString( str_bsd_path,
108                                     (char*)&psz_buf + dev_path_length,
109                                     sizeof(psz_buf) - dev_path_length,
110                                     kCFStringEncodingASCII ) )
111             {
112                 [p_list addObject: [NSString stringWithUTF8String: psz_buf]];
113             }
114  
115             CFRelease( str_bsd_path );
116  
117             IOObjectRelease( next_media );
118  
119         } while( ( next_media = IOIteratorNext( media_iterator ) ) );
120     }
121  
122     IOObjectRelease( media_iterator );
123
124     o_devices = [NSArray arrayWithArray: p_list];
125
126     return( o_devices );
127 }
128
129 /*****************************************************************************
130  * VLCOpen implementation
131  *****************************************************************************/
132 @implementation VLCOpen
133
134 static VLCOpen *_o_sharedMainInstance = nil;
135
136 + (VLCOpen *)sharedInstance
137 {
138     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
139 }
140
141 - (id)init
142 {
143     if( _o_sharedMainInstance) {
144         [self dealloc];
145     } else {
146         _o_sharedMainInstance = [super init];
147     }
148  
149     return _o_sharedMainInstance;
150 }
151
152 - (void)awakeFromNib
153 {
154     intf_thread_t * p_intf = VLCIntf;
155
156     [o_panel setTitle: _NS("Open Source")];
157     [o_mrl_lbl setTitle: _NS("Media Resource Locator (MRL)")];
158
159     [o_btn_ok setTitle: _NS("OK")];
160     [o_btn_cancel setTitle: _NS("Cancel")];
161
162     [[o_tabview tabViewItemAtIndex: 0] setLabel: _NS("File")];
163     [[o_tabview tabViewItemAtIndex: 1] setLabel: _NS("Disc")];
164     [[o_tabview tabViewItemAtIndex: 2] setLabel: _NS("Network")];
165
166     [o_file_btn_browse setTitle: _NS("Browse...")];
167     [o_file_stream setTitle: _NS("Treat as a pipe rather than as a file")];
168
169     [o_disc_device_lbl setStringValue: _NS("Device name")];
170     [o_disc_title_lbl setStringValue: _NS("Title")];
171     [o_disc_chapter_lbl setStringValue: _NS("Chapter")];
172     [o_disc_videots_btn_browse setTitle: _NS("Browse...")];
173     [o_disc_dvd_menus setTitle: _NS("No DVD menus")];
174
175     [[o_disc_type cellAtRow:0 column:0] setTitle: _NS("VIDEO_TS directory")];
176     [[o_disc_type cellAtRow:1 column:0] setTitle: _NS("DVD")];
177     [[o_disc_type cellAtRow:2 column:0] setTitle: _NS("VCD")];
178     [[o_disc_type cellAtRow:3 column:0] setTitle: _NS("Audio CD")];
179
180     [o_net_udp_port_lbl setStringValue: _NS("Port")];
181     [o_net_udpm_addr_lbl setStringValue: _NS("Address")];
182     [o_net_udpm_port_lbl setStringValue: _NS("Port")];
183     [o_net_http_url_lbl setStringValue: _NS("URL")];
184
185     [[o_net_mode cellAtRow:0 column:0] setTitle: _NS("UDP/RTP")];
186     [[o_net_mode cellAtRow:1 column:0] setTitle: _NS("UDP/RTP Multicast")];
187     [[o_net_mode cellAtRow:2 column:0] setTitle: _NS("HTTP/FTP/MMS/RTSP")];
188     [o_net_timeshift_ckbox setTitle: _NS("Allow timeshifting")];
189
190     [o_net_udp_port setIntValue: config_GetInt( p_intf, "server-port" )];
191     [o_net_udp_port_stp setIntValue: config_GetInt( p_intf, "server-port" )];
192  
193     [o_eyetv_chn_bgbar setUsesThreadedAnimation: YES];
194     /* FIXME: implement EyeTV l10n here */
195  
196     [self setSubPanel];
197
198
199     [[NSNotificationCenter defaultCenter] addObserver: self
200         selector: @selector(openFilePathChanged:)
201         name: NSControlTextDidChangeNotification
202         object: o_file_path];
203
204     [[NSNotificationCenter defaultCenter] addObserver: self
205         selector: @selector(openDiscInfoChanged:)
206         name: NSControlTextDidChangeNotification
207         object: o_disc_device];
208     [[NSNotificationCenter defaultCenter] addObserver: self
209         selector: @selector(openDiscInfoChanged:)
210         name: NSControlTextDidChangeNotification
211         object: o_disc_title];
212     [[NSNotificationCenter defaultCenter] addObserver: self
213         selector: @selector(openDiscInfoChanged:)
214         name: NSControlTextDidChangeNotification
215         object: o_disc_chapter];
216     [[NSNotificationCenter defaultCenter] addObserver: self
217         selector: @selector(openDiscInfoChanged:)
218         name: NSControlTextDidChangeNotification
219         object: o_disc_videots_folder];
220
221     [[NSNotificationCenter defaultCenter] addObserver: self
222         selector: @selector(openNetInfoChanged:)
223         name: NSControlTextDidChangeNotification
224         object: o_net_udp_port];
225     [[NSNotificationCenter defaultCenter] addObserver: self
226         selector: @selector(openNetInfoChanged:)
227         name: NSControlTextDidChangeNotification
228         object: o_net_udpm_addr];
229     [[NSNotificationCenter defaultCenter] addObserver: self
230         selector: @selector(openNetInfoChanged:)
231         name: NSControlTextDidChangeNotification
232         object: o_net_udpm_port];
233     [[NSNotificationCenter defaultCenter] addObserver: self
234         selector: @selector(openNetInfoChanged:)
235         name: NSControlTextDidChangeNotification
236         object: o_net_http_url];
237
238     /* wake up with the correct EyeTV GUI */
239     if( [[[VLCMain sharedInstance] getEyeTVController] isEyeTVrunning] == YES )
240     {
241         if( [[[VLCMain sharedInstance] getEyeTVController] isDeviceConnected] == YES )
242         {
243             [o_eyetv_tabView selectTabViewItemWithIdentifier:@"eyetvup"];
244             [self setupChannelInfo];
245         }
246         else 
247             [o_eyetv_tabView selectTabViewItemWithIdentifier:@"nodevice"];
248     }
249     else
250         [o_eyetv_tabView selectTabViewItemWithIdentifier:@"noeyetv"];
251
252     [[NSDistributedNotificationCenter defaultCenter] addObserver: self
253                                                         selector: @selector(eyetvChanged:)
254                                                             name: NULL
255                                                           object: @"VLCEyeTVSupport"
256                                               suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
257  
258     /* register clicks on text fields */
259     [[NSNotificationCenter defaultCenter] addObserver: self
260                                              selector: @selector(textFieldWasClicked:)
261                                                  name: @"VLCOpenTextFieldWasClicked"
262                                                object: nil];
263 }
264
265 - (void)setSubPanel
266 {
267     intf_thread_t * p_intf = VLCIntf;
268     int i_index;
269     module_config_t * p_item;
270
271     [o_file_sub_ckbox setTitle: _NS("Load subtitles file:")];
272     [o_file_sub_btn_settings setTitle: _NS("Settings...")];
273     [o_file_sub_btn_browse setTitle: _NS("Browse...")];
274     [o_file_sub_override setTitle: _NS("Override parametters")];
275     [o_file_sub_delay_lbl setStringValue: _NS("Delay")];
276     [o_file_sub_delay_stp setEnabled: NO];
277     [o_file_sub_fps_lbl setStringValue: _NS("FPS")];
278     [o_file_sub_fps_stp setEnabled: NO];
279     [o_file_sub_encoding_lbl setStringValue: _NS("Subtitles encoding")];
280     [o_file_sub_encoding_pop removeAllItems];
281     [o_file_sub_size_lbl setStringValue: _NS("Font size")];
282     [o_file_sub_size_pop removeAllItems];
283     [o_file_sub_align_lbl setStringValue: _NS("Subtitles alignment")];
284     [o_file_sub_align_pop removeAllItems];
285     [o_file_sub_ok_btn setStringValue: _NS("OK")];
286     [o_file_sub_font_box setTitle: _NS("Font Properties")];
287     [o_file_sub_file_box setTitle: _NS("Subtitle File")];
288
289     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-encoding" );
290
291     if( p_item )
292     {
293         for( i_index = 0; p_item->ppsz_list && p_item->ppsz_list[i_index];
294              i_index++ )
295         {
296             [o_file_sub_encoding_pop addItemWithTitle:
297                 [NSString stringWithUTF8String: p_item->ppsz_list[i_index]]];
298         }
299         [o_file_sub_encoding_pop selectItemWithTitle:
300                 [NSString stringWithUTF8String: p_item->value.psz]];
301     }
302
303     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-align" );
304
305     if ( p_item )
306     {
307         for ( i_index = 0; i_index < p_item->i_list; i_index++ )
308         {
309             [o_file_sub_align_pop addItemWithTitle:
310                 [NSString stringWithUTF8String:
311                 p_item->ppsz_list_text[i_index]]];
312         }
313         [o_file_sub_align_pop selectItemAtIndex: p_item->value.i];
314     }
315
316     p_item = config_FindConfig( VLC_OBJECT(p_intf), "freetype-rel-fontsize" );
317
318     if ( p_item )
319     {
320         for ( i_index = 0; i_index < p_item->i_list; i_index++ )
321         {
322             [o_file_sub_size_pop addItemWithTitle:
323                 [NSString stringWithUTF8String:
324                 p_item->ppsz_list_text[i_index]]];
325             if ( p_item->value.i == p_item->pi_list[i_index] )
326             {
327                 [o_file_sub_size_pop selectItemAtIndex: i_index];
328             }
329         }
330     }
331 }
332
333 - (void)openTarget:(int)i_type
334 {
335     int i_result;
336     intf_thread_t * p_intf = VLCIntf;
337
338     b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
339
340     [o_tabview selectTabViewItemAtIndex: i_type];
341     [o_file_sub_ckbox setState: NSOffState];
342  
343     i_result = [NSApp runModalForWindow: o_panel];
344     [o_panel close];
345
346     if( i_result )
347     {
348         NSMutableDictionary *o_dic;
349         NSMutableArray *o_options = [NSMutableArray array];
350         unsigned int i;
351
352         o_dic = [NSMutableDictionary dictionaryWithObject: [o_mrl stringValue] forKey: @"ITEM_URL"];
353         if( [o_file_sub_ckbox state] == NSOnState )
354         {
355             module_config_t * p_item;
356
357             [o_options addObject: [NSString stringWithFormat: @"sub-file=%@", [o_file_sub_path stringValue]]];
358             if( [o_file_sub_override state] == NSOnState )
359             {
360                 [o_options addObject: [NSString stringWithFormat: @"sub-delay=%i", (int)( [o_file_sub_delay intValue] * 10 )]];
361                 [o_options addObject: [NSString stringWithFormat: @"sub-fps=%f", [o_file_sub_fps floatValue]]];
362             }
363             [o_options addObject: [NSString stringWithFormat:
364                     @"subsdec-encoding=%@",
365                     [o_file_sub_encoding_pop titleOfSelectedItem]]];
366             [o_options addObject: [NSString stringWithFormat:
367                     @"subsdec-align=%i",
368                     [o_file_sub_align_pop indexOfSelectedItem]]];
369
370             p_item = config_FindConfig( VLC_OBJECT(p_intf),
371                                             "freetype-rel-fontsize" );
372
373             if ( p_item )
374             {
375                 [o_options addObject: [NSString stringWithFormat:
376                     @"freetype-rel-fontsize=%i",
377                     p_item->pi_list[[o_file_sub_size_pop indexOfSelectedItem]]]];
378             }
379         }
380         if( [o_output_ckbox state] == NSOnState )
381         {
382             for (i = 0 ; i < [[o_sout_options getMRL] count] ; i++)
383             {
384                 [o_options addObject: [NSString stringWithString:
385                       [[(VLCOutput *)o_sout_options getMRL] objectAtIndex: i]]];
386             }
387         }
388         if( [o_net_timeshift_ckbox state] == NSOnState )
389         {
390             [o_options addObject: [NSString stringWithString:
391                                                 @"access-filter=timeshift"]];
392         }
393         [o_dic setObject: (NSArray *)[o_options copy] forKey: @"ITEM_OPTIONS"];
394         if( b_autoplay )
395             [o_playlist appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:NO];
396         else
397             [o_playlist appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:YES];
398     }
399 }
400
401 - (void)tabView:(NSTabView *)o_tv didSelectTabViewItem:(NSTabViewItem *)o_tvi
402 {
403     NSString *o_label = [o_tvi label];
404
405     if( [o_label isEqualToString: _NS("File")] )
406     {
407         [self openFilePathChanged: nil];
408     }
409     else if( [o_label isEqualToString: _NS("Disc")] )
410     {
411         [self openDiscTypeChanged: nil];
412     }
413     else if( [o_label isEqualToString: _NS("Network")] )
414     {
415         [self openNetInfoChanged: nil];
416     }
417     else if( [o_label isEqualToString: _NS("EyeTV")] )
418     {
419         [o_mrl setStringValue: @"eyetv://"];
420     }
421 }
422
423 - (void)openFileGeneric
424 {
425     [self openFilePathChanged: nil];
426     [self openTarget: 0];
427 }
428
429 - (void)openDisc
430 {
431     [self openDiscTypeChanged: nil];
432     [self openTarget: 1];
433 }
434
435 - (void)openNet
436 {
437     [self openNetInfoChanged: nil];
438     [self openTarget: 2];
439 }
440
441 - (void)openFilePathChanged:(NSNotification *)o_notification
442 {
443     NSString *o_mrl_string;
444     NSString *o_filename = [o_file_path stringValue];
445     NSString *o_ext = [o_filename pathExtension];
446     bool b_stream = [o_file_stream state];
447     BOOL b_dir = NO;
448  
449     [[NSFileManager defaultManager] fileExistsAtPath:o_filename isDirectory:&b_dir];
450
451     if( b_dir )
452     {
453         o_mrl_string = [NSString stringWithFormat: @"dir:%@", o_filename];
454     }
455     else if( [o_ext isEqualToString: @"bin"] ||
456         [o_ext isEqualToString: @"cue"] ||
457         [o_ext isEqualToString: @"vob"] ||
458         [o_ext isEqualToString: @"iso"] )
459     {
460         o_mrl_string = o_filename;
461     }
462     else
463     {
464         o_mrl_string = [NSString stringWithFormat: @"%s://%@",
465                         b_stream ? "stream" : "file",
466                         o_filename];
467     }
468     [o_mrl setStringValue: o_mrl_string];
469 }
470
471 - (IBAction)openFileBrowse:(id)sender
472 {
473     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
474  
475     [o_open_panel setAllowsMultipleSelection: NO];
476     [o_open_panel setCanChooseDirectories: YES];
477     [o_open_panel setTitle: _NS("Open File")];
478     [o_open_panel setPrompt: _NS("Open")];
479
480     [o_open_panel beginSheetForDirectory:nil
481         file:nil
482         types:nil
483         modalForWindow:[sender window]
484         modalDelegate: self
485         didEndSelector: @selector(pathChosenInPanel:
486                         withReturn:
487                         contextInfo:)
488         contextInfo: nil];
489 }
490
491 - (void)pathChosenInPanel: (NSOpenPanel *) sheet withReturn:(int)returnCode contextInfo:(void  *)contextInfo
492 {
493     if (returnCode == NSFileHandlingPanelOKButton)
494     {
495         NSString *o_filename = [[sheet filenames] objectAtIndex: 0];
496         [o_file_path setStringValue: o_filename];
497         [self openFilePathChanged: nil];
498     }
499 }
500
501 - (IBAction)openFileStreamChanged:(id)sender
502 {
503     [self openFilePathChanged: nil];
504 }
505
506 - (IBAction)openDiscTypeChanged:(id)sender
507 {
508     NSString *o_type;
509     BOOL b_device, b_no_menus, b_title_chapter;
510  
511     [o_disc_device removeAllItems];
512     b_title_chapter = ![o_disc_dvd_menus state];
513  
514     o_type = [[o_disc_type selectedCell] title];
515
516     if ( [o_type isEqualToString: _NS("VIDEO_TS directory")] )
517     {
518         b_device = NO; b_no_menus = YES;
519     }
520     else
521     {
522         NSArray *o_devices;
523         NSString *o_disc;
524         const char *psz_class = NULL;
525         b_device = YES;
526
527         if ( [o_type isEqualToString: _NS("VCD")] )
528         {
529             psz_class = kIOCDMediaClass;
530             o_disc = o_type;
531             b_no_menus = NO; b_title_chapter = YES;
532                 }
533         else if ( [o_type isEqualToString: _NS("Audio CD")])
534         {
535             psz_class = kIOCDMediaClass;
536             o_disc = o_type;
537             b_no_menus = NO; b_title_chapter = NO;
538         }
539         else
540         {
541             psz_class = kIODVDMediaClass;
542             o_disc = o_type;
543             b_no_menus = YES;
544         }
545  
546         o_devices = GetEjectableMediaOfClass( psz_class );
547         if ( o_devices != nil )
548         {
549             int i_devices = [o_devices count];
550  
551             if ( i_devices )
552             {
553                                 for( int i = 0; i < i_devices; i++ )
554                 {
555                     [o_disc_device
556                         addItemWithObjectValue: [o_devices objectAtIndex: i]];
557                 }
558
559                 [o_disc_device selectItemAtIndex: 0];
560             }
561             else
562             {
563                 [o_disc_device setStringValue:
564                     [NSString stringWithFormat: _NS("No %@s found"), o_disc]];
565             }
566         }
567     }
568
569     [o_disc_device setEnabled: b_device];
570     [o_disc_title setEnabled: b_title_chapter];
571     [o_disc_title_stp setEnabled: b_title_chapter];
572     [o_disc_chapter setEnabled: b_title_chapter];
573     [o_disc_chapter_stp setEnabled: b_title_chapter];
574     [o_disc_videots_folder setEnabled: !b_device];
575     [o_disc_videots_btn_browse setEnabled: !b_device];
576     [o_disc_dvd_menus setEnabled: b_no_menus];
577
578     [self openDiscInfoChanged: nil];
579 }
580
581 - (IBAction)openDiscStepperChanged:(id)sender
582 {
583     int i_tag = [sender tag];
584
585     if( i_tag == 0 )
586     {
587         [o_disc_title setIntValue: [o_disc_title_stp intValue]];
588     }
589     else if( i_tag == 1 )
590     {
591         [o_disc_chapter setIntValue: [o_disc_chapter_stp intValue]];
592     }
593
594     [self openDiscInfoChanged: nil];
595 }
596
597 - (void)openDiscInfoChanged:(NSNotification *)o_notification
598 {
599     NSString *o_type;
600     NSString *o_device;
601     NSString *o_videots;
602     NSString *o_mrl_string;
603     int i_title, i_chapter;
604     BOOL b_no_menus;
605
606     o_type = [[o_disc_type selectedCell] title];
607     o_device = [o_disc_device stringValue];
608     i_title = [o_disc_title intValue];
609     i_chapter = [o_disc_chapter intValue];
610     o_videots = [o_disc_videots_folder stringValue];
611     b_no_menus = [o_disc_dvd_menus state];
612
613     if ( [o_type isEqualToString: _NS("VCD")] )
614     {
615         if ( [o_device isEqualToString:
616                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
617             o_device = @"";
618         o_mrl_string = [NSString stringWithFormat: @"vcd://%@@%i:%i",
619                         o_device, i_title, i_chapter];
620     }
621     else if ( [o_type isEqualToString: _NS("Audio CD")] )
622     {
623         if ( [o_device isEqualToString:
624                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
625             o_device = @"";
626         o_mrl_string = [NSString stringWithFormat: @"cdda://%@",
627                         o_device];
628     }
629     else if ( [o_type isEqualToString: _NS("DVD")] )
630     {
631         if ( [o_device isEqualToString:
632                 [NSString stringWithFormat: _NS("No %@s found"), o_type]] )
633             o_device = @"";
634         if ( b_no_menus )
635             o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i:%i-",
636                             o_device, i_title, i_chapter];
637         else
638                         o_mrl_string = [NSString stringWithFormat: @"dvdnav://%@",
639                             o_device];
640             
641     }
642     else /* VIDEO_TS folder */
643     {
644         if ( b_no_menus )
645             o_mrl_string = [NSString stringWithFormat: @"dvdread://%@@%i:%i",
646                             o_videots, i_title, i_chapter];
647         else
648                         o_mrl_string = [NSString stringWithFormat: @"dvdnav://%@",
649                             o_videots];            
650     }
651
652     [o_mrl setStringValue: o_mrl_string];
653 }
654
655 - (IBAction)openDiscMenusChanged:(id)sender
656 {
657     [self openDiscInfoChanged: nil];
658     [self openDiscTypeChanged: nil];
659 }
660
661 - (IBAction)openVTSBrowse:(id)sender
662 {
663     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
664
665     [o_open_panel setAllowsMultipleSelection: NO];
666     [o_open_panel setCanChooseFiles: NO];
667     [o_open_panel setCanChooseDirectories: YES];
668     [o_open_panel setTitle: _NS("Open VIDEO_TS Directory")];
669     [o_open_panel setPrompt: _NS("Open")];
670
671     if( [o_open_panel runModalForDirectory: nil
672             file: nil types: nil] == NSOKButton )
673     {
674         NSString *o_dirname = [[o_open_panel filenames] objectAtIndex: 0];
675         [o_disc_videots_folder setStringValue: o_dirname];
676         [self openDiscInfoChanged: nil];
677     }
678 }
679
680 - (void)textFieldWasClicked:(NSNotification *)o_notification
681 {
682     if( [o_notification object] == o_net_udp_port )
683         [o_net_mode selectCellAtRow: 0 column: 0];
684     else if( [o_notification object] == o_net_udpm_addr ||
685              [o_notification object] == o_net_udpm_port )
686         [o_net_mode selectCellAtRow: 1 column: 0];
687     else
688         [o_net_mode selectCellAtRow: 2 column: 0];
689
690     [self openNetInfoChanged: nil];
691 }
692
693 - (IBAction)openNetModeChanged:(id)sender
694 {
695     if( [[sender selectedCell] tag] == 0 )
696         [o_panel makeFirstResponder: o_net_udp_port];
697     else if ( [[sender selectedCell] tag] == 1 )
698         [o_panel makeFirstResponder: o_net_udpm_addr];
699     else
700         [o_panel makeFirstResponder: o_net_http_url];
701
702     [self openNetInfoChanged: nil];
703 }
704
705 - (IBAction)openNetStepperChanged:(id)sender
706 {
707     int i_tag = [sender tag];
708
709     if( i_tag == 0 )
710     {
711         [o_net_udp_port setIntValue: [o_net_udp_port_stp intValue]];
712         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
713                                                             object: o_net_udp_port];
714         [o_panel makeFirstResponder: o_net_udp_port];
715     }
716     else if( i_tag == 1 )
717     {
718         [o_net_udpm_port setIntValue: [o_net_udpm_port_stp intValue]];
719         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
720                                                             object: o_net_udpm_port];
721         [o_panel makeFirstResponder: o_net_udpm_port];
722     }
723
724     [self openNetInfoChanged: nil];
725 }
726
727 - (void)openNetInfoChanged:(NSNotification *)o_notification
728 {
729     NSString *o_mode;
730     NSString *o_mrl_string = [NSString string];
731     intf_thread_t * p_intf = VLCIntf;
732
733     o_mode = [[o_net_mode selectedCell] title];
734
735     if( [o_mode isEqualToString: _NS("UDP/RTP")] )
736     {
737         int i_port = [o_net_udp_port intValue];
738
739         o_mrl_string = [NSString stringWithString: @"udp://"];
740
741         if( i_port != config_GetInt( p_intf, "server-port" ) )
742         {
743             o_mrl_string =
744                 [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
745         }
746     }
747     else if( [o_mode isEqualToString: _NS("UDP/RTP Multicast")] )
748     {
749         NSString *o_addr = [o_net_udpm_addr stringValue];
750         int i_port = [o_net_udpm_port intValue];
751
752         o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
753
754         if( i_port != config_GetInt( p_intf, "server-port" ) )
755         {
756             o_mrl_string =
757                 [o_mrl_string stringByAppendingFormat: @":%i", i_port];
758         }
759     }
760     else if( [o_mode isEqualToString: _NS("HTTP/FTP/MMS/RTSP")] )
761     {
762         NSString *o_url = [o_net_http_url stringValue];
763
764         if ( ![o_url hasPrefix:@"http:"] && ![o_url hasPrefix:@"ftp:"]
765               && ![o_url hasPrefix:@"mms"] && ![o_url hasPrefix:@"rtsp"] )
766             o_mrl_string = [NSString stringWithFormat: @"http://%@", o_url];
767         else
768             o_mrl_string = o_url;
769     }
770     [o_mrl setStringValue: o_mrl_string];
771 }
772
773 - (void)openFile
774 {
775     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
776     int i;
777     b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
778  
779     [o_open_panel setAllowsMultipleSelection: YES];
780     [o_open_panel setCanChooseDirectories: YES];
781     [o_open_panel setTitle: _NS("Open File")];
782     [o_open_panel setPrompt: _NS("Open")];
783  
784     if( [o_open_panel runModalForDirectory: nil
785             file: nil types: nil] == NSOKButton )
786     {
787         NSArray *o_array = [NSArray array];
788         NSArray *o_values = [[o_open_panel filenames]
789                 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
790
791         for( i = 0; i < (int)[o_values count]; i++)
792         {
793             NSDictionary *o_dic;
794             o_dic = [NSDictionary dictionaryWithObject:[o_values objectAtIndex:i] forKey:@"ITEM_URL"];
795             o_array = [o_array arrayByAddingObject: o_dic];
796         }
797         if( b_autoplay )
798             [o_playlist appendArray: o_array atPos: -1 enqueue:NO];
799         else
800             [o_playlist appendArray: o_array atPos: -1 enqueue:YES];
801     }
802 }
803
804 - (IBAction)eyetvSwitchChannel:(id)sender
805 {
806     if( sender == o_eyetv_nextProgram_btn )
807     {
808         int chanNum = [[[VLCMain sharedInstance] getEyeTVController] switchChannelUp: YES];
809         [o_eyetv_channels_pop selectItemWithTag:chanNum];
810         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
811     }
812     else if( sender == o_eyetv_previousProgram_btn )
813     {
814         int chanNum = [[[VLCMain sharedInstance] getEyeTVController] switchChannelUp: NO];
815         [o_eyetv_channels_pop selectItemWithTag:chanNum];
816         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
817     }
818     else if( sender == o_eyetv_channels_pop )
819     {
820         int chanNum = [[sender selectedItem] tag];
821         [[[VLCMain sharedInstance] getEyeTVController] selectChannel:chanNum];
822         [o_mrl setStringValue: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
823     }
824     else
825         msg_Err( VLCIntf, "eyetvSwitchChannel sent by unknown object" );
826 }
827
828 - (IBAction)eyetvLaunch:(id)sender
829 {
830     [[[VLCMain sharedInstance] getEyeTVController] launchEyeTV];
831 }
832
833 - (void)eyetvChanged:(NSNotification *)o_notification
834 {
835     if( [[o_notification name] isEqualToString: @"DeviceAdded"] )
836     {
837         msg_Dbg( VLCIntf, "eyetv device was added" );
838         [o_eyetv_tabView selectTabViewItemWithIdentifier:@"eyetvup"];
839         [self setupChannelInfo];
840     }
841     else if( [[o_notification name] isEqualToString: @"DeviceRemoved"] )
842     {
843         /* leave the channel selection like that,
844          * switch to our "no device" tab */
845         msg_Dbg( VLCIntf, "eyetv device was removed" );
846         [o_eyetv_tabView selectTabViewItemWithIdentifier:@"nodevice"];
847     }
848     else if( [[o_notification name] isEqualToString: @"PluginQuit"] )
849     {
850         /* switch to the "launch eyetv" tab */
851         msg_Dbg( VLCIntf, "eyetv was terminated" );
852         [o_eyetv_tabView selectTabViewItemWithIdentifier:@"noeyetv"];
853     }
854     else if( [[o_notification name] isEqualToString: @"PluginInit"] )
855     {
856         /* we got no device yet */
857         msg_Dbg( VLCIntf, "eyetv was launched, no device yet" );
858         [o_eyetv_tabView selectTabViewItemWithIdentifier:@"nodevice"];
859     }
860     else
861         msg_Warn( VLCIntf, "unknown external notify '%s' received", [[o_notification name] UTF8String] );
862 }
863
864 /* little helper method, since this code needs to be run by multiple objects */
865 - (void)setupChannelInfo
866 {
867     /* set up channel selection */
868     [o_eyetv_channels_pop removeAllItems];
869     [o_eyetv_chn_bgbar setHidden: NO];
870     [o_eyetv_chn_bgbar animate: self];
871     [o_eyetv_chn_status_txt setStringValue: _NS("Retrieving Channel Info...")];
872     [o_eyetv_chn_status_txt setHidden: NO];
873  
874     /* retrieve info */
875     NSEnumerator *channels = [[[VLCMain sharedInstance] getEyeTVController] allChannels];
876     int x = -2;
877     [[[o_eyetv_channels_pop menu] addItemWithTitle: _NS("Composite input")
878                                                action: nil
879                                         keyEquivalent: @""] setTag:x++];
880     [[[o_eyetv_channels_pop menu] addItemWithTitle: _NS("S-Video input")
881                                                action: nil
882                                         keyEquivalent: @""] setTag:x++];
883     if( channels ) 
884     {
885         NSString *channel;
886         [[o_eyetv_channels_pop menu] addItem: [NSMenuItem separatorItem]];
887         while( channel = [channels nextObject] )
888         {
889             /* we have to add items this way, because we accept duplicates
890              * additionally, we save a bit of time */
891             [[[o_eyetv_channels_pop menu] addItemWithTitle: channel
892                                                    action: nil
893                                             keyEquivalent: @""] setTag:++x];
894         }
895         /* make Tuner the default */
896         [o_eyetv_channels_pop selectItemWithTag:[[[VLCMain sharedInstance] getEyeTVController] currentChannel]];
897     }
898  
899     /* clean up GUI */
900     [o_eyetv_chn_bgbar setHidden: YES];
901     [o_eyetv_chn_status_txt setHidden: YES];
902 }
903
904 - (IBAction)subsChanged:(id)sender
905 {
906     if ([o_file_sub_ckbox state] == NSOnState)
907     {
908         [o_file_sub_btn_settings setEnabled:YES];
909     }
910     else
911     {
912         [o_file_sub_btn_settings setEnabled:NO];
913     }
914 }
915
916 - (IBAction)subSettings:(id)sender
917 {
918     [NSApp beginSheet: o_file_sub_sheet
919         modalForWindow: [sender window]
920         modalDelegate: self
921         didEndSelector: NULL
922         contextInfo: nil];
923 }
924
925 - (IBAction)subFileBrowse:(id)sender
926 {
927     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
928  
929     [o_open_panel setAllowsMultipleSelection: NO];
930     [o_open_panel setTitle: _NS("Open File")];
931     [o_open_panel setPrompt: _NS("Open")];
932
933     if( [o_open_panel runModalForDirectory: nil
934             file: nil types: nil] == NSOKButton )
935     {
936         NSString *o_filename = [[o_open_panel filenames] objectAtIndex: 0];
937         [o_file_sub_path setStringValue: o_filename];
938     }
939 }
940
941 - (IBAction)subOverride:(id)sender
942 {
943     BOOL b_state = [o_file_sub_override state];
944     [o_file_sub_delay setEnabled: b_state];
945     [o_file_sub_delay_stp setEnabled: b_state];
946     [o_file_sub_fps setEnabled: b_state];
947     [o_file_sub_fps_stp setEnabled: b_state];
948 }
949
950 - (IBAction)subDelayStepperChanged:(id)sender
951 {
952     [o_file_sub_delay setIntValue: [o_file_sub_delay_stp intValue]];
953 }
954
955 - (IBAction)subFpsStepperChanged:(id)sender;
956 {
957     [o_file_sub_fps setFloatValue: [o_file_sub_fps_stp floatValue]];
958 }
959
960 - (IBAction)subCloseSheet:(id)sender
961 {
962     [o_file_sub_sheet orderOut:sender];
963     [NSApp endSheet: o_file_sub_sheet];
964 }
965
966 - (IBAction)panelCancel:(id)sender
967 {
968     [NSApp stopModalWithCode: 0];
969 }
970
971 - (IBAction)panelOk:(id)sender
972 {
973     if( [[o_mrl stringValue] length] )
974     {
975         [NSApp stopModalWithCode: 1];
976     }
977     else
978     {
979         NSBeep();
980     }
981 }
982
983 @end
984
985 @implementation VLCOpenTextField
986
987 - (void)mouseDown:(NSEvent *)theEvent
988 {
989     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
990                                                         object: self];
991     [super mouseDown: theEvent];
992 }
993
994 @end