]> git.sesse.net Git - vlc/blob - modules/gui/macosx/open.m
2ec450543c77c55a635d013175f36f60a815cdc8
[vlc] / modules / gui / macosx / open.m
1 /*****************************************************************************
2  * open.m: Open dialogues for VLC's MacOS X port
3  *****************************************************************************
4  * Copyright (C) 2002-2012 VLC authors and VideoLAN
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 Paul 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 #import <stdlib.h>                                      /* malloc(), free() */
32 #import <sys/param.h>                                    /* for MAXPATHLEN */
33
34 #import "CompatibilityFixes.h"
35
36 #import <paths.h>
37 #import <IOKit/IOBSD.h>
38 #import <IOKit/storage/IOMedia.h>
39 #import <IOKit/storage/IOCDMedia.h>
40 #import <IOKit/storage/IODVDMedia.h>
41 #import <IOKit/storage/IOBDMedia.h>
42 #import <Cocoa/Cocoa.h>
43 #import <QTKit/QTKit.h>
44
45 #import "intf.h"
46 #import "playlist.h"
47 #import "open.h"
48 #import "output.h"
49 #import "eyetv.h"
50
51 #import <vlc_url.h>
52
53 NSArray               *qtkvideoDevices;
54 #define setEyeTVUnconnected \
55 [o_capture_lbl setStringValue: _NS("No device is selected")]; \
56 [o_capture_long_lbl setStringValue: _NS("No device is selected.\n\nChoose available device in above pull-down menu\n.")]; \
57 [o_capture_lbl displayIfNeeded]; \
58 [o_capture_long_lbl displayIfNeeded]; \
59 [self showCaptureView: o_capture_label_view]
60
61 #define kVLCMediaAudioCD "AudioCD"
62 #define kVLCMediaDVD "DVD"
63 #define kVLCMediaVCD "VCD"
64 #define kVLCMediaSVCD "SVCD"
65 #define kVLCMediaBD "Bluray"
66 #define kVLCMediaVideoTSFolder "VIDEO_TS"
67 #define kVLCMediaBDMVFolder "BDMV"
68 #define kVLCMediaUnknown "Unknown"
69
70
71 /*****************************************************************************
72  * VLCOpen implementation
73  *****************************************************************************/
74 @implementation VLCOpen
75
76 static VLCOpen *_o_sharedMainInstance = nil;
77
78 + (VLCOpen *)sharedInstance
79 {
80     return _o_sharedMainInstance ? _o_sharedMainInstance : [[self alloc] init];
81 }
82
83 - (id)init
84 {
85     if( _o_sharedMainInstance) {
86         [self dealloc];
87     } else {
88         _o_sharedMainInstance = [super init];
89         p_intf = VLCIntf;
90     }
91  
92     return _o_sharedMainInstance;
93 }
94
95 - (void)dealloc
96 {
97     [o_specialMediaFolders release];
98     [o_opticalDevices release];
99     if( o_file_slave_path )
100         [o_file_slave_path release];
101     [o_mrl release];
102     if (o_sub_path)
103         [o_sub_path release];
104     [o_currentOpticalMediaIconView release];
105     [o_currentOpticalMediaView release];
106     [super dealloc];
107 }
108
109 - (void)awakeFromNib
110 {
111     if (OSX_LION)
112         [o_panel setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
113
114     [o_panel setTitle: _NS("Open Source")];
115     [o_mrl_lbl setStringValue: _NS("Media Resource Locator (MRL)")];
116
117     [o_btn_ok setTitle: _NS("Open")];
118     [o_btn_cancel setTitle: _NS("Cancel")];
119
120     [[o_tabview tabViewItemAtIndex: 0] setLabel: _NS("File")];
121     [[o_tabview tabViewItemAtIndex: 1] setLabel: _NS("Disc")];
122     [[o_tabview tabViewItemAtIndex: 2] setLabel: _NS("Network")];
123     [[o_tabview tabViewItemAtIndex: 3] setLabel: _NS("Capture")];
124
125     [o_file_name setStringValue: @""];
126     [o_file_name_stub setStringValue: _NS("Choose a file")];
127     [o_file_icon_well setImage: [NSImage imageNamed:@"generic"]];
128     [o_file_btn_browse setTitle: _NS("Browse...")];
129     [o_file_stream setTitle: _NS("Treat as a pipe rather than as a file")];
130     [o_file_stream setHidden: NO];
131     [o_file_slave_ckbox setTitle: _NS("Play another media synchronously")];
132     [o_file_slave_select_btn setTitle: _NS("Choose...")];
133     [o_file_slave_filename_lbl setStringValue: @""];
134     [o_file_slave_icon_well setImage: NULL];
135     [o_file_subtitles_filename_lbl setStringValue: @""];
136     [o_file_subtitles_icon_well setImage: NULL];
137
138     [o_disc_selector_pop removeAllItems];
139     [o_disc_selector_pop setHidden: NO];
140     NSString *o_videots = _NS("Open VIDEO_TS folder");
141     NSString *o_bdmv = _NS("Open BDMV folder");
142     [o_disc_nodisc_lbl setStringValue: _NS("Insert Disc")];
143     [o_disc_nodisc_videots_btn setTitle: o_videots];
144     [o_disc_nodisc_bdmv_btn setTitle: o_bdmv];
145     [o_disc_audiocd_lbl setStringValue: _NS("Audio CD")];
146     [o_disc_audiocd_trackcount_lbl setStringValue: @""];
147     [o_disc_audiocd_videots_btn setTitle: o_videots];
148     [o_disc_audiocd_bdmv_btn setTitle: o_bdmv];
149     [o_disc_dvd_lbl setStringValue: @""];
150     [o_disc_dvd_disablemenus_btn setTitle: _NS("Disable DVD menus")];
151     [o_disc_dvd_videots_btn setTitle: o_videots];
152     [o_disc_dvd_bdmv_btn setTitle: o_bdmv];
153     [o_disc_dvdwomenus_lbl setStringValue: @""];
154     [o_disc_dvdwomenus_enablemenus_btn setTitle: _NS("Enable DVD menus")];
155     [o_disc_dvdwomenus_videots_btn setTitle: o_videots];
156     [o_disc_dvdwomenus_bdmv_btn setTitle: o_bdmv];
157     [o_disc_dvdwomenus_title_lbl setStringValue: _NS("Title")];
158     [o_disc_dvdwomenus_chapter_lbl setStringValue: _NS("Chapter")];
159     [o_disc_vcd_title_lbl setStringValue: _NS("Title")];
160     [o_disc_vcd_chapter_lbl setStringValue: _NS("Chapter")];
161     [o_disc_vcd_videots_btn setTitle: o_videots];
162     [o_disc_vcd_bdmv_btn setTitle: o_bdmv];
163     [o_disc_bd_videots_btn setTitle: o_videots];
164     [o_disc_bd_bdmv_btn setTitle: o_bdmv];
165
166     [o_net_udp_port_lbl setStringValue: _NS("Port")];
167     [o_net_udpm_addr_lbl setStringValue: _NS("IP Address")];
168     [o_net_udpm_port_lbl setStringValue: _NS("Port")];
169     [o_net_http_url_lbl setStringValue: _NS("URL")];
170     [o_net_help_lbl setStringValue: _NS("To Open a usual network stream (HTTP, RTSP, RTMP, MMS, FTP, etc.), just enter the URL in the field above. If you want to open a RTP or UDP stream, press the button below.")];
171     [o_net_help_udp_lbl setStringValue: _NS("If you want to open a multicast stream, enter the respective IP address given by the stream provider. In unicast mode, VLC will use your machine's IP automatically.\n\nTo open a stream using a different protocol, just press Cancel to close this sheet.")];
172     [o_net_udp_cancel_btn setTitle: _NS("Cancel")];
173     [o_net_udp_ok_btn setTitle: _NS("Open")];
174     [o_net_openUDP_btn setTitle: _NS("Open RTP/UDP Stream")];
175     [o_net_udp_mode_lbl setStringValue: _NS("Mode")];
176     [o_net_udp_protocol_lbl setStringValue: _NS("Protocol")];
177     [o_net_udp_address_lbl setStringValue: _NS("Address")];
178
179     [[o_net_mode cellAtRow:0 column:0] setTitle: _NS("Unicast")];
180     [[o_net_mode cellAtRow:1 column:0] setTitle: _NS("Multicast")];
181
182     [o_net_udp_port setIntValue: config_GetInt( p_intf, "server-port" )];
183     [o_net_udp_port_stp setIntValue: config_GetInt( p_intf, "server-port" )];
184
185     [o_eyetv_chn_bgbar setUsesThreadedAnimation: YES];
186
187     [o_capture_mode_pop removeAllItems];
188     [o_capture_mode_pop addItemWithTitle: _NS("Capture Device")];
189
190     // our screen capture module isn't Lion-compatible, so let's hide it from the user if needed (trac #4799)
191     if( NSAppKitVersionNumber < 1115.2 )
192         [o_capture_mode_pop addItemWithTitle: _NS("Screen")];
193
194     [o_capture_mode_pop addItemWithTitle: @"EyeTV"];
195     [o_screen_long_lbl setStringValue: _NS("This input allows you to save, stream or display your current screen contents.")];
196     [o_screen_fps_lbl setStringValue: _NS("Frames per Second:")];
197     [o_screen_left_lbl setStringValue: _NS("Subscreen left:")];
198     [o_screen_top_lbl setStringValue: _NS("Subscreen top:")];
199     [o_screen_width_lbl setStringValue: _NS("Subscreen width:")];
200     [o_screen_height_lbl setStringValue: _NS("Subscreen height:")];
201     [o_screen_follow_mouse_ckb setTitle: _NS("Follow the mouse")];
202     [o_eyetv_currentChannel_lbl setStringValue: _NS("Current channel:")];
203     [o_eyetv_previousProgram_btn setTitle: _NS("Previous Channel")];
204     [o_eyetv_nextProgram_btn setTitle: _NS("Next Channel")];
205     [o_eyetv_chn_status_txt setStringValue: _NS("Retrieving Channel Info...")];
206     [o_eyetv_noInstance_lbl setStringValue: _NS("EyeTV is not launched")];
207     [o_eyetv_noInstanceLong_lbl setStringValue: _NS("VLC could not connect to EyeTV.\nMake sure that you installed VLC's EyeTV plugin.")];
208     [o_eyetv_launchEyeTV_btn setTitle: _NS("Launch EyeTV now")];
209     [o_eyetv_getPlugin_btn setTitle: _NS("Download Plugin")];
210     [o_qtk_long_lbl setStringValue: _NS("This input allows you to process input signals from QuickTime-compatible video devices.\nLive Audio input is not supported.")];
211     [o_capture_width_lbl setStringValue: _NS("Image width:")];
212     [o_capture_height_lbl setStringValue: _NS("Image height:")];
213
214     [self qtkvideoDevices];
215     [o_qtk_device_pop removeAllItems];
216     msg_Dbg( VLCIntf, "Found %lu capture devices", [qtkvideoDevices count] );
217     
218     if([qtkvideoDevices count] >= 1)
219     {
220         if (!qtk_currdevice_uid) {
221             qtk_currdevice_uid = [[[QTCaptureDevice defaultInputDeviceWithMediaType: QTMediaTypeVideo] uniqueID]
222                                                                 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
223         }
224         NSUInteger deviceCount = [qtkvideoDevices count];
225         for(int ivideo = 0; ivideo < deviceCount; ivideo++){
226             QTCaptureDevice *qtk_device;
227             qtk_device = [qtkvideoDevices objectAtIndex:ivideo];
228             [o_qtk_device_pop addItemWithTitle: [qtk_device localizedDisplayName]];
229             if([[[qtk_device uniqueID]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:qtk_currdevice_uid]){
230                 [o_qtk_device_pop selectItemAtIndex:ivideo];
231             }
232         }
233     }
234     else
235     {
236         [o_qtk_device_pop addItemWithTitle: _NS("None")];
237         [qtk_currdevice_uid release];
238     }
239
240     [self setSubPanel];
241
242     [[NSNotificationCenter defaultCenter] addObserver: self
243         selector: @selector(openNetInfoChanged:)
244         name: NSControlTextDidChangeNotification
245         object: o_net_udp_port];
246     [[NSNotificationCenter defaultCenter] addObserver: self
247         selector: @selector(openNetInfoChanged:)
248         name: NSControlTextDidChangeNotification
249         object: o_net_udpm_addr];
250     [[NSNotificationCenter defaultCenter] addObserver: self
251         selector: @selector(openNetInfoChanged:)
252         name: NSControlTextDidChangeNotification
253         object: o_net_udpm_port];
254     [[NSNotificationCenter defaultCenter] addObserver: self
255         selector: @selector(openNetInfoChanged:)
256         name: NSControlTextDidChangeNotification
257         object: o_net_http_url];
258
259     [[NSDistributedNotificationCenter defaultCenter] addObserver: self
260                                                         selector: @selector(eyetvChanged:)
261                                                             name: NULL
262                                                           object: @"VLCEyeTVSupport"
263                                               suspensionBehavior: NSNotificationSuspensionBehaviorDeliverImmediately];
264
265     [[NSNotificationCenter defaultCenter] addObserver: self
266                                              selector: @selector(screenFPSfieldChanged:)
267                                                  name: NSControlTextDidChangeNotification
268                                                object: o_screen_fps_fld];
269
270     /* register clicks on text fields */
271     [[NSNotificationCenter defaultCenter] addObserver: self
272                                              selector: @selector(textFieldWasClicked:)
273                                                  name: @"VLCOpenTextFieldWasClicked"
274                                                object: nil];
275
276     /* we want to be notified about removed or added media */
277     o_specialMediaFolders = [[NSMutableArray alloc] init];
278     o_opticalDevices = [[NSMutableArray alloc] init];
279     NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
280
281     [[sharedWorkspace notificationCenter] addObserver:self selector:@selector(scanOpticalMedia:) name:NSWorkspaceDidMountNotification object:nil];
282     [[sharedWorkspace notificationCenter] addObserver:self selector:@selector(scanOpticalMedia:) name:NSWorkspaceDidUnmountNotification object:nil];
283     [self performSelector:@selector(scanOpticalMedia:) withObject:nil afterDelay:2.0];
284     [self performSelector:@selector(qtkChanged:) withObject:nil afterDelay:2.5];
285
286     [self setMRL: @""];
287 }
288
289 - (void)setMRL:(NSString *)newMRL
290 {
291     [o_mrl release];
292     o_mrl = newMRL;
293     [o_mrl retain];
294     [o_mrl_fld setStringValue: newMRL];
295     if ([o_mrl length] > 0)
296         [o_btn_ok setEnabled: YES];
297     else
298         [o_btn_ok setEnabled: NO];
299 }
300
301 - (NSString *)MRL
302 {
303     return o_mrl;
304 }
305
306 - (void)setSubPanel
307 {
308     int i_index;
309     module_config_t * p_item;
310
311     [o_file_sub_ckbox setTitle: _NS("Load subtitles file:")];
312     [o_file_sub_path_lbl setStringValue: _NS("Choose a file")];
313     [o_file_sub_path_lbl setHidden: NO];
314     [o_file_sub_path_fld setStringValue: @""];
315     [o_file_sub_btn_settings setTitle: _NS("Choose...")];
316     [o_file_sub_btn_browse setTitle: _NS("Browse...")];
317     [o_file_sub_override setTitle: _NS("Override parameters")];
318     [o_file_sub_delay_lbl setStringValue: _NS("Delay")];
319     [o_file_sub_delay_stp setEnabled: NO];
320     [o_file_sub_fps_lbl setStringValue: _NS("FPS")];
321     [o_file_sub_fps_stp setEnabled: NO];
322     [o_file_sub_encoding_lbl setStringValue: _NS("Subtitles encoding")];
323     [o_file_sub_encoding_pop removeAllItems];
324     [o_file_sub_size_lbl setStringValue: _NS("Font size")];
325     [o_file_sub_size_pop removeAllItems];
326     [o_file_sub_align_lbl setStringValue: _NS("Subtitles alignment")];
327     [o_file_sub_align_pop removeAllItems];
328     [o_file_sub_ok_btn setStringValue: _NS("OK")];
329     [o_file_sub_font_box setTitle: _NS("Font Properties")];
330     [o_file_sub_file_box setTitle: _NS("Subtitle File")];
331
332     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-encoding" );
333
334     if( p_item )
335     {
336         for( i_index = 0; p_item->ppsz_list && p_item->ppsz_list[i_index];
337              i_index++ )
338         {
339             [o_file_sub_encoding_pop addItemWithTitle:
340                 [NSString stringWithUTF8String: p_item->ppsz_list[i_index]]];
341         }
342         [o_file_sub_encoding_pop selectItemWithTitle:
343                 [NSString stringWithUTF8String: p_item->value.psz]];
344     }
345
346     p_item = config_FindConfig( VLC_OBJECT(p_intf), "subsdec-align" );
347
348     if ( p_item )
349     {
350         for ( i_index = 0; i_index < p_item->i_list; i_index++ )
351         {
352             [o_file_sub_align_pop addItemWithTitle:
353              _NS(p_item->ppsz_list_text[i_index])];
354         }
355         [o_file_sub_align_pop selectItemAtIndex: p_item->value.i];
356     }
357
358     p_item = config_FindConfig( VLC_OBJECT(p_intf), "freetype-rel-fontsize" );
359
360     if ( p_item )
361     {
362         for ( i_index = 0; i_index < p_item->i_list; i_index++ )
363         {
364             [o_file_sub_size_pop addItemWithTitle: _NS(p_item->ppsz_list_text[i_index])];
365             if ( p_item->value.i == p_item->pi_list[i_index] )
366             {
367                 [o_file_sub_size_pop selectItemAtIndex: i_index];
368             }
369         }
370     }
371 }
372
373 - (void)openTarget:(int)i_type
374 {
375     int i_result;
376
377     b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
378
379     [o_tabview selectTabViewItemAtIndex: i_type];
380     [o_file_sub_ckbox setState: NSOffState];
381  
382     i_result = [NSApp runModalForWindow: o_panel];
383     [o_panel close];
384
385     if( i_result )
386     {
387         NSMutableDictionary *o_dic;
388         NSMutableArray *o_options = [NSMutableArray array];
389
390         o_dic = [NSMutableDictionary dictionaryWithObject: [self MRL] forKey: @"ITEM_URL"];
391         if( [o_file_sub_ckbox state] == NSOnState )
392         {
393             module_config_t * p_item;
394
395             [o_options addObject: [NSString stringWithFormat: @"sub-file=%@", o_sub_path]];
396             if( [o_file_sub_override state] == NSOnState )
397             {
398                 [o_options addObject: [NSString stringWithFormat: @"sub-delay=%i", (int)( [o_file_sub_delay intValue] * 10 )]];
399                 [o_options addObject: [NSString stringWithFormat: @"sub-fps=%f", [o_file_sub_fps floatValue]]];
400             }
401             [o_options addObject: [NSString stringWithFormat:
402                     @"subsdec-encoding=%@",
403                     [o_file_sub_encoding_pop titleOfSelectedItem]]];
404             [o_options addObject: [NSString stringWithFormat:
405                     @"subsdec-align=%i",
406                     [o_file_sub_align_pop indexOfSelectedItem]]];
407
408             p_item = config_FindConfig( VLC_OBJECT(p_intf),
409                                             "freetype-rel-fontsize" );
410
411             if ( p_item )
412             {
413                 [o_options addObject: [NSString stringWithFormat:
414                     @"freetype-rel-fontsize=%i",
415                     p_item->pi_list[[o_file_sub_size_pop indexOfSelectedItem]]]];
416             }
417         }
418         if( [o_output_ckbox state] == NSOnState )
419         {
420             NSUInteger count = [[o_sout_options mrl] count];
421             for (NSUInteger i = 0 ; i < count ; i++)
422             {
423                 [o_options addObject: [NSString stringWithString: [[(VLCOutput *)o_sout_options mrl] objectAtIndex: i]]];
424             }
425         }
426         if( [o_file_slave_ckbox state] && o_file_slave_path )
427            [o_options addObject: [NSString stringWithFormat: @"input-slave=%@", o_file_slave_path]];
428         if( [[[o_tabview selectedTabViewItem] label] isEqualToString: _NS("Capture")] )
429         {
430             if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: _NS("Screen")] )
431             {
432                 [o_options addObject: [NSString stringWithFormat: @"screen-fps=%f", [o_screen_fps_fld floatValue]]];
433                 [o_options addObject: [NSString stringWithFormat: @"screen-left=%i", [o_screen_left_fld intValue]]];
434                 [o_options addObject: [NSString stringWithFormat: @"screen-top=%i", [o_screen_top_fld intValue]]];
435                 [o_options addObject: [NSString stringWithFormat: @"screen-width=%i", [o_screen_width_fld intValue]]];
436                 [o_options addObject: [NSString stringWithFormat: @"screen-height=%i", [o_screen_height_fld intValue]]];
437                 if( [o_screen_follow_mouse_ckb intValue] == YES )
438                     [o_options addObject: @"screen-follow-mouse"];
439                 else
440                     [o_options addObject: @"no-screen-follow-mouse"];
441             }
442             else if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: _NS("Capture Device")] )
443             {
444                 [o_options addObject: [NSString stringWithFormat: @"qtcapture-width=%i", [o_capture_width_fld intValue]]];
445                 [o_options addObject: [NSString stringWithFormat: @"qtcapture-height=%i", [o_capture_height_fld intValue]]];
446             }
447         }
448
449         /* apply the options to our item(s) */
450         [o_dic setObject: (NSArray *)[o_options copy] forKey: @"ITEM_OPTIONS"];
451         if( b_autoplay )
452             [[[VLCMain sharedInstance] playlist] appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:NO];
453         else
454             [[[VLCMain sharedInstance] playlist] appendArray: [NSArray arrayWithObject: o_dic] atPos: -1 enqueue:YES];
455     }
456 }
457
458 - (IBAction)qtkChanged:(id)sender
459 {
460     NSInteger i_selectedDevice = [o_qtk_device_pop indexOfSelectedItem];
461
462     if (i_selectedDevice >= ([qtkvideoDevices count] - 1))
463     {
464         NSValue *sizes = [[[[qtkvideoDevices objectAtIndex:i_selectedDevice] formatDescriptions] objectAtIndex: 0] attributeForKey: QTFormatDescriptionVideoEncodedPixelsSizeAttribute];
465         [o_capture_width_fld setIntValue: [sizes sizeValue].width];
466         [o_capture_height_fld setIntValue: [sizes sizeValue].height];
467         [o_capture_width_stp setIntValue: [o_capture_width_fld intValue]];
468         [o_capture_height_stp setIntValue: [o_capture_height_fld intValue]];
469         qtk_currdevice_uid = [[(QTCaptureDevice *)[qtkvideoDevices objectAtIndex:i_selectedDevice] uniqueID] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
470
471         [self setMRL:[NSString stringWithFormat:@"qtcapture://%@", qtk_currdevice_uid]];
472     }
473 }
474
475 - (void)tabView:(NSTabView *)o_tv didSelectTabViewItem:(NSTabViewItem *)o_tvi
476 {
477     NSString *o_label = [o_tvi label];
478
479     if( [o_label isEqualToString: _NS("File")] )
480     {
481         [self openFilePathChanged: nil];
482     }
483     else if( [o_label isEqualToString: _NS("Disc")] )
484     {
485         [self scanOpticalMedia: nil];
486     }
487     else if( [o_label isEqualToString: _NS("Network")] )
488     {
489         [self openNetInfoChanged: nil];
490     }
491     else if( [o_label isEqualToString: _NS("Capture")] )
492     {
493         [self openCaptureModeChanged: nil];
494     }
495 }
496
497 - (IBAction)expandMRLfieldAction:(id)sender
498 {
499     NSRect o_win_rect, o_view_rect;
500     o_win_rect = [o_panel frame];
501     o_view_rect = [o_mrl_view frame];
502
503     if( [o_mrl_btn state] == NSOffState )
504     {
505         /* we need to collaps, restore the panel size */
506         o_win_rect.size.height = o_win_rect.size.height - o_view_rect.size.height;
507         o_win_rect.origin.y = ( o_win_rect.origin.y + o_view_rect.size.height ) - o_view_rect.size.height;
508
509         /* remove the MRL view */
510         [o_mrl_view removeFromSuperview];
511     } else {
512         /* we need to expand */
513         [o_mrl_view setFrame: NSMakeRect( 0,
514                                          [o_mrl_btn frame].origin.y,
515                                          o_view_rect.size.width,
516                                          o_view_rect.size.height )];
517         [o_mrl_view setNeedsDisplay: NO];
518         [o_mrl_view setAutoresizesSubviews: YES];
519
520         /* enlarge panel size for MRL view */
521         o_win_rect.size.height = o_win_rect.size.height + o_view_rect.size.height;
522     }
523
524     [[o_panel animator] setFrame: o_win_rect display:YES];
525 //    [o_panel displayIfNeeded];
526     if( [o_mrl_btn state] == NSOnState )
527         [[o_panel contentView] addSubview: o_mrl_view];
528 }
529
530 - (IBAction)inputSlaveAction:(id)sender
531 {
532     if( sender == o_file_slave_ckbox )
533         [o_file_slave_select_btn setEnabled: [o_file_slave_ckbox state]];
534     else
535     {
536         NSOpenPanel *o_open_panel;
537         o_open_panel = [NSOpenPanel openPanel];
538         [o_open_panel setCanChooseFiles: YES];
539         [o_open_panel setCanChooseDirectories: NO];
540         if( [o_open_panel runModal] == NSOKButton )
541         {
542             if( o_file_slave_path )
543                 [o_file_slave_path release];
544             o_file_slave_path = [[[o_open_panel URLs] objectAtIndex: 0] path];
545             [o_file_slave_path retain];
546         }
547     }
548     if( o_file_slave_path && [o_file_slave_ckbox state] == NSOnState)
549     {
550         [o_file_slave_filename_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath:o_file_slave_path]];
551         [o_file_slave_icon_well setImage: [[NSWorkspace sharedWorkspace] iconForFile: o_file_slave_path]];
552     }
553     else
554     {
555         [o_file_slave_filename_lbl setStringValue: @""];
556         [o_file_slave_icon_well setImage: NULL];
557     }
558 }
559
560 - (void)openFileGeneric
561 {
562     [self openFilePathChanged: nil];
563     [self openTarget: 0];
564 }
565
566 - (void)openDisc
567 {
568     [o_specialMediaFolders removeAllObjects];
569     [o_opticalDevices removeAllObjects];
570     [self scanOpticalMedia: nil];
571     [self openTarget: 1];
572 }
573
574 - (void)openNet
575 {
576     [self openNetInfoChanged: nil];
577     [self openTarget: 2];
578 }
579
580 - (void)openCapture
581 {
582     [self openCaptureModeChanged: nil];
583     [self openTarget: 3];
584 }
585
586 - (void)openFilePathChanged:(NSNotification *)o_notification
587 {
588     if ( o_file_path && [o_file_path length] > 0 )
589     {
590         bool b_stream = [o_file_stream state];
591         BOOL b_dir = NO;
592
593         [[NSFileManager defaultManager] fileExistsAtPath:o_file_path isDirectory:&b_dir];
594
595         char *psz_uri = make_URI([o_file_path UTF8String], "file");
596         if( !psz_uri ) return;
597
598         NSMutableString *o_mrl_string = [NSMutableString stringWithUTF8String: psz_uri ];
599         NSRange offile = [o_mrl_string rangeOfString:@"file"];
600         free( psz_uri );
601
602         if( b_dir )
603             [o_mrl_string replaceCharactersInRange:offile withString: @"directory"];
604         else if( b_stream )
605             [o_mrl_string replaceCharactersInRange:offile withString: @"stream"];
606
607         [o_file_name setStringValue: [[NSFileManager defaultManager] displayNameAtPath:o_file_path]];
608         [o_file_name_stub setHidden: YES];
609         [o_file_stream setHidden: NO];
610         [o_file_icon_well setImage: [[NSWorkspace sharedWorkspace] iconForFile: o_file_path]];
611         [o_file_icon_well setHidden: NO];
612         [self setMRL: o_mrl_string];
613     }
614     else
615     {
616         [o_file_name setStringValue: @""];
617         [o_file_name_stub setHidden: NO];
618         [o_file_stream setHidden: YES];
619         [o_file_icon_well setImage: [NSImage imageNamed:@"generic"]];
620         [self setMRL: @""];
621     }
622 }
623
624 - (IBAction)openFileBrowse:(id)sender
625 {
626     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
627  
628     [o_open_panel setAllowsMultipleSelection: NO];
629     [o_open_panel setCanChooseDirectories: YES];
630     [o_open_panel setTitle: _NS("Open File")];
631     [o_open_panel setPrompt: _NS("Open")];
632
633     [o_open_panel beginSheetForDirectory:nil
634         file:nil
635         types:nil
636         modalForWindow:[sender window]
637         modalDelegate: self
638         didEndSelector: @selector(pathChosenInPanel:
639                         withReturn:
640                         contextInfo:)
641         contextInfo: nil];
642 }
643
644 - (void)pathChosenInPanel: (NSOpenPanel *) sheet withReturn:(int)returnCode contextInfo:(void  *)contextInfo
645 {
646     if (returnCode == NSFileHandlingPanelOKButton)
647     {
648         if( o_file_path )
649             [o_file_path release];
650         o_file_path = [[[sheet URLs] objectAtIndex: 0] path];
651         [o_file_path retain];
652         [self openFilePathChanged: nil];
653     }
654 }
655
656 - (IBAction)openFileStreamChanged:(id)sender
657 {
658     [self openFilePathChanged: nil];
659 }
660
661 - (void)showOpticalMediaView: theView withIcon:(NSImage *)icon
662 {
663     NSRect o_view_rect;
664     o_view_rect = [theView frame];
665     [theView setFrame: NSMakeRect( 233, 0, o_view_rect.size.width, o_view_rect.size.height)];
666     [theView setAutoresizesSubviews: YES];
667     if (o_currentOpticalMediaView)
668     {
669         [[[[o_tabview tabViewItemAtIndex: [o_tabview indexOfTabViewItemWithIdentifier:@"optical"]] view] animator] replaceSubview: o_currentOpticalMediaView with: theView];
670         [o_currentOpticalMediaView release];
671     }
672     else
673         [[[[o_tabview tabViewItemAtIndex: [o_tabview indexOfTabViewItemWithIdentifier:@"optical"]] view] animator] addSubview: theView];
674     o_currentOpticalMediaView = theView;
675     [o_currentOpticalMediaView retain];
676
677     NSImageView *imageView;
678     imageView = [[NSImageView alloc] init];
679     [imageView setFrame: NSMakeRect( 53, 61, 128, 128 )];
680     [icon setSize: NSMakeSize(128,128)];
681     [imageView setImage: icon];
682     if (o_currentOpticalMediaIconView)
683     {
684         [[[[o_tabview tabViewItemAtIndex: [o_tabview indexOfTabViewItemWithIdentifier:@"optical"]] view] animator] replaceSubview: o_currentOpticalMediaIconView with: imageView];
685         [o_currentOpticalMediaIconView release];
686     }
687     else
688          [[[[o_tabview tabViewItemAtIndex: [o_tabview indexOfTabViewItemWithIdentifier:@"optical"]] view] animator] addSubview: imageView];
689     o_currentOpticalMediaIconView = imageView;
690     [o_currentOpticalMediaIconView retain];
691     [o_currentOpticalMediaView setNeedsDisplay: YES];
692     [o_currentOpticalMediaIconView setNeedsDisplay: YES];
693     [[[o_tabview tabViewItemAtIndex: [o_tabview indexOfTabViewItemWithIdentifier:@"optical"]] view] setNeedsDisplay: YES];
694     [[[o_tabview tabViewItemAtIndex: [o_tabview indexOfTabViewItemWithIdentifier:@"optical"]] view] displayIfNeeded];
695 }
696
697 - (NSString *) getBSDNodeFromMountPath:(NSString *)mountPath
698 {
699     OSStatus err;
700     FSRef ref;
701     FSVolumeRefNum actualVolume;
702     err = FSPathMakeRef ( (const UInt8 *) [mountPath fileSystemRepresentation], &ref, NULL );
703
704     // get a FSVolumeRefNum from mountPath
705     if ( noErr == err ) {
706         FSCatalogInfo   catalogInfo;
707         err = FSGetCatalogInfo ( &ref,
708                                 kFSCatInfoVolume,
709                                 &catalogInfo,
710                                 NULL,
711                                 NULL,
712                                 NULL
713                                 );
714         if ( noErr == err ) {
715             actualVolume = catalogInfo.volume;
716         }
717     }
718
719     GetVolParmsInfoBuffer volumeParms;
720     err = FSGetVolumeParms( actualVolume, &volumeParms, sizeof(volumeParms) );
721     if ( noErr != err ) {
722         msg_Err( p_intf, "error retrieving volume params, bailing out" );
723         return @"";
724     }
725
726     NSString *bsdName = [NSString stringWithUTF8String:(char *)volumeParms.vMDeviceID];
727     return [NSString stringWithFormat:@"/dev/r%@", bsdName];
728 }
729
730 - (char *)getVolumeTypeFromMountPath:(NSString *)mountPath
731 {
732     OSStatus err;
733     FSRef ref;
734     FSVolumeRefNum actualVolume;
735     err = FSPathMakeRef ( (const UInt8 *) [mountPath fileSystemRepresentation], &ref, NULL );
736
737     // get a FSVolumeRefNum from mountPath
738     if ( noErr == err ) {
739         FSCatalogInfo   catalogInfo;
740         err = FSGetCatalogInfo ( &ref,
741                                 kFSCatInfoVolume,
742                                 &catalogInfo,
743                                 NULL,
744                                 NULL,
745                                 NULL
746                                 );
747         if ( noErr == err ) {
748             actualVolume = catalogInfo.volume;
749         }
750     }
751
752     GetVolParmsInfoBuffer volumeParms;
753     err = FSGetVolumeParms( actualVolume, &volumeParms, sizeof(volumeParms) );
754
755     CFMutableDictionaryRef matchingDict;
756     io_service_t service;
757
758     matchingDict = IOBSDNameMatching(kIOMasterPortDefault, 0, volumeParms.vMDeviceID);
759     service = IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict);
760
761     char *returnValue;
762     if (IO_OBJECT_NULL != service) {
763         if (IOObjectConformsTo(service, kIOCDMediaClass)) {
764             returnValue = kVLCMediaAudioCD;
765         }
766         else if(IOObjectConformsTo(service, kIODVDMediaClass))
767             returnValue = kVLCMediaDVD;
768         else if(IOObjectConformsTo(service, kIOBDMediaClass))
769             returnValue = kVLCMediaBD;
770         else
771         {
772             if ([mountPath rangeOfString:@"VIDEO_TS"].location != NSNotFound)
773                 returnValue = kVLCMediaVideoTSFolder;
774             else if ([mountPath rangeOfString:@"BDMV"].location != NSNotFound)
775                 returnValue = kVLCMediaBDMVFolder;
776             else
777             {
778                 NSArray * topLevelItems;
779                 topLevelItems = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath: mountPath error: NULL];
780                 NSUInteger itemCount = [topLevelItems count];
781                 for (int i = 0; i < itemCount; i++) {
782                     if([[topLevelItems objectAtIndex:i] rangeOfString:@"SVCD"].location != NSNotFound) {
783                         returnValue = kVLCMediaSVCD;
784                         break;
785                     }
786                     if([[topLevelItems objectAtIndex:i] rangeOfString:@"VCD"].location != NSNotFound) {
787                         returnValue = kVLCMediaVCD;
788                         break;
789                     }
790                 }
791                 if(!returnValue)
792                     returnValue = kVLCMediaUnknown;
793             }
794         }
795
796         IOObjectRelease(service);
797     }
798     return returnValue;
799 }
800
801 - (void)showOpticalAtIndex: (NSNumber *)n_index
802 {
803     NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
804
805     if (!o_opticalDevices)
806         return;
807
808     if ([o_opticalDevices count] == 0)
809         return;
810
811     unsigned int index = [n_index intValue];
812
813     id o_currentOpticalDevice = [o_opticalDevices objectAtIndex: index];
814     char *diskType = [self getVolumeTypeFromMountPath:o_currentOpticalDevice];
815
816     if (diskType == kVLCMediaDVD || diskType == kVLCMediaVideoTSFolder)
817     {
818         [o_disc_dvd_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath:o_currentOpticalDevice]];
819         [o_disc_dvdwomenus_lbl setStringValue: [o_disc_dvd_lbl stringValue]];
820         NSString *pathToOpen;
821         if (diskType == kVLCMediaVideoTSFolder)
822             pathToOpen = o_currentOpticalDevice;
823         else
824             pathToOpen = [self getBSDNodeFromMountPath: o_currentOpticalDevice];
825         if (!b_nodvdmenus) {
826             [self setMRL: [NSString stringWithFormat: @"dvdnav://%@", pathToOpen]];
827             [self showOpticalMediaView: o_disc_dvd_view withIcon: [[NSWorkspace sharedWorkspace] iconForFile: o_currentOpticalDevice]];
828         } else {
829             [self setMRL: [NSString stringWithFormat: @"dvdread://%@#%i:%i-", pathToOpen, [o_disc_dvdwomenus_title intValue], [o_disc_dvdwomenus_chapter intValue]]];
830             [self showOpticalMediaView: o_disc_dvdwomenus_view withIcon: [[NSWorkspace sharedWorkspace] iconForFile: o_currentOpticalDevice]];
831         }
832     }
833     else if (diskType == kVLCMediaAudioCD)
834     {
835         [o_disc_audiocd_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath: o_currentOpticalDevice]];
836         [o_disc_audiocd_trackcount_lbl setStringValue: [NSString stringWithFormat:_NS("%i tracks"), [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath: o_currentOpticalDevice error:NULL] count] - 1]]; // minus .TOC.plist
837         [self showOpticalMediaView: o_disc_audiocd_view withIcon: [[NSWorkspace sharedWorkspace] iconForFile: o_currentOpticalDevice]];
838         [self setMRL: [NSString stringWithFormat: @"cdda://%@", [self getBSDNodeFromMountPath: o_currentOpticalDevice]]];
839     }
840     else if (diskType == kVLCMediaVCD)
841     {
842         [o_disc_vcd_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath: o_currentOpticalDevice]];
843         [self showOpticalMediaView: o_disc_vcd_view withIcon: [[NSWorkspace sharedWorkspace] iconForFile: o_currentOpticalDevice]];
844         [self setMRL: [NSString stringWithFormat: @"vcd://%@#%i:%i", [self getBSDNodeFromMountPath: o_currentOpticalDevice], [o_disc_vcd_title intValue], [o_disc_vcd_chapter intValue]]];
845     }
846     else if (diskType == kVLCMediaSVCD)
847     {
848         [o_disc_vcd_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath: o_currentOpticalDevice]];
849         [self showOpticalMediaView: o_disc_vcd_view withIcon: [[NSWorkspace sharedWorkspace] iconForFile: o_currentOpticalDevice]];
850         [self setMRL: [NSString stringWithFormat: @"vcd://%@@%i:%i", [self getBSDNodeFromMountPath: o_currentOpticalDevice], [o_disc_vcd_title intValue], [o_disc_vcd_chapter intValue]]];
851     }
852     else if (diskType == kVLCMediaBD || diskType == kVLCMediaBDMVFolder)
853     {
854         [o_disc_bd_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath: o_currentOpticalDevice]];
855         [self showOpticalMediaView: o_disc_bd_view withIcon: [[NSWorkspace sharedWorkspace] iconForFile: o_currentOpticalDevice]];
856         [self setMRL: [NSString stringWithFormat: @"bluray://%@", o_currentOpticalDevice]];
857     }
858     else
859     {
860         msg_Warn( VLCIntf, "unknown disk type, no idea what to display" );
861         [self showOpticalMediaView: o_disc_nodisc_view withIcon: [NSImage imageNamed:@"NSApplicationIcon"]];
862     }
863
864     [o_pool release];
865 }
866
867 - (void)scanOpticalMedia:(NSNotification *)o_notification
868 {
869     [o_opticalDevices removeAllObjects];
870     [o_disc_selector_pop removeAllItems];
871     [o_opticalDevices addObjectsFromArray: [[NSWorkspace sharedWorkspace] mountedRemovableMedia]];
872     if ([o_specialMediaFolders count] > 0)
873         [o_opticalDevices addObjectsFromArray: o_specialMediaFolders];
874     if ([o_opticalDevices count] > 0) {
875         NSUInteger deviceCount = [o_opticalDevices count];
876         for (NSUInteger i = 0; i < deviceCount ; i++)
877             [o_disc_selector_pop addItemWithTitle: [[NSFileManager defaultManager] displayNameAtPath:[o_opticalDevices objectAtIndex: i]]];
878
879         if ([o_disc_selector_pop numberOfItems] <= 1)
880             [o_disc_selector_pop setHidden: YES];
881         else
882             [o_disc_selector_pop setHidden: NO];
883
884         [NSThread detachNewThreadSelector:@selector(showOpticalAtIndex:) toTarget:self withObject:[NSNumber numberWithInt:[o_disc_selector_pop indexOfSelectedItem]]];
885     }
886     else
887     {
888         msg_Dbg( VLCIntf, "no optical media found" );
889         [o_disc_selector_pop setHidden: YES];
890         [self showOpticalMediaView: o_disc_nodisc_view withIcon: [NSImage imageNamed: @"NSApplicationIcon"]];
891     }
892 }
893
894 - (IBAction)discSelectorChanged:(id)sender
895 {
896     [NSThread detachNewThreadSelector:@selector(showOpticalAtIndex:) toTarget:self withObject:[NSNumber numberWithInt:[o_disc_selector_pop indexOfSelectedItem]]];
897 }
898
899 - (IBAction)openSpecialMediaFolder:(id)sender
900 {
901     /* this is currently for VIDEO_TS and BDMV folders */
902     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
903
904     [o_open_panel setAllowsMultipleSelection: NO];
905     [o_open_panel setCanChooseFiles: NO];
906     [o_open_panel setCanChooseDirectories: YES];
907     [o_open_panel setTitle: [sender title]];
908     [o_open_panel setPrompt: _NS("Open")];
909
910     if ([o_open_panel runModal] == NSOKButton)
911     {
912         NSString *o_path = [[[o_open_panel URLs] objectAtIndex: 0] path];
913         if ([o_path length] > 0 )
914         {
915             if ([o_path rangeOfString:@"VIDEO_TS"].location != NSNotFound || [o_path rangeOfString:@"BDMV"].location != NSNotFound)
916             {
917                 [o_specialMediaFolders addObject: o_path];
918                 [self scanOpticalMedia: nil];
919             }
920             else
921                 msg_Dbg( VLCIntf, "chosen directory is no suitable special media folder" );
922         }
923     }
924 }
925
926 - (IBAction)dvdreadOptionChanged:(id)sender
927 {
928     if (sender == o_disc_dvdwomenus_enablemenus_btn) {
929         b_nodvdmenus = NO;
930         [self setMRL: [NSString stringWithFormat: @"dvdnav://%@", [self getBSDNodeFromMountPath:[o_opticalDevices objectAtIndex: [o_disc_selector_pop indexOfSelectedItem]]]]];
931         [self showOpticalMediaView: o_disc_dvd_view withIcon: [o_currentOpticalMediaIconView image]];
932         return;
933     }
934     if (sender == o_disc_dvd_disablemenus_btn) {
935         b_nodvdmenus = YES;
936         [self showOpticalMediaView: o_disc_dvdwomenus_view withIcon: [o_currentOpticalMediaIconView image]];
937     }
938
939     if (sender == o_disc_dvdwomenus_title)
940         [o_disc_dvdwomenus_title_stp setIntValue: [o_disc_dvdwomenus_title intValue]];
941     if (sender == o_disc_dvdwomenus_title_stp)
942         [o_disc_dvdwomenus_title setIntValue: [o_disc_dvdwomenus_title_stp intValue]];
943     if (sender == o_disc_dvdwomenus_chapter)
944         [o_disc_dvdwomenus_chapter_stp setIntValue: [o_disc_dvdwomenus_chapter intValue]];
945     if (sender == o_disc_dvdwomenus_chapter_stp)
946         [o_disc_dvdwomenus_chapter setIntValue: [o_disc_dvdwomenus_chapter_stp intValue]];
947
948     [self setMRL: [NSString stringWithFormat: @"dvdread://%@#%i:%i-", [self getBSDNodeFromMountPath:[o_opticalDevices objectAtIndex: [o_disc_selector_pop indexOfSelectedItem]]], [o_disc_dvdwomenus_title intValue], [o_disc_dvdwomenus_chapter intValue]]];
949 }
950
951 - (IBAction)vcdOptionChanged:(id)sender
952 {
953     if (sender == o_disc_vcd_title)
954         [o_disc_vcd_title_stp setIntValue: [o_disc_vcd_title intValue]];
955     if (sender == o_disc_vcd_title_stp)
956         [o_disc_vcd_title setIntValue: [o_disc_vcd_title_stp intValue]];
957     if (sender == o_disc_vcd_chapter)
958         [o_disc_vcd_chapter_stp setIntValue: [o_disc_vcd_chapter intValue]];
959     if (sender == o_disc_vcd_chapter_stp)
960         [o_disc_vcd_chapter setIntValue: [o_disc_vcd_chapter_stp intValue]];
961
962     [self setMRL: [NSString stringWithFormat: @"vcd://%@@%i:%i", [self getBSDNodeFromMountPath:[o_opticalDevices objectAtIndex: [o_disc_selector_pop indexOfSelectedItem]]], [o_disc_vcd_title intValue], [o_disc_vcd_chapter intValue]]];
963 }
964
965 - (void)textFieldWasClicked:(NSNotification *)o_notification
966 {
967     if( [o_notification object] == o_net_udp_port )
968         [o_net_mode selectCellAtRow: 0 column: 0];
969     else if( [o_notification object] == o_net_udpm_addr ||
970              [o_notification object] == o_net_udpm_port )
971         [o_net_mode selectCellAtRow: 1 column: 0];
972     else
973         [o_net_mode selectCellAtRow: 2 column: 0];
974
975     [self openNetInfoChanged: nil];
976 }
977
978 - (IBAction)openNetModeChanged:(id)sender
979 {
980     if( sender == o_net_mode )
981     {
982         if( [[sender selectedCell] tag] == 0 )
983             [o_panel makeFirstResponder: o_net_udp_port];
984         else if ( [[sender selectedCell] tag] == 1 )
985             [o_panel makeFirstResponder: o_net_udpm_addr];
986         else
987             msg_Warn( p_intf, "Unknown sender tried to change UDP/RTP mode" );
988     }
989
990     [self openNetInfoChanged: nil];
991 }
992
993 - (IBAction)openNetStepperChanged:(id)sender
994 {
995     int i_tag = [sender tag];
996
997     if( i_tag == 0 )
998     {
999         [o_net_udp_port setIntValue: [o_net_udp_port_stp intValue]];
1000         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
1001                                                             object: o_net_udp_port];
1002         [o_panel makeFirstResponder: o_net_udp_port];
1003     }
1004     else if( i_tag == 1 )
1005     {
1006         [o_net_udpm_port setIntValue: [o_net_udpm_port_stp intValue]];
1007         [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
1008                                                             object: o_net_udpm_port];
1009         [o_panel makeFirstResponder: o_net_udpm_port];
1010     }
1011
1012     [self openNetInfoChanged: nil];
1013 }
1014
1015 - (void)openNetInfoChanged:(NSNotification *)o_notification
1016 {
1017     NSString *o_mrl_string = [NSString string];
1018
1019     if( [o_net_udp_panel isVisible] )
1020     {
1021         NSString *o_mode;
1022         o_mode = [[o_net_mode selectedCell] title];
1023
1024         if( [o_mode isEqualToString: _NS("Unicast")] )
1025         {
1026             int i_port = [o_net_udp_port intValue];
1027
1028             if( [[o_net_udp_protocol_mat selectedCell] tag] == 0 )
1029                 o_mrl_string = [NSString stringWithString: @"udp://"];
1030             else
1031                 o_mrl_string = [NSString stringWithString: @"rtp://"];
1032
1033             if( i_port != config_GetInt( p_intf, "server-port" ) )
1034             {
1035                 o_mrl_string =
1036                     [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
1037             }
1038         }
1039         else if( [o_mode isEqualToString: _NS("Multicast")] )
1040         {
1041             NSString *o_addr = [o_net_udpm_addr stringValue];
1042             int i_port = [o_net_udpm_port intValue];
1043
1044             if( [[o_net_udp_protocol_mat selectedCell] tag] == 0 )
1045                 o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
1046             else
1047                 o_mrl_string = [NSString stringWithFormat: @"rtp://@%@", o_addr];
1048
1049             if( i_port != config_GetInt( p_intf, "server-port" ) )
1050             {
1051                 o_mrl_string =
1052                     [o_mrl_string stringByAppendingFormat: @":%i", i_port];
1053             }
1054         }
1055     }
1056     else
1057     {
1058         o_mrl_string = [o_net_http_url stringValue];
1059     }
1060     [self setMRL: o_mrl_string];
1061 }
1062
1063 - (IBAction)openNetUDPButtonAction:(id)sender
1064 {
1065     if( sender == o_net_openUDP_btn )
1066     {
1067         [NSApp beginSheet: o_net_udp_panel
1068            modalForWindow: o_panel
1069             modalDelegate: self
1070            didEndSelector: NULL
1071               contextInfo: nil];
1072         [self openNetInfoChanged: nil];
1073     }
1074     else if( sender == o_net_udp_cancel_btn )
1075     {
1076         [o_net_udp_panel orderOut: sender];
1077         [NSApp endSheet: o_net_udp_panel];
1078     }
1079     else if( sender == o_net_udp_ok_btn )
1080     {
1081         NSString *o_mrl_string = [NSString string];
1082         if( [[[o_net_mode selectedCell] title] isEqualToString: _NS("Unicast")] )
1083         {
1084             int i_port = [o_net_udp_port intValue];
1085
1086             if( [[o_net_udp_protocol_mat selectedCell] tag] == 0 )
1087                 o_mrl_string = [NSString stringWithString: @"udp://"];
1088             else
1089                 o_mrl_string = [NSString stringWithString: @"rtp://"];
1090
1091             if( i_port != config_GetInt( p_intf, "server-port" ) )
1092             {
1093                 o_mrl_string =
1094                 [o_mrl_string stringByAppendingFormat: @"@:%i", i_port];
1095             }
1096         }
1097         else if( [[[o_net_mode selectedCell] title] isEqualToString: _NS("Multicast")] )
1098         {
1099             NSString *o_addr = [o_net_udpm_addr stringValue];
1100             int i_port = [o_net_udpm_port intValue];
1101
1102             if( [[o_net_udp_protocol_mat selectedCell] tag] == 0 )
1103                 o_mrl_string = [NSString stringWithFormat: @"udp://@%@", o_addr];
1104             else
1105                 o_mrl_string = [NSString stringWithFormat: @"rtp://@%@", o_addr];
1106
1107             if( i_port != config_GetInt( p_intf, "server-port" ) )
1108             {
1109                 o_mrl_string =
1110                 [o_mrl_string stringByAppendingFormat: @":%i", i_port];
1111             }
1112         }
1113         [self setMRL: o_mrl_string];
1114         [o_net_http_url setStringValue: o_mrl_string];
1115         [o_net_udp_panel orderOut: sender];
1116         [NSApp endSheet: o_net_udp_panel];
1117     }
1118 }
1119
1120 - (void)openFile
1121 {
1122     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
1123     b_autoplay = config_GetInt( VLCIntf, "macosx-autoplay" );
1124
1125     [o_open_panel setAllowsMultipleSelection: YES];
1126     [o_open_panel setCanChooseDirectories: YES];
1127     [o_open_panel setTitle: _NS("Open File")];
1128     [o_open_panel setPrompt: _NS("Open")];
1129  
1130     if( [o_open_panel runModal] == NSOKButton )
1131     {
1132         NSArray * o_urls = [o_open_panel URLs];
1133         NSUInteger count = [o_urls count];
1134         NSMutableArray *o_values = [NSMutableArray arrayWithCapacity:count];
1135         NSMutableArray *o_array = [NSMutableArray arrayWithCapacity:count];
1136         for( NSUInteger i = 0; i < count; i++ )
1137         {
1138             [o_values addObject: [[o_urls objectAtIndex: i] path]];
1139         }
1140         [o_values sortUsingSelector:@selector(caseInsensitiveCompare:)];
1141
1142         for( NSUInteger i = 0; i < count; i++ )
1143         {
1144             NSDictionary *o_dic;
1145             char *psz_uri = make_URI([[o_values objectAtIndex:i] UTF8String], "file");
1146             if( !psz_uri )
1147                 continue;
1148
1149             o_dic = [NSDictionary dictionaryWithObject:[NSString stringWithCString:psz_uri encoding:NSUTF8StringEncoding] forKey:@"ITEM_URL"];
1150
1151             free( psz_uri );
1152
1153             [o_array addObject: o_dic];
1154         }
1155         if( b_autoplay )
1156             [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:NO];
1157         else
1158             [[[VLCMain sharedInstance] playlist] appendArray: o_array atPos: -1 enqueue:YES];
1159     }
1160 }
1161
1162 - (void)showCaptureView: theView
1163 {
1164     NSRect o_view_rect;
1165     o_view_rect = [theView frame];
1166     [theView setFrame: NSMakeRect( 0, -10, o_view_rect.size.width, o_view_rect.size.height)];
1167     [theView setAutoresizesSubviews: YES];
1168     if (o_currentCaptureView)
1169     {
1170         [[[[o_tabview tabViewItemAtIndex: 3] view] animator] replaceSubview: o_currentCaptureView with: theView];
1171         [o_currentCaptureView release];
1172     }
1173     else
1174          [[[[o_tabview tabViewItemAtIndex: 3] view] animator] addSubview: theView];
1175     o_currentCaptureView = theView;
1176     [o_currentCaptureView retain];
1177 }
1178
1179 - (IBAction)openCaptureModeChanged:(id)sender
1180 {
1181     if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: @"EyeTV"] )
1182     {
1183         if( [[[VLCMain sharedInstance] eyeTVController] isEyeTVrunning] == YES )
1184         {
1185             if( [[[VLCMain sharedInstance] eyeTVController] isDeviceConnected] == YES )
1186             {
1187                 [self showCaptureView: o_eyetv_running_view];
1188                 [self setupChannelInfo];
1189             }
1190             else
1191             {
1192                 setEyeTVUnconnected;
1193             }
1194         }
1195         else
1196             [self showCaptureView: o_eyetv_notLaunched_view];
1197         [self setMRL: @""];
1198     } 
1199     else if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: _NS("Screen")] )
1200     {
1201         [self showCaptureView: o_screen_view];
1202         [self setMRL: @"screen://"];
1203         [o_screen_height_fld setIntValue: config_GetInt( p_intf, "screen-height" )];
1204         [o_screen_width_fld setIntValue: config_GetInt( p_intf, "screen-width" )];
1205         [o_screen_fps_fld setFloatValue: config_GetFloat( p_intf, "screen-fps" )];
1206         [o_screen_left_fld setIntValue: config_GetInt( p_intf, "screen-left" )];
1207         [o_screen_top_fld setIntValue: config_GetInt( p_intf, "screen-top" )];
1208         [o_screen_follow_mouse_ckb setIntValue: config_GetInt( p_intf, "screen-follow-mouse" )];
1209     }
1210     else if( [[[o_capture_mode_pop selectedItem] title] isEqualToString: _NS("Capture Device")] )
1211     {
1212         [self showCaptureView: o_qtk_view];
1213         if ([o_capture_width_fld intValue] <= 0)
1214             [self qtkChanged:nil];
1215
1216         if(!qtk_currdevice_uid)
1217             [self setMRL: @""];
1218         else
1219             [self setMRL:[NSString stringWithFormat:@"qtcapture://%@", qtk_currdevice_uid]];
1220     }
1221 }
1222
1223 - (IBAction)screenStepperChanged:(id)sender
1224 {
1225     [o_screen_fps_fld setFloatValue: [o_screen_fps_stp floatValue]];
1226     [o_panel makeFirstResponder: o_screen_fps_fld];
1227     [self setMRL: @"screen://"];
1228 }
1229
1230 - (void)screenFPSfieldChanged:(NSNotification *)o_notification
1231 {
1232     [o_screen_fps_stp setFloatValue: [o_screen_fps_fld floatValue]];
1233     if( [[o_screen_fps_fld stringValue] isEqualToString: @""] )
1234         [o_screen_fps_fld setFloatValue: 1.0];
1235     [self setMRL: @"screen://"];
1236 }
1237
1238 - (IBAction)eyetvSwitchChannel:(id)sender
1239 {
1240     if( sender == o_eyetv_nextProgram_btn )
1241     {
1242         int chanNum = [[[VLCMain sharedInstance] eyeTVController] switchChannelUp: YES];
1243         [o_eyetv_channels_pop selectItemWithTag:chanNum];
1244         [self setMRL: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
1245     }
1246     else if( sender == o_eyetv_previousProgram_btn )
1247     {
1248         int chanNum = [[[VLCMain sharedInstance] eyeTVController] switchChannelUp: NO];
1249         [o_eyetv_channels_pop selectItemWithTag:chanNum];
1250         [self setMRL: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
1251     }
1252     else if( sender == o_eyetv_channels_pop )
1253     {
1254         int chanNum = [[sender selectedItem] tag];
1255         [[[VLCMain sharedInstance] eyeTVController] selectChannel:chanNum];
1256         [self setMRL: [NSString stringWithFormat:@"eyetv:// :eyetv-channel=%d", chanNum]];
1257     }
1258     else
1259         msg_Err( VLCIntf, "eyetvSwitchChannel sent by unknown object" );
1260 }
1261
1262 - (IBAction)eyetvLaunch:(id)sender
1263 {
1264     [[[VLCMain sharedInstance] eyeTVController] launchEyeTV];
1265 }
1266
1267 - (IBAction)eyetvGetPlugin:(id)sender
1268 {
1269     [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: @"http://www.videolan.org/vlc/eyetv"]];
1270 }
1271
1272 - (void)eyetvChanged:(NSNotification *)o_notification
1273 {
1274     if( [[o_notification name] isEqualToString: @"DeviceAdded"] )
1275     {
1276         msg_Dbg( VLCIntf, "eyetv device was added" );
1277         [self showCaptureView: o_eyetv_running_view];
1278         [self setupChannelInfo];
1279     }
1280     else if( [[o_notification name] isEqualToString: @"DeviceRemoved"] )
1281     {
1282         /* leave the channel selection like that,
1283          * switch to our "no device" tab */
1284         msg_Dbg( VLCIntf, "eyetv device was removed" );
1285         setEyeTVUnconnected;
1286     }
1287     else if( [[o_notification name] isEqualToString: @"PluginQuit"] )
1288     {
1289         /* switch to the "launch eyetv" tab */
1290         msg_Dbg( VLCIntf, "eyetv was terminated" );
1291         [self showCaptureView: o_eyetv_notLaunched_view];
1292     }
1293     else if( [[o_notification name] isEqualToString: @"PluginInit"] )
1294     {
1295         /* we got no device yet */
1296         msg_Dbg( VLCIntf, "eyetv was launched, no device yet" );
1297         setEyeTVUnconnected;
1298     }
1299 }
1300
1301 /* little helper method, since this code needs to be run by multiple objects */
1302 - (void)setupChannelInfo
1303 {
1304     /* set up channel selection */
1305     [o_eyetv_channels_pop removeAllItems];
1306     [o_eyetv_chn_bgbar setHidden: NO];
1307     [o_eyetv_chn_bgbar animate: self];
1308     [o_eyetv_chn_status_txt setStringValue: _NS("Retrieving Channel Info...")];
1309     [o_eyetv_chn_status_txt setHidden: NO];
1310  
1311     /* retrieve info */
1312     NSEnumerator *channels = [[[VLCMain sharedInstance] eyeTVController] allChannels];
1313     int x = -2;
1314     [[[o_eyetv_channels_pop menu] addItemWithTitle: _NS("Composite input")
1315                                                action: nil
1316                                         keyEquivalent: @""] setTag:x++];
1317     [[[o_eyetv_channels_pop menu] addItemWithTitle: _NS("S-Video input")
1318                                                action: nil
1319                                         keyEquivalent: @""] setTag:x++];
1320     if( channels ) 
1321     {
1322         NSString *channel;
1323         [[o_eyetv_channels_pop menu] addItem: [NSMenuItem separatorItem]];
1324         while( channel = [channels nextObject] )
1325         {
1326             /* we have to add items this way, because we accept duplicates
1327              * additionally, we save a bit of time */
1328             [[[o_eyetv_channels_pop menu] addItemWithTitle: channel
1329                                                    action: nil
1330                                             keyEquivalent: @""] setTag:++x];
1331         }
1332         /* make Tuner the default */
1333         [o_eyetv_channels_pop selectItemWithTag:[[[VLCMain sharedInstance] eyeTVController] currentChannel]];
1334     }
1335  
1336     /* clean up GUI */
1337     [o_eyetv_chn_bgbar setHidden: YES];
1338     [o_eyetv_chn_status_txt setHidden: YES];
1339 }
1340
1341 - (IBAction)subsChanged:(id)sender
1342 {
1343     if ([o_file_sub_ckbox state] == NSOnState)
1344     {
1345         [o_file_sub_btn_settings setEnabled:YES];
1346         if (o_sub_path) {
1347             [o_file_subtitles_filename_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath:o_sub_path]];
1348             [o_file_subtitles_icon_well setImage: [[NSWorkspace sharedWorkspace] iconForFile:o_sub_path]];
1349         }
1350     }
1351     else
1352     {
1353         [o_file_sub_btn_settings setEnabled:NO];
1354         [o_file_subtitles_filename_lbl setStringValue: @""];
1355         [o_file_subtitles_icon_well setImage: NULL];
1356     }
1357 }
1358
1359 - (IBAction)subSettings:(id)sender
1360 {
1361     [NSApp beginSheet: o_file_sub_sheet
1362         modalForWindow: [sender window]
1363         modalDelegate: self
1364         didEndSelector: NULL
1365         contextInfo: nil];
1366 }
1367
1368 - (IBAction)subCloseSheet:(id)sender
1369 {
1370     [self subsChanged: nil];
1371     [o_file_sub_sheet orderOut:sender];
1372     [NSApp endSheet: o_file_sub_sheet];
1373 }
1374
1375 - (IBAction)subFileBrowse:(id)sender
1376 {
1377     NSOpenPanel *o_open_panel = [NSOpenPanel openPanel];
1378  
1379     [o_open_panel setAllowsMultipleSelection: NO];
1380     [o_open_panel setTitle: _NS("Open File")];
1381     [o_open_panel setPrompt: _NS("Open")];
1382
1383     if( [o_open_panel runModal] == NSOKButton )
1384     {
1385         o_sub_path = [[[o_open_panel URLs] objectAtIndex: 0] path];
1386         [o_sub_path retain];
1387         [o_file_subtitles_filename_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath:o_sub_path]];
1388         [o_file_sub_path_fld setStringValue: [o_file_subtitles_filename_lbl stringValue]];
1389         [o_file_sub_path_lbl setHidden: YES];
1390         [o_file_subtitles_icon_well setImage: [[NSWorkspace sharedWorkspace] iconForFile:o_sub_path]];
1391         [o_file_sub_icon_view setImage: [o_file_subtitles_icon_well image]];
1392     }
1393     else
1394     {
1395         [o_file_sub_path_lbl setHidden: NO];
1396         [o_file_sub_path_fld setStringValue:@""];
1397         [o_file_subtitles_filename_lbl setStringValue:@""];
1398         [o_file_subtitles_icon_well setImage: nil];
1399         [o_file_sub_icon_view setImage: nil];
1400     }
1401 }
1402
1403 - (IBAction)subOverride:(id)sender
1404 {
1405     BOOL b_state = [o_file_sub_override state];
1406     [o_file_sub_delay setEnabled: b_state];
1407     [o_file_sub_delay_stp setEnabled: b_state];
1408     [o_file_sub_fps setEnabled: b_state];
1409     [o_file_sub_fps_stp setEnabled: b_state];
1410 }
1411
1412 - (IBAction)subDelayStepperChanged:(id)sender
1413 {
1414     [o_file_sub_delay setIntValue: [o_file_sub_delay_stp intValue]];
1415 }
1416
1417 - (IBAction)subFpsStepperChanged:(id)sender;
1418 {
1419     [o_file_sub_fps setFloatValue: [o_file_sub_fps_stp floatValue]];
1420 }
1421
1422 - (IBAction)panelCancel:(id)sender
1423 {
1424     [NSApp stopModalWithCode: 0];
1425 }
1426
1427 - (IBAction)panelOk:(id)sender
1428 {
1429     if( [[self MRL] length] )
1430         [NSApp stopModalWithCode: 1];
1431     else
1432         NSBeep();
1433 }
1434
1435 - (NSArray *)qtkvideoDevices
1436 {
1437     if (!qtkvideoDevices)
1438         [self qtkrefreshDevices];
1439     return qtkvideoDevices;
1440 }
1441
1442 - (void)qtkrefreshDevices
1443 {
1444     [qtkvideoDevices release];
1445     qtkvideoDevices = [[[QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeVideo] arrayByAddingObjectsFromArray:[QTCaptureDevice inputDevicesWithMediaType:QTMediaTypeMuxed]] retain];
1446 }
1447
1448 @end
1449
1450 @implementation VLCOpenTextField
1451
1452 - (void)mouseDown:(NSEvent *)theEvent
1453 {
1454     [[NSNotificationCenter defaultCenter] postNotificationName: @"VLCOpenTextFieldWasClicked"
1455                                                         object: self];
1456     [super mouseDown: theEvent];
1457 }
1458
1459 @end