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