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