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