]> git.sesse.net Git - vlc/blob - modules/gui/macosx/ConvertAndSave.m
macosx: use modern ObjC syntax for C-Numbers to NSNumber conversations
[vlc] / modules / gui / macosx / ConvertAndSave.m
1 /*****************************************************************************
2  * ConvertAndSave.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2012 Felix Paul Kühne
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #import "ConvertAndSave.h"
25 #import "intf.h"
26 #import "playlist.h"
27 #import <vlc_common.h>
28 #import <vlc_url.h>
29
30 /* mini doc:
31  * the used NSMatrix includes a bunch of cells referenced most easily by tags. There you go: */
32 #define MPEGTS 0
33 #define WEBM 1
34 #define OGG 2
35 #define MP4 3
36 #define MPEGPS 4
37 #define MJPEG 5
38 #define WAV 6
39 #define FLV 7
40 #define MPEG1 8
41 #define MKV 9
42 #define RAW 10
43 #define AVI 11
44 #define ASF 12
45 /* 13-15 are present, but not set */
46
47 @interface VLCConvertAndSave (Internal)
48 - (void)updateDropView;
49 - (void)updateOKButton;
50 - (void)resetCustomizationSheetBasedOnProfile:(NSString *)profileString;
51 - (void)selectCellByEncapsulationFormat:(NSString *)format;
52 - (NSString *)currentEncapsulationFormatAsFileExtension:(BOOL)b_extension;
53 - (NSString *)composedOptions;
54 - (void)updateCurrentProfile;
55 - (void)storeProfilesOnDisk;
56 - (void)recreateProfilePopup;
57 @end
58
59 @implementation VLCConvertAndSave
60
61 @synthesize MRL=_MRL, outputDestination=_outputDestination, profileNames=_profileNames, profileValueList=_profileValueList, currentProfile=_currentProfile;
62
63 static VLCConvertAndSave *_o_sharedInstance = nil;
64
65 #pragma mark -
66 #pragma mark Initialization
67
68 + (void)initialize{
69     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
70
71     /* We are using the same format as the Qt4 intf here:
72      * Container(string), transcode video(bool), transcode audio(bool),
73      * use subtitles(bool), video codec(string), video bitrate(integer),
74      * scale(float), fps(float), width(integer, height(integer),
75      * audio codec(string), audio bitrate(integer), channels(integer),
76      * samplerate(integer), subtitle codec(string), subtitle overlay(bool) */
77     NSArray * defaultProfiles = [[NSArray alloc] initWithObjects:
78                                  @"mp4;1;1;0;h264;0;0;0;0;0;mpga;128;2;44100;0;1",
79                                  @"webm;1;1;0;VP80;2000;0;0;0;0;vorb;128;2;44100;0;1",
80                                  @"ts;1;1;0;h264;800;1;0;0;0;mpga;128;2;44100;0;0",
81                                  @"ts;1;1;0;drac;800;1;0;0;0;mpga;128;2;44100;0;0",
82                                  @"ogg;1;1;0;theo;800;1;0;0;0;vorb;128;2;44100;0;0",
83                                  @"ogg;1;1;0;theo;800;1;0;0;0;flac;128;2;44100;0;0",
84                                  @"ts;1;1;0;mp2v;800;1;0;0;0;mpga;128;2;44100;0;0",
85                                  @"asf;1;1;0;WMV2;800;1;0;0;0;wma2;128;2;44100;0;0",
86                                  @"asf;1;1;0;DIV3;800;1;0;0;0;mp3;128;2;44100;0;0",
87                                  @"ogg;1;1;0;none;800;1;0;0;0;vorb;128;2;44100;none;0",
88                                  @"raw;1;1;0;none;800;1;0;0;0;mp3;128;2;44100;none;0",
89                                  @"mp4;1;1;0;none;800;1;0;0;0;mpga;128;2;44100;none;0",
90                                  @"raw;1;1;0;none;800;1;0;0;0;flac;128;2;44100;none;0",
91                                  @"wav;1;1;0;none;800;1;0;0;0;s16l;128;2;44100;none;0", nil];
92
93     NSArray * defaultProfileNames = [[NSArray alloc] initWithObjects:
94                                      @"Video - H.264 + MP3 (MP4)",
95                                      @"Video - VP80 + Vorbis (Webm)",
96                                      @"Video - H.264 + MP3 (TS)",
97                                      @"Video - Dirac + MP3 (TS)",
98                                      @"Video - Theora + Vorbis (OGG)",
99                                      @"Video - Theora + Flac (OGG)",
100                                      @"Video - MPEG-2 + MPGA (TS)",
101                                      @"Video - WMV + WMA (ASF)",
102                                      @"Video - DIV3 + MP3 (ASF)",
103                                      @"Audio - Vorbis (OGG)",
104                                      @"Audio - MP3",
105                                      @"Audio - MP3 (MP4)",
106                                      @"Audio - FLAC",
107                                      @"Audio - CD",
108                                      nil];
109
110     NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:defaultProfiles, @"CASProfiles", defaultProfileNames, @"CASProfileNames", nil];
111
112     [defaults registerDefaults:appDefaults];
113     [defaultProfiles release];
114     [defaultProfileNames release];
115 }
116
117 + (VLCConvertAndSave *)sharedInstance
118 {
119     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
120 }
121
122 - (id)init
123 {
124     if (_o_sharedInstance) {
125         [self dealloc];
126     } else {
127         _o_sharedInstance = [super init];
128     }
129
130     return _o_sharedInstance;
131 }
132
133 - (void)dealloc
134 {
135     if (_currentProfile)
136         [_currentProfile release];
137
138     [_profileNames release];
139     [_profileValueList release];
140     [_videoCodecs release];
141     [_audioCodecs release];
142     [_subsCodecs release];
143
144     [super dealloc];
145 }
146
147 - (void)awakeFromNib
148 {
149     [_window setTitle: _NS("Convert & Stream")];
150     [_ok_btn setTitle: _NS("Go!")];
151     [_drop_lbl setStringValue: _NS("Drop media here")];
152     [_drop_btn setTitle: _NS("Open media...")];
153     [_profile_lbl setStringValue: _NS("Choose Profile")];
154     [_profile_btn setTitle: _NS("Customize...")];
155     [_destination_lbl setStringValue: _NS("Choose Destination")];
156     [_destination_filename_stub_lbl setStringValue: _NS("Choose an output location")];
157     [_destination_filename_lbl setHidden: YES];
158     [_destination_browse_btn setTitle:_NS("Browse...")];
159     [_destination_stream_btn setTitle:_NS("Setup Streaming...")];
160     [_destination_stream_lbl setStringValue:@"Select Streaming Method"];
161     [_destination_itwantafile_btn setTitle:_NS("Save as File")];
162     [_destination_itwantastream_btn setTitle:_NS("Stream")];
163     [_destination_cancel_btn setHidden:YES];
164     [_customize_ok_btn setTitle: _NS("Apply")];
165     [_customize_cancel_btn setTitle: _NS("Cancel")];
166     [_customize_newProfile_btn setTitle: _NS("Save as new Profile...")];
167     [[_customize_tabview tabViewItemAtIndex:0] setLabel: _NS("Encapsulation")];
168     [[_customize_tabview tabViewItemAtIndex:1] setLabel: _NS("Video codec")];
169     [[_customize_tabview tabViewItemAtIndex:2] setLabel: _NS("Audio codec")];
170     [[_customize_tabview tabViewItemAtIndex:3] setLabel: _NS("Subtitles")];
171     [_customize_tabview selectTabViewItemAtIndex: 0];
172     [_customize_vid_ckb setTitle: _NS("Video")];
173     [_customize_vid_keep_ckb setTitle: _NS("Keep original video track")];
174     [_customize_vid_codec_lbl setStringValue: _NS("Codec")];
175     [_customize_vid_bitrate_lbl setStringValue: _NS("Bitrate")];
176     [_customize_vid_framerate_lbl setStringValue: _NS("Frame Rate")];
177     [_customize_vid_res_box setTitle: _NS("Resolution")];
178     [_customize_vid_res_lbl setStringValue: _NS("You just need to fill one of the three following parameters, VLC will autodetect the other using the original aspect ratio")];
179     [_customize_vid_width_lbl setStringValue: _NS("Width")];
180     [_customize_vid_height_lbl setStringValue: _NS("Height")];
181     [_customize_vid_scale_lbl setStringValue: _NS("Scale")];
182     [_customize_aud_ckb setTitle: _NS("Audio")];
183     [_customize_aud_keep_ckb setTitle: _NS("Keep original audio track")];
184     [_customize_aud_codec_lbl setStringValue: _NS("Codec")];
185     [_customize_aud_bitrate_lbl setStringValue: _NS("Bitrate")];
186     [_customize_aud_channels_lbl setStringValue: _NS("Channels")];
187     [_customize_aud_samplerate_lbl setStringValue: _NS("Samplerate")];
188     [_customize_subs_ckb setTitle: _NS("Subtitles")];
189     [_customize_subs_overlay_ckb setTitle: _NS("Overlay subtitles on the video")];
190     [_stream_ok_btn setTitle:_NS("OK")];
191     [_stream_destination_lbl setStringValue:_NS("Stream Destination")];
192     [_stream_announcement_lbl setStringValue:_NS("Stream Announcement")];
193     [_stream_type_lbl setStringValue:_NS("Type")];
194     [_stream_address_lbl setStringValue:_NS("Address")];
195     [_stream_ttl_lbl setStringValue:_NS("TTL")];
196     [_stream_ttl_fld setEnabled:NO];
197     [_stream_ttl_stepper setEnabled:NO];
198     [_stream_port_lbl setStringValue:_NS("Port")];
199     [_stream_sap_ckb setStringValue:_NS("SAP Announcement")];
200     [[_stream_sdp_matrix cellWithTag:0] setTitle:_NS("None")];
201     [[_stream_sdp_matrix cellWithTag:1] setTitle:_NS("HTTP Announcement")];
202     [[_stream_sdp_matrix cellWithTag:2] setTitle:_NS("RTSP Announcement")];
203     [[_stream_sdp_matrix cellWithTag:3] setTitle:_NS("Export SDP as file")];
204     [_stream_sap_ckb setState:NSOffState];
205     [_stream_sdp_matrix setEnabled:NO];
206
207     /* there is no way to hide single cells, so replace the existing ones with empty cells.. */
208     id blankCell = [[[NSCell alloc] init] autorelease];
209     [blankCell setEnabled:NO];
210     [_customize_encap_matrix putCell:blankCell atRow:3 column:1];
211     [_customize_encap_matrix putCell:blankCell atRow:3 column:2];
212     [_customize_encap_matrix putCell:blankCell atRow:3 column:3];
213
214     /* fetch profiles from defaults */
215     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
216     [self setProfileValueList: [defaults arrayForKey:@"CASProfiles"]];
217     [self setProfileNames: [defaults arrayForKey:@"CASProfileNames"]];
218     [self recreateProfilePopup];
219
220     _videoCodecs = @[@[@"MPEG-1", @"MPEG-2", @"MPEG-4", @"DIVX 1", @"DIVX 2", @"DIVX 3", @"H.263", @"H.264", @"VP8", @"WMV1", @"WMV2", @"M-JPEG", @"Theora", @"Dirac"],
221   @[@"mpgv", @"mp2v", @"mp4v", @"DIV1", @"DIV2", @"DIV3", @"H263", @"h264", @"VP80", @"WMV1", @"WMV2", @"MJPG", @"theo", @"drac"]];
222     _audioCodecs = @[@[@"MPEG Audio", @"MP3", @"MPEG 4 Audio (AAC)", @"A52/AC-3", @"Vorbis", @"Flac", @"Speex", @"WAV", @"WMA2"],
223                     @[@"mpga", @"mp3", @"mp4a", @"a52", @"vorb", @"flac", @"spx", @"s16l", @"wma2"]];
224     _subsCodecs = @[@[@"DVB subtitle", @"T.140"],
225                    @[@"dvbs", @"t140"]];
226
227     [_customize_vid_codec_pop removeAllItems];
228     [_customize_vid_scale_pop removeAllItems];
229     [_customize_aud_codec_pop removeAllItems];
230     [_customize_aud_samplerate_pop removeAllItems];
231     [_customize_subs_pop removeAllItems];
232
233     [_customize_vid_codec_pop addItemsWithTitles:[_videoCodecs objectAtIndex:0]];
234     [_customize_aud_codec_pop addItemsWithTitles:[_audioCodecs objectAtIndex:0]];
235     [_customize_subs_pop addItemsWithTitles:[_subsCodecs objectAtIndex:0]];
236
237     [_customize_aud_samplerate_pop addItemWithTitle:@"8000"];
238     [_customize_aud_samplerate_pop addItemWithTitle:@"11025"];
239     [_customize_aud_samplerate_pop addItemWithTitle:@"22050"];
240     [_customize_aud_samplerate_pop addItemWithTitle:@"44100"];
241     [_customize_aud_samplerate_pop addItemWithTitle:@"48000"];
242
243     [_customize_vid_scale_pop addItemWithTitle:@"1"];
244     [_customize_vid_scale_pop addItemWithTitle:@"0.25"];
245     [_customize_vid_scale_pop addItemWithTitle:@"0.5"];
246     [_customize_vid_scale_pop addItemWithTitle:@"0.75"];
247     [_customize_vid_scale_pop addItemWithTitle:@"1.25"];
248     [_customize_vid_scale_pop addItemWithTitle:@"1.5"];
249     [_customize_vid_scale_pop addItemWithTitle:@"1.75"];
250     [_customize_vid_scale_pop addItemWithTitle:@"2"];
251
252     [_ok_btn setEnabled: NO];
253
254     [self resetCustomizationSheetBasedOnProfile:[self.profileValueList objectAtIndex:0]];
255 }
256
257 # pragma mark -
258 # pragma mark Code to Communicate with other objects
259
260 - (void)toggleWindow
261 {
262     [_window makeKeyAndOrderFront: nil];
263 }
264
265 # pragma mark -
266 # pragma mark User Interaction
267
268 - (IBAction)finalizePanel:(id)sender
269 {
270     if (b_streaming) {
271         if ([[[_stream_type_pop selectedItem] title] isEqualToString:@"HTTP"]) {
272             NSString *muxformat = [self.currentProfile objectAtIndex:0];
273             if ([muxformat isEqualToString:@"wav"] || [muxformat isEqualToString:@"mov"] || [muxformat isEqualToString:@"mp4"] || [muxformat isEqualToString:@"mkv"]) {
274                 NSBeginInformationalAlertSheet(_NS("Invalid container format for HTTP streaming"), _NS("OK"), @"", @"", _window,
275                                                nil, nil, nil, nil,
276                                                _NS("Media encapsulated as %@ cannot be streamed through the HTTP protocol for technical reasons."),
277                                                [[self currentEncapsulationFormatAsFileExtension:YES] uppercaseString]);
278                 return;
279             }
280         }
281     }
282
283     playlist_t * p_playlist = pl_Get(VLCIntf);
284
285     input_item_t *p_input = input_item_New([_MRL UTF8String], [[_dropin_media_lbl stringValue] UTF8String]);
286     if (!p_input)
287         return;
288
289     input_item_AddOption(p_input, [[self composedOptions] UTF8String], VLC_INPUT_OPTION_TRUSTED);
290     if (b_streaming)
291         input_item_AddOption(p_input, [[NSString stringWithFormat:@"ttl=%@", [_stream_ttl_fld stringValue]] UTF8String], VLC_INPUT_OPTION_TRUSTED);
292
293     int returnValue;
294     returnValue = playlist_AddInput(p_playlist, p_input, PLAYLIST_STOP, PLAYLIST_END, true, pl_Unlocked);
295
296     if (returnValue == VLC_SUCCESS) {
297         /* let's "play" */
298         PL_LOCK;
299         playlist_item_t *p_item = playlist_ItemGetByInput(p_playlist, p_input);
300         playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, NULL,
301                          p_item);
302         PL_UNLOCK;
303     }
304     else
305         msg_Err(VLCIntf, "CAS: playlist add input failed :(");
306
307     /* we're done with this input */
308     vlc_gc_decref(p_input);
309
310     [_window performClose:sender];
311 }
312
313 - (IBAction)openMedia:(id)sender
314 {
315     /* preliminary implementation until the open panel is cleaned up */
316     NSOpenPanel * openPanel = [NSOpenPanel openPanel];
317     [openPanel setCanChooseDirectories:NO];
318     [openPanel setResolvesAliases:YES];
319     [openPanel setAllowsMultipleSelection:NO];
320     [openPanel beginSheetModalForWindow:_window completionHandler:^(NSInteger returnCode) {
321         if (returnCode == NSOKButton)
322         {
323             [self setMRL: @(vlc_path2uri([[[openPanel URL] path] UTF8String], NULL))];
324             [self updateOKButton];
325             [self updateDropView];
326         }
327     }];
328 }
329
330 - (IBAction)switchProfile:(id)sender
331 {
332     NSUInteger index = [_profile_pop indexOfSelectedItem];
333     if (index < ([self.profileValueList count] - 1))
334         [self resetCustomizationSheetBasedOnProfile:[self.profileValueList objectAtIndex:index]];
335 }
336
337 - (IBAction)customizeProfile:(id)sender
338 {
339     [NSApp beginSheet:_customize_panel modalForWindow:_window modalDelegate:self didEndSelector:NULL contextInfo:nil];
340 }
341
342 - (IBAction)closeCustomizationSheet:(id)sender
343 {
344     [_customize_panel orderOut:sender];
345     [NSApp endSheet: _customize_panel];
346
347     if (sender == _customize_ok_btn)
348         [self updateCurrentProfile];
349 }
350
351 - (IBAction)newProfileAction:(id)sender
352 {
353     /* show panel */
354     VLCEnterTextPanel * panel = [VLCEnterTextPanel sharedInstance];
355     [panel setTitle: _NS("Save as new profile")];
356     [panel setSubTitle: _NS("Enter a name for the new profile:")];
357     [panel setCancelButtonLabel: _NS("Cancel")];
358     [panel setOKButtonLabel: _NS("Save")];
359     [panel setTarget:self];
360
361     [panel runModalForWindow:_customize_panel];
362 }
363
364 - (IBAction)deleteProfileAction:(id)sender
365 {
366     /* show panel */
367     VLCSelectItemInPopupPanel * panel = [VLCSelectItemInPopupPanel sharedInstance];
368     [panel setTitle:_NS("Remove a profile")];
369     [panel setSubTitle:_NS("Select the profile you would like to remove:")];
370     [panel setOKButtonLabel:_NS("Remove")];
371     [panel setCancelButtonLabel:_NS("Cancel")];
372     [panel setPopupButtonContent:self.profileNames];
373     [panel setTarget:self];
374
375     [panel runModalForWindow:_window];
376 }
377
378 - (IBAction)iWantAFile:(id)sender
379 {
380     NSRect boxFrame = [_destination_box frame];
381     NSRect subViewFrame = [_destination_itwantafile_view frame];
382     subViewFrame.origin.x = (boxFrame.size.width - subViewFrame.size.width) / 2;
383     subViewFrame.origin.y = ((boxFrame.size.height - subViewFrame.size.height) / 2) - 15.;
384     [_destination_itwantafile_view setFrame: subViewFrame];
385     [[_destination_itwantafile_btn animator] setHidden: YES];
386     [[_destination_itwantastream_btn animator] setHidden: YES];
387     [_destination_box performSelector:@selector(addSubview:) withObject:_destination_itwantafile_view afterDelay:0.2];
388     [[_destination_cancel_btn animator] setHidden:NO];
389     b_streaming = NO;
390     [_ok_btn setTitle:_NS("Save")];
391 }
392
393 - (IBAction)iWantAStream:(id)sender
394 {
395     NSRect boxFrame = [_destination_box frame];
396     NSRect subViewFrame = [_destination_itwantastream_view frame];
397     subViewFrame.origin.x = (boxFrame.size.width - subViewFrame.size.width) / 2;
398     subViewFrame.origin.y = ((boxFrame.size.height - subViewFrame.size.height) / 2) - 15.;
399     [_destination_itwantastream_view setFrame: subViewFrame];
400     [[_destination_itwantafile_btn animator] setHidden: YES];
401     [[_destination_itwantastream_btn animator] setHidden: YES];
402     [_destination_box performSelector:@selector(addSubview:) withObject:_destination_itwantastream_view afterDelay:0.2];
403     [[_destination_cancel_btn animator] setHidden:NO];
404     b_streaming = YES;
405     [_ok_btn setTitle:_NS("Stream")];
406 }
407
408 - (IBAction)cancelDestination:(id)sender
409 {
410     if ([_destination_itwantastream_view superview] != nil)
411         [_destination_itwantastream_view removeFromSuperview];
412     if ([_destination_itwantafile_view superview] != nil)
413         [_destination_itwantafile_view removeFromSuperview];
414
415     [_destination_cancel_btn setHidden:YES];
416     [[_destination_itwantafile_btn animator] setHidden: NO];
417     [[_destination_itwantastream_btn animator] setHidden: NO];
418     b_streaming = NO;
419 }
420
421 - (IBAction)browseFileDestination:(id)sender
422 {
423     NSSavePanel * saveFilePanel = [NSSavePanel savePanel];
424     [saveFilePanel setCanSelectHiddenExtension: YES];
425     [saveFilePanel setCanCreateDirectories: YES];
426     if ([[_customize_encap_matrix selectedCell] tag] != RAW) // there is no clever guess for this
427         [saveFilePanel setAllowedFileTypes:@[[self currentEncapsulationFormatAsFileExtension:YES]]];
428     [saveFilePanel beginSheetModalForWindow:_window completionHandler:^(NSInteger returnCode) {
429         if (returnCode == NSOKButton) {
430             [self setOutputDestination:[[saveFilePanel URL] path]];
431             [_destination_filename_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath:_outputDestination]];
432             [[_destination_filename_stub_lbl animator] setHidden: YES];
433             [[_destination_filename_lbl animator] setHidden: NO];
434         } else {
435             [self setOutputDestination:@""];
436             [[_destination_filename_lbl animator] setHidden: YES];
437             [[_destination_filename_stub_lbl animator] setHidden: NO];
438         }
439         [self updateOKButton];
440     }];
441 }
442
443 - (IBAction)showStreamPanel:(id)sender
444 {
445     [NSApp beginSheet:_stream_panel modalForWindow:_window modalDelegate:self didEndSelector:NULL contextInfo:nil];
446 }
447
448 - (IBAction)closeStreamPanel:(id)sender
449 {
450     /* provide a summary of the user selections */
451     NSMutableString * labelContent = [[NSMutableString alloc] initWithFormat:_NS("%@ stream to %@:%@"), [_stream_type_pop titleOfSelectedItem], [_stream_address_fld stringValue], [_stream_port_fld stringValue]];
452
453     if ([_stream_type_pop indexOfSelectedItem] > 1)
454         [labelContent appendFormat:@" (\"%@\")", [_stream_channel_fld stringValue]];
455
456     [_destination_stream_lbl setStringValue:labelContent];
457     [labelContent release];
458
459     /* catch obvious errors */
460     if (![[_stream_address_fld stringValue] length] > 0) {
461         NSBeginInformationalAlertSheet(_NS("No Address given"),
462                                        _NS("OK"), @"", @"", _stream_panel, nil, nil, nil, nil,
463                                        @"%@", _NS("In order to stream, a valid destination address is required."));
464         return;
465     }
466
467     if ([_stream_sap_ckb state] && ![[_stream_channel_fld stringValue] length] > 0) {
468         NSBeginInformationalAlertSheet(_NS("No Channel Name given"),
469                                        _NS("OK"), @"", @"", _stream_panel, nil, nil, nil, nil,
470                                        @"%@", _NS("SAP stream announcement is enabled. However, no channel name is provided."));
471         return;
472     }
473
474     if ([_stream_sdp_matrix isEnabled] && [_stream_sdp_matrix selectedCell] != [_stream_sdp_matrix cellWithTag:0] && ![[_stream_sdp_fld stringValue] length] > 0) {
475         NSBeginInformationalAlertSheet(_NS("No SDP URL given"),
476                                        _NS("OK"), @"", @"", _stream_panel, nil, nil, nil, nil,
477                                        @"%@", _NS("A SDP export is requested, but no URL is provided."));
478         return;
479     }
480
481     /* store destination for further reference and update UI */
482     [self setOutputDestination: [_stream_address_fld stringValue]];
483     [self updateOKButton];
484
485     [_stream_panel orderOut:sender];
486     [NSApp endSheet: _stream_panel];
487 }
488
489 - (IBAction)streamTypeToggle:(id)sender
490 {
491     NSUInteger index = [_stream_type_pop indexOfSelectedItem];
492     if (index <= 1) { // HTTP, MMSH
493         [_stream_ttl_fld setEnabled:NO];
494         [_stream_ttl_stepper setEnabled:NO];
495         [_stream_sap_ckb setEnabled:NO];
496         [_stream_sdp_matrix setEnabled:NO];
497     } else if (index == 2) { // RTP
498         [_stream_ttl_fld setEnabled:YES];
499         [_stream_ttl_stepper setEnabled:YES];
500         [_stream_sap_ckb setEnabled:YES];
501         [_stream_sdp_matrix setEnabled:YES];
502     } else { // UDP
503         [_stream_ttl_fld setEnabled:YES];
504         [_stream_ttl_stepper setEnabled:YES];
505         [_stream_sap_ckb setEnabled:YES];
506         [_stream_sdp_matrix setEnabled:NO];
507     }
508     [self streamAnnouncementToggle:sender];
509 }
510
511 - (IBAction)streamAnnouncementToggle:(id)sender
512 {
513     [_stream_channel_fld setEnabled:[_stream_sap_ckb state] && [_stream_sap_ckb isEnabled]];
514     [_stream_sdp_fld setEnabled:[_stream_sdp_matrix isEnabled] && ([_stream_sdp_matrix selectedCell] != [_stream_sdp_matrix cellWithTag:0])];
515
516     if ([[_stream_sdp_matrix selectedCell] tag] == 3)
517         [_stream_sdp_browsefile_btn setEnabled: YES];
518     else
519         [_stream_sdp_browsefile_btn setEnabled: NO];
520 }
521
522 - (IBAction)sdpFileLocationSelector:(id)sender
523 {
524     NSSavePanel * saveFilePanel = [NSSavePanel savePanel];
525     [saveFilePanel setCanSelectHiddenExtension: YES];
526     [saveFilePanel setCanCreateDirectories: YES];
527     [saveFilePanel setAllowedFileTypes:@[@"sdp"]];
528     [saveFilePanel beginSheetModalForWindow:_stream_panel completionHandler:^(NSInteger returnCode) {
529         if (returnCode == NSOKButton)
530             [_stream_sdp_fld setStringValue:[[saveFilePanel URL] path]];
531     }];
532 }
533
534 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
535 {
536     NSPasteboard *paste = [sender draggingPasteboard];
537     NSArray *types = @[NSFilenamesPboardType, @"VLCPlaylistItemPboardType"];
538     NSString *desired_type = [paste availableTypeFromArray: types];
539     NSData *carried_data = [paste dataForType: desired_type];
540
541     if (carried_data) {
542         if ([desired_type isEqualToString:NSFilenamesPboardType]) {
543             NSArray *values = [[paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
544
545             if ([values count] > 0) {
546                 [self setMRL: @(vlc_path2uri([[values objectAtIndex:0] UTF8String], NULL))];
547                 [self updateOKButton];
548                 [self updateDropView];
549                 return YES;
550             }
551         } else if ([desired_type isEqualToString:@"VLCPlaylistItemPboardType"]) {
552             NSArray * array = [[[VLCMain sharedInstance] playlist] draggedItems];
553             NSUInteger count = [array count];
554             if (count > 0) {
555                 playlist_t * p_playlist = pl_Get(VLCIntf);
556                 playlist_item_t * p_item = NULL;
557
558                 PL_LOCK;
559                 /* let's look for the first proper input item */
560                 for (NSUInteger x = 0; x < count; x++) {
561                     p_item = [[array objectAtIndex:x] pointerValue];
562                     if (p_item) {
563                         if (p_item->p_input) {
564                             if (p_item->p_input->psz_uri != nil) {
565                                 [self setMRL: [NSString stringWithFormat:@"%s", p_item->p_input->psz_uri]];
566                                 [self updateDropView];
567                                 [self updateOKButton];
568
569                                 PL_UNLOCK;
570
571                                 return YES;
572                             }
573                         }
574                     }
575                 }
576                 PL_UNLOCK;
577             }
578         }
579     }
580     return NO;
581 }
582
583 - (void)panel:(VLCEnterTextPanel *)panel returnValue:(NSUInteger)value text:(NSString *)text
584 {
585     if (value == NSOKButton) {
586         if ([text length] > 0) {
587             /* prepare current data */
588             [self updateCurrentProfile];
589
590             /* add profile to arrays */
591             NSMutableArray * workArray = [[NSMutableArray alloc] initWithArray:self.profileNames];
592             [workArray addObject:text];
593             [self setProfileNames:[[[NSArray alloc] initWithArray:workArray] autorelease]];
594             [workArray release];
595             workArray = [[NSMutableArray alloc] initWithArray:self.profileValueList];
596             [workArray addObject:[[[NSArray alloc] initWithArray:self.currentProfile] autorelease]];
597             [self setProfileValueList:[[[NSArray alloc] initWithArray:workArray] autorelease]];
598             [workArray release];
599
600             /* update UI */
601             [self recreateProfilePopup];
602             [_profile_pop selectItemWithTitle:text];
603
604             /* update internals */
605             [self switchProfile:self];
606             [self storeProfilesOnDisk];
607         }
608     }
609 }
610
611 - (void)panel:(VLCSelectItemInPopupPanel *)panel returnValue:(NSUInteger)value item:(NSUInteger)item
612 {
613     if (value == NSOKButton) {
614         /* remove requested profile from the arrays */
615         NSMutableArray * workArray = [[NSMutableArray alloc] initWithArray:self.profileNames];
616         [workArray removeObjectAtIndex:item];
617         [self setProfileNames:[[[NSArray alloc] initWithArray:workArray] autorelease]];
618         [workArray release];
619         workArray = [[NSMutableArray alloc] initWithArray:self.profileValueList];
620         [workArray removeObjectAtIndex:item];
621         [self setProfileValueList:[[[NSArray alloc] initWithArray:workArray] autorelease]];
622         [workArray release];
623
624         /* update UI */
625         [_profile_pop removeAllItems];
626         [_profile_pop addItemsWithTitles:self.profileNames];
627         [_profile_pop addItemWithTitle:_NS("Custom")];
628         [[_profile_pop menu] addItem:[NSMenuItem separatorItem]];
629         [_profile_pop addItemWithTitle:_NS("Organize Profiles...")];
630         [[_profile_pop lastItem] setTarget: self];
631         [[_profile_pop lastItem] setAction: @selector(deleteProfileAction:)];
632
633         /* update internals */
634         [self switchProfile:self];
635         [self storeProfilesOnDisk];
636     }
637 }
638
639 # pragma mark -
640 # pragma mark Private Functionality
641 - (void)updateDropView
642 {
643     if ([_MRL length] > 0) {
644         NSString * path = [[NSURL URLWithString:_MRL] path];
645         [_dropin_media_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath: path]];
646         NSImage * image = [[NSWorkspace sharedWorkspace] iconForFile: path];
647         [image setSize:NSMakeSize(128,128)];
648         [_dropin_icon_view setImage: image];
649
650         if (![_dropin_view superview]) {
651             NSRect boxFrame = [_drop_box frame];
652             NSRect subViewFrame = [_dropin_view frame];
653             subViewFrame.origin.x = (boxFrame.size.width - subViewFrame.size.width) / 2;
654             subViewFrame.origin.y = (boxFrame.size.height - subViewFrame.size.height) / 2;
655             [_dropin_view setFrame: subViewFrame];
656             [[_drop_image_view animator] setHidden: YES];
657             [_drop_box performSelector:@selector(addSubview:) withObject:_dropin_view afterDelay:0.6];
658         }
659     } else {
660         [_dropin_view removeFromSuperview];
661         [[_drop_image_view animator] setHidden: NO];
662     }
663 }
664
665 - (void)updateOKButton
666 {
667     if ([_outputDestination length] > 0 && [_MRL length] > 0)
668         [_ok_btn setEnabled: YES];
669     else
670         [_ok_btn setEnabled: NO];
671 }
672
673 - (void)resetCustomizationSheetBasedOnProfile:(NSString *)profileString
674 {
675     /* Container(string), transcode video(bool), transcode audio(bool),
676     * use subtitles(bool), video codec(string), video bitrate(integer),
677     * scale(float), fps(float), width(integer, height(integer),
678     * audio codec(string), audio bitrate(integer), channels(integer),
679     * samplerate(integer), subtitle codec(string), subtitle overlay(bool) */
680
681     NSArray * components = [profileString componentsSeparatedByString:@";"];
682     if ([components count] != 16) {
683         msg_Err(VLCIntf, "CAS: the requested profile '%s' is invalid", [profileString UTF8String]);
684         return;
685     }
686
687     [self selectCellByEncapsulationFormat:[components objectAtIndex:0]];
688     [_customize_vid_ckb setState:[[components objectAtIndex:1] intValue]];
689     [_customize_aud_ckb setState:[[components objectAtIndex:2] intValue]];
690     [_customize_subs_ckb setState:[[components objectAtIndex:3] intValue]];
691     [_customize_vid_bitrate_fld setStringValue:[components objectAtIndex:5]];
692     [_customize_vid_scale_pop selectItemWithTitle:[components objectAtIndex:6]];
693     [_customize_vid_framerate_fld setStringValue:[components objectAtIndex:7]];
694     [_customize_vid_width_fld setStringValue:[components objectAtIndex:8]];
695     [_customize_vid_height_fld setStringValue:[components objectAtIndex:9]];
696     [_customize_aud_bitrate_fld setStringValue:[components objectAtIndex:11]];
697     [_customize_aud_channels_fld setStringValue:[components objectAtIndex:12]];
698     [_customize_aud_samplerate_pop selectItemWithTitle:[components objectAtIndex:13]];
699     [_customize_subs_overlay_ckb setState:[[components objectAtIndex:15] intValue]];
700
701     /* since there is no proper lookup mechanism in arrays, we need to implement a string specific one ourselves */
702     NSArray * tempArray = [_videoCodecs objectAtIndex:1];
703     NSUInteger count = [tempArray count];
704     NSString * searchString = [components objectAtIndex:4];
705     if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
706         [_customize_vid_codec_pop selectItemAtIndex:-1];
707     } else {
708         for (NSUInteger x = 0; x < count; x++) {
709             if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
710                 [_customize_vid_codec_pop selectItemAtIndex:x];
711                 break;
712             }
713         }
714     }
715
716     tempArray = [_audioCodecs objectAtIndex:1];
717     count = [tempArray count];
718     searchString = [components objectAtIndex:10];
719     if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
720         [_customize_aud_codec_pop selectItemAtIndex:-1];
721     } else {
722         for (NSUInteger x = 0; x < count; x++) {
723             if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
724                 [_customize_aud_codec_pop selectItemAtIndex:x];
725                 break;
726             }
727         }
728     }
729
730     tempArray = [_subsCodecs objectAtIndex:1];
731     count = [tempArray count];
732     searchString = [components objectAtIndex:14];
733     if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
734         [_customize_subs_pop selectItemAtIndex:-1];
735     } else {
736         for (NSUInteger x = 0; x < count; x++) {
737             if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
738                 [_customize_subs_pop selectItemAtIndex:x];
739                 break;
740             }
741         }
742     }
743
744     [self setCurrentProfile: [[[NSMutableArray alloc] initWithArray: [profileString componentsSeparatedByString:@";"]] autorelease]];
745 }
746
747 - (void)selectCellByEncapsulationFormat:(NSString *)format
748 {
749     if ([format isEqualToString:@"ts"])
750         [_customize_encap_matrix selectCellWithTag:MPEGTS];
751     else if ([format isEqualToString:@"webm"])
752         [_customize_encap_matrix selectCellWithTag:WEBM];
753     else if ([format isEqualToString:@"ogg"])
754         [_customize_encap_matrix selectCellWithTag:OGG];
755     else if ([format isEqualToString:@"ogm"])
756         [_customize_encap_matrix selectCellWithTag:OGG];
757     else if ([format isEqualToString:@"mp4"])
758         [_customize_encap_matrix selectCellWithTag:MP4];
759     else if ([format isEqualToString:@"mov"])
760         [_customize_encap_matrix selectCellWithTag:MP4];
761     else if ([format isEqualToString:@"ps"])
762         [_customize_encap_matrix selectCellWithTag:MPEGPS];
763     else if ([format isEqualToString:@"mpjpeg"])
764         [_customize_encap_matrix selectCellWithTag:MJPEG];
765     else if ([format isEqualToString:@"wav"])
766         [_customize_encap_matrix selectCellWithTag:WAV];
767     else if ([format isEqualToString:@"flv"])
768         [_customize_encap_matrix selectCellWithTag:FLV];
769     else if ([format isEqualToString:@"mpeg1"])
770         [_customize_encap_matrix selectCellWithTag:MPEG1];
771     else if ([format isEqualToString:@"mkv"])
772         [_customize_encap_matrix selectCellWithTag:MKV];
773     else if ([format isEqualToString:@"raw"])
774         [_customize_encap_matrix selectCellWithTag:RAW];
775     else if ([format isEqualToString:@"avi"])
776         [_customize_encap_matrix selectCellWithTag:AVI];
777     else if ([format isEqualToString:@"asf"])
778         [_customize_encap_matrix selectCellWithTag:ASF];
779     else if ([format isEqualToString:@"wmv"])
780         [_customize_encap_matrix selectCellWithTag:ASF];
781     else
782         msg_Err(VLCIntf, "CAS: unknown encap format requested for customization");
783 }
784
785 - (NSString *)currentEncapsulationFormatAsFileExtension:(BOOL)b_extension
786 {
787     NSUInteger cellTag = [[_customize_encap_matrix selectedCell] tag];
788     NSString * returnValue;
789     switch (cellTag) {
790         case MPEGTS:
791             returnValue = @"ts";
792             break;
793         case WEBM:
794             returnValue = @"webm";
795             break;
796         case OGG:
797             returnValue = @"ogg";
798             break;
799         case MP4:
800         {
801             if (b_extension)
802                 returnValue = @"m4v";
803             else
804                 returnValue = @"mp4";
805             break;
806         }
807         case MPEGPS:
808         {
809             if (b_extension)
810                 returnValue = @"mpg";
811             else
812                 returnValue = @"ps";
813             break;
814         }
815         case MJPEG:
816             returnValue = @"mjpeg";
817             break;
818         case WAV:
819             returnValue = @"wav";
820             break;
821         case FLV:
822             returnValue = @"flv";
823             break;
824         case MPEG1:
825         {
826             if (b_extension)
827                 returnValue = @"mpg";
828             else
829                 returnValue = @"mpeg1";
830             break;
831         }
832         case MKV:
833             returnValue = @"mkv";
834             break;
835         case RAW:
836             returnValue = @"raw";
837             break;
838         case AVI:
839             returnValue = @"avi";
840             break;
841         case ASF:
842             returnValue = @"asf";
843             break;
844
845         default:
846             returnValue = @"none";
847             break;
848     }
849
850     return returnValue;
851 }
852
853 - (NSString *)composedOptions
854 {
855     NSMutableString *composedOptions = [[NSMutableString alloc] initWithString:@":sout=#transcode{"];
856     if ([[self.currentProfile objectAtIndex:1] intValue]) {
857         // video is enabled
858         [composedOptions appendFormat:@"vcodec=%@", [self.currentProfile objectAtIndex:4]];
859         if (![[self.currentProfile objectAtIndex:4] isEqualToString:@"none"]) {
860             if ([[self.currentProfile objectAtIndex:5] intValue] > 0) // bitrate
861                 [composedOptions appendFormat:@",vb=%@", [self.currentProfile objectAtIndex:5]];
862             if ([[self.currentProfile objectAtIndex:6] floatValue] > 0.) // scale
863                 [composedOptions appendFormat:@",scale=%@", [self.currentProfile objectAtIndex:6]];
864             if ([[self.currentProfile objectAtIndex:7] floatValue] > 0.) // fps
865                 [composedOptions appendFormat:@",fps=%@", [self.currentProfile objectAtIndex:7]];
866             if ([[self.currentProfile objectAtIndex:8] intValue] > 0) // width
867                 [composedOptions appendFormat:@",width=%@", [self.currentProfile objectAtIndex:8]];
868             if ([[self.currentProfile objectAtIndex:9] intValue] > 0) // height
869                 [composedOptions appendFormat:@",height=%@", [self.currentProfile objectAtIndex:9]];
870         }
871     }
872     if ([[self.currentProfile objectAtIndex:2] intValue]) {
873         // audio is enabled
874
875         // add another comma in case video is enabled
876         if ([[self.currentProfile objectAtIndex:1] intValue])
877             [composedOptions appendString:@","];
878
879         [composedOptions appendFormat:@"acodec=%@", [self.currentProfile objectAtIndex:10]];
880         if (![[self.currentProfile objectAtIndex:10] isEqualToString:@"none"]) {
881             [composedOptions appendFormat:@",ab=%@", [self.currentProfile objectAtIndex:11]]; // bitrate
882             [composedOptions appendFormat:@",channels=%@", [self.currentProfile objectAtIndex:12]]; // channel number
883             [composedOptions appendFormat:@",samplerate=%@", [self.currentProfile objectAtIndex:13]]; // sample rate
884         }
885     }
886     if ([self.currentProfile objectAtIndex:3]) {
887         // subtitles enabled
888         [composedOptions appendFormat:@",scodec=%@", [self.currentProfile objectAtIndex:14]];
889         if ([[self.currentProfile objectAtIndex:15] intValue])
890             [composedOptions appendFormat:@",soverlay"];
891     }
892
893     if (!b_streaming) {
894         /* file transcoding */
895         // add muxer
896         [composedOptions appendFormat:@"}:standard{mux=%@", [self.currentProfile objectAtIndex:0]];
897
898         // add output destination
899         [composedOptions appendFormat:@",access=file{no-overwrite},dst=%@}", _outputDestination];
900     } else {
901         /* streaming */
902         if ([[[_stream_type_pop selectedItem] title] isEqualToString:@"RTP"])
903             [composedOptions appendFormat:@":rtp{mux=ts,dst=%@,port=%@", _outputDestination, [_stream_port_fld stringValue]];
904         else if ([[[_stream_type_pop selectedItem] title] isEqualToString:@"UDP"])
905             [composedOptions appendFormat:@":standard{mux=ts,dst=%@,port=%@,access=udp", _outputDestination, [_stream_port_fld stringValue]];
906         else if ([[[_stream_type_pop selectedItem] title] isEqualToString:@"MMSH"])
907             [composedOptions appendFormat:@":standard{mux=asfh,dst=%@,port=%@,access=mmsh", _outputDestination, [_stream_port_fld stringValue]];
908         else
909             [composedOptions appendFormat:@":standard{mux=%@,dst=%@,port=%@,access=http", [self.currentProfile objectAtIndex:0], [_stream_port_fld stringValue], _outputDestination];
910
911         if ([_stream_sap_ckb state])
912             [composedOptions appendFormat:@",sap,name=\"%@\"", [_stream_channel_fld stringValue]];
913         if ([_stream_sdp_matrix selectedCell] != [_stream_sdp_matrix cellWithTag:0]) {
914             NSInteger tag = [[_stream_sdp_matrix selectedCell] tag];
915             switch (tag) {
916                 case 1:
917                     [composedOptions appendFormat:@",sdp=\"http://%@\"", [_stream_sdp_fld stringValue]];
918                     break;
919                 case 2:
920                     [composedOptions appendFormat:@",sdp=\"rtsp://%@\"", [_stream_sdp_fld stringValue]];
921                     break;
922                 case 3:
923                     [composedOptions appendFormat:@",sdp=\"file://%s\"", vlc_path2uri([[_stream_sdp_fld stringValue] UTF8String], NULL)];
924                 default:
925                     break;
926             }
927         }
928
929         [composedOptions appendString:@"} :sout-keep"];
930     }
931
932     NSString * returnString = [NSString stringWithString:composedOptions];
933     [composedOptions release];
934
935     return returnString;
936 }
937
938 - (void)updateCurrentProfile
939 {
940     [self.currentProfile removeAllObjects];
941
942     NSInteger i;
943     [self.currentProfile addObject: [self currentEncapsulationFormatAsFileExtension:NO]];
944     [self.currentProfile addObject: [NSString stringWithFormat:@"%li", [_customize_vid_ckb state]]];
945     [self.currentProfile addObject: [NSString stringWithFormat:@"%li", [_customize_aud_ckb state]]];
946     [self.currentProfile addObject: [NSString stringWithFormat:@"%li", [_customize_subs_ckb state]]];
947     i = [_customize_vid_codec_pop indexOfSelectedItem];
948     if (i >= 0)
949         [self.currentProfile addObject: [[_videoCodecs objectAtIndex:1] objectAtIndex:i]];
950     else
951         [self.currentProfile addObject: @"none"];
952     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_vid_bitrate_fld intValue]]];
953     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [[[_customize_vid_scale_pop selectedItem] title] intValue]]];
954     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_vid_framerate_fld intValue]]];
955     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_vid_width_fld intValue]]];
956     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_vid_height_fld intValue]]];
957     i = [_customize_aud_codec_pop indexOfSelectedItem];
958     if (i >= 0)
959         [self.currentProfile addObject: [[_audioCodecs objectAtIndex:1] objectAtIndex:i]];
960     else
961         [self.currentProfile addObject: @"none"];
962     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_aud_bitrate_fld intValue]]];
963     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_aud_channels_fld intValue]]];
964     [self.currentProfile addObject: [[_customize_aud_samplerate_pop selectedItem] title]];
965     i = [_customize_subs_pop indexOfSelectedItem];
966     if (i >= 0)
967         [self.currentProfile addObject: [[_subsCodecs objectAtIndex:1] objectAtIndex:i]];
968     else
969         [self.currentProfile addObject: @"none"];
970     [self.currentProfile addObject: [NSString stringWithFormat:@"%li", [_customize_subs_overlay_ckb state]]];
971 }
972
973 - (void)storeProfilesOnDisk
974 {
975     NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
976     [defaults setObject:_profileNames forKey:@"CASProfileNames"];
977     [defaults setObject:_profileValueList forKey:@"CASProfiles"];
978     [defaults synchronize];
979 }
980
981 - (void)recreateProfilePopup
982 {
983     [_profile_pop removeAllItems];
984     [_profile_pop addItemsWithTitles:self.profileNames];
985     [_profile_pop addItemWithTitle:_NS("Custom")];
986     [[_profile_pop menu] addItem:[NSMenuItem separatorItem]];
987     [_profile_pop addItemWithTitle:_NS("Organize Profiles...")];
988     [[_profile_pop lastItem] setTarget: self];
989     [[_profile_pop lastItem] setAction: @selector(deleteProfileAction:)];
990 }
991
992 @end
993
994 # pragma mark -
995 # pragma mark Drag and drop handling
996
997 @implementation VLCDropEnabledBox
998
999 - (void)awakeFromNib
1000 {
1001     [self registerForDraggedTypes:@[NSFilenamesPboardType, @"VLCPlaylistItemPboardType"]];
1002 }
1003
1004 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
1005 {
1006     b_activeDragAndDrop = YES;
1007     [self setNeedsDisplay:YES];
1008
1009     [[NSCursor dragCopyCursor] set];
1010
1011     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
1012         return NSDragOperationGeneric;
1013
1014     return NSDragOperationNone;
1015 }
1016
1017 - (void)draggingEnded:(id < NSDraggingInfo >)sender
1018 {
1019     [[NSCursor arrowCursor] set];
1020     b_activeDragAndDrop = NO;
1021     [self setNeedsDisplay:YES];
1022 }
1023
1024 - (void)draggingExited:(id < NSDraggingInfo >)sender
1025 {
1026     [[NSCursor arrowCursor] set];
1027     b_activeDragAndDrop = NO;
1028     [self setNeedsDisplay:YES];
1029 }
1030
1031 - (void)drawRect:(NSRect)dirtyRect
1032 {
1033     if (b_activeDragAndDrop) {
1034         [[NSColor colorWithCalibratedRed:(.154/.255) green:(.154/.255) blue:(.154/.255) alpha:1.] setFill];
1035         NSRect frameRect = [[self contentView] bounds];
1036         frameRect.origin.x += 10;
1037         frameRect.origin.y += 10;
1038         frameRect.size.width -= 17;
1039         frameRect.size.height -= 17;
1040         NSFrameRectWithWidthUsingOperation(frameRect, 4., NSCompositeHighlight);
1041     }
1042
1043     [super drawRect:dirtyRect];
1044 }
1045
1046 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
1047 {
1048     return YES;
1049 }
1050
1051 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
1052 {
1053     return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
1054 }
1055
1056 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
1057 {
1058     [self setNeedsDisplay:YES];
1059 }
1060
1061 @end
1062
1063 @implementation VLCDropEnabledImageView
1064
1065 - (void)awakeFromNib
1066 {
1067     [self registerForDraggedTypes:@[NSFilenamesPboardType, @"VLCPlaylistItemPboardType"]];
1068 }
1069
1070 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
1071 {
1072     return [[[self superview] superview] draggingEntered:sender];
1073 }
1074
1075 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
1076 {
1077     return YES;
1078 }
1079
1080 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
1081 {
1082     return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
1083 }
1084
1085 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
1086 {
1087     [self setNeedsDisplay:YES];
1088 }
1089
1090 @end
1091
1092 @implementation VLCDropEnabledButton
1093
1094 - (void)awakeFromNib
1095 {
1096     [self registerForDraggedTypes:@[NSFilenamesPboardType, @"VLCPlaylistItemPboardType"]];
1097 }
1098
1099 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
1100 {
1101     return [[[self superview] superview] draggingEntered:sender];
1102 }
1103
1104 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
1105 {
1106     return YES;
1107 }
1108
1109 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
1110 {
1111     return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
1112 }
1113
1114 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
1115 {
1116     [self setNeedsDisplay:YES];
1117 }
1118
1119 @end