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