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