]> git.sesse.net Git - vlc/blob - modules/gui/macosx/ConvertAndSave.m
f504d3c0bb98d38bf36eb0a5c08d44cb28bc7747
[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 & Save")];
150     [_ok_btn setTitle: _NS("Save")];
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("Sample Rate")];
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("Close")];
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 = [[NSArray alloc] initWithObjects:
221                     [NSArray arrayWithObjects:@"MPEG-1", @"MPEG-2", @"MPEG-4", @"DIVX 1", @"DIVX 2", @"DIVX 3", @"H.263", @"H.264", @"VP8", @"WMV1", @"WMV2", @"M-JPEG", @"Theora", @"Dirac", nil],
222                     [NSArray arrayWithObjects:@"mpgv", @"mp2v", @"mp4v", @"DIV1", @"DIV2", @"DIV3", @"H263", @"h264", @"VP80", @"WMV1", @"WMV2", @"MJPG", @"theo", @"drac", nil],
223                     nil];
224     _audioCodecs = [[NSArray alloc] initWithObjects:
225                     [NSArray arrayWithObjects:@"MPEG Audio", @"MP3", @"MPEG 4 Audio (AAC)", @"A52/AC-3", @"Vorbis", @"Flac", @"Speex", @"WAV", @"WMA2", nil],
226                     [NSArray arrayWithObjects:@"mpga", @"mp3", @"mp4a", @"a52", @"vorb", @"flac", @"spx", @"s16l", @"wma2", nil],
227                     nil];
228     _subsCodecs = [[NSArray alloc] initWithObjects:
229                    [NSArray arrayWithObjects:@"DVB subtitle", @"T.140", nil],
230                    [NSArray arrayWithObjects:@"dvbs", @"t140", nil],
231                    nil];
232
233     [_customize_vid_codec_pop removeAllItems];
234     [_customize_vid_scale_pop removeAllItems];
235     [_customize_aud_codec_pop removeAllItems];
236     [_customize_aud_samplerate_pop removeAllItems];
237     [_customize_subs_pop removeAllItems];
238
239     [_customize_vid_codec_pop addItemsWithTitles:[_videoCodecs objectAtIndex:0]];
240     [_customize_aud_codec_pop addItemsWithTitles:[_audioCodecs objectAtIndex:0]];
241     [_customize_subs_pop addItemsWithTitles:[_subsCodecs objectAtIndex:0]];
242
243     [_customize_aud_samplerate_pop addItemWithTitle:@"8000"];
244     [_customize_aud_samplerate_pop addItemWithTitle:@"11025"];
245     [_customize_aud_samplerate_pop addItemWithTitle:@"22050"];
246     [_customize_aud_samplerate_pop addItemWithTitle:@"44100"];
247     [_customize_aud_samplerate_pop addItemWithTitle:@"48000"];
248
249     [_customize_vid_scale_pop addItemWithTitle:@"1"];
250     [_customize_vid_scale_pop addItemWithTitle:@"0.25"];
251     [_customize_vid_scale_pop addItemWithTitle:@"0.5"];
252     [_customize_vid_scale_pop addItemWithTitle:@"0.75"];
253     [_customize_vid_scale_pop addItemWithTitle:@"1.25"];
254     [_customize_vid_scale_pop addItemWithTitle:@"1.5"];
255     [_customize_vid_scale_pop addItemWithTitle:@"1.75"];
256     [_customize_vid_scale_pop addItemWithTitle:@"2"];
257
258     [_ok_btn setEnabled: NO];
259
260     [self resetCustomizationSheetBasedOnProfile:[self.profileValueList objectAtIndex:0]];
261 }
262
263 # pragma mark -
264 # pragma mark Code to Communicate with other objects
265
266 - (void)toggleWindow
267 {
268     [_window makeKeyAndOrderFront: nil];
269 }
270
271 # pragma mark -
272 # pragma mark User Interaction
273
274 - (IBAction)finalizePanel:(id)sender
275 {
276     // HTTP, TODO: mux checking, since only MPEG PS, MPEG TS, MPEG 1, OGG, RAW and ASF are allowed
277
278     playlist_t * p_playlist = pl_Get(VLCIntf);
279
280     input_item_t *p_input = input_item_New([_MRL UTF8String], [[_dropin_media_lbl stringValue] UTF8String]);
281     if (!p_input)
282         return;
283
284     input_item_AddOption(p_input, [[self composedOptions] UTF8String], VLC_INPUT_OPTION_TRUSTED);
285     if (b_streaming)
286         input_item_AddOption(p_input, [[NSString stringWithFormat:@"ttl=%@", [_stream_ttl_fld stringValue]] UTF8String], VLC_INPUT_OPTION_TRUSTED);
287
288     int returnValue;
289     returnValue = playlist_AddInput(p_playlist, p_input, PLAYLIST_STOP, PLAYLIST_END, true, pl_Unlocked);
290
291     if (returnValue == VLC_SUCCESS) {
292         /* let's "play" */
293         PL_LOCK;
294         playlist_item_t *p_item = playlist_ItemGetByInput(p_playlist, p_input);
295         playlist_Control(p_playlist, PLAYLIST_VIEWPLAY, pl_Locked, NULL,
296                          p_item);
297         PL_UNLOCK;
298     }
299     else
300         msg_Err(VLCIntf, "CAS: playlist add input failed :(");
301
302     /* we're done with this input */
303     vlc_gc_decref(p_input);
304
305     [_window performClose:sender];
306 }
307
308 - (IBAction)openMedia:(id)sender
309 {
310     /* preliminary implementation until the open panel is cleaned up */
311     NSOpenPanel * openPanel = [NSOpenPanel openPanel];
312     [openPanel setCanChooseDirectories:NO];
313     [openPanel setResolvesAliases:YES];
314     [openPanel setAllowsMultipleSelection:NO];
315     [openPanel beginSheetModalForWindow:_window completionHandler:^(NSInteger returnCode) {
316         if (returnCode == NSOKButton)
317         {
318             [self setMRL: [NSString stringWithUTF8String:vlc_path2uri([[[openPanel URL] path] UTF8String], NULL)]];
319             [self updateOKButton];
320             [self updateDropView];
321         }
322     }];
323 }
324
325 - (IBAction)switchProfile:(id)sender
326 {
327     NSUInteger index = [_profile_pop indexOfSelectedItem];
328     if (index < ([self.profileValueList count] - 1))
329         [self resetCustomizationSheetBasedOnProfile:[self.profileValueList objectAtIndex:index]];
330 }
331
332 - (IBAction)customizeProfile:(id)sender
333 {
334     [NSApp beginSheet:_customize_panel modalForWindow:_window modalDelegate:self didEndSelector:NULL contextInfo:nil];
335 }
336
337 - (IBAction)closeCustomizationSheet:(id)sender
338 {
339     [_customize_panel orderOut:sender];
340     [NSApp endSheet: _customize_panel];
341
342     if (sender == _customize_ok_btn)
343         [self updateCurrentProfile];
344 }
345
346 - (IBAction)newProfileAction:(id)sender
347 {
348     /* show panel */
349     VLCEnterTextPanel * panel = [VLCEnterTextPanel sharedInstance];
350     [panel setTitle: _NS("Save as new profile")];
351     [panel setSubTitle: _NS("Enter a name for the new profile:")];
352     [panel setCancelButtonLabel: _NS("Cancel")];
353     [panel setOKButtonLabel: _NS("Save")];
354     [panel setTarget:self];
355
356     [panel runModalForWindow:_customize_panel];
357 }
358
359 - (IBAction)deleteProfileAction:(id)sender
360 {
361     /* show panel */
362     VLCSelectItemInPopupPanel * panel = [VLCSelectItemInPopupPanel sharedInstance];
363     [panel setTitle:_NS("Remove a profile")];
364     [panel setSubTitle:_NS("Select the profile you would like to remove:")];
365     [panel setOKButtonLabel:_NS("Remove")];
366     [panel setCancelButtonLabel:_NS("Cancel")];
367     [panel setPopupButtonContent:self.profileNames];
368     [panel setTarget:self];
369
370     [panel runModalForWindow:_window];
371 }
372
373 - (IBAction)iWantAFile:(id)sender
374 {
375     NSRect boxFrame = [_destination_box frame];
376     NSRect subViewFrame = [_destination_itwantafile_view frame];
377     subViewFrame.origin.x = (boxFrame.size.width - subViewFrame.size.width) / 2;
378     subViewFrame.origin.y = ((boxFrame.size.height - subViewFrame.size.height) / 2) - 15.;
379     [_destination_itwantafile_view setFrame: subViewFrame];
380     [[_destination_itwantafile_btn animator] setHidden: YES];
381     [[_destination_itwantastream_btn animator] setHidden: YES];
382     [_destination_box performSelector:@selector(addSubview:) withObject:_destination_itwantafile_view afterDelay:0.2];
383     [[_destination_cancel_btn animator] setHidden:NO];
384     b_streaming = NO;
385     [_ok_btn setTitle:_NS("Save")];
386 }
387
388 - (IBAction)iWantAStream:(id)sender
389 {
390     NSRect boxFrame = [_destination_box frame];
391     NSRect subViewFrame = [_destination_itwantastream_view frame];
392     subViewFrame.origin.x = (boxFrame.size.width - subViewFrame.size.width) / 2;
393     subViewFrame.origin.y = ((boxFrame.size.height - subViewFrame.size.height) / 2) - 15.;
394     [_destination_itwantastream_view setFrame: subViewFrame];
395     [[_destination_itwantafile_btn animator] setHidden: YES];
396     [[_destination_itwantastream_btn animator] setHidden: YES];
397     [_destination_box performSelector:@selector(addSubview:) withObject:_destination_itwantastream_view afterDelay:0.2];
398     [[_destination_cancel_btn animator] setHidden:NO];
399     b_streaming = YES;
400     [_ok_btn setTitle:_NS("Stream")];
401 }
402
403 - (IBAction)cancelDestination:(id)sender
404 {
405     if ([_destination_itwantastream_view superview] != nil)
406         [_destination_itwantastream_view removeFromSuperview];
407     if ([_destination_itwantafile_view superview] != nil)
408         [_destination_itwantafile_view removeFromSuperview];
409
410     [_destination_cancel_btn setHidden:YES];
411     [[_destination_itwantafile_btn animator] setHidden: NO];
412     [[_destination_itwantastream_btn animator] setHidden: NO];
413     b_streaming = NO;
414 }
415
416 - (IBAction)browseFileDestination:(id)sender
417 {
418     NSSavePanel * saveFilePanel = [NSSavePanel savePanel];
419     [saveFilePanel setCanSelectHiddenExtension: YES];
420     [saveFilePanel setCanCreateDirectories: YES];
421     if ([[_customize_encap_matrix selectedCell] tag] != RAW) // there is no clever guess for this
422         [saveFilePanel setAllowedFileTypes:[NSArray arrayWithObject:[self currentEncapsulationFormatAsFileExtension:YES]]];
423     [saveFilePanel beginSheetModalForWindow:_window completionHandler:^(NSInteger returnCode) {
424         if (returnCode == NSOKButton) {
425             [self setOutputDestination:[[saveFilePanel URL] path]];
426             [_destination_filename_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath:_outputDestination]];
427             [[_destination_filename_stub_lbl animator] setHidden: YES];
428             [[_destination_filename_lbl animator] setHidden: NO];
429         } else {
430             [self setOutputDestination:@""];
431             [[_destination_filename_lbl animator] setHidden: YES];
432             [[_destination_filename_stub_lbl animator] setHidden: NO];
433         }
434         [self updateOKButton];
435     }];
436 }
437
438 - (IBAction)showStreamPanel:(id)sender
439 {
440     [NSApp beginSheet:_stream_panel modalForWindow:_window modalDelegate:self didEndSelector:NULL contextInfo:nil];
441 }
442
443 - (IBAction)closeStreamPanel:(id)sender
444 {
445     /* provide a summary of the user selections */
446     NSMutableString * labelContent = [[NSMutableString alloc] initWithFormat:_NS("%@ stream to %@:%@"), [_stream_type_pop titleOfSelectedItem], [_stream_address_fld stringValue], [_stream_port_fld stringValue]];
447
448     if ([_stream_type_pop indexOfSelectedItem] > 1)
449         [labelContent appendFormat:@" (\"%@\")", [_stream_channel_fld stringValue]];
450
451     [_destination_stream_lbl setStringValue:labelContent];
452     [labelContent release];
453
454     /* catch obvious errors */
455     if ([_stream_sap_ckb state] && ![[_stream_channel_fld stringValue] length] > 0) {
456         NSBeginInformationalAlertSheet(_NS("No Channel Name given"),
457                                        _NS("OK"), @"", @"", _stream_panel, nil, nil, nil, nil,
458                                        @"%@", _NS("SAP stream announcement is enabled. However, no channel name is provided."));
459         return;
460     }
461
462     if ([_stream_sdp_matrix isEnabled] && [_stream_sdp_matrix selectedCell] != [_stream_sdp_matrix cellWithTag:0] && ![[_stream_sdp_fld stringValue] length] > 0) {
463         NSBeginInformationalAlertSheet(_NS("No SDP URL given"),
464                                        _NS("OK"), @"", @"", _stream_panel, nil, nil, nil, nil,
465                                        @"%@", _NS("A SDP export is requested, but no URL is provided."));
466         return;
467     }
468
469     /* store destination for further reference and update UI */
470     [self setOutputDestination: [_stream_address_fld stringValue]];
471     [self updateOKButton];
472
473     [_stream_panel orderOut:sender];
474     [NSApp endSheet: _stream_panel];
475 }
476
477 - (IBAction)streamTypeToggle:(id)sender
478 {
479     NSUInteger index = [_stream_type_pop indexOfSelectedItem];
480     if (index <= 1) { // HTTP, MMSH
481         [_stream_ttl_fld setEnabled:NO];
482         [_stream_ttl_stepper setEnabled:NO];
483         [_stream_sap_ckb setEnabled:NO];
484         [_stream_sdp_matrix setEnabled:NO];
485     } else if (index == 2) { // RTP
486         [_stream_ttl_fld setEnabled:YES];
487         [_stream_ttl_stepper setEnabled:YES];
488         [_stream_sap_ckb setEnabled:YES];
489         [_stream_sdp_matrix setEnabled:YES];
490     } else { // UDP
491         [_stream_ttl_fld setEnabled:YES];
492         [_stream_ttl_stepper setEnabled:YES];
493         [_stream_sap_ckb setEnabled:YES];
494         [_stream_sdp_matrix setEnabled:NO];
495     }
496     [self streamAnnouncementToggle:sender];
497 }
498
499 - (IBAction)streamAnnouncementToggle:(id)sender
500 {
501     [_stream_channel_fld setEnabled:[_stream_sap_ckb state] && [_stream_sap_ckb isEnabled]];
502     [_stream_sdp_fld setEnabled:[_stream_sdp_matrix isEnabled] && ([_stream_sdp_matrix selectedCell] != [_stream_sdp_matrix cellWithTag:0])];
503
504     if ([[_stream_sdp_matrix selectedCell] tag] == 3)
505         [_stream_sdp_browsefile_btn setEnabled: YES];
506     else
507         [_stream_sdp_browsefile_btn setEnabled: NO];
508 }
509
510 - (IBAction)sdpFileLocationSelector:(id)sender
511 {
512     NSSavePanel * saveFilePanel = [NSSavePanel savePanel];
513     [saveFilePanel setCanSelectHiddenExtension: YES];
514     [saveFilePanel setCanCreateDirectories: YES];
515     [saveFilePanel setAllowedFileTypes:[NSArray arrayWithObject:@"sdp"]];
516     [saveFilePanel beginSheetModalForWindow:_stream_panel completionHandler:^(NSInteger returnCode) {
517         if (returnCode == NSOKButton)
518             [_stream_sdp_fld setStringValue:[[saveFilePanel URL] path]];
519     }];
520 }
521
522 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
523 {
524     NSPasteboard *paste = [sender draggingPasteboard];
525     NSArray *types = [NSArray arrayWithObjects: NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil];
526     NSString *desired_type = [paste availableTypeFromArray: types];
527     NSData *carried_data = [paste dataForType: desired_type];
528
529     if (carried_data) {
530         if ([desired_type isEqualToString:NSFilenamesPboardType]) {
531             NSArray *values = [[paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
532
533             if ([values count] > 0) {
534                 [self setMRL: [NSString stringWithUTF8String:vlc_path2uri([[values objectAtIndex:0] UTF8String], NULL)]];
535                 [self updateOKButton];
536                 [self updateDropView];
537                 return YES;
538             }
539         } else if ([desired_type isEqualToString:@"VLCPlaylistItemPboardType"]) {
540             NSArray * array = [[[VLCMain sharedInstance] playlist] draggedItems];
541             NSUInteger count = [array count];
542             if (count > 0) {
543                 playlist_t * p_playlist = pl_Get(VLCIntf);
544                 playlist_item_t * p_item = NULL;
545
546                 PL_LOCK;
547                 /* let's look for the first proper input item */
548                 for (NSUInteger x = 0; x < count; x++) {
549                     p_item = [[array objectAtIndex:x] pointerValue];
550                     if (p_item) {
551                         if (p_item->p_input) {
552                             if (p_item->p_input->psz_uri != nil) {
553                                 [self setMRL: [NSString stringWithFormat:@"%s", p_item->p_input->psz_uri]];
554                                 [self updateDropView];
555                                 [self updateOKButton];
556
557                                 PL_UNLOCK;
558
559                                 return YES;
560                             }
561                         }
562                     }
563                 }
564                 PL_UNLOCK;
565             }
566         }
567     }
568     return NO;
569 }
570
571 - (void)panel:(VLCEnterTextPanel *)panel returnValue:(NSUInteger)value text:(NSString *)text
572 {
573     if (value == NSOKButton) {
574         if ([text length] > 0) {
575             /* prepare current data */
576             [self updateCurrentProfile];
577
578             /* add profile to arrays */
579             NSMutableArray * workArray = [[NSMutableArray alloc] initWithArray:self.profileNames];
580             [workArray addObject:text];
581             [self setProfileNames:[[[NSArray alloc] initWithArray:workArray] autorelease]];
582             [workArray release];
583             workArray = [[NSMutableArray alloc] initWithArray:self.profileValueList];
584             [workArray addObject:[[[NSArray alloc] initWithArray:self.currentProfile] autorelease]];
585             [self setProfileValueList:[[[NSArray alloc] initWithArray:workArray] autorelease]];
586             [workArray release];
587
588             /* update UI */
589             [self recreateProfilePopup];
590             [_profile_pop selectItemWithTitle:text];
591
592             /* update internals */
593             [self switchProfile:self];
594             [self storeProfilesOnDisk];
595         }
596     }
597 }
598
599 - (void)panel:(VLCSelectItemInPopupPanel *)panel returnValue:(NSUInteger)value item:(NSUInteger)item
600 {
601     if (value == NSOKButton) {
602         /* remove requested profile from the arrays */
603         NSMutableArray * workArray = [[NSMutableArray alloc] initWithArray:self.profileNames];
604         [workArray removeObjectAtIndex:item];
605         [self setProfileNames:[[[NSArray alloc] initWithArray:workArray] autorelease]];
606         [workArray release];
607         workArray = [[NSMutableArray alloc] initWithArray:self.profileValueList];
608         [workArray removeObjectAtIndex:item];
609         [self setProfileValueList:[[[NSArray alloc] initWithArray:workArray] autorelease]];
610         [workArray release];
611
612         /* update UI */
613         [_profile_pop removeAllItems];
614         [_profile_pop addItemsWithTitles:self.profileNames];
615         [_profile_pop addItemWithTitle:_NS("Custom")];
616         [[_profile_pop menu] addItem:[NSMenuItem separatorItem]];
617         [_profile_pop addItemWithTitle:_NS("Organize Profiles...")];
618         [[_profile_pop lastItem] setTarget: self];
619         [[_profile_pop lastItem] setAction: @selector(deleteProfileAction:)];
620
621         /* update internals */
622         [self switchProfile:self];
623         [self storeProfilesOnDisk];
624     }
625 }
626
627 # pragma mark -
628 # pragma mark Private Functionality
629 - (void)updateDropView
630 {
631     if ([_MRL length] > 0) {
632         NSString * path = [[NSURL URLWithString:_MRL] path];
633         [_dropin_media_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath: path]];
634         NSImage * image = [[NSWorkspace sharedWorkspace] iconForFile: path];
635         [image setSize:NSMakeSize(128,128)];
636         [_dropin_icon_view setImage: image];
637
638         if (![_dropin_view superview]) {
639             NSRect boxFrame = [_drop_box frame];
640             NSRect subViewFrame = [_dropin_view frame];
641             subViewFrame.origin.x = (boxFrame.size.width - subViewFrame.size.width) / 2;
642             subViewFrame.origin.y = (boxFrame.size.height - subViewFrame.size.height) / 2;
643             [_dropin_view setFrame: subViewFrame];
644             [[_drop_image_view animator] setHidden: YES];
645             [_drop_box performSelector:@selector(addSubview:) withObject:_dropin_view afterDelay:0.6];
646         }
647     } else {
648         [_dropin_view removeFromSuperview];
649         [[_drop_image_view animator] setHidden: NO];
650     }
651 }
652
653 - (void)updateOKButton
654 {
655     if ([_outputDestination length] > 0 && [_MRL length] > 0)
656         [_ok_btn setEnabled: YES];
657     else
658         [_ok_btn setEnabled: NO];
659 }
660
661 - (void)resetCustomizationSheetBasedOnProfile:(NSString *)profileString
662 {
663     /* Container(string), transcode video(bool), transcode audio(bool),
664     * use subtitles(bool), video codec(string), video bitrate(integer),
665     * scale(float), fps(float), width(integer, height(integer),
666     * audio codec(string), audio bitrate(integer), channels(integer),
667     * samplerate(integer), subtitle codec(string), subtitle overlay(bool) */
668
669     NSArray * components = [profileString componentsSeparatedByString:@";"];
670     if ([components count] != 16) {
671         msg_Err(VLCIntf, "CAS: the requested profile '%s' is invalid", [profileString UTF8String]);
672         return;
673     }
674
675     [self selectCellByEncapsulationFormat:[components objectAtIndex:0]];
676     [_customize_vid_ckb setState:[[components objectAtIndex:1] intValue]];
677     [_customize_aud_ckb setState:[[components objectAtIndex:2] intValue]];
678     [_customize_subs_ckb setState:[[components objectAtIndex:3] intValue]];
679     [_customize_vid_bitrate_fld setStringValue:[components objectAtIndex:5]];
680     [_customize_vid_scale_pop selectItemWithTitle:[components objectAtIndex:6]];
681     [_customize_vid_framerate_fld setStringValue:[components objectAtIndex:7]];
682     [_customize_vid_width_fld setStringValue:[components objectAtIndex:8]];
683     [_customize_vid_height_fld setStringValue:[components objectAtIndex:9]];
684     [_customize_aud_bitrate_fld setStringValue:[components objectAtIndex:11]];
685     [_customize_aud_channels_fld setStringValue:[components objectAtIndex:12]];
686     [_customize_aud_samplerate_pop selectItemWithTitle:[components objectAtIndex:13]];
687     [_customize_subs_overlay_ckb setState:[[components objectAtIndex:15] intValue]];
688
689     /* since there is no proper lookup mechanism in arrays, we need to implement a string specific one ourselves */
690     NSArray * tempArray = [_videoCodecs objectAtIndex:1];
691     NSUInteger count = [tempArray count];
692     NSString * searchString = [components objectAtIndex:4];
693     if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
694         [_customize_vid_codec_pop selectItemAtIndex:-1];
695     } else {
696         for (NSUInteger x = 0; x < count; x++) {
697             if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
698                 [_customize_vid_codec_pop selectItemAtIndex:x];
699                 break;
700             }
701         }
702     }
703
704     tempArray = [_audioCodecs objectAtIndex:1];
705     count = [tempArray count];
706     searchString = [components objectAtIndex:10];
707     if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
708         [_customize_aud_codec_pop selectItemAtIndex:-1];
709     } else {
710         for (NSUInteger x = 0; x < count; x++) {
711             if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
712                 [_customize_aud_codec_pop selectItemAtIndex:x];
713                 break;
714             }
715         }
716     }
717
718     tempArray = [_subsCodecs objectAtIndex:1];
719     count = [tempArray count];
720     searchString = [components objectAtIndex:14];
721     if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
722         [_customize_subs_pop selectItemAtIndex:-1];
723     } else {
724         for (NSUInteger x = 0; x < count; x++) {
725             if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
726                 [_customize_subs_pop selectItemAtIndex:x];
727                 break;
728             }
729         }
730     }
731
732     [self setCurrentProfile: [[[NSMutableArray alloc] initWithArray: [profileString componentsSeparatedByString:@";"]] autorelease]];
733 }
734
735 - (void)selectCellByEncapsulationFormat:(NSString *)format
736 {
737     if ([format isEqualToString:@"ts"])
738         [_customize_encap_matrix selectCellWithTag:MPEGTS];
739     else if ([format isEqualToString:@"webm"])
740         [_customize_encap_matrix selectCellWithTag:WEBM];
741     else if ([format isEqualToString:@"ogg"])
742         [_customize_encap_matrix selectCellWithTag:OGG];
743     else if ([format isEqualToString:@"ogm"])
744         [_customize_encap_matrix selectCellWithTag:OGG];
745     else if ([format isEqualToString:@"mp4"])
746         [_customize_encap_matrix selectCellWithTag:MP4];
747     else if ([format isEqualToString:@"mov"])
748         [_customize_encap_matrix selectCellWithTag:MP4];
749     else if ([format isEqualToString:@"ps"])
750         [_customize_encap_matrix selectCellWithTag:MPEGPS];
751     else if ([format isEqualToString:@"mpjpeg"])
752         [_customize_encap_matrix selectCellWithTag:MJPEG];
753     else if ([format isEqualToString:@"wav"])
754         [_customize_encap_matrix selectCellWithTag:WAV];
755     else if ([format isEqualToString:@"flv"])
756         [_customize_encap_matrix selectCellWithTag:FLV];
757     else if ([format isEqualToString:@"mpeg1"])
758         [_customize_encap_matrix selectCellWithTag:MPEG1];
759     else if ([format isEqualToString:@"mkv"])
760         [_customize_encap_matrix selectCellWithTag:MKV];
761     else if ([format isEqualToString:@"raw"])
762         [_customize_encap_matrix selectCellWithTag:RAW];
763     else if ([format isEqualToString:@"avi"])
764         [_customize_encap_matrix selectCellWithTag:AVI];
765     else if ([format isEqualToString:@"asf"])
766         [_customize_encap_matrix selectCellWithTag:ASF];
767     else if ([format isEqualToString:@"wmv"])
768         [_customize_encap_matrix selectCellWithTag:ASF];
769     else
770         msg_Err(VLCIntf, "CAS: unknown encap format requested for customization");
771 }
772
773 - (NSString *)currentEncapsulationFormatAsFileExtension:(BOOL)b_extension
774 {
775     NSUInteger cellTag = [[_customize_encap_matrix selectedCell] tag];
776     NSString * returnValue;
777     switch (cellTag) {
778         case MPEGTS:
779             returnValue = @"ts";
780             break;
781         case WEBM:
782             returnValue = @"webm";
783             break;
784         case OGG:
785             returnValue = @"ogg";
786             break;
787         case MP4:
788         {
789             if (b_extension)
790                 returnValue = @"m4v";
791             else
792                 returnValue = @"mp4";
793             break;
794         }
795         case MPEGPS:
796         {
797             if (b_extension)
798                 returnValue = @"mpg";
799             else
800                 returnValue = @"ps";
801             break;
802         }
803         case MJPEG:
804             returnValue = @"mjpeg";
805             break;
806         case WAV:
807             returnValue = @"wav";
808             break;
809         case FLV:
810             returnValue = @"flv";
811             break;
812         case MPEG1:
813         {
814             if (b_extension)
815                 returnValue = @"mpg";
816             else
817                 returnValue = @"mpeg1";
818             break;
819         }
820         case MKV:
821             returnValue = @"mkv";
822             break;
823         case RAW:
824             returnValue = @"raw";
825             break;
826         case AVI:
827             returnValue = @"avi";
828             break;
829         case ASF:
830             returnValue = @"asf";
831             break;
832
833         default:
834             returnValue = @"none";
835             break;
836     }
837
838     return returnValue;
839 }
840
841 - (NSString *)composedOptions
842 {
843     NSMutableString *composedOptions = [[NSMutableString alloc] initWithString:@":sout=#transcode{"];
844     if ([[self.currentProfile objectAtIndex:1] intValue]) {
845         // video is enabled
846         [composedOptions appendFormat:@"vcodec=%@", [self.currentProfile objectAtIndex:4]];
847         if (![[self.currentProfile objectAtIndex:4] isEqualToString:@"none"]) {
848             if ([[self.currentProfile objectAtIndex:5] intValue] > 0) // bitrate
849                 [composedOptions appendFormat:@",vb=%@", [self.currentProfile objectAtIndex:5]];
850             if ([[self.currentProfile objectAtIndex:6] floatValue] > 0.) // scale
851                 [composedOptions appendFormat:@",scale=%@", [self.currentProfile objectAtIndex:6]];
852             if ([[self.currentProfile objectAtIndex:7] floatValue] > 0.) // fps
853                 [composedOptions appendFormat:@",fps=%@", [self.currentProfile objectAtIndex:7]];
854             if ([[self.currentProfile objectAtIndex:8] intValue] > 0) // width
855                 [composedOptions appendFormat:@",width=%@", [self.currentProfile objectAtIndex:8]];
856             if ([[self.currentProfile objectAtIndex:9] intValue] > 0) // height
857                 [composedOptions appendFormat:@",height=%@", [self.currentProfile objectAtIndex:9]];
858         }
859     }
860     if ([[self.currentProfile objectAtIndex:2] intValue]) {
861         // audio is enabled
862
863         // add another comma in case video is enabled
864         if ([[self.currentProfile objectAtIndex:1] intValue])
865             [composedOptions appendString:@","];
866
867         [composedOptions appendFormat:@"acodec=%@", [self.currentProfile objectAtIndex:10]];
868         if (![[self.currentProfile objectAtIndex:10] isEqualToString:@"none"]) {
869             [composedOptions appendFormat:@",ab=%@", [self.currentProfile objectAtIndex:11]]; // bitrate
870             [composedOptions appendFormat:@",channels=%@", [self.currentProfile objectAtIndex:12]]; // channel number
871             [composedOptions appendFormat:@",samplerate=%@", [self.currentProfile objectAtIndex:13]]; // sample rate
872         }
873     }
874     if ([self.currentProfile objectAtIndex:3]) {
875         // subtitles enabled
876         [composedOptions appendFormat:@",scodec=%@", [self.currentProfile objectAtIndex:14]];
877         if ([[self.currentProfile objectAtIndex:15] intValue])
878             [composedOptions appendFormat:@",soverlay"];
879     }
880
881     if (!b_streaming) {
882         /* file transcoding */
883         // add muxer
884         [composedOptions appendFormat:@"}:standard{mux=%@", [self.currentProfile objectAtIndex:0]];
885
886         // add output destination
887         [composedOptions appendFormat:@",dst=%@,access=file}", _outputDestination];
888     } else {
889         /* streaming */
890         if ([[[_stream_type_pop selectedItem] title] isEqualToString:@"RTP"])
891             [composedOptions appendFormat:@":rtp{mux=ts,dst=%@,port=%@", _outputDestination, [_stream_port_fld stringValue]];
892         else if ([[[_stream_type_pop selectedItem] title] isEqualToString:@"UDP"])
893             [composedOptions appendFormat:@":standard{mux=ts,dst=%@,port=%@,access=udp", _outputDestination, [_stream_port_fld stringValue]];
894         else if ([[[_stream_type_pop selectedItem] title] isEqualToString:@"MMSH"])
895             [composedOptions appendFormat:@":standard{mux=asfh,dst=%@,port=%@,access=mmsh", _outputDestination, [_stream_port_fld stringValue]];
896         else
897             [composedOptions appendFormat:@":standard{mux=%@,dst=%@,port=%@,access=http", [self.currentProfile objectAtIndex:0], [_stream_port_fld stringValue], _outputDestination];
898
899         if ([_stream_sap_ckb state])
900             [composedOptions appendFormat:@",sap,name=\"%@\"", [_stream_channel_fld stringValue]];
901         if ([_stream_sdp_matrix selectedCell] != [_stream_sdp_matrix cellWithTag:0]) {
902             NSInteger tag = [[_stream_sdp_matrix selectedCell] tag];
903             switch (tag) {
904                 case 1:
905                     [composedOptions appendFormat:@",sdp=\"http://%@\"", [_stream_sdp_fld stringValue]];
906                     break;
907                 case 2:
908                     [composedOptions appendFormat:@",sdp=\"rtsp://%@\"", [_stream_sdp_fld stringValue]];
909                     break;
910                 case 3:
911                     [composedOptions appendFormat:@",sdp=\"file://%s\"", vlc_path2uri([[_stream_sdp_fld stringValue] UTF8String], NULL)];
912                 default:
913                     break;
914             }
915         }
916
917         [composedOptions appendString:@"} :sout-keep"];
918     }
919
920     NSString * returnString = [NSString stringWithString:composedOptions];
921     [composedOptions release];
922
923     return returnString;
924 }
925
926 - (void)updateCurrentProfile
927 {
928     [self.currentProfile removeAllObjects];
929
930     NSInteger i;
931     [self.currentProfile addObject: [self currentEncapsulationFormatAsFileExtension:NO]];
932     [self.currentProfile addObject: [NSString stringWithFormat:@"%li", [_customize_vid_ckb state]]];
933     [self.currentProfile addObject: [NSString stringWithFormat:@"%li", [_customize_aud_ckb state]]];
934     [self.currentProfile addObject: [NSString stringWithFormat:@"%li", [_customize_subs_ckb state]]];
935     i = [_customize_vid_codec_pop indexOfSelectedItem];
936     if (i >= 0)
937         [self.currentProfile addObject: [[_videoCodecs objectAtIndex:1] objectAtIndex:i]];
938     else
939         [self.currentProfile addObject: @"none"];
940     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_vid_bitrate_fld intValue]]];
941     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [[[_customize_vid_scale_pop selectedItem] title] intValue]]];
942     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_vid_framerate_fld intValue]]];
943     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_vid_width_fld intValue]]];
944     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_vid_height_fld intValue]]];
945     i = [_customize_aud_codec_pop indexOfSelectedItem];
946     if (i >= 0)
947         [self.currentProfile addObject: [[_audioCodecs objectAtIndex:1] objectAtIndex:i]];
948     else
949         [self.currentProfile addObject: @"none"];
950     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_aud_bitrate_fld intValue]]];
951     [self.currentProfile addObject: [NSString stringWithFormat:@"%i", [_customize_aud_channels_fld intValue]]];
952     [self.currentProfile addObject: [[_customize_aud_samplerate_pop selectedItem] title]];
953     i = [_customize_subs_pop indexOfSelectedItem];
954     if (i >= 0)
955         [self.currentProfile addObject: [[_subsCodecs objectAtIndex:1] objectAtIndex:i]];
956     else
957         [self.currentProfile addObject: @"none"];
958     [self.currentProfile addObject: [NSString stringWithFormat:@"%li", [_customize_subs_overlay_ckb state]]];
959 }
960
961 - (void)storeProfilesOnDisk
962 {
963     NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
964     [defaults setObject:_profileNames forKey:@"CASProfileNames"];
965     [defaults setObject:_profileValueList forKey:@"CASProfiles"];
966     [defaults synchronize];
967 }
968
969 - (void)recreateProfilePopup
970 {
971     [_profile_pop removeAllItems];
972     [_profile_pop addItemsWithTitles:self.profileNames];
973     [_profile_pop addItemWithTitle:_NS("Custom")];
974     [[_profile_pop menu] addItem:[NSMenuItem separatorItem]];
975     [_profile_pop addItemWithTitle:_NS("Organize Profiles...")];
976     [[_profile_pop lastItem] setTarget: self];
977     [[_profile_pop lastItem] setAction: @selector(deleteProfileAction:)];
978 }
979
980 @end
981
982 # pragma mark -
983 # pragma mark Drag and drop handling
984
985 @implementation VLCDropEnabledBox
986
987 - (void)awakeFromNib
988 {
989     [self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
990 }
991
992 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
993 {
994     b_activeDragAndDrop = YES;
995     [self setNeedsDisplay:YES];
996
997     [[NSCursor dragCopyCursor] set];
998
999     if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
1000         return NSDragOperationGeneric;
1001
1002     return NSDragOperationNone;
1003 }
1004
1005 - (void)draggingEnded:(id < NSDraggingInfo >)sender
1006 {
1007     [[NSCursor arrowCursor] set];
1008     b_activeDragAndDrop = NO;
1009     [self setNeedsDisplay:YES];
1010 }
1011
1012 - (void)draggingExited:(id < NSDraggingInfo >)sender
1013 {
1014     [[NSCursor arrowCursor] set];
1015     b_activeDragAndDrop = NO;
1016     [self setNeedsDisplay:YES];
1017 }
1018
1019 - (void)drawRect:(NSRect)dirtyRect
1020 {
1021     if (b_activeDragAndDrop) {
1022         [[NSColor colorWithCalibratedRed:(.154/.255) green:(.154/.255) blue:(.154/.255) alpha:1.] setFill];
1023         NSRect frameRect = [[self contentView] bounds];
1024         frameRect.origin.x += 10;
1025         frameRect.origin.y += 10;
1026         frameRect.size.width -= 17;
1027         frameRect.size.height -= 17;
1028         NSFrameRectWithWidthUsingOperation(frameRect, 4., NSCompositeHighlight);
1029     }
1030
1031     [super drawRect:dirtyRect];
1032 }
1033
1034 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
1035 {
1036     return YES;
1037 }
1038
1039 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
1040 {
1041     return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
1042 }
1043
1044 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
1045 {
1046     [self setNeedsDisplay:YES];
1047 }
1048
1049 @end
1050
1051 @implementation VLCDropEnabledImageView
1052
1053 - (void)awakeFromNib
1054 {
1055     [self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
1056 }
1057
1058 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
1059 {
1060     return [[[self superview] superview] draggingEntered:sender];
1061 }
1062
1063 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
1064 {
1065     return YES;
1066 }
1067
1068 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
1069 {
1070     return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
1071 }
1072
1073 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
1074 {
1075     [self setNeedsDisplay:YES];
1076 }
1077
1078 @end
1079
1080 @implementation VLCDropEnabledButton
1081
1082 - (void)awakeFromNib
1083 {
1084     [self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, @"VLCPlaylistItemPboardType", nil]];
1085 }
1086
1087 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
1088 {
1089     return [[[self superview] superview] draggingEntered:sender];
1090 }
1091
1092 - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
1093 {
1094     return YES;
1095 }
1096
1097 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
1098 {
1099     return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
1100 }
1101
1102 - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
1103 {
1104     [self setNeedsDisplay:YES];
1105 }
1106
1107 @end