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