OSDN Git Service

MacGui: Fix Quit command, especially during encoding
[handbrake-jp/handbrake-jp-git.git] / macosx / Controller.mm
1 /* $Id: Controller.mm,v 1.79 2005/11/04 19:41:32 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "Controller.h"
8 #include "a52dec/a52.h"
9 #import "HBOutputPanelController.h"
10 #import "HBPreferencesController.h"
11 /* Added to integrate scanning into HBController */
12 #include <IOKit/IOKitLib.h>
13 #include <IOKit/storage/IOMedia.h>
14 #include <IOKit/storage/IODVDMedia.h>
15 #include "HBDVDDetector.h"
16 #include "dvdread/dvd_reader.h"
17
18 #define _(a) NSLocalizedString(a,NULL)
19
20 static int FormatSettings[4][10] =
21   { { HB_MUX_MP4 | HB_VCODEC_FFMPEG | HB_ACODEC_FAAC,
22           HB_MUX_MP4 | HB_VCODEC_X264   | HB_ACODEC_FAAC,
23           0,
24           0},
25     { HB_MUX_AVI | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
26           HB_MUX_AVI | HB_VCODEC_FFMPEG | HB_ACODEC_AC3,
27           HB_MUX_AVI | HB_VCODEC_X264   | HB_ACODEC_LAME,
28           HB_MUX_AVI | HB_VCODEC_X264   | HB_ACODEC_AC3},
29     { HB_MUX_OGM | HB_VCODEC_FFMPEG | HB_ACODEC_VORBIS,
30           HB_MUX_OGM | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
31           0,
32           0 },
33     { HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_FAAC,
34           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_AC3,
35           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_LAME,
36           HB_MUX_MKV | HB_VCODEC_FFMPEG | HB_ACODEC_VORBIS,
37           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_FAAC,
38           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_AC3,
39           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_LAME,
40           HB_MUX_MKV | HB_VCODEC_X264   | HB_ACODEC_VORBIS,
41           0,
42           0 } };
43
44
45 /* We setup the toolbar values here */
46 static NSString *        ToggleDrawerIdentifier             = @"Toggle Drawer Item Identifier";
47 static NSString *        StartEncodingIdentifier            = @"Start Encoding Item Identifier";
48 static NSString *        PauseEncodingIdentifier            = @"Pause Encoding Item Identifier";
49 static NSString *        ShowQueueIdentifier                = @"Show Queue Item Identifier";
50 static NSString *        AddToQueueIdentifier               = @"Add to Queue Item Identifier";
51 static NSString *        ShowActivityIdentifier             = @"Debug Output Item Identifier";
52 static NSString *        ChooseSourceIdentifier             = @"Choose Source Item Identifier";
53
54
55 /*******************************
56  * HBController implementation *
57  *******************************/
58 @implementation HBController
59
60 - init
61 {
62     self = [super init];
63     [HBPreferencesController registerUserDefaults];
64     fHandle = NULL;
65     outputPanel = [[HBOutputPanelController alloc] init];
66     fPictureController = [[PictureController alloc] initWithDelegate:self];
67     fQueueController = [[HBQueueController alloc] init];
68     fAdvancedOptions = [[HBAdvancedController alloc] init];
69     fPreferencesController = [[HBPreferencesController alloc] init];
70     return self;
71 }
72
73 - (void) applicationDidFinishLaunching: (NSNotification *) notification
74 {
75     int    build;
76     char * version;
77
78     // Init libhb
79         int debugLevel = [[NSUserDefaults standardUserDefaults] boolForKey:@"ShowVerboseOutput"] ? HB_DEBUG_ALL : HB_DEBUG_NONE;
80     fHandle = hb_init(debugLevel, [[NSUserDefaults standardUserDefaults] boolForKey:@"CheckForUpdates"]);
81
82         // Set the Growl Delegate
83     [GrowlApplicationBridge setGrowlDelegate: self];    
84     /* Init others controllers */
85     [fPictureController SetHandle: fHandle];
86     [fQueueController   setHandle: fHandle];
87     [fQueueController   setHBController: self];
88         
89     fChapterTitlesDelegate = [[ChapterTitles alloc] init];
90     [fChapterTable setDataSource:fChapterTitlesDelegate];
91
92      /* Call UpdateUI every 1/2 sec */
93     [[NSRunLoop currentRunLoop] addTimer: [NSTimer
94         scheduledTimerWithTimeInterval: 0.5 target: self
95         selector: @selector( updateUI: ) userInfo: NULL repeats: YES]
96         forMode: NSEventTrackingRunLoopMode];
97
98     if( ( build = hb_check_update( fHandle, &version ) ) > -1 )
99     {
100         /* Update available - tell the user */
101         
102         NSBeginInformationalAlertSheet( _( @"Update is available" ),
103             _( @"Go get it!" ), _( @"Discard" ), NULL, fWindow, self,
104             @selector( updateAlertDone:returnCode:contextInfo: ),
105             NULL, NULL, [NSString stringWithFormat:
106             _( @"HandBrake %s (build %d) is now available for download." ),
107             version, build] );
108         return;
109
110     }
111
112     // Open debug output window now if it was visible when HB was closed
113     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"OutputPanelIsOpen"])
114         [self showDebugOutputPanel:nil];
115
116     // Open queue window now if it was visible when HB was closed
117     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QueueWindowIsOpen"])
118         [self showQueueWindow:nil];
119
120         [self openMainWindow:nil];
121         
122     /* Show scan panel ASAP */
123     [self performSelectorOnMainThread: @selector(showScanPanel:)
124         withObject: NULL waitUntilDone: NO];
125 }
126
127 - (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) app
128 {
129     // Warn if encoding a movie
130     hb_state_t s;
131     hb_get_state( fHandle, &s );
132     hb_job_t * job = hb_current_job( fHandle );
133     if ( job && ( s.state != HB_STATE_IDLE ) )
134     {
135         hb_job_t * job = hb_current_job( fHandle );
136         int result = NSRunCriticalAlertPanel(
137                 NSLocalizedString(@"Are you sure you want to quit HandBrake?", nil),
138                 NSLocalizedString(@"%@ is currently encoding. If you quit HandBrake, your movie will be lost. Do you want to quit anyway?", nil),
139                 NSLocalizedString(@"Quit", nil), NSLocalizedString(@"Don't Quit", nil), nil,
140                 job ? [NSString stringWithUTF8String:job->title->name] : @"A movie" );
141         
142         if (result == NSAlertDefaultReturn)
143         {
144             [self doCancelCurrentJob];
145             return NSTerminateNow;
146         }
147         else
148             return NSTerminateCancel;
149     }
150     
151     // Warn if items still in the queue
152     else if ( hb_count( fHandle ) > 0 )
153     {
154         int result = NSRunCriticalAlertPanel(
155                 NSLocalizedString(@"Are you sure you want to quit HandBrake?", nil),
156                 NSLocalizedString(@"One or more encodes are queued for encoding. Do you want to quit anyway?", nil),
157                 NSLocalizedString(@"Quit", nil), NSLocalizedString(@"Don't Quit", nil), nil);
158         
159         if ( result == NSAlertDefaultReturn )
160             return NSTerminateNow;
161         else
162             return NSTerminateCancel;
163     }
164     
165     return NSTerminateNow;
166 }
167
168 - (void)applicationWillTerminate:(NSNotification *)aNotification
169 {
170         [outputPanel release];
171         [fQueueController release];
172         hb_close(&fHandle);
173 }
174
175
176 - (void) awakeFromNib
177 {
178     [fWindow center];
179     [fWindow setExcludedFromWindowsMenu:YES];
180     [fAdvancedOptions setView:fAdvancedView];
181
182     /* Initialize currentScanCount so HB can use it to
183                 evaluate successive scans */
184         currentScanCount = 0;
185         
186     /* Init UserPresets .plist */
187         [self loadPresets];
188                 
189         fRipIndicatorShown = NO;  // initially out of view in the nib
190         
191         /* Show/Dont Show Presets drawer upon launch based
192                 on user preference DefaultPresetsDrawerShow*/
193         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultPresetsDrawerShow"] > 0)
194         {
195                 [fPresetDrawer open];
196         }
197         
198         
199     /* Destination box*/
200     [fDstFormatPopUp removeAllItems];
201     [fDstFormatPopUp addItemWithTitle: _( @"MP4 file" )];
202     [fDstFormatPopUp addItemWithTitle: _( @"AVI file" )];
203     [fDstFormatPopUp addItemWithTitle: _( @"OGM file" )];
204         [fDstFormatPopUp addItemWithTitle: _( @"MKV file" )];
205     [fDstFormatPopUp selectItemAtIndex: 0];
206         
207     [self formatPopUpChanged: NULL];
208     
209         /* We enable the create chapters checkbox here since we are .mp4 */     
210         [fCreateChapterMarkers setEnabled: YES];
211         if ([fDstFormatPopUp indexOfSelectedItem] == 0 && [[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultChapterMarkers"] > 0)
212         {
213                 [fCreateChapterMarkers setState: NSOnState];
214         }
215         
216         
217         
218         
219     [fDstFile2Field setStringValue: [NSString stringWithFormat:
220         @"%@/Desktop/Movie.mp4", NSHomeDirectory()]];
221         
222     /* Video encoder */
223     [fVidEncoderPopUp removeAllItems];
224     [fVidEncoderPopUp addItemWithTitle: @"FFmpeg"];
225     [fVidEncoderPopUp addItemWithTitle: @"XviD"];
226         
227     
228         
229     /* Video quality */
230     [fVidTargetSizeField setIntValue: 700];
231         [fVidBitrateField    setIntValue: 1000];
232         
233     [fVidQualityMatrix   selectCell: fVidBitrateCell];
234     [self videoMatrixChanged: NULL];
235         
236     /* Video framerate */
237     [fVidRatePopUp removeAllItems];
238         [fVidRatePopUp addItemWithTitle: _( @"Same as source" )];
239     for( int i = 0; i < hb_video_rates_count; i++ )
240     {
241         if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%.3f",23.976]])
242                 {
243                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
244                                 [NSString stringWithCString: hb_video_rates[i].string], @" (NTSC Film)"]];
245                 }
246                 else if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%d",25]])
247                 {
248                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
249                                 [NSString stringWithCString: hb_video_rates[i].string], @" (PAL Film/Video)"]];
250                 }
251                 else if ([[NSString stringWithCString: hb_video_rates[i].string] isEqualToString: [NSString stringWithFormat: @"%.2f",29.97]])
252                 {
253                         [fVidRatePopUp addItemWithTitle:[NSString stringWithFormat: @"%@%@",
254                                 [NSString stringWithCString: hb_video_rates[i].string], @" (NTSC Video)"]];
255                 }
256                 else
257                 {
258                         [fVidRatePopUp addItemWithTitle:
259                                 [NSString stringWithCString: hb_video_rates[i].string]];
260                 }
261     }
262     [fVidRatePopUp selectItemAtIndex: 0];
263         
264         /* Picture Settings */
265         [fPicLabelPAROutputX setStringValue: @""];
266         [fPicSettingPARWidth setStringValue: @""];
267         [fPicSettingPARHeight setStringValue:  @""];
268         
269         /* Set Auto Crop to On at launch */
270     [fPictureController setAutoCrop:YES];
271         
272         /* Audio bitrate */
273     [fAudBitratePopUp removeAllItems];
274     for( int i = 0; i < hb_audio_bitrates_count; i++ )
275     {
276         [fAudBitratePopUp addItemWithTitle:
277                                 [NSString stringWithCString: hb_audio_bitrates[i].string]];
278
279     }
280     [fAudBitratePopUp selectItemAtIndex: hb_audio_bitrates_default];
281         
282     /* Audio samplerate */
283     [fAudRatePopUp removeAllItems];
284     for( int i = 0; i < hb_audio_rates_count; i++ )
285     {
286         [fAudRatePopUp addItemWithTitle:
287             [NSString stringWithCString: hb_audio_rates[i].string]];
288     }
289     [fAudRatePopUp selectItemAtIndex: hb_audio_rates_default];
290         
291     /* Bottom */
292     [fStatusField setStringValue: @""];
293         
294     [self enableUI: NO];
295         [self setupToolbar];
296         
297         [fPresetsActionButton setMenu:fPresetsActionMenu];
298         
299         /* We disable the Turbo 1st pass checkbox since we are not x264 */
300         [fVidTurboPassCheck setEnabled: NO];
301         [fVidTurboPassCheck setState: NSOffState];
302         
303         
304         /* lets get our default prefs here */
305         [self getDefaultPresets: NULL];
306         /* lets initialize the current successful scancount here to 0 */
307         currentSuccessfulScanCount = 0;
308     
309 }
310
311 - (void) loadPresets {
312         /* We declare the default NSFileManager into fileManager */
313         NSFileManager * fileManager = [NSFileManager defaultManager];
314         /* we set the files and support paths here */
315         AppSupportDirectory = @"~/Library/Application Support/HandBrake";
316     AppSupportDirectory = [AppSupportDirectory stringByExpandingTildeInPath];
317     //UserPresetsFile = @"~/Library/Application Support/HandBrake/UserPresets.plist";
318     //UserPresetsFile = [UserPresetsFile stringByExpandingTildeInPath];
319         /* We check for the app support directory for handbrake */
320         if ([fileManager fileExistsAtPath:AppSupportDirectory] == 0) 
321         {
322                 // If it doesnt exist yet, we create it here 
323                 [fileManager createDirectoryAtPath:AppSupportDirectory attributes:nil];
324         }
325         /* We check for the presets.plist here */
326         if ([fileManager fileExistsAtPath:UserPresetsFile] == 0) 
327         {
328                 [fileManager createFileAtPath:UserPresetsFile contents:nil attributes:nil];
329         }
330         UserPresetsFile = @"~/Library/Application Support/HandBrake/UserPresets.plist";
331         UserPresetsFile = [[UserPresetsFile stringByExpandingTildeInPath]retain];
332         
333         UserPresets = [[NSMutableArray alloc] initWithContentsOfFile:UserPresetsFile];
334         if (nil == UserPresets) 
335         {
336                 UserPresets = [[NSMutableArray alloc] init];
337                 [self addFactoryPresets:NULL];
338         }
339         
340 }
341
342 // ============================================================
343 // NSToolbar Related Methods
344 // ============================================================
345
346 - (void) setupToolbar {
347     toolbar = [[[NSToolbar alloc] initWithIdentifier: @"HandBrake Toolbar"] autorelease];
348     
349     [toolbar setAllowsUserCustomization: YES];
350     [toolbar setAutosavesConfiguration: YES];
351     [toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
352     
353     [toolbar setDelegate: self];
354     
355     [fWindow setToolbar: toolbar];
356 }
357
358 - (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier:
359     (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
360     NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdent];
361     
362     if ([itemIdent isEqualToString: ToggleDrawerIdentifier])
363     {
364         [item setLabel: @"Toggle Presets"];
365         [item setPaletteLabel: @"Toggler Presets"];
366         [item setToolTip: @"Open/Close Preset Drawer"];
367         [item setImage: [NSImage imageNamed: @"Drawer-List2"]];
368         [item setTarget: self];
369         [item setAction: @selector(toggleDrawer:)];
370         [item setAutovalidates: NO];
371     }
372     else if ([itemIdent isEqualToString: StartEncodingIdentifier])
373     {
374         [item setLabel: @"Start"];
375         [item setPaletteLabel: @"Start Encoding"];
376         [item setToolTip: @"Start Encoding"];
377         [item setImage: [NSImage imageNamed: @"Play"]];
378         [item setTarget: self];
379         [item setAction: @selector(Rip:)];
380     }
381     else if ([itemIdent isEqualToString: ShowQueueIdentifier])
382     {
383         [item setLabel: @"Show Queue"];
384         [item setPaletteLabel: @"Show Queue"];
385         [item setToolTip: @"Show Queue"];
386         [item setImage: [NSImage imageNamed: @"Brushed Window"]];
387         [item setTarget: self];
388         [item setAction: @selector(showQueueWindow:)];
389         [item setAutovalidates: NO];
390     }
391     else if ([itemIdent isEqualToString: AddToQueueIdentifier])
392     {
393         [item setLabel: @"Add to Queue"];
394         [item setPaletteLabel: @"Add to Queue"];
395         [item setToolTip: @"Add to Queue"];
396         [item setImage: [NSImage imageNamed: @"Add"]];
397         [item setTarget: self];
398         [item setAction: @selector(addToQueue:)];
399     }
400     else if ([itemIdent isEqualToString: PauseEncodingIdentifier])
401     {
402         [item setLabel: @"Pause"];
403         [item setPaletteLabel: @"Pause Encoding"];
404         [item setToolTip: @"Pause Encoding"];
405         [item setImage: [NSImage imageNamed: @"Pause"]];
406         [item setTarget: self];
407         [item setAction: @selector(Pause:)];
408     }
409     else if ([itemIdent isEqualToString: ShowActivityIdentifier]) {
410         [item setLabel: @"Activity Window"];
411         [item setPaletteLabel: @"Show Activity Window"];
412         [item setToolTip: @"Show Activity Window"];
413         [item setImage: [NSImage imageNamed: @"Terminal"]];
414         [item setTarget: self];
415         [item setAction: @selector(showDebugOutputPanel:)];
416         [item setAutovalidates: NO];
417     }
418     else if ([itemIdent isEqualToString: ChooseSourceIdentifier])
419     {
420         [item setLabel: @"Source"];
421         [item setPaletteLabel: @"Source"];
422         [item setToolTip: @"Choose Video Source"];
423         [item setImage: [NSImage imageNamed: @"Disc"]];
424         [item setTarget: self];
425         [item setAction: @selector(showScanPanel:)];
426         [item setAutovalidates: NO];
427     }
428     else
429     {
430         [item release];
431         return nil;
432     }
433
434     return item;
435 }
436
437 - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
438 {
439     return [NSArray arrayWithObjects: ChooseSourceIdentifier, NSToolbarSeparatorItemIdentifier, StartEncodingIdentifier,
440         PauseEncodingIdentifier, AddToQueueIdentifier, ShowQueueIdentifier, NSToolbarFlexibleSpaceItemIdentifier, 
441                 NSToolbarSpaceItemIdentifier, ShowActivityIdentifier, ToggleDrawerIdentifier, nil];
442 }
443
444 - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
445 {
446     return [NSArray arrayWithObjects:  StartEncodingIdentifier, PauseEncodingIdentifier, AddToQueueIdentifier,
447         ChooseSourceIdentifier, ShowQueueIdentifier, ShowActivityIdentifier, ToggleDrawerIdentifier,
448         NSToolbarCustomizeToolbarItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier,
449         NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil];
450 }
451
452 - (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
453 {
454     NSString * ident = [toolbarItem itemIdentifier];
455         
456     if (fHandle)
457     {
458         hb_state_t s;
459         hb_get_state2( fHandle, &s );
460         
461         if (s.state == HB_STATE_WORKING || s.state == HB_STATE_MUXING)
462         {
463             if ([ident isEqualToString: StartEncodingIdentifier])
464             {
465                 [toolbarItem setImage: [NSImage imageNamed: @"Stop"]];
466                 [toolbarItem setLabel: @"Stop"];
467                 [toolbarItem setPaletteLabel: @"Stop"];
468                 [toolbarItem setToolTip: @"Stop Encoding"];
469                 return YES;
470             }
471             if ([ident isEqualToString: PauseEncodingIdentifier])
472             {
473                 [toolbarItem setImage: [NSImage imageNamed: @"Pause"]];
474                 [toolbarItem setLabel: @"Pause"];
475                 [toolbarItem setPaletteLabel: @"Pause Encoding"];
476                 [toolbarItem setToolTip: @"Pause Encoding"];
477                 return YES;
478             }
479             if (SuccessfulScan)
480                 if ([ident isEqualToString: AddToQueueIdentifier])
481                     return YES;
482         }
483         else if (s.state == HB_STATE_PAUSED)
484         {
485             if ([ident isEqualToString: PauseEncodingIdentifier])
486             {
487                 [toolbarItem setImage: [NSImage imageNamed: @"Play"]];
488                 [toolbarItem setLabel: @"Resume"];
489                 [toolbarItem setPaletteLabel: @"Resume Encoding"];
490                 [toolbarItem setToolTip: @"Resume Encoding"];
491                 return YES;
492             }
493             if ([ident isEqualToString: StartEncodingIdentifier])
494                 return YES;
495             if ([ident isEqualToString: AddToQueueIdentifier])
496                 return YES;
497         }
498         else if (s.state == HB_STATE_SCANNING)
499             return NO;
500         else if (s.state == HB_STATE_WORKDONE || s.state == HB_STATE_SCANDONE || SuccessfulScan)
501         {
502             if ([ident isEqualToString: StartEncodingIdentifier])
503             {
504                 [toolbarItem setImage: [NSImage imageNamed: @"Play"]];
505                 if (hb_count(fHandle) > 0)
506                     [toolbarItem setLabel: @"Start Queue"];
507                 else
508                     [toolbarItem setLabel: @"Start"];
509                 [toolbarItem setPaletteLabel: @"Start Encoding"];
510                 [toolbarItem setToolTip: @"Start Encoding"];
511                 return YES;
512             }
513             if ([ident isEqualToString: AddToQueueIdentifier])
514                 return YES;
515         }
516
517     }
518     
519     if ([ident isEqualToString: ShowQueueIdentifier])
520         return YES;
521     if ([ident isEqualToString: ToggleDrawerIdentifier])
522         return YES;
523     if ([ident isEqualToString: ChooseSourceIdentifier])
524         return YES;
525     if ([ident isEqualToString: ShowActivityIdentifier])
526         return YES;
527     
528     return NO;
529 }
530
531 - (BOOL) validateMenuItem: (NSMenuItem *) menuItem
532 {
533     SEL action = [menuItem action];
534     
535     hb_state_t s;
536     hb_get_state2( fHandle, &s );
537     
538     if (fHandle)
539     {
540         if (action == @selector(addToQueue:) || action == @selector(showPicturePanel:) || action == @selector(showAddPresetPanel:))
541             return SuccessfulScan && [fWindow attachedSheet] == nil;
542         
543         if (action == @selector(showScanPanel:))
544         {
545             if (s.state == HB_STATE_SCANNING)
546                 return NO;
547             else
548                 return [fWindow attachedSheet] == nil;
549         }
550         if (action == @selector(selectDefaultPreset:))
551             return [tableView selectedRow] >= 0 && [fWindow attachedSheet] == nil;
552         if (action == @selector(Pause:))
553         {
554             if (s.state == HB_STATE_WORKING)
555             {
556                 if(![[menuItem title] isEqualToString:@"Pause Encoding"])
557                     [menuItem setTitle:@"Pause Encoding"];
558                 return YES;
559             }
560             else if (s.state == HB_STATE_PAUSED)
561             {
562                 if(![[menuItem title] isEqualToString:@"Resume Encoding"])
563                     [menuItem setTitle:@"Resume Encoding"];
564                 return YES;
565             }
566             else
567                 return NO;
568         }
569         if (action == @selector(Rip:))
570             if (s.state == HB_STATE_WORKING || s.state == HB_STATE_MUXING || s.state == HB_STATE_PAUSED)
571             {
572                 if(![[menuItem title] isEqualToString:@"Stop Encoding"])
573                     [menuItem setTitle:@"Stop Encoding"];
574                 return YES;
575             }
576             else if (SuccessfulScan)
577             {
578                 if(![[menuItem title] isEqualToString:@"Start Encoding"])
579                     [menuItem setTitle:@"Start Encoding"];
580                 return [fWindow attachedSheet] == nil;
581             }
582             else
583                 return NO;
584         }
585     
586     return YES;
587 }
588
589
590 // register a test notification and make
591 // it enabled by default
592 #define SERVICE_NAME @"Encode Done"
593 - (NSDictionary *)registrationDictionaryForGrowl 
594
595     NSDictionary *registrationDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
596     [NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_ALL, 
597     [NSArray arrayWithObjects:SERVICE_NAME,nil], GROWL_NOTIFICATIONS_DEFAULT, 
598     nil]; 
599
600     return registrationDictionary; 
601
602
603 - (void) TranslateStrings
604 {
605     [fSrcTitleField     setStringValue: _( @"Title:" )];
606     [fSrcChapterField   setStringValue: _( @"Chapters:" )];
607     [fSrcChapterToField setStringValue: _( @"to" )];
608     [fSrcDuration1Field setStringValue: _( @"Duration:" )];
609
610     [fDstFormatField    setStringValue: _( @"Format:" )];
611     [fDstCodecsField    setStringValue: _( @"Codecs:" )];
612     [fDstFile1Field     setStringValue: _( @"File:" )];
613     [fDstBrowseButton   setTitle:       _( @"Browse" )];
614
615     [fVidRateField      setStringValue: _( @"Framerate (fps):" )];
616     [fVidEncoderField   setStringValue: _( @"Encoder:" )];
617     [fVidQualityField   setStringValue: _( @"Quality:" )];
618 }
619
620 /***********************************************************************
621  * UpdateDockIcon
622  ***********************************************************************
623  * Shows a progression bar on the dock icon, filled according to
624  * 'progress' (0.0 <= progress <= 1.0).
625  * Called with progress < 0.0 or progress > 1.0, restores the original
626  * icon.
627  **********************************************************************/
628 - (void) UpdateDockIcon: (float) progress
629 {
630     NSImage * icon;
631     NSData * tiff;
632     NSBitmapImageRep * bmp;
633     uint32_t * pen;
634     uint32_t black = htonl( 0x000000FF );
635     uint32_t red   = htonl( 0xFF0000FF );
636     uint32_t white = htonl( 0xFFFFFFFF );
637     int row_start, row_end;
638     int i, j;
639
640     /* Get application original icon */
641     icon = [NSImage imageNamed: @"NSApplicationIcon"];
642
643     if( progress < 0.0 || progress > 1.0 )
644     {
645         [NSApp setApplicationIconImage: icon];
646         return;
647     }
648
649     /* Get it in a raw bitmap form */
650     tiff = [icon TIFFRepresentationUsingCompression:
651             NSTIFFCompressionNone factor: 1.0];
652     bmp = [NSBitmapImageRep imageRepWithData: tiff];
653     
654     /* Draw the progression bar */
655     /* It's pretty simple (ugly?) now, but I'm no designer */
656
657     row_start = 3 * (int) [bmp size].height / 4;
658     row_end   = 7 * (int) [bmp size].height / 8;
659
660     for( i = row_start; i < row_start + 2; i++ )
661     {
662         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
663         for( j = 0; j < (int) [bmp size].width; j++ )
664         {
665             pen[j] = black;
666         }
667     }
668     for( i = row_start + 2; i < row_end - 2; i++ )
669     {
670         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
671         pen[0] = black;
672         pen[1] = black;
673         for( j = 2; j < (int) [bmp size].width - 2; j++ )
674         {
675             if( j < 2 + (int) ( ( [bmp size].width - 4.0 ) * progress ) )
676             {
677                 pen[j] = red;
678             }
679             else
680             {
681                 pen[j] = white;
682             }
683         }
684         pen[j]   = black;
685         pen[j+1] = black;
686     }
687     for( i = row_end - 2; i < row_end; i++ )
688     {
689         pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
690         for( j = 0; j < (int) [bmp size].width; j++ )
691         {
692             pen[j] = black;
693         }
694     }
695
696     /* Now update the dock icon */
697     tiff = [bmp TIFFRepresentationUsingCompression:
698             NSTIFFCompressionNone factor: 1.0];
699     icon = [[NSImage alloc] initWithData: tiff];
700     [NSApp setApplicationIconImage: icon];
701     [icon release];
702 }
703
704 - (void) updateUI: (NSTimer *) timer
705 {
706
707     hb_list_t  * list;
708     list = hb_get_titles( fHandle );    
709     /* check to see if there has been a new scan done
710         this bypasses the constraints of HB_STATE_WORKING
711         not allowing setting a newly scanned source */
712         int checkScanCount = hb_get_scancount( fHandle );
713         if (checkScanCount > currentScanCount)
714         {
715                 
716                 currentScanCount = checkScanCount;
717                 //[fScanController Cancel: NULL];
718                 [fScanIndicator setIndeterminate: NO];
719                 [fScanIndicator setDoubleValue: 0.0];
720                 [fScanIndicator setHidden: YES];
721
722                 [self showNewScan: NULL];
723         }
724         
725     hb_state_t s;
726     hb_get_state( fHandle, &s );
727         
728         
729     switch( s.state )
730     {
731         case HB_STATE_IDLE:
732                 break;
733 #define p s.param.scanning                      
734         case HB_STATE_SCANNING:
735                 {
736             [fSrcDVD2Field setStringValue: [NSString stringWithFormat:
737                 _( @"Scanning title %d of %d..." ),
738                 p.title_cur, p.title_count]];
739             [fScanIndicator setIndeterminate: NO];
740                         [fScanIndicator setDoubleValue: 100.0 * ( p.title_cur - 1 ) /
741                 p.title_count];
742             break;
743                 }
744 #undef p
745         
746 #define p s.param.scandone
747         case HB_STATE_SCANDONE:
748         {
749                         
750                         [fScanIndicator setIndeterminate: NO];
751             [fScanIndicator setDoubleValue: 0.0];
752                         [fScanIndicator setHidden: YES];
753                         [self showNewScan: NULL];
754             [toolbar validateVisibleItems];
755                         break;
756         }
757 #undef p
758                         
759 #define p s.param.working
760         case HB_STATE_WORKING:
761         {
762             float progress_total;
763             NSMutableString * string;
764                         /* Currently, p.job_cur and p.job_count get screwed up when adding
765                                 jobs during encoding, if they cannot be fixed in libhb, will implement a
766                                 nasty but working cocoa solution */
767                         /* Update text field */
768                         string = [NSMutableString stringWithFormat: _( @"Encoding: task %d of %d, %.2f %%" ), p.job_cur, p.job_count, 100.0 * p.progress];
769             
770                         if( p.seconds > -1 )
771             {
772                 [string appendFormat:
773                     _( @" (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)" ),
774                     p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds];
775             }
776             [fStatusField setStringValue: string];
777                         
778             /* Update slider */
779                         progress_total = ( p.progress + p.job_cur - 1 ) / p.job_count;
780             [fRipIndicator setIndeterminate: NO];
781             [fRipIndicator setDoubleValue: 100.0 * progress_total];
782                         
783             // If progress bar hasn't been revealed at the bottom of the window, do
784             // that now. This code used to be in doRip. I moved it to here to handle
785             // the case where hb_start is called by HBQueueController and not from
786             // HBController.
787             if (!fRipIndicatorShown)
788             {
789                 NSRect frame = [fWindow frame];
790                 if (frame.size.width <= 591)
791                     frame.size.width = 591;
792                 frame.size.height += 36;
793                 frame.origin.y -= 36;
794                 [fWindow setFrame:frame display:YES animate:YES];
795                 fRipIndicatorShown = YES;
796             }
797
798             /* Update dock icon */
799             [self UpdateDockIcon: progress_total];
800                         
801             // Has current job changed? That means the queue has probably changed as
802                         // well so update it
803             if (fLastKnownCurrentJob != hb_current_job(fHandle))
804             {
805                 fLastKnownCurrentJob = hb_current_job(fHandle);
806                 [fQueueController updateQueueUI];
807             }
808             [fQueueController updateCurrentJobUI];
809             
810             break;
811         }
812 #undef p
813                         
814 #define p s.param.muxing
815         case HB_STATE_MUXING:
816         {
817             NSMutableString * string;
818                         
819             /* Update text field */
820             string = [NSMutableString stringWithFormat:
821                 _( @"Muxing..." )];
822             [fStatusField setStringValue: string];
823                         
824             /* Update slider */
825             [fRipIndicator setIndeterminate: YES];
826             [fRipIndicator startAnimation: nil];
827                         
828             /* Update dock icon */
829             [self UpdateDockIcon: 1.0];
830                         
831                         // Pass along the info to HBQueueController
832             [fQueueController updateCurrentJobUI];
833                         
834             break;
835         }
836 #undef p
837                         
838         case HB_STATE_PAUSED:
839                     [fStatusField setStringValue: _( @"Paused" )];
840             
841                         // Pass along the info to HBQueueController
842             [fQueueController updateCurrentJobUI];
843
844             break;
845                         
846         case HB_STATE_WORKDONE:
847         {
848             // HB_STATE_WORKDONE happpens as a result of hblib finishing all its jobs
849             // or someone calling hb_stop. In the latter case, hb_stop does not clear
850             // out the remaining passes/jobs in the queue. We'll do that here.
851                         
852             // Delete all remaining scans of this job, ie, delete whole encodes.
853             hb_job_t * job;
854             while( ( job = hb_job( fHandle, 0 ) ) && (job->sequence_id != 0) )
855                 hb_rem( fHandle, job );
856
857             // Start processing back up if jobs still left in queue
858             if (hb_count(fHandle) > 0)
859             {
860                 hb_start(fHandle);
861                 fEncodeState = 1;
862                 // Validate the toolbar (hack). The toolbar will usually get autovalidated
863                 // before we had the chance to restart the queue, hence it will now be in
864                 // the wrong state.
865                 [toolbar validateVisibleItems];
866                 break;
867             }
868
869             [fStatusField setStringValue: _( @"Done." )];
870             [fRipIndicator setIndeterminate: NO];
871             [fRipIndicator setDoubleValue: 0.0];
872             [toolbar validateVisibleItems];
873
874             /* Restore dock icon */
875             [self UpdateDockIcon: -1.0];
876
877             if (fRipIndicatorShown)
878             {
879                 NSRect frame = [fWindow frame];
880                 if (frame.size.width <= 591)
881                                     frame.size.width = 591;
882                 frame.size.height += -36;
883                 frame.origin.y -= -36;
884                 [fWindow setFrame:frame display:YES animate:YES];
885                                 fRipIndicatorShown = NO;
886                         }
887                         
888             // Queue has been modified so update the UI
889                         fLastKnownCurrentJob = nil;
890             [fQueueController updateQueueUI];
891             [fQueueController updateCurrentJobUI];
892                         
893             /* Check to see if the encode state has not been cancelled
894                                 to determine if we should check for encode done notifications */
895                         if (fEncodeState != 2)                  {
896                                 /* If Growl Notification or Window and Growl has been selected */
897                                 if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Growl Notification"] || 
898                                         [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"])
899                 {
900                                         /*Growl Notification*/
901                                         [self showGrowlDoneNotification: NULL];
902                 }
903                 /* If Alert Window or Window and Growl has been selected */
904                                 if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window"] || 
905                                         [[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Alert Window And Growl"])
906                 {
907                                         /*On Screen Notification*/
908                                         int status;
909                                         NSBeep();
910                                         status = NSRunAlertPanel(@"Put down that cocktail...",@"Your HandBrake encode is done!", @"OK", nil, nil);
911                                         [NSApp requestUserAttention:NSCriticalRequest];
912                                         if ( status == NSAlertDefaultReturn ) 
913                                         {
914                                                 [self enableUI: YES];
915                                         }
916                 }
917                                 else
918                                 {
919                                         [self enableUI: YES];
920                                 }
921                                    /* If sleep has been selected */ 
922             if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Put Computer To Sleep"]) 
923                 { 
924                /* Sleep */ 
925                NSDictionary* errorDict; 
926                NSAppleEventDescriptor* returnDescriptor = NULL; 
927                NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: 
928                         @"tell application \"Finder\" to sleep"]; 
929                returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; 
930                [scriptObject release]; 
931                [self enableUI: YES]; 
932                 } 
933             /* If Shutdown has been selected */ 
934             if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AlertWhenDone"] isEqualToString: @"Shut Down Computer"]) 
935                 { 
936                /* Shut Down */ 
937                NSDictionary* errorDict; 
938                NSAppleEventDescriptor* returnDescriptor = NULL; 
939                NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: 
940                         @"tell application \"Finder\" to shut down"]; 
941                returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; 
942                [scriptObject release]; 
943                [self enableUI: YES]; 
944                 }
945                         
946                                                 // MetaX insertion via AppleScript
947                         if([[NSUserDefaults standardUserDefaults] boolForKey: @"sendToMetaX"] == YES)
948                         {
949                         NSAppleScript *myScript = [[NSAppleScript alloc] initWithSource: [NSString stringWithFormat: @"%@%@%@", @"tell application \"MetaX\" to open (POSIX file \"", [fDstFile2Field stringValue], @"\")"]];
950                         [myScript executeAndReturnError: nil];
951                         [myScript release];
952                         }
953                         
954                         
955                         }
956                         else
957                         {
958                                 [self enableUI: YES];
959                         }
960             break;
961         }
962     }
963         
964     /* Lets show the queue status here in the main window */
965         int queue_count = hb_count( fHandle );
966         if( queue_count )
967         {
968                 [fQueueStatus setStringValue: [NSString stringWithFormat:
969                         @"%d pass%s in the queue",
970                                                  queue_count, ( queue_count > 1 ) ? "es" : ""]];
971         }
972         else
973         {
974                 [fQueueStatus setStringValue: @""];
975         }
976 }
977
978 - (IBAction) showNewScan:(id)sender
979 {
980         hb_list_t  * list;
981         hb_title_t * title;
982         int indxpri=0;    // Used to search the longuest title (default in combobox)
983         int longuestpri=0; // Used to search the longuest title (default in combobox)
984         
985         list = hb_get_titles( fHandle );
986         
987         if( !hb_list_count( list ) )
988         {
989                 /* We display a message if a valid dvd source was not chosen */
990                 [fSrcDVD2Field setStringValue: @"No Valid Title Found"];
991         SuccessfulScan = 0;
992         }
993         else
994         {
995      /* We increment the successful scancount here by one,
996                    which we use at the end of this function to tell the gui
997                    if this is the first successful scan since launch and whether
998                    or not we should set all settings to the defaults */
999                 
1000         SuccessfulScan = 1;
1001         currentSuccessfulScanCount++;
1002
1003                 [self enableUI: YES];
1004
1005         [toolbar validateVisibleItems];
1006                 
1007                 [fSrcTitlePopUp removeAllItems];
1008                 for( int i = 0; i < hb_list_count( list ); i++ )
1009                 {
1010                         title = (hb_title_t *) hb_list_item( list, i );
1011                         /*Set DVD Name at top of window*/
1012                         [fSrcDVD2Field setStringValue:[NSString stringWithUTF8String: title->name]];
1013                         
1014                         currentSource = [NSString stringWithUTF8String: title->dvd];
1015                         
1016                         
1017                         /* Use the dvd name in the default output field here 
1018                                 May want to add code to remove blank spaces for some dvd names*/
1019                         /* Check to see if the last destination has been set,use if so, if not, use Desktop */
1020                         if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastDestinationDirectory"])
1021                         {
1022                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1023                                         @"%@/%@.mp4", [[NSUserDefaults standardUserDefaults] stringForKey:@"LastDestinationDirectory"],[NSString
1024                   stringWithUTF8String: title->name]]];
1025                         }
1026                         else
1027                         {
1028                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1029                                         @"%@/Desktop/%@.mp4", NSHomeDirectory(),[NSString
1030                   stringWithUTF8String: title->name]]];
1031                         }
1032                         
1033                         
1034                         if (longuestpri < title->hours*60*60 + title->minutes *60 + title->seconds)
1035                         {
1036                                 longuestpri=title->hours*60*60 + title->minutes *60 + title->seconds;
1037                                 indxpri=i;
1038                         }
1039                         
1040                         
1041                         int format = [fDstFormatPopUp indexOfSelectedItem];
1042                         char * ext = NULL;
1043                         switch( format )
1044                         {
1045                                 case 0:
1046                                         
1047                                         /*Get Default MP4 File Extension for mpeg4 (.mp4 or .m4v) from prefs*/
1048                                         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultMpegName"] > 0)
1049                                         {
1050                                                 ext = "m4v";
1051                                         }
1052                                         else
1053                                         {
1054                                                 ext = "mp4";
1055                                         }
1056                                         break;
1057                                 case 1: 
1058                                         ext = "avi";
1059                                         break;
1060                                 case 2:
1061                                         ext = "ogm";
1062                                         break;
1063                                 case 3:
1064                                         ext = "mkv";
1065                                         break;
1066                         }
1067                         
1068                         
1069                         NSString * string = [fDstFile2Field stringValue];
1070                         /* Add/replace File Output name to the correct extension*/
1071                         if( [string characterAtIndex: [string length] - 4] == '.' )
1072                         {
1073                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1074                                         @"%@.%s", [string substringToIndex: [string length] - 4],
1075                                         ext]];
1076                         }
1077                         else
1078                         {
1079                                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1080                                         @"%@.%s", string, ext]];
1081                         }
1082                         
1083                         
1084                         [fSrcTitlePopUp addItemWithTitle: [NSString
1085                     stringWithFormat: @"%d - %02dh%02dm%02ds",
1086                                 title->index, title->hours, title->minutes,
1087                                 title->seconds]];
1088                         
1089                 }
1090                 // Select the longuest title
1091                 [fSrcTitlePopUp selectItemAtIndex: indxpri];
1092                 [self titlePopUpChanged: NULL];
1093                 
1094                 
1095                 
1096                 [self enableUI: YES];
1097                 
1098                 /* we record the current source name here in case the next scan is unsuccessful,
1099                                 then we can replace the scan progress with the old name if necessary */
1100        sourceDisplayName = [NSString stringWithFormat:[fSrcDVD2Field stringValue]];
1101        
1102        /* if its the initial successful scan after awakeFromNib */
1103            if (currentSuccessfulScanCount == 1)
1104            {
1105            [self selectDefaultPreset: NULL];
1106            /* if Deinterlace upon launch is specified in the prefs, then set to 1 for "Fast",
1107            if not, then set to 0 for none */
1108            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultDeinterlaceOn"] > 0)
1109                [fPictureController setDeinterlace:1];
1110            else
1111                [fPictureController setDeinterlace:0];
1112            }
1113        
1114         }
1115 }
1116
1117 -(IBAction)showGrowlDoneNotification:(id)sender
1118 {
1119   [GrowlApplicationBridge 
1120           notifyWithTitle:@"Put down that cocktail..." 
1121               description:@"your HandBrake encode is done!" 
1122          notificationName:SERVICE_NAME
1123                  iconData:nil 
1124                  priority:0 
1125                  isSticky:1 
1126              clickContext:nil];
1127 }
1128
1129 - (void) enableUI: (bool) b
1130 {
1131     NSControl * controls[] =
1132       { fSrcTitleField, fSrcTitlePopUp,
1133         fSrcChapterField, fSrcChapterStartPopUp, fSrcChapterToField,
1134         fSrcChapterEndPopUp, fSrcDuration1Field, fSrcDuration2Field,
1135         fDstFormatField, fDstFormatPopUp, fDstCodecsField,
1136         fDstCodecsPopUp, fDstFile1Field, fDstFile2Field,
1137         fDstBrowseButton, fVidRateField, fVidRatePopUp,
1138         fVidEncoderField, fVidEncoderPopUp, fVidQualityField,
1139         fVidQualityMatrix, fVidGrayscaleCheck, fSubField, fSubPopUp,
1140         fAudLang1Field, fAudLang1PopUp, fAudLang2Field, fAudLang2PopUp,
1141         fAudTrack1MixLabel, fAudTrack1MixPopUp, fAudTrack2MixLabel, fAudTrack2MixPopUp,
1142         fAudRateField, fAudRatePopUp, fAudBitrateField,
1143         fAudBitratePopUp, fPictureButton,fQueueStatus, 
1144                 fPicSrcWidth,fPicSrcHeight,fPicSettingWidth,fPicSettingHeight,fPicSettingARkeep,
1145                 fPicSettingDeinterlace,fPicLabelSettings,fPicLabelSrc,fPicLabelOutp,
1146                 fPicLabelAr,fPicLabelDeinterlace,fPicLabelSrcX,fPicLabelOutputX,
1147                 fPicLabelPAROutputX,fPicSettingPARWidth,fPicSettingPARHeight,
1148                 fPicSettingPAR,fPicLabelAnamorphic,tableView,fPresetsAdd,fPresetsDelete,
1149                 fCreateChapterMarkers,fVidTurboPassCheck,fDstMpgLargeFileCheck,fPicLabelAutoCrop,
1150                 fPicSettingAutoCrop,fPicSettingDetelecine,fPicLabelDetelecine,fPicLabelDenoise,fPicSettingDenoise,fSubForcedCheck,};
1151
1152     for( unsigned i = 0;
1153          i < sizeof( controls ) / sizeof( NSControl * ); i++ )
1154     {
1155         if( [[controls[i] className] isEqualToString: @"NSTextField"] )
1156         {
1157             NSTextField * tf = (NSTextField *) controls[i];
1158             if( ![tf isBezeled] )
1159             {
1160                 [tf setTextColor: b ? [NSColor controlTextColor] :
1161                     [NSColor disabledControlTextColor]];
1162                 continue;
1163             }
1164         }
1165         [controls[i] setEnabled: b];
1166
1167     }
1168         
1169         if (b) {
1170
1171         /* if we're enabling the interface, check if the audio mixdown controls need to be enabled or not */
1172         /* these will have been enabled by the mass control enablement above anyway, so we're sense-checking it here */
1173         [self setEnabledStateOfAudioMixdownControls: NULL];
1174         
1175         } else {
1176
1177                 [tableView setEnabled: NO];
1178         
1179         }
1180
1181     [self videoMatrixChanged: NULL];
1182     [fAdvancedOptions enableUI:b];
1183 }
1184
1185 - (IBAction) showScanPanel: (id) sender
1186 {
1187     [self enableUI: NO];
1188         
1189         [self browseSources:NULL];
1190 }
1191
1192 - (void) browseSources: (id) sender
1193 {
1194     NSOpenPanel * panel;
1195         
1196     panel = [NSOpenPanel openPanel];
1197     [panel setAllowsMultipleSelection: NO];
1198     [panel setCanChooseFiles: YES];
1199     [panel setCanChooseDirectories: YES ];
1200     NSString * sourceDirectory;
1201         if ([[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"])
1202         {
1203                 sourceDirectory = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastSourceDirectory"];
1204         }
1205         else
1206         {
1207                 sourceDirectory = @"~/Desktop";
1208                 sourceDirectory = [sourceDirectory stringByExpandingTildeInPath];
1209         }
1210    [panel beginSheetForDirectory: sourceDirectory file: nil types: nil
1211                                    modalForWindow: fWindow modalDelegate: self
1212                                    didEndSelector: @selector( browseSourcesDone:returnCode:contextInfo: )
1213                                           contextInfo: nil];
1214 }
1215
1216 - (void) browseSourcesDone: (NSOpenPanel *) sheet
1217                  returnCode: (int) returnCode contextInfo: (void *) contextInfo
1218 {
1219     /* User selected a file to open */
1220         if( returnCode == NSOKButton )
1221     {
1222         [fSrcDVD2Field setStringValue: _( @"Opening a new source ..." )];
1223                 [fScanIndicator setHidden: NO];
1224             [fScanIndicator setIndeterminate: YES];
1225         [fScanIndicator startAnimation: nil];
1226                 
1227                 /* we set the last source directory in the prefs here */
1228                 NSString *sourceDirectory = [[[sheet filenames] objectAtIndex: 0] stringByDeletingLastPathComponent];
1229                 [[NSUserDefaults standardUserDefaults] setObject:sourceDirectory forKey:@"LastSourceDirectory"];
1230                 
1231         NSString *path = [[sheet filenames] objectAtIndex: 0];
1232         HBDVDDetector *detector = [HBDVDDetector detectorForPath:path];
1233         if( [detector isVideoDVD] )
1234         {
1235             // The chosen path was actually on a DVD, so use the raw block
1236             // device path instead.
1237             path = [detector devicePath];
1238         }
1239                 
1240                 hb_scan( fHandle, [path UTF8String], 0 );
1241         }
1242         else // User clicked Cancel in browse window
1243         {
1244                 /* if we have a title loaded up */
1245                 if ([[fSrcDVD2Field stringValue] length] > 0)
1246                 {
1247             [self enableUI: YES];
1248         }
1249         }
1250 }
1251
1252 - (IBAction) openMainWindow: (id) sender
1253 {
1254     [fWindow  makeKeyAndOrderFront:nil];
1255     [fWindow setReleasedWhenClosed: YES];
1256 }
1257
1258 - (BOOL) windowShouldClose: (id) sender
1259 {
1260     [fWindow setReleasedWhenClosed: NO];
1261     return YES;
1262 }
1263
1264 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
1265 {
1266     if( !flag ) {
1267         [fWindow  makeKeyAndOrderFront:nil];
1268         [fWindow setReleasedWhenClosed: YES];
1269         
1270         return YES;
1271     }
1272     
1273     return NO;
1274 }
1275
1276 - (IBAction) videoMatrixChanged: (id) sender;
1277 {
1278     bool target, bitrate, quality;
1279
1280     target = bitrate = quality = false;
1281     if( [fVidQualityMatrix isEnabled] )
1282     {
1283         switch( [fVidQualityMatrix selectedRow] )
1284         {
1285             case 0:
1286                 target = true;
1287                 break;
1288             case 1:
1289                 bitrate = true;
1290                 break;
1291             case 2:
1292                 quality = true;
1293                 break;
1294         }
1295     }
1296     [fVidTargetSizeField  setEnabled: target];
1297     [fVidBitrateField     setEnabled: bitrate];
1298     [fVidQualitySlider    setEnabled: quality];
1299     [fVidTwoPassCheck     setEnabled: !quality &&
1300         [fVidQualityMatrix isEnabled]];
1301     if( quality )
1302     {
1303         [fVidTwoPassCheck setState: NSOffState];
1304                 [fVidTurboPassCheck setHidden: YES];
1305                 [fVidTurboPassCheck setState: NSOffState];
1306     }
1307
1308     [self qualitySliderChanged: sender];
1309     [self calculateBitrate: sender];
1310         [self customSettingUsed: sender];
1311 }
1312
1313 - (IBAction) qualitySliderChanged: (id) sender
1314 {
1315     [fVidConstantCell setTitle: [NSString stringWithFormat:
1316         _( @"Constant quality: %.0f %%" ), 100.0 *
1317         [fVidQualitySlider floatValue]]];
1318                 [self customSettingUsed: sender];
1319 }
1320
1321 - (IBAction) browseFile: (id) sender
1322 {
1323     /* Open a panel to let the user choose and update the text field */
1324     NSSavePanel * panel = [NSSavePanel savePanel];
1325         /* We get the current file name and path from the destination field here */
1326         [panel beginSheetForDirectory: [[fDstFile2Field stringValue] stringByDeletingLastPathComponent] file: [[fDstFile2Field stringValue] lastPathComponent]
1327                                    modalForWindow: fWindow modalDelegate: self
1328                                    didEndSelector: @selector( browseFileDone:returnCode:contextInfo: )
1329                                           contextInfo: NULL];
1330 }
1331
1332 - (void) browseFileDone: (NSSavePanel *) sheet
1333     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1334 {
1335     if( returnCode == NSOKButton )
1336     {
1337         [fDstFile2Field setStringValue: [sheet filename]];
1338     }
1339 }
1340
1341 - (IBAction) showPicturePanel: (id) sender
1342 {
1343         hb_list_t  * list  = hb_get_titles( fHandle );
1344     hb_title_t * title = (hb_title_t *) hb_list_item( list,
1345             [fSrcTitlePopUp indexOfSelectedItem] );
1346     [fPictureController showPanelInWindow:fWindow forTitle:title];
1347 }
1348
1349 - (void)pictureSettingsDidChange {
1350         [self calculatePictureSizing: NULL];
1351 }
1352
1353 - (void) PrepareJob
1354 {
1355     hb_list_t  * list  = hb_get_titles( fHandle );
1356     hb_title_t * title = (hb_title_t *) hb_list_item( list,
1357             [fSrcTitlePopUp indexOfSelectedItem] );
1358     hb_job_t * job = title->job;
1359     //int i;
1360
1361     /* Chapter selection */
1362     job->chapter_start = [fSrcChapterStartPopUp indexOfSelectedItem] + 1;
1363     job->chapter_end   = [fSrcChapterEndPopUp   indexOfSelectedItem] + 1;
1364         
1365     /* Format and codecs */
1366     int format = [fDstFormatPopUp indexOfSelectedItem];
1367     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
1368     job->mux    = FormatSettings[format][codecs] & HB_MUX_MASK;
1369     job->vcodec = FormatSettings[format][codecs] & HB_VCODEC_MASK;
1370     job->acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
1371     /* If mpeg-4, then set mpeg-4 specific options like chapters and > 4gb file sizes */
1372         if ([fDstFormatPopUp indexOfSelectedItem] == 0)
1373         {
1374         /* We set the largeFileSize (64 bit formatting) variable here to allow for > 4gb files based on the format being
1375                 mpeg4 and the checkbox being checked 
1376                 *Note: this will break compatibility with some target devices like iPod, etc.!!!!*/
1377                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AllowLargeFiles"] > 0 && [fDstMpgLargeFileCheck state] == NSOnState)
1378                 {
1379                         job->largeFileSize = 1;
1380                 }
1381                 else
1382                 {
1383                         job->largeFileSize = 0;
1384                 }
1385         }
1386         if ([fDstFormatPopUp indexOfSelectedItem] == 0 || [fDstFormatPopUp indexOfSelectedItem] == 3)
1387         {
1388           /* We set the chapter marker extraction here based on the format being
1389                 mpeg4 or mkv and the checkbox being checked */
1390                 if ([fCreateChapterMarkers state] == NSOnState)
1391                 {
1392                         job->chapter_markers = 1;
1393                 }
1394                 else
1395                 {
1396                         job->chapter_markers = 0;
1397                 }
1398         }
1399         if( ( job->vcodec & HB_VCODEC_FFMPEG ) &&
1400         [fVidEncoderPopUp indexOfSelectedItem] > 0 )
1401     {
1402         job->vcodec = HB_VCODEC_XVID;
1403     }
1404     if( job->vcodec & HB_VCODEC_X264 )
1405     {
1406                 if ([fVidEncoderPopUp indexOfSelectedItem] > 0 )
1407             {
1408                         /* Just use new Baseline Level 3.0 
1409                         Lets Deprecate Baseline Level 1.3h264_level*/
1410                         job->h264_level = 30;
1411                         job->mux = HB_MUX_IPOD;
1412                         /* move sanity check for iPod Encoding here */
1413                         job->pixel_ratio = 0 ;
1414                         
1415                 }
1416                 
1417                 /* Set this flag to switch from Constant Quantizer(default) to Constant Rate Factor Thanks jbrjake
1418                 Currently only used with Constant Quality setting*/
1419                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultCrf"] > 0 && [fVidQualityMatrix selectedRow] == 2)
1420                 {
1421                 job->crf = 1;
1422                 }
1423                 
1424                 /* Below Sends x264 options to the core library if x264 is selected*/
1425                 /* Lets use this as per Nyx, Thanks Nyx!*/
1426                 job->x264opts = (char *)calloc(1024, 1); /* Fixme, this just leaks */
1427                 /* Turbo first pass if two pass and Turbo First pass is selected */
1428                 if( [fVidTwoPassCheck state] == NSOnState && [fVidTurboPassCheck state] == NSOnState )
1429                 {
1430                         /* pass the "Turbo" string to be appended to the existing x264 opts string into a variable for the first pass */
1431                         NSString *firstPassOptStringTurbo = @":ref=1:subme=1:me=dia:analyse=none:trellis=0:no-fast-pskip=0:8x8dct=0";
1432                         /* append the "Turbo" string variable to the existing opts string.
1433                         Note: the "Turbo" string must be appended, not prepended to work properly*/
1434                         NSString *firstPassOptStringCombined = [[fAdvancedOptions optionsString] stringByAppendingString:firstPassOptStringTurbo];
1435                         strcpy(job->x264opts, [firstPassOptStringCombined UTF8String]);
1436                 }
1437                 else
1438                 {
1439                         strcpy(job->x264opts, [[fAdvancedOptions optionsString] UTF8String]);
1440                 }
1441                 
1442         job->h264_13 = [fVidEncoderPopUp indexOfSelectedItem];
1443     }
1444
1445     /* Video settings */
1446     if( [fVidRatePopUp indexOfSelectedItem] > 0 )
1447     {
1448         job->vrate      = 27000000;
1449         job->vrate_base = hb_video_rates[[fVidRatePopUp
1450             indexOfSelectedItem]-1].rate;
1451     }
1452     else
1453     {
1454         job->vrate      = title->rate;
1455         job->vrate_base = title->rate_base;
1456     }
1457
1458     switch( [fVidQualityMatrix selectedRow] )
1459     {
1460         case 0:
1461             /* Target size.
1462                Bitrate should already have been calculated and displayed
1463                in fVidBitrateField, so let's just use it */
1464         case 1:
1465             job->vquality = -1.0;
1466             job->vbitrate = [fVidBitrateField intValue];
1467             break;
1468         case 2:
1469             job->vquality = [fVidQualitySlider floatValue];
1470             job->vbitrate = 0;
1471             break;
1472     }
1473
1474     job->grayscale = ( [fVidGrayscaleCheck state] == NSOnState );
1475
1476     /* Subtitle settings */
1477     job->subtitle = [fSubPopUp indexOfSelectedItem] - 2;
1478
1479     /* Audio tracks and mixdowns */
1480     /* check for the condition where track 2 has an audio selected, but track 1 does not */
1481     /* we will use track 2 as track 1 in this scenario */
1482     if ([fAudLang1PopUp indexOfSelectedItem] > 0)
1483     {
1484         job->audios[0] = [fAudLang1PopUp indexOfSelectedItem] - 1;
1485         job->audios[1] = [fAudLang2PopUp indexOfSelectedItem] - 1; /* will be -1 if "none" is selected */
1486         job->audios[2] = -1;
1487         job->audio_mixdowns[0] = [[fAudTrack1MixPopUp selectedItem] tag];
1488         job->audio_mixdowns[1] = [[fAudTrack2MixPopUp selectedItem] tag];
1489     }
1490     else if ([fAudLang2PopUp indexOfSelectedItem] > 0)
1491     {
1492         job->audios[0] = [fAudLang2PopUp indexOfSelectedItem] - 1;
1493         job->audio_mixdowns[0] = [[fAudTrack2MixPopUp selectedItem] tag];
1494         job->audios[1] = -1;
1495     }
1496     else
1497     {
1498         job->audios[0] = -1;
1499     }
1500
1501     /* Audio settings */
1502     job->arate = hb_audio_rates[[fAudRatePopUp
1503                      indexOfSelectedItem]].rate;
1504     job->abitrate = [[fAudBitratePopUp selectedItem] tag];
1505     
1506     job->filters = hb_list_init();
1507    
1508         /* Detelecine */
1509     if ([fPictureController detelecine])
1510     {
1511         hb_list_add( job->filters, &hb_filter_detelecine );
1512     }
1513    
1514     /* Deinterlace */
1515     if ([fPictureController deinterlace] == 1)
1516     {
1517         /* Run old deinterlacer by default */
1518         hb_filter_deinterlace.settings = "-1"; 
1519         hb_list_add( job->filters, &hb_filter_deinterlace );
1520     }
1521     else if ([fPictureController deinterlace] == 2)
1522     {
1523         /* Yadif mode 0 (1-pass with spatial deinterlacing.) */
1524         hb_filter_deinterlace.settings = "0"; 
1525         hb_list_add( job->filters, &hb_filter_deinterlace );            
1526     }
1527     else if ([fPictureController deinterlace] == 3)
1528     {
1529         /* Yadif (1-pass w/o spatial deinterlacing) and Mcdeint */
1530         hb_filter_deinterlace.settings = "2:-1:1"; 
1531         hb_list_add( job->filters, &hb_filter_deinterlace );            
1532     }
1533     else if ([fPictureController deinterlace] == 4)
1534     {
1535         /* Yadif (2-pass w/ spatial deinterlacing) and Mcdeint*/
1536         hb_filter_deinterlace.settings = "1:-1:1"; 
1537         hb_list_add( job->filters, &hb_filter_deinterlace );            
1538     }
1539         
1540         /* Denoise */
1541         
1542         if ([fPictureController denoise] == 1) // Weak in popup
1543         {
1544                 hb_filter_denoise.settings = "2:1:2:3"; 
1545         hb_list_add( job->filters, &hb_filter_denoise );        
1546         }
1547         else if ([fPictureController denoise] == 2) // Medium in popup
1548         {
1549                 hb_filter_denoise.settings = "3:2:2:3"; 
1550         hb_list_add( job->filters, &hb_filter_denoise );        
1551         }
1552         else if ([fPictureController denoise] == 3) // Strong in popup
1553         {
1554                 hb_filter_denoise.settings = "7:7:5:5"; 
1555         hb_list_add( job->filters, &hb_filter_denoise );        
1556         }
1557
1558 }
1559
1560
1561
1562 /* addToQueue: puts up an alert before ultimately calling doAddToQueue
1563 */
1564 - (IBAction) addToQueue: (id) sender
1565 {
1566         /* We get the destination directory from the destination field here */
1567         NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1568         /* We check for a valid destination here */
1569         if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0) 
1570         {
1571                 NSRunAlertPanel(@"Warning!", @"This is not a valid destination directory!", @"OK", nil, nil);
1572         return;
1573         }
1574
1575     /* We check for duplicate name here */
1576         if( [[NSFileManager defaultManager] fileExistsAtPath:
1577             [fDstFile2Field stringValue]] )
1578     {
1579         NSBeginCriticalAlertSheet( _( @"File already exists" ),
1580             _( @"Cancel" ), _( @"Overwrite" ), NULL, fWindow, self,
1581             @selector( overwriteAddToQueueAlertDone:returnCode:contextInfo: ),
1582             NULL, NULL, [NSString stringWithFormat:
1583             _( @"Do you want to overwrite %@?" ),
1584             [fDstFile2Field stringValue]] );
1585         // overwriteAddToQueueAlertDone: will be called when the alert is dismissed.
1586     }
1587     else
1588     {
1589         [self doAddToQueue];
1590     }
1591 }
1592
1593 /* overwriteAddToQueueAlertDone: called from the alert posted by addToQueue that asks
1594    the user if they want to overwrite an exiting movie file.
1595 */
1596 - (void) overwriteAddToQueueAlertDone: (NSWindow *) sheet
1597     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1598 {
1599     if( returnCode == NSAlertAlternateReturn )
1600         [self doAddToQueue];
1601 }
1602
1603 - (void) doAddToQueue
1604 {
1605     hb_list_t  * list  = hb_get_titles( fHandle );
1606     hb_title_t * title = (hb_title_t *) hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
1607     hb_job_t * job = title->job;
1608
1609     // Assign a sequence number, starting at zero, to each job added so they can
1610     // be lumped together in the UI.
1611     job->sequence_id = -1;
1612
1613     [self PrepareJob];
1614
1615     /* Destination file */
1616     job->file = [[fDstFile2Field stringValue] UTF8String];
1617
1618     if( [fSubForcedCheck state] == NSOnState )
1619         job->subtitle_force = 1;
1620     else
1621         job->subtitle_force = 0;
1622
1623     /*
1624     * subtitle of -1 is a scan
1625     */
1626     if( job->subtitle == -1 )
1627     {
1628         char *x264opts_tmp;
1629
1630         /*
1631         * When subtitle scan is enabled do a fast pre-scan job
1632         * which will determine which subtitles to enable, if any.
1633         */
1634         job->pass = -1;
1635         x264opts_tmp = job->x264opts;
1636         job->subtitle = -1;
1637
1638         job->x264opts = NULL;
1639
1640         job->indepth_scan = 1;  
1641
1642         job->select_subtitle = (hb_subtitle_t**)malloc(sizeof(hb_subtitle_t*));
1643         *(job->select_subtitle) = NULL;
1644
1645         /*
1646         * Add the pre-scan job
1647         */
1648         job->sequence_id++; // for job grouping
1649         hb_add( fHandle, job );
1650
1651         job->x264opts = x264opts_tmp;
1652     }
1653     else
1654         job->select_subtitle = NULL;
1655
1656     /* No subtitle were selected, so reset the subtitle to -1 (which before
1657     * this point meant we were scanning
1658     */
1659     if( job->subtitle == -2 )
1660         job->subtitle = -1;
1661
1662     if( [fVidTwoPassCheck state] == NSOnState )
1663     {
1664         hb_subtitle_t **subtitle_tmp = job->select_subtitle;
1665         job->indepth_scan = 0;
1666
1667         job->pass = 1;
1668         job->sequence_id++; // for job grouping
1669         hb_add( fHandle, job );
1670
1671         job->pass = 2;
1672         job->sequence_id++; // for job grouping
1673
1674         job->x264opts = (char *)calloc(1024, 1); /* Fixme, this just leaks */  
1675         strcpy(job->x264opts, [[fAdvancedOptions optionsString] UTF8String]);
1676
1677         job->select_subtitle = subtitle_tmp;
1678
1679         hb_add( fHandle, job );
1680     }
1681     else
1682     {
1683         job->indepth_scan = 0;
1684         job->pass = 0;
1685         job->sequence_id++; // for job grouping
1686         hb_add( fHandle, job );
1687     }
1688         
1689     NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1690         [[NSUserDefaults standardUserDefaults] setObject:destinationDirectory forKey:@"LastDestinationDirectory"];
1691         /* Lets try to update stuff, taken from remove in the queue controller */
1692         [fQueueController performSelectorOnMainThread: @selector( updateQueueUI )
1693         withObject: NULL waitUntilDone: NO];
1694 }
1695
1696 /* Rip: puts up an alert before ultimately calling doRip
1697 */
1698 - (IBAction) Rip: (id) sender
1699 {
1700     /* Rip or Cancel ? */
1701     hb_state_t s;
1702     hb_get_state2( fHandle, &s );
1703
1704     if(s.state == HB_STATE_WORKING || s.state == HB_STATE_PAUSED)
1705         {
1706         [self Cancel: sender];
1707         return;
1708     }
1709     
1710     // If there are jobs in the queue, then this is a rip the queue
1711     
1712     if (hb_count( fHandle ) > 0)
1713     {
1714         [self doRip];
1715         return;
1716     }
1717
1718     // Before adding jobs to the queue, check for a valid destination.
1719
1720     NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1721     if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0) 
1722     {
1723         NSRunAlertPanel(@"Warning!", @"This is not a valid destination directory!", @"OK", nil, nil);
1724         return;
1725     }
1726
1727     /* We check for duplicate name here */
1728     if( [[NSFileManager defaultManager] fileExistsAtPath:[fDstFile2Field stringValue]] )
1729     {
1730         NSBeginCriticalAlertSheet( _( @"File already exists" ),
1731             _( @"Cancel" ), _( @"Overwrite" ), NULL, fWindow, self,
1732             @selector( overWriteAlertDone:returnCode:contextInfo: ),
1733             NULL, NULL, [NSString stringWithFormat:
1734             _( @"Do you want to overwrite %@?" ),
1735             [fDstFile2Field stringValue]] );
1736             
1737         // overWriteAlertDone: will be called when the alert is dismissed. It will call doRip.
1738     }
1739     else
1740     {
1741         /* if there are no jobs in the queue, then add this one to the queue and rip 
1742         otherwise, just rip the queue */
1743         if( hb_count( fHandle ) == 0)
1744         {
1745             [self doAddToQueue];
1746         }
1747
1748         NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1749         [[NSUserDefaults standardUserDefaults] setObject:destinationDirectory forKey:@"LastDestinationDirectory"];
1750         [self doRip];
1751     }
1752 }
1753
1754 /* overWriteAlertDone: called from the alert posted by Rip: that asks the user if they
1755    want to overwrite an exiting movie file.
1756 */
1757 - (void) overWriteAlertDone: (NSWindow *) sheet
1758     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1759 {
1760     if( returnCode == NSAlertAlternateReturn )
1761     {
1762         /* if there are no jobs in the queue, then add this one to the queue and rip 
1763         otherwise, just rip the queue */
1764         if( hb_count( fHandle ) == 0 )
1765         {
1766             [self doAddToQueue];
1767         }
1768
1769         NSString *destinationDirectory = [[fDstFile2Field stringValue] stringByDeletingLastPathComponent];
1770         [[NSUserDefaults standardUserDefaults] setObject:destinationDirectory forKey:@"LastDestinationDirectory"];
1771         [self doRip];
1772     }
1773 }
1774
1775 - (void) updateAlertDone: (NSWindow *) sheet
1776     returnCode: (int) returnCode contextInfo: (void *) contextInfo
1777 {
1778     if( returnCode == NSAlertAlternateReturn )
1779     {
1780         /* Show scan panel */
1781         [self performSelectorOnMainThread: @selector(showScanPanel:)
1782             withObject: NULL waitUntilDone: NO];
1783         return;
1784     }
1785
1786     /* Go to HandBrake homepage and exit */
1787     [self openHomepage: NULL];
1788     [NSApp terminate: self];
1789 }
1790
1791 - (void) doRip
1792 {
1793     /* Let libhb do the job */
1794     hb_start( fHandle );
1795         /*set the fEncodeState State */
1796         fEncodeState = 1;
1797 }
1798
1799
1800
1801
1802
1803 //------------------------------------------------------------------------------------
1804 // Cancels the current job and proceeds with the next one in the queue.
1805 //------------------------------------------------------------------------------------
1806 - (void) doCancelCurrentJob
1807 {
1808     // Stop the current job. hb_stop will only cancel the current pass and then set
1809     // its state to HB_STATE_WORKDONE. It also does this asynchronously. So when we
1810     // see the state has changed to HB_STATE_WORKDONE (in updateUI), we'll delete the
1811     // remaining passes of the job and then start the queue back up if there are any
1812     // remaining jobs.
1813      
1814     hb_stop( fHandle );
1815     fEncodeState = 2;   // don't alert at end of processing since this was a cancel
1816     
1817 }
1818
1819 //------------------------------------------------------------------------------------
1820 // Displays an alert asking user if the want to cancel encoding of current job.
1821 // Cancel: returns immediately after posting the alert. Later, when the user
1822 // acknowledges the alert, doCancelCurrentJob is called.
1823 //------------------------------------------------------------------------------------
1824 - (IBAction)Cancel: (id)sender
1825 {
1826     if (!fHandle) return;
1827     
1828     hb_job_t * job = hb_current_job(fHandle);
1829     if (!job) return;
1830
1831     NSString * alertTitle = [NSString stringWithFormat:NSLocalizedString(@"Do you want to stop encoding of %@?", nil),
1832             [NSString stringWithUTF8String:job->title->name]];
1833     
1834     // Which window to attach the sheet to?
1835     NSWindow * docWindow;
1836     if ([sender respondsToSelector: @selector(window)])
1837         docWindow = [sender window];
1838     else
1839         docWindow = fWindow;
1840         
1841     NSBeginCriticalAlertSheet(
1842             alertTitle,
1843             NSLocalizedString(@"Keep Encoding", nil), NSLocalizedString(@"Stop Encoding", nil), nil, docWindow, self,
1844             nil, @selector(didDimissCancelCurrentJob:returnCode:contextInfo:), nil,
1845             NSLocalizedString(@"Your movie will be lost if you don't continue encoding.", nil),
1846             [NSString stringWithUTF8String:job->title->name]);
1847     
1848     // didDimissCancelCurrentJob:returnCode:contextInfo: will be called when the dialog is dismissed
1849
1850     // N.B.: didDimissCancelCurrentJob:returnCode:contextInfo: is designated as the dismiss
1851     // selector to prevent a crash. As a dismiss selector, the alert window will
1852     // have already be dismissed. If we don't do it this way, the dismissing of
1853     // the alert window will cause the table view to be redrawn at a point where
1854     // current job has been deleted by hblib but we don't know about it yet. This
1855     // is a prime example of wy we need to NOT be relying on hb_current_job!!!!
1856 }
1857
1858 - (void) didDimissCancelCurrentJob: (NSWindow *)sheet returnCode: (int)returnCode contextInfo: (void *)contextInfo
1859 {
1860     if (returnCode == NSAlertAlternateReturn)
1861         [self doCancelCurrentJob];
1862 }
1863
1864
1865
1866
1867
1868 - (IBAction) Pause: (id) sender
1869 {
1870     hb_state_t s;
1871     hb_get_state2( fHandle, &s );
1872
1873     if( s.state == HB_STATE_PAUSED )
1874     {
1875         hb_resume( fHandle );
1876     }
1877     else
1878     {
1879         hb_pause( fHandle );
1880     }
1881 }
1882
1883 - (IBAction) titlePopUpChanged: (id) sender
1884 {
1885     hb_list_t  * list  = hb_get_titles( fHandle );
1886     hb_title_t * title = (hb_title_t*)
1887         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
1888                 
1889                 
1890     /* If Auto Naming is on. We create an output filename of dvd name - title number */
1891     if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultAutoNaming"] > 0)
1892         {
1893                 [fDstFile2Field setStringValue: [NSString stringWithFormat:
1894                         @"%@/%@-%d.%@", [[fDstFile2Field stringValue] stringByDeletingLastPathComponent],
1895                         [NSString stringWithUTF8String: title->name],
1896                           title->index,
1897                         [[fDstFile2Field stringValue] pathExtension]]]; 
1898         }
1899
1900     /* Update chapter popups */
1901     [fSrcChapterStartPopUp removeAllItems];
1902     [fSrcChapterEndPopUp   removeAllItems];
1903     for( int i = 0; i < hb_list_count( title->list_chapter ); i++ )
1904     {
1905         [fSrcChapterStartPopUp addItemWithTitle: [NSString
1906             stringWithFormat: @"%d", i + 1]];
1907         [fSrcChapterEndPopUp addItemWithTitle: [NSString
1908             stringWithFormat: @"%d", i + 1]];
1909     }
1910     [fSrcChapterStartPopUp selectItemAtIndex: 0];
1911     [fSrcChapterEndPopUp   selectItemAtIndex:
1912         hb_list_count( title->list_chapter ) - 1];
1913     [self chapterPopUpChanged: NULL];
1914
1915 /* Start Get and set the initial pic size for display */
1916         hb_job_t * job = title->job;
1917         fTitle = title; 
1918         /* Turn Deinterlace on/off depending on the preference */
1919         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultDeinterlaceOn"] > 0)
1920         {
1921                 [fPictureController setDeinterlace:1];
1922         }
1923         else
1924         {
1925                 [fPictureController setDeinterlace:0];
1926         }
1927         
1928         /* Pixel Ratio Setting */
1929         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"PixelRatio"])
1930     {
1931                 job->pixel_ratio = 1 ;
1932         }
1933         else
1934         {
1935                 job->pixel_ratio = 0 ;
1936         }
1937         /*Set Source Size Fields Here */
1938         [fPicSrcWidth setStringValue: [NSString stringWithFormat:
1939                                                          @"%d", fTitle->width]];
1940         [fPicSrcHeight setStringValue: [NSString stringWithFormat:
1941                                                          @"%d", fTitle->height]];
1942                                                          
1943         /* Set Auto Crop to on upon selecting a new title */
1944     [fPictureController setAutoCrop:YES];
1945     
1946         /* We get the originial output picture width and height and put them
1947         in variables for use with some presets later on */
1948         PicOrigOutputWidth = job->width;
1949         PicOrigOutputHeight = job->height;
1950         AutoCropTop = job->crop[0];
1951         AutoCropBottom = job->crop[1];
1952         AutoCropLeft = job->crop[2];
1953         AutoCropRight = job->crop[3];
1954         /* we test getting the max output value for pic sizing here to be used later*/
1955         [fPicSettingWidth setStringValue: [NSString stringWithFormat:
1956                 @"%d", PicOrigOutputWidth]];
1957         [fPicSettingHeight setStringValue: [NSString stringWithFormat:
1958                 @"%d", PicOrigOutputHeight]];
1959         /* we run the picture size values through
1960         calculatePictureSizing to get all picture size
1961         information*/
1962         [self calculatePictureSizing: NULL];
1963         /* Run Through encoderPopUpChanged to see if there
1964                 needs to be any pic value modifications based on encoder settings */
1965         //[self encoderPopUpChanged: NULL];
1966         /* END Get and set the initial pic size for display */ 
1967
1968     /* Update subtitle popups */
1969     hb_subtitle_t * subtitle;
1970     [fSubPopUp removeAllItems];
1971     [fSubPopUp addItemWithTitle: @"None"];
1972     [fSubPopUp addItemWithTitle: @"Autoselect"];
1973     for( int i = 0; i < hb_list_count( title->list_subtitle ); i++ )
1974     {
1975         subtitle = (hb_subtitle_t *) hb_list_item( title->list_subtitle, i );
1976
1977         /* We cannot use NSPopUpButton's addItemWithTitle because
1978            it checks for duplicate entries */
1979         [[fSubPopUp menu] addItemWithTitle: [NSString stringWithCString:
1980             subtitle->lang] action: NULL keyEquivalent: @""];
1981     }
1982     [fSubPopUp selectItemAtIndex: 0];
1983         
1984         [self subtitleSelectionChanged: NULL];
1985     
1986     /* Update chapter table */
1987     [fChapterTitlesDelegate resetWithTitle:title];
1988     [fChapterTable reloadData];
1989
1990     /* Update audio popups */
1991     [self addAllAudioTracksToPopUp: fAudLang1PopUp];
1992     [self addAllAudioTracksToPopUp: fAudLang2PopUp];
1993     /* search for the first instance of our prefs default language for track 1, and set track 2 to "none" */
1994         NSString * audioSearchPrefix = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultLanguage"];
1995         [self selectAudioTrackInPopUp: fAudLang1PopUp searchPrefixString: audioSearchPrefix selectIndexIfNotFound: 1];
1996     [self selectAudioTrackInPopUp: fAudLang2PopUp searchPrefixString: NULL selectIndexIfNotFound: 0];
1997         
1998         /* changing the title may have changed the audio channels on offer, */
1999         /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
2000         [self audioTrackPopUpChanged: fAudLang1PopUp];
2001         [self audioTrackPopUpChanged: fAudLang2PopUp];
2002         /* lets call tableViewSelected to make sure that any preset we have selected is enforced after a title change */
2003         [self tableViewSelected:NULL];
2004 }
2005
2006 - (IBAction) chapterPopUpChanged: (id) sender
2007 {
2008     
2009         /* If start chapter popup is greater than end chapter popup,
2010         we set the end chapter popup to the same as start chapter popup */
2011         if ([fSrcChapterStartPopUp indexOfSelectedItem] > [fSrcChapterEndPopUp indexOfSelectedItem])
2012         {
2013                 [fSrcChapterEndPopUp selectItemAtIndex: [fSrcChapterStartPopUp indexOfSelectedItem]];
2014     }
2015
2016                 
2017         hb_list_t  * list  = hb_get_titles( fHandle );
2018     hb_title_t * title = (hb_title_t *)
2019         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
2020
2021     hb_chapter_t * chapter;
2022     int64_t        duration = 0;
2023     for( int i = [fSrcChapterStartPopUp indexOfSelectedItem];
2024          i <= [fSrcChapterEndPopUp indexOfSelectedItem]; i++ )
2025     {
2026         chapter = (hb_chapter_t *) hb_list_item( title->list_chapter, i );
2027         duration += chapter->duration;
2028     }
2029     
2030     duration /= 90000; /* pts -> seconds */
2031     [fSrcDuration2Field setStringValue: [NSString stringWithFormat:
2032         @"%02lld:%02lld:%02lld", duration / 3600, ( duration / 60 ) % 60,
2033         duration % 60]];
2034
2035     [self calculateBitrate: sender];
2036 }
2037
2038 - (IBAction) formatPopUpChanged: (id) sender
2039 {
2040     NSString * string = [fDstFile2Field stringValue];
2041     int format = [fDstFormatPopUp indexOfSelectedItem];
2042     char * ext = NULL;
2043         /* Initially set the large file (64 bit formatting) output checkbox to hidden */
2044     [fDstMpgLargeFileCheck setHidden: YES];
2045     /* Update the codecs popup */
2046     [fDstCodecsPopUp removeAllItems];
2047     switch( format )
2048     {
2049         case 0:
2050                         /*Get Default MP4 File Extension*/
2051                         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultMpegName"] > 0)
2052                         {
2053                                 ext = "m4v";
2054                         }
2055                         else
2056                         {
2057                                 ext = "mp4";
2058                         }
2059             [fDstCodecsPopUp addItemWithTitle:
2060                 _( @"MPEG-4 Video / AAC Audio" )];
2061             [fDstCodecsPopUp addItemWithTitle:
2062                 _( @"AVC/H.264 Video / AAC Audio" )];
2063                         /* We enable the create chapters checkbox here since we are .mp4*/
2064                         [fCreateChapterMarkers setEnabled: YES];
2065                         /* We show the Large File (64 bit formatting) checkbox since we are .mp4 
2066                         if we have enabled the option in the global preferences*/
2067                         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"AllowLargeFiles"] > 0)
2068                         {
2069                                 [fDstMpgLargeFileCheck setHidden: NO];
2070                         }
2071                                 else
2072                                 {
2073                                         /* if not enable in global preferences, we additionaly sanity check that the
2074                                         hidden checkbox is set to off. */
2075                                         [fDstMpgLargeFileCheck setState: NSOffState];
2076                                 }
2077                                 break;
2078         case 1: 
2079             ext = "avi";
2080             [fDstCodecsPopUp addItemWithTitle:
2081                 _( @"MPEG-4 Video / MP3 Audio" )];
2082             [fDstCodecsPopUp addItemWithTitle:
2083                 _( @"MPEG-4 Video / AC-3 Audio" )];
2084             [fDstCodecsPopUp addItemWithTitle:
2085                 _( @"AVC/H.264 Video / MP3 Audio" )];
2086             [fDstCodecsPopUp addItemWithTitle:
2087                 _( @"AVC/H.264 Video / AC-3 Audio" )];
2088                         /* We disable the create chapters checkbox here since we are NOT .mp4 
2089                         and make sure it is unchecked*/
2090                         [fCreateChapterMarkers setEnabled: NO];
2091                         [fCreateChapterMarkers setState: NSOffState];
2092                         break;
2093         case 2:
2094             ext = "ogm";
2095             [fDstCodecsPopUp addItemWithTitle:
2096                 _( @"MPEG-4 Video / Vorbis Audio" )];
2097             [fDstCodecsPopUp addItemWithTitle:
2098                 _( @"MPEG-4 Video / MP3 Audio" )];
2099             /* We disable the create chapters checkbox here since we are NOT .mp4 
2100                         and make sure it is unchecked*/
2101                         [fCreateChapterMarkers setEnabled: NO];
2102                         [fCreateChapterMarkers setState: NSOffState];
2103                         break;
2104                 case 3:
2105             ext = "mkv";
2106             [fDstCodecsPopUp addItemWithTitle:
2107                 _( @"MPEG-4 Video / AAC Audio" )];
2108                                 [fDstCodecsPopUp addItemWithTitle:
2109                 _( @"MPEG-4 Video / AC-3 Audio" )];
2110                         [fDstCodecsPopUp addItemWithTitle:
2111                 _( @"MPEG-4 Video / MP3 Audio" )];
2112                         [fDstCodecsPopUp addItemWithTitle:
2113                 _( @"MPEG-4 Video / Vorbis Audio" )];
2114             
2115                         [fDstCodecsPopUp addItemWithTitle:
2116                 _( @"AVC/H.264 Video / AAC Audio" )];
2117                         [fDstCodecsPopUp addItemWithTitle:
2118                 _( @"AVC/H.264 Video / AC-3 Audio" )];
2119                         [fDstCodecsPopUp addItemWithTitle:
2120                 _( @"AVC/H.264 Video / MP3 Audio" )];
2121                         [fDstCodecsPopUp addItemWithTitle:
2122                 _( @"AVC/H.264 Video / Vorbis Audio" )];
2123             /* We enable the create chapters checkbox here since */
2124                         [fCreateChapterMarkers setEnabled: YES];
2125                         break;
2126     }
2127     [self codecsPopUpChanged: NULL];
2128
2129     /* Add/replace to the correct extension */
2130     if( [string characterAtIndex: [string length] - 4] == '.' )
2131     {
2132         [fDstFile2Field setStringValue: [NSString stringWithFormat:
2133             @"%@.%s", [string substringToIndex: [string length] - 4],
2134             ext]];
2135     }
2136     else
2137     {
2138         [fDstFile2Field setStringValue: [NSString stringWithFormat:
2139             @"%@.%s", string, ext]];
2140     }
2141
2142         /* changing the format may mean that we can / can't offer mono or 6ch, */
2143         /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
2144         [self audioTrackPopUpChanged: fAudLang1PopUp];
2145         [self audioTrackPopUpChanged: fAudLang2PopUp];
2146         /* We call the method to properly enable/disable turbo 2 pass */
2147         [self twoPassCheckboxChanged: sender];
2148         /* We call method method to change UI to reflect whether a preset is used or not*/
2149         [self customSettingUsed: sender];       
2150         
2151 }
2152
2153 - (IBAction) codecsPopUpChanged: (id) sender
2154 {
2155     int format = [fDstFormatPopUp indexOfSelectedItem];
2156     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2157         
2158     [fAdvancedOptions setHidden:YES];
2159
2160     /* Update the encoder popup*/
2161     if( ( FormatSettings[format][codecs] & HB_VCODEC_X264 ) )
2162     {
2163         /* MPEG-4 -> H.264 */
2164         [fVidEncoderPopUp removeAllItems];
2165                 [fVidEncoderPopUp addItemWithTitle: @"x264 (h.264 Main)"];
2166                 [fVidEncoderPopUp addItemWithTitle: @"x264 (h.264 iPod)"];
2167                 [fVidEncoderPopUp selectItemAtIndex: 0];
2168         [fAdvancedOptions setHidden:NO];
2169     }
2170     
2171     else if( ( FormatSettings[format][codecs] & HB_VCODEC_FFMPEG ) )
2172     {
2173         /* H.264 -> MPEG-4 */
2174         [fVidEncoderPopUp removeAllItems];
2175         [fVidEncoderPopUp addItemWithTitle: @"FFmpeg"];
2176         [fVidEncoderPopUp addItemWithTitle: @"XviD"];
2177         [fVidEncoderPopUp selectItemAtIndex: 0];
2178                                 
2179     }
2180
2181     if( FormatSettings[format][codecs] & HB_ACODEC_AC3 )
2182     {
2183         /* AC-3 pass-through: disable samplerate and bitrate */
2184         [fAudRatePopUp    setEnabled: NO];
2185         [fAudBitratePopUp setEnabled: NO];
2186     }
2187     else
2188     {
2189         [fAudRatePopUp    setEnabled: YES];
2190         [fAudBitratePopUp setEnabled: YES];
2191     }
2192     /* changing the codecs on offer may mean that we can / can't offer mono or 6ch, */
2193         /* so call audioTrackPopUpChanged for both audio tracks to update the mixdown popups */
2194         [self audioTrackPopUpChanged: fAudLang1PopUp];
2195         [self audioTrackPopUpChanged: fAudLang2PopUp];
2196
2197     [self calculateBitrate: sender];
2198     [self twoPassCheckboxChanged: sender];
2199 }
2200
2201 - (IBAction) encoderPopUpChanged: (id) sender
2202 {
2203     
2204         /* Check to see if we need to modify the job pic values based on x264 (iPod) encoder selection */
2205     if ([fDstFormatPopUp indexOfSelectedItem] == 0 && [fDstCodecsPopUp indexOfSelectedItem] == 1 && [fVidEncoderPopUp indexOfSelectedItem] == 1)
2206     {
2207                 hb_job_t * job = fTitle->job;
2208                 job->pixel_ratio = 0 ;
2209                 
2210                 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DefaultPicSizeAutoiPod"] > 0)
2211                 {
2212                         
2213                         if (job->width > 640)
2214                         {
2215                                 job->width = 640;
2216                         }
2217                         job->keep_ratio = 1;
2218                         hb_fix_aspect( job, HB_KEEP_WIDTH );
2219                         
2220                 }
2221                 /* Make sure the 64bit formatting checkbox is off */
2222                 [fDstMpgLargeFileCheck setState: NSOffState];
2223         }
2224     
2225         [self calculatePictureSizing: sender];
2226         [self twoPassCheckboxChanged: sender];
2227 }
2228
2229 - (IBAction) twoPassCheckboxChanged: (id) sender
2230 {
2231         /* check to see if x264 is chosen */
2232         int format = [fDstFormatPopUp indexOfSelectedItem];
2233     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2234         if( ( FormatSettings[format][codecs] & HB_VCODEC_X264 ) )
2235     {
2236                 if( [fVidTwoPassCheck state] == NSOnState)
2237                 {
2238                         [fVidTurboPassCheck setHidden: NO];
2239                 }
2240                 else
2241                 {
2242                         [fVidTurboPassCheck setHidden: YES];
2243                         [fVidTurboPassCheck setState: NSOffState];
2244                 }
2245                 /* Make sure Two Pass is checked if Turbo is checked */
2246                 if( [fVidTurboPassCheck state] == NSOnState)
2247                 {
2248                         [fVidTwoPassCheck setState: NSOnState];
2249                 }
2250         }
2251         else
2252         {
2253                 [fVidTurboPassCheck setHidden: YES];
2254                 [fVidTurboPassCheck setState: NSOffState];
2255         }
2256         
2257         /* We call method method to change UI to reflect whether a preset is used or not*/
2258         [self customSettingUsed: sender];
2259 }
2260
2261 - (IBAction ) videoFrameRateChanged: (id) sender
2262 {
2263     /* We call method method to calculatePictureSizing to error check detelecine*/
2264     [self calculatePictureSizing: sender];
2265
2266     /* We call method method to change UI to reflect whether a preset is used or not*/
2267         [self customSettingUsed: sender];
2268 }
2269
2270 - (IBAction) setEnabledStateOfAudioMixdownControls: (id) sender
2271 {
2272
2273     /* enable/disable the mixdown text and popupbutton for audio track 1 */
2274     [fAudTrack1MixPopUp setEnabled: ([fAudLang1PopUp indexOfSelectedItem] == 0) ? NO : YES];
2275     [fAudTrack1MixLabel setTextColor: ([fAudLang1PopUp indexOfSelectedItem] == 0) ?
2276         [NSColor disabledControlTextColor] : [NSColor controlTextColor]];
2277
2278     /* enable/disable the mixdown text and popupbutton for audio track 2 */
2279     [fAudTrack2MixPopUp setEnabled: ([fAudLang2PopUp indexOfSelectedItem] == 0) ? NO : YES];
2280     [fAudTrack2MixLabel setTextColor: ([fAudLang2PopUp indexOfSelectedItem] == 0) ?
2281         [NSColor disabledControlTextColor] : [NSColor controlTextColor]];
2282
2283 }
2284
2285 - (IBAction) addAllAudioTracksToPopUp: (id) sender
2286 {
2287
2288     hb_list_t  * list  = hb_get_titles( fHandle );
2289     hb_title_t * title = (hb_title_t*)
2290         hb_list_item( list, [fSrcTitlePopUp indexOfSelectedItem] );
2291
2292         hb_audio_t * audio;
2293
2294     [sender removeAllItems];
2295     [sender addItemWithTitle: _( @"None" )];
2296     for( int i = 0; i < hb_list_count( title->list_audio ); i++ )
2297     {
2298         audio = (hb_audio_t *) hb_list_item( title->list_audio, i );
2299         [[sender menu] addItemWithTitle:
2300             [NSString stringWithCString: audio->lang]
2301             action: NULL keyEquivalent: @""];
2302     }
2303     [sender selectItemAtIndex: 0];
2304
2305 }
2306
2307 - (IBAction) selectAudioTrackInPopUp: (id) sender searchPrefixString: (NSString *) searchPrefixString selectIndexIfNotFound: (int) selectIndexIfNotFound
2308 {
2309
2310     /* this method can be used to find a language, or a language-and-source-format combination, by passing in the appropriate string */
2311     /* e.g. to find the first French track, pass in an NSString * of "Francais" */
2312     /* e.g. to find the first English 5.1 AC3 track, pass in an NSString * of "English (AC3) (5.1 ch)" */
2313     /* if no matching track is found, then selectIndexIfNotFound is used to choose which track to select instead */
2314     
2315         if (searchPrefixString != NULL) 
2316         {
2317
2318         for( int i = 0; i < [sender numberOfItems]; i++ )
2319         {
2320             /* Try to find the desired search string */
2321             if ([[[sender itemAtIndex: i] title] hasPrefix:searchPrefixString])
2322             {
2323                 [sender selectItemAtIndex: i];
2324                 return;
2325             }
2326         }
2327         /* couldn't find the string, so select the requested "search string not found" item */
2328         /* index of 0 means select the "none" item */
2329         /* index of 1 means select the first audio track */
2330         [sender selectItemAtIndex: selectIndexIfNotFound];
2331         }
2332     else
2333     {
2334         /* if no search string is provided, then select the selectIndexIfNotFound item */
2335         [sender selectItemAtIndex: selectIndexIfNotFound];
2336     }
2337
2338 }
2339
2340 - (IBAction) audioTrackPopUpChanged: (id) sender
2341 {
2342     /* utility function to call audioTrackPopUpChanged without passing in a mixdown-to-use */
2343     [self audioTrackPopUpChanged: sender mixdownToUse: 0];
2344 }
2345
2346 - (IBAction) audioTrackPopUpChanged: (id) sender mixdownToUse: (int) mixdownToUse
2347 {
2348
2349     /* make sure we have a selected title before continuing */
2350     if (fTitle == NULL) return;
2351
2352     /* find out if audio track 1 or 2 was changed - this is passed to us in the tag of the sender */
2353     /* the sender will have been either fAudLang1PopUp (tag = 0) or fAudLang2PopUp (tag = 1) */
2354     int thisAudio = [sender tag];
2355
2356     /* get the index of the selected audio */
2357     int thisAudioIndex = [sender indexOfSelectedItem] - 1;
2358
2359     /* Handbrake can't currently cope with ripping the same source track twice */
2360     /* So, if this audio is also selected in the other audio track popup, set that popup's selection to "none" */
2361     /* get a reference to the two audio track popups */
2362     NSPopUpButton * thisAudioPopUp  = (thisAudio == 1 ? fAudLang2PopUp : fAudLang1PopUp);
2363     NSPopUpButton * otherAudioPopUp = (thisAudio == 1 ? fAudLang1PopUp : fAudLang2PopUp);
2364     /* if the same track is selected in the other audio popup, then select "none" in that popup */
2365     /* unless, of course, both are selected as "none!" */
2366     if ([thisAudioPopUp indexOfSelectedItem] != 0 && [thisAudioPopUp indexOfSelectedItem] == [otherAudioPopUp indexOfSelectedItem]) {
2367         [otherAudioPopUp selectItemAtIndex: 0];
2368         [self audioTrackPopUpChanged: otherAudioPopUp];
2369     }
2370
2371     /* pointer for the hb_audio_s struct we will use later on */
2372     hb_audio_t * audio;
2373
2374     /* find out what the currently-selected output audio codec is */
2375     int format = [fDstFormatPopUp indexOfSelectedItem];
2376     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2377     int acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
2378
2379     /* pointer to this track's mixdown NSPopUpButton */
2380     NSTextField   * mixdownTextField;
2381     NSPopUpButton * mixdownPopUp;
2382
2383     /* find our mixdown NSTextField and NSPopUpButton */
2384     if (thisAudio == 0)
2385     {
2386         mixdownTextField = fAudTrack1MixLabel;
2387         mixdownPopUp = fAudTrack1MixPopUp;
2388     }
2389     else
2390     {
2391         mixdownTextField = fAudTrack2MixLabel;
2392         mixdownPopUp = fAudTrack2MixPopUp;
2393     }
2394
2395     /* delete the previous audio mixdown options */
2396     [mixdownPopUp removeAllItems];
2397
2398     /* check if the audio mixdown controls need their enabled state changing */
2399     [self setEnabledStateOfAudioMixdownControls: NULL];
2400
2401     if (thisAudioIndex != -1)
2402     {
2403
2404         /* get the audio */
2405         audio = (hb_audio_t *) hb_list_item( fTitle->list_audio, thisAudioIndex );
2406         if (audio != NULL)
2407         {
2408
2409             /* find out if our selected output audio codec supports mono and / or 6ch */
2410             /* we also check for an input codec of AC3 or DCA,
2411                as they are the only libraries able to do the mixdown to mono / conversion to 6-ch */
2412             /* audioCodecsSupportMono and audioCodecsSupport6Ch are the same for now,
2413                but this may change in the future, so they are separated for flexibility */
2414             int audioCodecsSupportMono = ((audio->codec == HB_ACODEC_AC3 ||
2415                 audio->codec == HB_ACODEC_DCA) && acodec == HB_ACODEC_FAAC);
2416             int audioCodecsSupport6Ch =  ((audio->codec == HB_ACODEC_AC3 ||
2417                 audio->codec == HB_ACODEC_DCA) && acodec == HB_ACODEC_FAAC);
2418
2419             /* check for AC-3 passthru */
2420             if (audio->codec == HB_ACODEC_AC3 && acodec == HB_ACODEC_AC3)
2421             {
2422                     [[mixdownPopUp menu] addItemWithTitle:
2423                         [NSString stringWithCString: "AC3 Passthru"]
2424                         action: NULL keyEquivalent: @""];
2425             }
2426             else
2427             {
2428
2429                 /* add the appropriate audio mixdown menuitems to the popupbutton */
2430                 /* in each case, we set the new menuitem's tag to be the amixdown value for that mixdown,
2431                    so that we can reference the mixdown later */
2432
2433                 /* keep a track of the min and max mixdowns we used, so we can select the best match later */
2434                 int minMixdownUsed = 0;
2435                 int maxMixdownUsed = 0;
2436                 
2437                 /* get the input channel layout without any lfe channels */
2438                 int layout = audio->input_channel_layout & HB_INPUT_CH_LAYOUT_DISCRETE_NO_LFE_MASK;
2439
2440                 /* do we want to add a mono option? */
2441                 if (audioCodecsSupportMono == 1) {
2442                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2443                         [NSString stringWithCString: hb_audio_mixdowns[0].human_readable_name]
2444                         action: NULL keyEquivalent: @""];
2445                     [menuItem setTag: hb_audio_mixdowns[0].amixdown];
2446                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[0].amixdown;
2447                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[0].amixdown);
2448                 }
2449
2450                 /* do we want to add a stereo option? */
2451                 /* offer stereo if we have a mono source and non-mono-supporting codecs, as otherwise we won't have a mixdown at all */
2452                 /* also offer stereo if we have a stereo-or-better source */
2453                 if ((layout == HB_INPUT_CH_LAYOUT_MONO && audioCodecsSupportMono == 0) || layout >= HB_INPUT_CH_LAYOUT_STEREO) {
2454                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2455                         [NSString stringWithCString: hb_audio_mixdowns[1].human_readable_name]
2456                         action: NULL keyEquivalent: @""];
2457                     [menuItem setTag: hb_audio_mixdowns[1].amixdown];
2458                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[1].amixdown;
2459                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[1].amixdown);
2460                 }
2461
2462                 /* do we want to add a dolby surround (DPL1) option? */
2463                 if (layout == HB_INPUT_CH_LAYOUT_3F1R || layout == HB_INPUT_CH_LAYOUT_3F2R || layout == HB_INPUT_CH_LAYOUT_DOLBY) {
2464                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2465                         [NSString stringWithCString: hb_audio_mixdowns[2].human_readable_name]
2466                         action: NULL keyEquivalent: @""];
2467                     [menuItem setTag: hb_audio_mixdowns[2].amixdown];
2468                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[2].amixdown;
2469                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[2].amixdown);
2470                 }
2471
2472                 /* do we want to add a dolby pro logic 2 (DPL2) option? */
2473                 if (layout == HB_INPUT_CH_LAYOUT_3F2R) {
2474                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2475                         [NSString stringWithCString: hb_audio_mixdowns[3].human_readable_name]
2476                         action: NULL keyEquivalent: @""];
2477                     [menuItem setTag: hb_audio_mixdowns[3].amixdown];
2478                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[3].amixdown;
2479                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[3].amixdown);
2480                 }
2481
2482                 /* do we want to add a 6-channel discrete option? */
2483                 if (audioCodecsSupport6Ch == 1 && layout == HB_INPUT_CH_LAYOUT_3F2R && (audio->input_channel_layout & HB_INPUT_CH_LAYOUT_HAS_LFE)) {
2484                     id<NSMenuItem> menuItem = [[mixdownPopUp menu] addItemWithTitle:
2485                         [NSString stringWithCString: hb_audio_mixdowns[4].human_readable_name]
2486                         action: NULL keyEquivalent: @""];
2487                     [menuItem setTag: hb_audio_mixdowns[4].amixdown];
2488                     if (minMixdownUsed == 0) minMixdownUsed = hb_audio_mixdowns[4].amixdown;
2489                     maxMixdownUsed = MAX(maxMixdownUsed, hb_audio_mixdowns[4].amixdown);
2490                 }
2491
2492                 /* auto-select the best mixdown based on our saved mixdown preference */
2493                 
2494                 /* for now, this is hard-coded to a "best" mixdown of HB_AMIXDOWN_DOLBYPLII */
2495                 /* ultimately this should be a prefs option */
2496                 int useMixdown;
2497                 
2498                 /* if we passed in a mixdown to use - in order to load a preset - then try and use it */
2499                 if (mixdownToUse > 0)
2500                 {
2501                     useMixdown = mixdownToUse;
2502                 }
2503                 else
2504                 {
2505                     useMixdown = HB_AMIXDOWN_DOLBYPLII;
2506                 }
2507                 
2508                 /* if useMixdown > maxMixdownUsed, then use maxMixdownUsed */
2509                 if (useMixdown > maxMixdownUsed) useMixdown = maxMixdownUsed;
2510
2511                 /* if useMixdown < minMixdownUsed, then use minMixdownUsed */
2512                 if (useMixdown < minMixdownUsed) useMixdown = minMixdownUsed;
2513
2514                 /* select the (possibly-amended) preferred mixdown */
2515                 [mixdownPopUp selectItemWithTag: useMixdown];
2516                                 
2517                                 /* lets call the audioTrackMixdownChanged method here to determine appropriate bitrates, etc. */
2518                 [self audioTrackMixdownChanged: NULL];
2519             }
2520
2521         }
2522         
2523     }
2524
2525         /* see if the new audio track choice will change the bitrate we need */
2526     [self calculateBitrate: sender];    
2527
2528 }
2529 - (IBAction) audioTrackMixdownChanged: (id) sender
2530 {
2531
2532     /* find out what the currently-selected output audio codec is */
2533     int format = [fDstFormatPopUp indexOfSelectedItem];
2534     int codecs = [fDstCodecsPopUp indexOfSelectedItem];
2535     int acodec = FormatSettings[format][codecs] & HB_ACODEC_MASK;
2536     
2537     /* storage variable for the min and max bitrate allowed for this codec */
2538     int minbitrate;
2539     int maxbitrate;
2540     
2541     switch( acodec )
2542     {
2543         case HB_ACODEC_FAAC:
2544             /* check if we have a 6ch discrete conversion in either audio track */
2545             if ([[fAudTrack1MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH || [[fAudTrack2MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH)
2546             {
2547                 /* FAAC is happy using our min bitrate of 32 kbps, even for 6ch */
2548                 minbitrate = 32;
2549                 /* If either mixdown popup includes 6-channel discrete, then allow up to 384 kbps */
2550                 maxbitrate = 384;
2551                 break;
2552             }
2553             else
2554             {
2555                 /* FAAC is happy using our min bitrate of 32 kbps for stereo or mono */
2556                 minbitrate = 32;
2557                 /* FAAC won't honour anything more than 160 for stereo, so let's not offer it */
2558                 /* note: haven't dealt with mono separately here, FAAC will just use the max it can */
2559                 maxbitrate = 160;
2560                 break;
2561             }
2562
2563         case HB_ACODEC_LAME:
2564             /* Lame is happy using our min bitrate of 32 kbps */
2565             minbitrate = 32;
2566             /* Lame won't encode if the bitrate is higher than 320 kbps */
2567             maxbitrate = 320;
2568             break;
2569
2570         case HB_ACODEC_VORBIS:
2571             /* Vorbis causes a crash if we use a bitrate below 48 kbps */
2572             minbitrate = 48;
2573             /* Vorbis can cope with 384 kbps quite happily, even for stereo */
2574             maxbitrate = 384;
2575             break;
2576
2577         default:
2578             /* AC3 passthru disables the bitrate dropdown anyway, so we might as well just use the min and max bitrate */
2579             minbitrate = 32;
2580             maxbitrate = 384;
2581         
2582     }
2583
2584     [fAudBitratePopUp removeAllItems];
2585
2586     for( int i = 0; i < hb_audio_bitrates_count; i++ )
2587     {
2588         if (hb_audio_bitrates[i].rate >= minbitrate && hb_audio_bitrates[i].rate <= maxbitrate)
2589         {
2590             /* add a new menuitem for this bitrate */
2591             id<NSMenuItem> menuItem = [[fAudBitratePopUp menu] addItemWithTitle:
2592                 [NSString stringWithCString: hb_audio_bitrates[i].string]
2593                 action: NULL keyEquivalent: @""];
2594             /* set its tag to be the actual bitrate as an integer, so we can retrieve it later */
2595             [menuItem setTag: hb_audio_bitrates[i].rate];
2596         }
2597     }
2598
2599     /* select the default bitrate (but use 384 for 6-ch AAC) */
2600     if ([[fAudTrack1MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH || [[fAudTrack2MixPopUp selectedItem] tag] == HB_AMIXDOWN_6CH)
2601     {
2602         [fAudBitratePopUp selectItemWithTag: 384];
2603     }
2604     else
2605     {
2606         [fAudBitratePopUp selectItemWithTag: hb_audio_bitrates[hb_audio_bitrates_default].rate];
2607     }
2608
2609 }
2610
2611 - (IBAction) subtitleSelectionChanged: (id) sender
2612 {
2613         if ([fSubPopUp indexOfSelectedItem] == 0)
2614         {
2615         [fSubForcedCheck setState: NSOffState];
2616         [fSubForcedCheck setEnabled: NO];       
2617         }
2618         else
2619         {
2620         [fSubForcedCheck setEnabled: YES];      
2621         }
2622         
2623 }
2624
2625 /* lets set the picture size back to the max from right after title scan
2626    Lets use an IBAction here as down the road we could always use a checkbox
2627    in the gui to easily take the user back to max. Remember, the compiler
2628    resolves IBActions down to -(void) during compile anyway */
2629 - (IBAction) revertPictureSizeToMax: (id) sender
2630 {
2631         hb_job_t * job = fTitle->job;
2632         /* We use the output picture width and height
2633         as calculated from libhb right after title is set
2634         in TitlePopUpChanged */
2635         job->width = PicOrigOutputWidth;
2636         job->height = PicOrigOutputHeight;
2637     [fPictureController setAutoCrop:YES];
2638         /* Here we use the auto crop values determined right after scan */
2639         job->crop[0] = AutoCropTop;
2640         job->crop[1] = AutoCropBottom;
2641         job->crop[2] = AutoCropLeft;
2642         job->crop[3] = AutoCropRight;
2643                                 
2644                                 
2645                                 [self calculatePictureSizing: sender];
2646                                 /* We call method method to change UI to reflect whether a preset is used or not*/    
2647                                 [self customSettingUsed: sender];
2648 }
2649
2650
2651 /* Get and Display Current Pic Settings in main window */
2652 - (IBAction) calculatePictureSizing: (id) sender
2653 {
2654         [fPicSettingWidth setStringValue: [NSString stringWithFormat:@"%d", fTitle->job->width]];
2655         [fPicSettingHeight setStringValue: [NSString stringWithFormat:@"%d", fTitle->job->height]];
2656                 
2657         if (fTitle->job->pixel_ratio == 1)
2658         {
2659         int titlewidth = fTitle->width-fTitle->job->crop[2]-fTitle->job->crop[3];
2660         int arpwidth = fTitle->job->pixel_aspect_width;
2661         int arpheight = fTitle->job->pixel_aspect_height;
2662         int displayparwidth = titlewidth * arpwidth / arpheight;
2663         int displayparheight = fTitle->height-fTitle->job->crop[0]-fTitle->job->crop[1];
2664         
2665         [fPicSettingWidth setStringValue: [NSString stringWithFormat:@"%d", titlewidth]];
2666         [fPicSettingHeight setStringValue: [NSString stringWithFormat:@"%d", displayparheight]];
2667         [fPicLabelPAROutputX setStringValue: @"x"];
2668         [fPicSettingPARWidth setStringValue: [NSString stringWithFormat:@"%d", displayparwidth]];
2669         [fPicSettingPARHeight setStringValue: [NSString stringWithFormat:@"%d", displayparheight]];
2670         
2671         fTitle->job->keep_ratio = 0;
2672         }
2673         else
2674         {
2675         [fPicLabelPAROutputX setStringValue: @""];
2676         [fPicSettingPARWidth setStringValue: @""];
2677         [fPicSettingPARHeight setStringValue:  @""];
2678         }
2679                                 
2680         /* Set ON/Off values for the deinterlace/keep aspect ratio according to boolean */      
2681         if (fTitle->job->keep_ratio > 0)
2682         {
2683                 [fPicSettingARkeep setStringValue: @"On"];
2684         }
2685         else
2686         {
2687                 [fPicSettingARkeep setStringValue: @"Off"];
2688         }       
2689     /* Detelecine */
2690     if ([fPictureController detelecine]) {
2691         [fPicSettingDetelecine setStringValue: @"Yes"];
2692     }
2693     else {
2694         [fPicSettingDetelecine setStringValue: @"No"];
2695     }
2696
2697         /* Deinterlace */
2698         if ([fPictureController deinterlace] == 0)
2699         {
2700                 [fPicSettingDeinterlace setStringValue: @"Off"];
2701         }
2702         else if ([fPictureController deinterlace] == 1)
2703         {
2704                 [fPicSettingDeinterlace setStringValue: @"Fast"];
2705         }
2706         else if ([fPictureController deinterlace] == 2)
2707         {
2708                 [fPicSettingDeinterlace setStringValue: @"Slow"];
2709         }
2710         else if ([fPictureController deinterlace] == 3)
2711         {
2712                 [fPicSettingDeinterlace setStringValue: @"Slower"];
2713         }
2714         else if ([fPictureController deinterlace] ==4)
2715         {
2716                 [fPicSettingDeinterlace setStringValue: @"Slowest"];
2717         }
2718         /* Denoise */
2719         if ([fPictureController denoise] == 0)
2720         {
2721                 [fPicSettingDenoise setStringValue: @"Off"];
2722         }
2723         else if ([fPictureController denoise] == 1)
2724         {
2725                 [fPicSettingDenoise setStringValue: @"Weak"];
2726         }
2727         else if ([fPictureController denoise] == 2)
2728         {
2729                 [fPicSettingDenoise setStringValue: @"Medium"];
2730         }
2731         else if ([fPictureController denoise] == 3)
2732         {
2733                 [fPicSettingDenoise setStringValue: @"Strong"];
2734         }
2735         
2736         if (fTitle->job->pixel_ratio > 0)
2737         {
2738                 [fPicSettingPAR setStringValue: @""];
2739         }
2740         else
2741         {
2742                 [fPicSettingPAR setStringValue: @"Off"];
2743         }
2744         /* Set the display field for crop as per boolean */
2745         if (![fPictureController autoCrop])
2746         {
2747             [fPicSettingAutoCrop setStringValue: @"Custom"];
2748         }
2749         else
2750         {
2751                 [fPicSettingAutoCrop setStringValue: @"Auto"];
2752         }       
2753         /* check video framerate and turn off detelecine if necessary */
2754         if (fTitle->rate_base == 1126125 || [[fVidRatePopUp titleOfSelectedItem] isEqualToString: @"23.976 (NTSC Film)"])
2755         {
2756                 [fPictureController setDetelecine:0];
2757         }
2758         
2759         
2760         
2761         /* below will trigger the preset, if selected, to be
2762         changed to "Custom". Lets comment out for now until
2763         we figure out a way to determine if the picture values
2764         changed modify the preset values */     
2765         //[self customSettingUsed: sender];
2766 }
2767
2768 - (IBAction) calculateBitrate: (id) sender
2769 {
2770     if( !fHandle || [fVidQualityMatrix selectedRow] != 0 || !SuccessfulScan )
2771     {
2772         return;
2773     }
2774
2775     hb_list_t  * list  = hb_get_titles( fHandle );
2776     hb_title_t * title = (hb_title_t *) hb_list_item( list,
2777             [fSrcTitlePopUp indexOfSelectedItem] );
2778     hb_job_t * job = title->job;
2779
2780     [self PrepareJob];
2781
2782     [fVidBitrateField setIntValue: hb_calc_bitrate( job,
2783             [fVidTargetSizeField intValue] )];
2784 }
2785
2786 /* Method to determine if we should change the UI
2787 To reflect whether or not a Preset is being used or if
2788 the user is using "Custom" settings by determining the sender*/
2789 - (IBAction) customSettingUsed: (id) sender
2790 {
2791         if ([sender stringValue] != NULL)
2792         {
2793                 /* Deselect the currently selected Preset if there is one*/
2794                 [tableView deselectRow:[tableView selectedRow]];
2795                 [[fPresetsActionMenu itemAtIndex:0] setEnabled: NO];
2796                 /* Change UI to show "Custom" settings are being used */
2797                 [fPresetSelectedDisplay setStringValue: @"Custom"];
2798                 
2799                 curUserPresetChosenNum = nil;
2800         }
2801
2802 }
2803
2804    /* We use this method to recreate new, updated factory
2805    presets */
2806 - (IBAction)addFactoryPresets:(id)sender
2807 {
2808     /* First, we delete any existing built in presets */
2809     [self deleteFactoryPresets: sender];
2810     /* Then, we re-create new built in presets programmatically CreateIpodOnlyPreset*/
2811     [UserPresets addObject:[self createNormalPreset]];
2812     [UserPresets addObject:[self createClassicPreset]];
2813     [UserPresets addObject:[self createQuickTimePreset]];
2814         [UserPresets addObject:[self createIpodLowPreset]];
2815         [UserPresets addObject:[self createIpodHighPreset]];
2816         [UserPresets addObject:[self createAppleTVPreset]];
2817     [UserPresets addObject:[self createiPhonePreset]];
2818         [UserPresets addObject:[self createPSThreePreset]];
2819         [UserPresets addObject:[self createPSPPreset]];
2820         [UserPresets addObject:[self createFilmPreset]];
2821     [UserPresets addObject:[self createTelevisionPreset]];
2822     [UserPresets addObject:[self createAnimationPreset]];
2823     [UserPresets addObject:[self createBedlamPreset]];
2824     [UserPresets addObject:[self createDeuxSixQuatrePreset]];
2825     [UserPresets addObject:[self createBrokePreset]];
2826     [UserPresets addObject:[self createBlindPreset]];
2827     [UserPresets addObject:[self createCRFPreset]];
2828     
2829     [self addPreset];
2830 }
2831 - (IBAction)deleteFactoryPresets:(id)sender
2832 {
2833     //int status;
2834     NSEnumerator *enumerator = [UserPresets objectEnumerator];
2835         id tempObject;
2836     
2837         //NSNumber *index;
2838     NSMutableArray *tempArray;
2839
2840
2841         tempArray = [NSMutableArray array];
2842         /* we look here to see if the preset is we move on to the next one */
2843         while ( tempObject = [enumerator nextObject] )  
2844                 {
2845                         /* if the preset is "Factory" then we put it in the array of
2846                         presets to delete */
2847                         if ([[tempObject objectForKey:@"Type"] intValue] == 0)
2848                         {
2849                                 [tempArray addObject:tempObject];
2850                         }
2851         }
2852         
2853         [UserPresets removeObjectsInArray:tempArray];
2854         [tableView reloadData];
2855         [self savePreset];   
2856
2857 }
2858
2859 - (IBAction) showAddPresetPanel: (id) sender
2860 {
2861     /* Deselect the currently selected Preset if there is one*/
2862     [tableView deselectRow:[tableView selectedRow]];
2863
2864     /* Populate the preset picture settings popup here */
2865     [fPresetNewPicSettingsPopUp removeAllItems];
2866     [fPresetNewPicSettingsPopUp addItemWithTitle:@"None"];
2867     [fPresetNewPicSettingsPopUp addItemWithTitle:@"Current"];
2868     [fPresetNewPicSettingsPopUp addItemWithTitle:@"Source Maximum (post source scan)"];
2869     [fPresetNewPicSettingsPopUp selectItemAtIndex: 0];  
2870         
2871     /* Erase info from the input fields fPresetNewDesc*/
2872         [fPresetNewName setStringValue: @""];
2873         [fPresetNewDesc setStringValue: @""];
2874         /* Show the panel */
2875         [NSApp beginSheet: fAddPresetPanel modalForWindow: fWindow modalDelegate: NULL didEndSelector: NULL contextInfo: NULL];
2876 }
2877
2878 - (IBAction) closeAddPresetPanel: (id) sender
2879 {
2880     [NSApp endSheet: fAddPresetPanel];
2881     [fAddPresetPanel orderOut: self];
2882 }
2883
2884 - (IBAction)addUserPreset:(id)sender
2885 {
2886     if (![[fPresetNewName stringValue] length])
2887             NSRunAlertPanel(@"Warning!", @"You need to insert a name for the preset.", @"OK", nil , nil);
2888     else
2889     {
2890         /* Here we create a custom user preset */
2891         [UserPresets addObject:[self createPreset]];
2892         [self addPreset];
2893         
2894         [self closeAddPresetPanel:NULL];
2895     }
2896 }
2897 - (void)addPreset
2898 {
2899
2900         
2901         /* We Sort the Presets By Factory or Custom */
2902         NSSortDescriptor * presetTypeDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"Type" 
2903                                                     ascending:YES] autorelease];
2904         /* We Sort the Presets Alphabetically by name */
2905         NSSortDescriptor * presetNameDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"PresetName" 
2906                                                     ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];
2907         NSArray *sortDescriptors=[NSArray arrayWithObjects:presetTypeDescriptor,presetNameDescriptor,nil];
2908         NSArray *sortedArray=[UserPresets sortedArrayUsingDescriptors:sortDescriptors];
2909         [UserPresets setArray:sortedArray];
2910         
2911         
2912         /* We Reload the New Table data for presets */
2913     [tableView reloadData];
2914    /* We save all of the preset data here */
2915     [self savePreset];
2916 }
2917
2918 - (IBAction)insertPreset:(id)sender
2919 {
2920     int index = [tableView selectedRow];
2921     [UserPresets insertObject:[self createPreset] atIndex:index];
2922     [tableView reloadData];
2923     [self savePreset];
2924 }
2925
2926 - (NSDictionary *)createPreset
2927 {
2928     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
2929         /* Get the New Preset Name from the field in the AddPresetPanel */
2930     [preset setObject:[fPresetNewName stringValue] forKey:@"PresetName"];
2931         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
2932         [preset setObject:[NSNumber numberWithInt:1] forKey:@"Type"];
2933         /*Set whether or not this is default, at creation set to 0*/
2934         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
2935         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
2936         [preset setObject:[NSNumber numberWithInt:[fPresetNewPicSettingsPopUp indexOfSelectedItem]] forKey:@"UsesPictureSettings"];
2937         /* Get New Preset Description from the field in the AddPresetPanel*/
2938         [preset setObject:[fPresetNewDesc stringValue] forKey:@"PresetDescription"];
2939         /* File Format */
2940     [preset setObject:[fDstFormatPopUp titleOfSelectedItem] forKey:@"FileFormat"];
2941         /* Chapter Markers fCreateChapterMarkers*/
2942         [preset setObject:[NSNumber numberWithInt:[fCreateChapterMarkers state]] forKey:@"ChapterMarkers"];
2943         /* Allow Mpeg4 64 bit formatting +4GB file sizes */
2944         [preset setObject:[NSNumber numberWithInt:[fDstMpgLargeFileCheck state]] forKey:@"Mp4LargeFile"];
2945         /* Codecs */
2946         [preset setObject:[fDstCodecsPopUp titleOfSelectedItem] forKey:@"FileCodecs"];
2947         /* Video encoder */
2948         [preset setObject:[fVidEncoderPopUp titleOfSelectedItem] forKey:@"VideoEncoder"];
2949         /* x264 Option String */
2950         [preset setObject:[fAdvancedOptions optionsString] forKey:@"x264Option"];
2951         
2952         [preset setObject:[NSNumber numberWithInt:[fVidQualityMatrix selectedRow]] forKey:@"VideoQualityType"];
2953         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
2954         [preset setObject:[fVidBitrateField stringValue] forKey:@"VideoAvgBitrate"];
2955         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
2956         
2957         /* Video framerate */
2958         [preset setObject:[fVidRatePopUp titleOfSelectedItem] forKey:@"VideoFramerate"];
2959         /* GrayScale */
2960         [preset setObject:[NSNumber numberWithInt:[fVidGrayscaleCheck state]] forKey:@"VideoGrayScale"];
2961         /* 2 Pass Encoding */
2962         [preset setObject:[NSNumber numberWithInt:[fVidTwoPassCheck state]] forKey:@"VideoTwoPass"];
2963         /* Turbo 2 pass Encoding fVidTurboPassCheck*/
2964         [preset setObject:[NSNumber numberWithInt:[fVidTurboPassCheck state]] forKey:@"VideoTurboTwoPass"];
2965         /*Picture Settings*/
2966         hb_job_t * job = fTitle->job;
2967         /* Basic Picture Settings */
2968         /* Use Max Picture settings for whatever the dvd is.*/
2969         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
2970         [preset setObject:[NSNumber numberWithInt:fTitle->job->width] forKey:@"PictureWidth"];
2971         [preset setObject:[NSNumber numberWithInt:fTitle->job->height] forKey:@"PictureHeight"];
2972         [preset setObject:[NSNumber numberWithInt:fTitle->job->keep_ratio] forKey:@"PictureKeepRatio"];
2973         [preset setObject:[NSNumber numberWithInt:[fPictureController deinterlace]] forKey:@"PictureDeinterlace"];
2974         [preset setObject:[NSNumber numberWithInt:fTitle->job->pixel_ratio] forKey:@"PicturePAR"];
2975         [preset setObject:[NSNumber numberWithInt:[fPictureController detelecine]] forKey:@"PictureDetelecine"];
2976         [preset setObject:[NSNumber numberWithInt:[fPictureController denoise]] forKey:@"PictureDenoise"]; 
2977         /* Set crop settings here */
2978         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
2979         [preset setObject:[NSNumber numberWithInt:[fPictureController autoCrop]] forKey:@"PictureAutoCrop"];
2980
2981         [preset setObject:[NSNumber numberWithInt:job->crop[0]] forKey:@"PictureTopCrop"];
2982     [preset setObject:[NSNumber numberWithInt:job->crop[1]] forKey:@"PictureBottomCrop"];
2983         [preset setObject:[NSNumber numberWithInt:job->crop[2]] forKey:@"PictureLeftCrop"];
2984         [preset setObject:[NSNumber numberWithInt:job->crop[3]] forKey:@"PictureRightCrop"];
2985         
2986         /*Audio*/
2987         /* Audio Sample Rate*/
2988         [preset setObject:[fAudRatePopUp titleOfSelectedItem] forKey:@"AudioSampleRate"];
2989         /* Audio Bitrate Rate*/
2990         [preset setObject:[fAudBitratePopUp titleOfSelectedItem] forKey:@"AudioBitRate"];
2991         /* Subtitles*/
2992         [preset setObject:[fSubPopUp titleOfSelectedItem] forKey:@"Subtitles"];
2993         
2994
2995     [preset autorelease];
2996     return preset;
2997
2998 }
2999
3000 - (NSDictionary *)createIpodLowPreset
3001 {
3002     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3003         /* Get the New Preset Name from the field in the AddPresetPanel */
3004     [preset setObject:@"iPod Low-Rez" forKey:@"PresetName"];
3005         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3006         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3007         /*Set whether or not this is default, at creation set to 0*/
3008         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3009         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3010         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3011         /* Get the New Preset Description from the field in the AddPresetPanel */
3012     [preset setObject:@"HandBrake's low resolution settings for the iPod. Optimized for great playback on the iPod screen, with smaller file size." forKey:@"PresetDescription"];
3013         /* File Format */
3014     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3015         /* Chapter Markers*/
3016          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3017     /* Codecs */
3018         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3019         /* Video encoder */
3020         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
3021         /* x264 Option String */
3022         [preset setObject:@"keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=768:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1" forKey:@"x264Option"];
3023         /* Video quality */
3024         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3025         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3026         [preset setObject:@"700" forKey:@"VideoAvgBitrate"];
3027         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3028         
3029         /* Video framerate */
3030         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3031         /* GrayScale */
3032         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3033         /* 2 Pass Encoding */
3034         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3035         
3036         /*Picture Settings*/
3037         //hb_job_t * job = fTitle->job;
3038         /* Basic Picture Settings */
3039         /* Use Max Picture settings for whatever the dvd is.*/
3040         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3041         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3042         [preset setObject:[NSNumber numberWithInt:320] forKey:@"PictureWidth"];
3043         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3044         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3045         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3046         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3047         /* Set crop settings here */
3048         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3049         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3050     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3051         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3052         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3053         
3054         /*Audio*/
3055         /* Audio Sample Rate*/
3056         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3057         /* Audio Bitrate Rate*/
3058         [preset setObject:@"160" forKey:@"AudioBitRate"];
3059         /* Subtitles*/
3060         [preset setObject:@"None" forKey:@"Subtitles"];
3061         
3062
3063     [preset autorelease];
3064     return preset;
3065
3066 }
3067
3068
3069 - (NSDictionary *)createIpodHighPreset
3070 {
3071     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3072         /* Get the New Preset Name from the field in the AddPresetPanel */
3073     [preset setObject:@"iPod High-Rez" forKey:@"PresetName"];
3074         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3075         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3076         /*Set whether or not this is default, at creation set to 0*/
3077         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3078         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3079         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3080         /* Get the New Preset Description from the field in the AddPresetPanel */
3081     [preset setObject:@"HandBrake's high resolution settings for the iPod. Good video quality, great for viewing on a TV using your iPod" forKey:@"PresetDescription"];
3082         /* File Format */
3083     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3084         /* Chapter Markers*/
3085          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3086     /* Codecs */
3087         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3088         /* Video encoder */
3089         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
3090         /* x264 Option String */
3091         [preset setObject:@"keyint=300:keyint-min=30:bframes=0:cabac=0:ref=1:vbv-maxrate=1500:vbv-bufsize=2000:analyse=all:me=umh:subme=6:no-fast-pskip=1" forKey:@"x264Option"];
3092         /* Video quality */
3093         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3094         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3095         [preset setObject:@"1500" forKey:@"VideoAvgBitrate"];
3096         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3097         
3098         /* Video framerate */
3099         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3100         /* GrayScale */
3101         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3102         /* 2 Pass Encoding */
3103         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3104         
3105         /*Picture Settings*/
3106         //hb_job_t * job = fTitle->job;
3107         /* Basic Picture Settings */
3108         /* Use Max Picture settings for whatever the dvd is.*/
3109         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3110         [preset setObject:[NSNumber numberWithInt:640] forKey:@"PictureWidth"];
3111         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3112         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3113         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3114         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3115         /* Set crop settings here */
3116         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3117         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3118         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3119     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3120         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3121         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3122         
3123         /*Audio*/
3124         /* Audio Sample Rate*/
3125         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3126         /* Audio Bitrate Rate*/
3127         [preset setObject:@"160" forKey:@"AudioBitRate"];
3128         /* Subtitles*/
3129         [preset setObject:@"None" forKey:@"Subtitles"];
3130         
3131
3132     [preset autorelease];
3133     return preset;
3134
3135 }
3136
3137 - (NSDictionary *)createAppleTVPreset
3138 {
3139     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3140         /* Get the New Preset Name from the field in the AddPresetPanel */
3141     [preset setObject:@"AppleTV" forKey:@"PresetName"];
3142         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
3143         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3144         /*Set whether or not this is default, at creation set to 0*/
3145         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3146         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3147         [preset setObject:[NSNumber numberWithInt:2] forKey:@"UsesPictureSettings"];
3148         /* Get the New Preset Description from the field in the AddPresetPanel */
3149     [preset setObject:@"HandBrake's settings for the AppleTV. Provides a good balance between quality and file size, and optimizes performance." forKey:@"PresetDescription"];
3150         /* File Format */
3151     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3152         /* Chapter Markers*/
3153          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3154         /* Codecs */
3155         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3156         /* Video encoder */
3157         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3158         /* x264 Option String (We can use this to tweak the appleTV output)*/
3159         [preset setObject:@"bframes=3:ref=1:subme=5:me=umh:no-fast-pskip=1:trellis=2:cabac=0" forKey:@"x264Option"];
3160         /* Video quality */
3161         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3162         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3163         [preset setObject:@"2500" forKey:@"VideoAvgBitrate"];
3164         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3165         
3166         /* Video framerate */
3167         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3168         /* GrayScale */
3169         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3170         /* 2 Pass Encoding */
3171         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3172         
3173         /*Picture Settings*/
3174         /* For AppleTV we only want to retain UsesMaxPictureSettings
3175         which depend on the source dvd picture settings, so we don't
3176         record the current dvd's picture info since it will vary from
3177         source to source*/
3178         //hb_job_t * job = fTitle->job;
3179         //hb_job_t * job = title->job;
3180         /* Basic Picture Settings */
3181         /* Use Max Picture settings for whatever the dvd is.*/
3182         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3183         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3184         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3185         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3186         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3187         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3188         /* Set crop settings here */
3189         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3190         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3191     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3192         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3193         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3194         
3195         /*Audio*/
3196         /* Audio Sample Rate*/
3197         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3198         /* Audio Bitrate Rate*/
3199         [preset setObject:@"160" forKey:@"AudioBitRate"];
3200         /* Subtitles*/
3201         [preset setObject:@"None" forKey:@"Subtitles"];
3202         
3203
3204     [preset autorelease];
3205     return preset;
3206
3207 }
3208
3209 - (NSDictionary *)createPSThreePreset
3210 {
3211     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3212         /* Get the New Preset Name from the field in the AddPresetPanel */
3213     [preset setObject:@"PS3" forKey:@"PresetName"];
3214         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
3215         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3216         /*Set whether or not this is default, at creation set to 0*/
3217         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3218         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3219         [preset setObject:[NSNumber numberWithInt:2] forKey:@"UsesPictureSettings"];
3220         /* Get the New Preset Description from the field in the AddPresetPanel */
3221     [preset setObject:@"HandBrake's settings for the Sony PlayStation 3." forKey:@"PresetDescription"];
3222         /* File Format */
3223     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3224         /* Chapter Markers*/
3225          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3226         /* Codecs */
3227         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3228         /* Video encoder */
3229         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3230         /* x264 Option String (We can use this to tweak the appleTV output)*/
3231         [preset setObject:@"level=41:subme=5:me=umh" forKey:@"x264Option"];
3232         /* Video quality */
3233         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3234         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3235         [preset setObject:@"2500" forKey:@"VideoAvgBitrate"];
3236         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3237         
3238         /* Video framerate */
3239         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3240         /* GrayScale */
3241         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3242         /* 2 Pass Encoding */
3243         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3244         
3245         /*Picture Settings*/
3246         /* For PS3 we only want to retain UsesMaxPictureSettings
3247         which depend on the source dvd picture settings, so we don't
3248         record the current dvd's picture info since it will vary from
3249         source to source*/
3250         /* Use Max Picture settings for whatever the dvd is.*/
3251         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3252         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3253         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3254         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3255         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3256         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3257         /* Set crop settings here */
3258         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3259         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3260     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3261         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3262         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3263         
3264         /*Audio*/
3265         /* Audio Sample Rate*/
3266         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3267         /* Audio Bitrate Rate*/
3268         [preset setObject:@"160" forKey:@"AudioBitRate"];
3269         /* Subtitles*/
3270         [preset setObject:@"None" forKey:@"Subtitles"];
3271         
3272
3273     [preset autorelease];
3274     return preset;
3275
3276 }
3277 - (NSDictionary *)createPSPPreset
3278 {
3279     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3280         /* Get the New Preset Name from the field in the AddPresetPanel */
3281     [preset setObject:@"PSP" forKey:@"PresetName"];
3282         /*Set whether or not this is a user preset where 0 is factory, 1 is user*/
3283         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3284         /*Set whether or not this is default, at creation set to 0*/
3285         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3286         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3287         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3288         /* Get the New Preset Description from the field in the AddPresetPanel */
3289     [preset setObject:@"HandBrake's settings for the Sony PlayStation Portable." forKey:@"PresetDescription"];
3290         /* File Format */
3291     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3292         /* Chapter Markers*/
3293          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3294         /* Codecs */
3295         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
3296         /* Video encoder */
3297         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
3298         /* x264 Option String (We can use this to tweak the appleTV output)*/
3299         [preset setObject:@"" forKey:@"x264Option"];
3300         /* Video quality */
3301         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3302         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3303         [preset setObject:@"1024" forKey:@"VideoAvgBitrate"];
3304         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3305         
3306         /* Video framerate */
3307         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3308         /* GrayScale */
3309         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3310         /* 2 Pass Encoding */
3311         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3312         
3313         /*Picture Settings*/
3314         /* For PS3 we only want to retain UsesMaxPictureSettings
3315         which depend on the source dvd picture settings, so we don't
3316         record the current dvd's picture info since it will vary from
3317         source to source*/
3318         /* Use Max Picture settings for whatever the dvd is.*/
3319         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3320         [preset setObject:@"368" forKey:@"PictureWidth"];
3321         [preset setObject:@"208" forKey:@"PictureHeight"];
3322         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3323         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3324         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3325         /* Set crop settings here */
3326         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3327         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3328         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3329     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3330         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3331         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3332         
3333         /*Audio*/
3334         /* Audio Sample Rate*/
3335         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3336         /* Audio Bitrate Rate*/
3337         [preset setObject:@"128" forKey:@"AudioBitRate"];
3338         /* Subtitles*/
3339         [preset setObject:@"None" forKey:@"Subtitles"];
3340         
3341
3342     [preset autorelease];
3343     return preset;
3344
3345 }
3346
3347 - (NSDictionary *)createNormalPreset
3348 {
3349     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3350         /* Get the New Preset Name from the field in the AddPresetPanel */
3351     [preset setObject:@"Normal" forKey:@"PresetName"];
3352         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3353         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3354         /*Set whether or not this is default, at creation set to 0*/
3355         [preset setObject:[NSNumber numberWithInt:1] forKey:@"Default"];
3356         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3357         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3358         /* Get the New Preset Description from the field in the AddPresetPanel */
3359     [preset setObject:@"HandBrake's normal, default settings." forKey:@"PresetDescription"];
3360         /* File Format */
3361     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3362         /* Chapter Markers*/
3363          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3364     /* Codecs */
3365         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3366         /* Video encoder */
3367         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3368         /* x264 Option String */
3369         [preset setObject:@"ref=2:bframes=2:subme=5:me=umh" forKey:@"x264Option"];
3370         /* Video quality */
3371         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3372         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3373         [preset setObject:@"1500" forKey:@"VideoAvgBitrate"];
3374         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3375         
3376         /* Video framerate */
3377         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3378         /* GrayScale */
3379         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3380         /* 2 Pass Encoding */
3381         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3382         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3383         
3384         /*Picture Settings*/
3385         //hb_job_t * job = fTitle->job;
3386         /* Basic Picture Settings */
3387         /* Use Max Picture settings for whatever the dvd is.*/
3388         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3389         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3390         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3391         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3392         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3393         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3394         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3395         /* Set crop settings here */
3396         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3397         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3398     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3399         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3400         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3401         
3402         /*Audio*/
3403         /* Audio Sample Rate*/
3404         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3405         /* Audio Bitrate Rate*/
3406         [preset setObject:@"160" forKey:@"AudioBitRate"];
3407         /* Subtitles*/
3408         [preset setObject:@"None" forKey:@"Subtitles"];
3409         
3410
3411     [preset autorelease];
3412     return preset;
3413
3414 }
3415
3416 - (NSDictionary *)createClassicPreset
3417 {
3418     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3419         /* Get the New Preset Name from the field in the AddPresetPanel */
3420     [preset setObject:@"Classic" forKey:@"PresetName"];
3421         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3422         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3423         /*Set whether or not this is default, at creation set to 0*/
3424         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3425         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3426         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3427         /* Get the New Preset Description from the field in the AddPresetPanel */
3428     [preset setObject:@"HandBrake's traditional, faster, lower-quality settings." forKey:@"PresetDescription"];
3429         /* File Format */
3430     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3431         /* Chapter Markers*/
3432          [preset setObject:[NSNumber numberWithInt:0] forKey:@"ChapterMarkers"];
3433     /* Codecs */
3434         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
3435         /* Video encoder */
3436         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
3437         /* x264 Option String */
3438         [preset setObject:@"" forKey:@"x264Option"];
3439         /* Video quality */
3440         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3441         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3442         [preset setObject:@"1000" forKey:@"VideoAvgBitrate"];
3443         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3444         
3445         /* Video framerate */
3446         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3447         /* GrayScale */
3448         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3449         /* 2 Pass Encoding */
3450         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3451         
3452         /*Picture Settings*/
3453         //hb_job_t * job = fTitle->job;
3454         /* Basic Picture Settings */
3455         /* Use Max Picture settings for whatever the dvd is.*/
3456         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3457         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3458         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3459         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3460         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3461         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3462         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3463         /* Set crop settings here */
3464         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3465         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3466     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3467         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3468         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3469         
3470         /*Audio*/
3471         /* Audio Sample Rate*/
3472         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3473         /* Audio Bitrate Rate*/
3474         [preset setObject:@"160" forKey:@"AudioBitRate"];
3475         /* Subtitles*/
3476         [preset setObject:@"None" forKey:@"Subtitles"];
3477         
3478
3479     [preset autorelease];
3480     return preset;
3481
3482 }
3483
3484 - (NSDictionary *)createFilmPreset
3485 {
3486     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3487         /* Get the New Preset Name from the field in the AddPresetPanel */
3488     [preset setObject:@"Film" forKey:@"PresetName"];
3489         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3490         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3491         /*Set whether or not this is default, at creation set to 0*/
3492         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3493         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3494         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3495         /* Get the New Preset Description from the field in the AddPresetPanel */
3496     [preset setObject:@"HandBrake's preset for feature films." forKey:@"PresetDescription"];
3497         /* File Format */
3498     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3499         /* Chapter Markers*/
3500          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3501     /* Codecs */
3502         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
3503         /* Video encoder */
3504         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3505         /* x264 Option String */
3506         [preset setObject:@"ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:no-fast-pskip" forKey:@"x264Option"];
3507         /* Video quality */
3508         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3509         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3510         [preset setObject:@"2000" forKey:@"VideoAvgBitrate"];
3511         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3512         
3513         /* Video framerate */
3514         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3515         /* GrayScale */
3516         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3517         /* 2 Pass Encoding */
3518         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3519         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3520         
3521         /*Picture Settings*/
3522         //hb_job_t * job = fTitle->job;
3523         /* Basic Picture Settings */
3524         /* Use Max Picture settings for whatever the dvd is.*/
3525         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3526         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3527         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3528         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3529         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3530         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3531         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3532         /* Set crop settings here */
3533         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3534         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3535     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3536         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3537         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3538         
3539         /*Audio*/
3540         /* Audio Sample Rate*/
3541         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3542         /* Audio Bitrate Rate*/
3543         [preset setObject:@"160" forKey:@"AudioBitRate"];
3544         /* Subtitles*/
3545         [preset setObject:@"None" forKey:@"Subtitles"];
3546         
3547
3548     [preset autorelease];
3549     return preset;
3550
3551 }
3552
3553 - (NSDictionary *)createTelevisionPreset
3554 {
3555     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3556         /* Get the New Preset Name from the field in the AddPresetPanel */
3557     [preset setObject:@"Television" forKey:@"PresetName"];
3558         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3559         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3560         /*Set whether or not this is default, at creation set to 0*/
3561         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3562         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3563         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3564         /* Get the New Preset Description from the field in the AddPresetPanel */
3565     [preset setObject:@"HandBrake's settings for video from television." forKey:@"PresetDescription"];
3566         /* File Format */
3567     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3568         /* Chapter Markers*/
3569          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3570     /* Codecs */
3571         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3572         /* Video encoder */
3573         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3574         /* x264 Option String */
3575         [preset setObject:@"ref=3:mixed-refs:bframes=6:bime:weightb:direct=auto:b-pyramid:me=umh:subme=6:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip" forKey:@"x264Option"];
3576         /* Video quality */
3577         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3578         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3579         [preset setObject:@"1300" forKey:@"VideoAvgBitrate"];
3580         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3581         
3582         /* Video framerate */
3583         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3584         /* GrayScale */
3585         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3586         /* 2 Pass Encoding */
3587         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3588         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3589         
3590         /*Picture Settings*/
3591         //hb_job_t * job = fTitle->job;
3592         /* Basic Picture Settings */
3593         /* Use Max Picture settings for whatever the dvd is.*/
3594         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3595         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3596         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3597         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3598         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3599         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureDeinterlace"];
3600         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3601         /* Set crop settings here */
3602         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3603         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3604     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3605         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3606         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3607         
3608         /*Audio*/
3609         /* Audio Sample Rate*/
3610         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3611         /* Audio Bitrate Rate*/
3612         [preset setObject:@"160" forKey:@"AudioBitRate"];
3613         /* Subtitles*/
3614         [preset setObject:@"None" forKey:@"Subtitles"];
3615         
3616
3617     [preset autorelease];
3618     return preset;
3619
3620 }
3621
3622 - (NSDictionary *)createAnimationPreset
3623 {
3624     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3625         /* Get the New Preset Name from the field in the AddPresetPanel */
3626     [preset setObject:@"Animation" forKey:@"PresetName"];
3627         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3628         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3629         /*Set whether or not this is default, at creation set to 0*/
3630         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3631         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3632         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3633         /* Get the New Preset Description from the field in the AddPresetPanel */
3634     [preset setObject:@"HandBrake's settings for cartoons, anime, and CGI." forKey:@"PresetDescription"];
3635         /* File Format */
3636     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3637         /* Chapter Markers*/
3638          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3639     /* Codecs */
3640         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3641         /* Video encoder */
3642         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3643         /* x264 Option String */
3644         [preset setObject:@"ref=5:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=5:analyse=all:8x8dct:trellis=1:nr=150:no-fast-pskip:filter=2,2" forKey:@"x264Option"];
3645         /* Video quality */
3646         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3647         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3648         [preset setObject:@"1000" forKey:@"VideoAvgBitrate"];
3649         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3650         
3651         /* Video framerate */
3652         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3653         /* GrayScale */
3654         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3655         /* 2 Pass Encoding */
3656         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3657         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3658         
3659         /*Picture Settings*/
3660         //hb_job_t * job = fTitle->job;
3661         /* Basic Picture Settings */
3662         /* Use Max Picture settings for whatever the dvd is.*/
3663         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3664         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3665         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3666         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3667         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3668         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureDeinterlace"];
3669         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3670         /* Set crop settings here */
3671         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3672         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3673     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3674         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3675         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3676         
3677         /*Audio*/
3678         /* Audio Sample Rate*/
3679         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3680         /* Audio Bitrate Rate*/
3681         [preset setObject:@"160" forKey:@"AudioBitRate"];
3682         /* Subtitles*/
3683         [preset setObject:@"None" forKey:@"Subtitles"];
3684         
3685
3686     [preset autorelease];
3687     return preset;
3688
3689 }
3690
3691 - (NSDictionary *)createQuickTimePreset
3692 {
3693     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3694         /* Get the New Preset Name from the field in the AddPresetPanel */
3695     [preset setObject:@"QuickTime" forKey:@"PresetName"];
3696         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3697         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3698         /*Set whether or not this is default, at creation set to 0*/
3699         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3700         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3701         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3702         /* Get the New Preset Description from the field in the AddPresetPanel */
3703     [preset setObject:@"HandBrake's high quality settings for use with QuickTime. It can be slow, so use it when the Normal preset doesn't look good enough." forKey:@"PresetDescription"];
3704         /* File Format */
3705     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3706         /* Chapter Markers*/
3707          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3708     /* Codecs */
3709         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3710         /* Video encoder */
3711         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3712         /* x264 Option String */
3713         [preset setObject:@"ref=3:mixed-refs:bframes=3:bime:weightb:b-rdo:direct=auto:me=umh:subme=5:analyse=all:8x8dct:trellis=1:no-fast-pskip" forKey:@"x264Option"];
3714         /* Video quality */
3715         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3716         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3717         [preset setObject:@"2000" forKey:@"VideoAvgBitrate"];
3718         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3719         
3720         /* Video framerate */
3721         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3722         /* GrayScale */
3723         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3724         /* 2 Pass Encoding */
3725         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3726         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3727         
3728         /*Picture Settings*/
3729         //hb_job_t * job = fTitle->job;
3730         /* Basic Picture Settings */
3731         /* Use Max Picture settings for whatever the dvd is.*/
3732         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3733         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3734         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3735         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3736         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3737         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3738         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3739         /* Set crop settings here */
3740         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3741         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3742     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3743         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3744         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3745         
3746         /*Audio*/
3747         /* Audio Sample Rate*/
3748         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3749         /* Audio Bitrate Rate*/
3750         [preset setObject:@"160" forKey:@"AudioBitRate"];
3751         /* Subtitles*/
3752         [preset setObject:@"None" forKey:@"Subtitles"];
3753         
3754
3755     [preset autorelease];
3756     return preset;
3757
3758 }
3759
3760 - (NSDictionary *)createBedlamPreset
3761 {
3762     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3763         /* Get the New Preset Name from the field in the AddPresetPanel */
3764     [preset setObject:@"Bedlam" forKey:@"PresetName"];
3765         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3766         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3767         /*Set whether or not this is default, at creation set to 0*/
3768         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3769         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3770         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3771         /* Get the New Preset Description from the field in the AddPresetPanel */
3772     [preset setObject:@"HandBrake's settings maxed out for slowest encoding and highest quality. Use at your own risk. So slow it's not just insane...it's a trip to the looney bin." forKey:@"PresetDescription"];
3773         /* File Format */
3774     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3775         /* Chapter Markers*/
3776          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3777     /* Codecs */
3778         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
3779         /* Video encoder */
3780         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3781         /* x264 Option String */
3782         [preset setObject:@"ref=16:mixed-refs:bframes=6:bime:weightb:b-rdo:direct=auto:b-pyramid:me=umh:subme=7:me-range=64:analyse=all:8x8dct:trellis=2:no-fast-pskip:no-dct-decimate:filter=-2,-1" forKey:@"x264Option"];
3783         /* Video quality */
3784         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3785         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3786         [preset setObject:@"1800" forKey:@"VideoAvgBitrate"];
3787         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3788         
3789         /* Video framerate */
3790         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3791         /* GrayScale */
3792         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3793         /* 2 Pass Encoding */
3794         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3795         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3796         
3797         /*Picture Settings*/
3798         //hb_job_t * job = fTitle->job;
3799         /* Basic Picture Settings */
3800         /* Use Max Picture settings for whatever the dvd is.*/
3801         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3802         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3803         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3804         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3805         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3806         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3807         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3808         /* Set crop settings here */
3809         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3810         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3811     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3812         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3813         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3814         
3815         /*Audio*/
3816         /* Audio Sample Rate*/
3817         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3818         /* Audio Bitrate Rate*/
3819         [preset setObject:@"160" forKey:@"AudioBitRate"];
3820         /* Subtitles*/
3821         [preset setObject:@"None" forKey:@"Subtitles"];
3822         
3823
3824     [preset autorelease];
3825     return preset;
3826
3827 }
3828
3829 - (NSDictionary *)createiPhonePreset
3830 {
3831     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3832         /* Get the New Preset Name from the field in the AddPresetPanel */
3833     [preset setObject:@"iPhone" forKey:@"PresetName"];
3834         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3835         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3836         /*Set whether or not this is default, at creation set to 0*/
3837         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3838         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3839         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3840         /* Get the New Preset Description from the field in the AddPresetPanel */
3841     [preset setObject:@"HandBrake's settings for the iPhone." forKey:@"PresetDescription"];
3842         /* File Format */
3843     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3844         /* Chapter Markers*/
3845          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3846     /* Codecs */
3847         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3848         /* Video encoder */
3849         [preset setObject:@"x264 (h.264 iPod)" forKey:@"VideoEncoder"];
3850         /* x264 Option String */
3851         [preset setObject:@"cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1" forKey:@"x264Option"];
3852         /* Video quality */
3853         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3854         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3855         [preset setObject:@"960" forKey:@"VideoAvgBitrate"];
3856         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3857         
3858         /* Video framerate */
3859         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3860         /* GrayScale */
3861         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3862         /* 2 Pass Encoding */
3863         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
3864         
3865         /*Picture Settings*/
3866         //hb_job_t * job = fTitle->job;
3867         /* Basic Picture Settings */
3868         /* Use Max Picture settings for whatever the dvd is.*/
3869         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
3870         [preset setObject:[NSNumber numberWithInt:480] forKey:@"PictureWidth"];
3871         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3872         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
3873         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3874         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
3875         /* Set crop settings here */
3876         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3877         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3878         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3879     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3880         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3881         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3882         
3883         /*Audio*/
3884         /* Audio Sample Rate*/
3885         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3886         /* Audio Bitrate Rate*/
3887         [preset setObject:@"128" forKey:@"AudioBitRate"];
3888         /* Subtitles*/
3889         [preset setObject:@"None" forKey:@"Subtitles"];
3890         
3891
3892     [preset autorelease];
3893     return preset;
3894
3895 }
3896
3897 - (NSDictionary *)createDeuxSixQuatrePreset
3898 {
3899     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3900         /* Get the New Preset Name from the field in the AddPresetPanel */
3901     [preset setObject:@"Deux Six Quatre" forKey:@"PresetName"];
3902         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3903         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3904         /*Set whether or not this is default, at creation set to 0*/
3905         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3906         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3907         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3908         /* Get the New Preset Description from the field in the AddPresetPanel */
3909     [preset setObject:@"HandBrake's preset for true high profile x264 quality. A good balance of quality and speed, based on community standards found in the wild. This preset will give you a much better sense of x264's capabilities than vanilla main profile." forKey:@"PresetDescription"];
3910         /* File Format */
3911     [preset setObject:@"MKV file" forKey:@"FileFormat"];
3912         /* Chapter Markers*/
3913          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3914     /* Codecs */
3915         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
3916         /* Video encoder */
3917         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3918         /* x264 Option String */
3919         [preset setObject:@"ref=5:mixed-refs:bframes=3:bime:weightb:b-rdo:b-pyramid:me=umh:subme=7:trellis=1:analyse=all:8x8dct:no-fast-pskip" forKey:@"x264Option"];
3920         /* Video quality */
3921         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
3922         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
3923         [preset setObject:@"1600" forKey:@"VideoAvgBitrate"];
3924         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3925         
3926         /* Video framerate */
3927         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3928         /* GrayScale */
3929         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3930         /* 2 Pass Encoding */
3931         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
3932         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
3933         
3934         /*Picture Settings*/
3935         //hb_job_t * job = fTitle->job;
3936         /* Basic Picture Settings */
3937         /* Use Max Picture settings for whatever the dvd is.*/
3938         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
3939         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
3940         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
3941         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
3942         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
3943         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
3944         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
3945         /* Set crop settings here */
3946         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
3947         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
3948     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
3949         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
3950         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
3951         
3952         /*Audio*/
3953         /* Audio Sample Rate*/
3954         [preset setObject:@"48" forKey:@"AudioSampleRate"];
3955         /* Audio Bitrate Rate*/
3956         [preset setObject:@"160" forKey:@"AudioBitRate"];
3957         /* Subtitles*/
3958         [preset setObject:@"None" forKey:@"Subtitles"];
3959         
3960
3961     [preset autorelease];
3962     return preset;
3963
3964 }
3965
3966 - (NSDictionary *)createBrokePreset
3967 {
3968     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
3969         /* Get the New Preset Name from the field in the AddPresetPanel */
3970     [preset setObject:@"Broke" forKey:@"PresetName"];
3971         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
3972         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
3973         /*Set whether or not this is default, at creation set to 0*/
3974         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
3975         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
3976         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
3977         /* Get the New Preset Description from the field in the AddPresetPanel */
3978     [preset setObject:@"HandBrake's preset for people without a lot of money to waste on hard drives. Tries to maximize quality for burning to CDs, so you can party like it's 1999." forKey:@"PresetDescription"];
3979         /* File Format */
3980     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
3981         /* Chapter Markers*/
3982          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
3983     /* Codecs */
3984         [preset setObject:@"AVC/H.264 Video / AAC Audio" forKey:@"FileCodecs"];
3985         /* Video encoder */
3986         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
3987         /* x264 Option String */
3988         [preset setObject:@"ref=3:mixed-refs:bframes=6:bime:weightb:b-rdo:b-pyramid:direct=auto:me=umh:subme=6:trellis=1:analyse=all:8x8dct:no-fast-pskip" forKey:@"x264Option"];
3989         /* Video quality */
3990         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoQualityType"];
3991         [preset setObject:@"695" forKey:@"VideoTargetSize"];
3992         [preset setObject:@"1600" forKey:@"VideoAvgBitrate"];
3993         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
3994         
3995         /* Video framerate */
3996         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
3997         /* GrayScale */
3998         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
3999         /* 2 Pass Encoding */
4000         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTwoPass"];
4001         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoTurboTwoPass"];
4002         
4003         /*Picture Settings*/
4004         //hb_job_t * job = fTitle->job;
4005         /* Basic Picture Settings */
4006         /* Use Max Picture settings for whatever the dvd is.*/
4007         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
4008         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4009         [preset setObject:[NSNumber numberWithInt:640] forKey:@"PictureWidth"];
4010         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4011         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
4012         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4013         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
4014         /* Set crop settings here */
4015         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4016         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4017     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4018         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4019         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4020         
4021         /*Audio*/
4022         /* Audio Sample Rate*/
4023         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4024         /* Audio Bitrate Rate*/
4025         [preset setObject:@"128" forKey:@"AudioBitRate"];
4026         /* Subtitles*/
4027         [preset setObject:@"None" forKey:@"Subtitles"];
4028         
4029
4030     [preset autorelease];
4031     return preset;
4032
4033 }
4034
4035 - (NSDictionary *)createBlindPreset
4036 {
4037     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4038         /* Get the New Preset Name from the field in the AddPresetPanel */
4039     [preset setObject:@"Blind" forKey:@"PresetName"];
4040         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4041         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4042         /*Set whether or not this is default, at creation set to 0*/
4043         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4044         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4045         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4046         /* Get the New Preset Description from the field in the AddPresetPanel */
4047     [preset setObject:@"HandBrake's preset for impatient people who don't care about picture quality." forKey:@"PresetDescription"];
4048         /* File Format */
4049     [preset setObject:@"MP4 file" forKey:@"FileFormat"];
4050         /* Chapter Markers*/
4051          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4052     /* Codecs */
4053         [preset setObject:@"MPEG-4 Video / AAC Audio" forKey:@"FileCodecs"];
4054         /* Video encoder */
4055         [preset setObject:@"FFmpeg" forKey:@"VideoEncoder"];
4056         /* x264 Option String */
4057         [preset setObject:@"" forKey:@"x264Option"];
4058         /* Video quality */
4059         [preset setObject:[NSNumber numberWithInt:1] forKey:@"VideoQualityType"];
4060         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4061         [preset setObject:@"512" forKey:@"VideoAvgBitrate"];
4062         [preset setObject:[NSNumber numberWithFloat:[fVidQualitySlider floatValue]] forKey:@"VideoQualitySlider"];
4063         
4064         /* Video framerate */
4065         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4066         /* GrayScale */
4067         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4068         /* 2 Pass Encoding */
4069         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4070         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTurboTwoPass"];
4071         
4072         /*Picture Settings*/
4073         //hb_job_t * job = fTitle->job;
4074         /* Basic Picture Settings */
4075         /* Use Max Picture settings for whatever the dvd is.*/
4076         [preset setObject:[NSNumber numberWithInt:0] forKey:@"UsesMaxPictureSettings"];
4077         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4078         [preset setObject:[NSNumber numberWithInt:512] forKey:@"PictureWidth"];
4079         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4080         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureKeepRatio"];
4081         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4082         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PicturePAR"];
4083         /* Set crop settings here */
4084         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4085         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4086     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4087         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4088         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4089         
4090         /*Audio*/
4091         /* Audio Sample Rate*/
4092         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4093         /* Audio Bitrate Rate*/
4094         [preset setObject:@"128" forKey:@"AudioBitRate"];
4095         /* Subtitles*/
4096         [preset setObject:@"None" forKey:@"Subtitles"];
4097         
4098
4099     [preset autorelease];
4100     return preset;
4101
4102 }
4103
4104 - (NSDictionary *)createCRFPreset
4105 {
4106     NSMutableDictionary *preset = [[NSMutableDictionary alloc] init];
4107         /* Get the New Preset Name from the field in the AddPresetPanel */
4108     [preset setObject:@"Constant Quality Rate" forKey:@"PresetName"];
4109         /*Set whether or not this is a user preset or factory 0 is factory, 1 is user*/
4110         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Type"];
4111         /*Set whether or not this is default, at creation set to 0*/
4112         [preset setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4113         /*Get the whether or not to apply pic settings in the AddPresetPanel*/
4114         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesPictureSettings"];
4115         /* Get the New Preset Description from the field in the AddPresetPanel */
4116     [preset setObject:@"HandBrake's preset for consistently excellent quality in one pass, with the downside of entirely unpredictable file sizes and bitrates." forKey:@"PresetDescription"];
4117         /* File Format */
4118     [preset setObject:@"MKV file" forKey:@"FileFormat"];
4119         /* Chapter Markers*/
4120          [preset setObject:[NSNumber numberWithInt:1] forKey:@"ChapterMarkers"];
4121     /* Codecs */
4122         [preset setObject:@"AVC/H.264 Video / AC-3 Audio" forKey:@"FileCodecs"];
4123         /* Video encoder */
4124         [preset setObject:@"x264 (h.264 Main)" forKey:@"VideoEncoder"];
4125         /* x264 Option String */
4126         [preset setObject:@"ref=3:mixed-refs:bframes=3:b-pyramid:b-rdo:bime:weightb:filter=-2,-1:subme=6:trellis=1:analyse=all:8x8dct:me=umh" forKey:@"x264Option"];
4127         /* Video quality */
4128         [preset setObject:[NSNumber numberWithInt:2] forKey:@"VideoQualityType"];
4129         [preset setObject:[fVidTargetSizeField stringValue] forKey:@"VideoTargetSize"];
4130         [preset setObject:@"2000" forKey:@"VideoAvgBitrate"];
4131         [preset setObject:[NSNumber numberWithFloat:0.6471] forKey:@"VideoQualitySlider"];
4132         
4133         /* Video framerate */
4134         [preset setObject:@"Same as source" forKey:@"VideoFramerate"];
4135         /* GrayScale */
4136         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoGrayScale"];
4137         /* 2 Pass Encoding */
4138         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTwoPass"];
4139         [preset setObject:[NSNumber numberWithInt:0] forKey:@"VideoTurboTwoPass"];
4140         
4141         /*Picture Settings*/
4142         //hb_job_t * job = fTitle->job;
4143         /* Basic Picture Settings */
4144         /* Use Max Picture settings for whatever the dvd is.*/
4145         [preset setObject:[NSNumber numberWithInt:1] forKey:@"UsesMaxPictureSettings"];
4146         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PictureAutoCrop"];
4147         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureWidth"];
4148         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureHeight"];
4149         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureKeepRatio"];
4150         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureDeinterlace"];
4151         [preset setObject:[NSNumber numberWithInt:1] forKey:@"PicturePAR"];
4152         /* Set crop settings here */
4153         /* The Auto Crop Matrix in the Picture Window autodetects differences in crop settings */
4154         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureTopCrop"];
4155     [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureBottomCrop"];
4156         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureLeftCrop"];
4157         [preset setObject:[NSNumber numberWithInt:0] forKey:@"PictureRightCrop"];
4158         
4159         /*Audio*/
4160         /* Audio Sample Rate*/
4161         [preset setObject:@"48" forKey:@"AudioSampleRate"];
4162         /* Audio Bitrate Rate*/
4163         [preset setObject:@"160" forKey:@"AudioBitRate"];
4164         /* Subtitles*/
4165         [preset setObject:@"None" forKey:@"Subtitles"];
4166         
4167
4168     [preset autorelease];
4169     return preset;
4170
4171 }
4172
4173
4174 - (IBAction)deletePreset:(id)sender
4175 {
4176     int status;
4177     NSEnumerator *enumerator;
4178     NSNumber *index;
4179     NSMutableArray *tempArray;
4180     id tempObject;
4181     
4182     if ( [tableView numberOfSelectedRows] == 0 )
4183         return;
4184     /* Alert user before deleting preset */
4185         /* Comment out for now, tie to user pref eventually */
4186
4187     //NSBeep();
4188     status = NSRunAlertPanel(@"Warning!", @"Are you sure that you want to delete the selected preset?", @"OK", @"Cancel", nil);
4189     
4190     if ( status == NSAlertDefaultReturn ) {
4191         enumerator = [tableView selectedRowEnumerator];
4192         tempArray = [NSMutableArray array];
4193         
4194         while ( (index = [enumerator nextObject]) ) {
4195             tempObject = [UserPresets objectAtIndex:[index intValue]];
4196             [tempArray addObject:tempObject];
4197         }
4198         
4199         [UserPresets removeObjectsInArray:tempArray];
4200         [tableView reloadData];
4201         [self savePreset];   
4202     }
4203 }
4204
4205 - (IBAction)getDefaultPresets:(id)sender
4206 {
4207         int i = 0;
4208     NSEnumerator *enumerator = [UserPresets objectEnumerator];
4209         id tempObject;
4210         while (tempObject = [enumerator nextObject])
4211         {
4212                 NSDictionary *thisPresetDict = tempObject;
4213                 if ([[thisPresetDict objectForKey:@"Default"] intValue] == 1) // 1 is HB default
4214                 {
4215                         presetHbDefault = i;    
4216                 }
4217                 if ([[thisPresetDict objectForKey:@"Default"] intValue] == 2) // 2 is User specified default
4218                 {
4219                         presetUserDefault = i;  
4220                 }
4221                 i++;
4222         }
4223 }
4224
4225 - (IBAction)setDefaultPreset:(id)sender
4226 {
4227     int i = 0;
4228     NSEnumerator *enumerator = [UserPresets objectEnumerator];
4229         id tempObject;
4230         /* First make sure the old user specified default preset is removed */
4231         while (tempObject = [enumerator nextObject])
4232         {
4233                 /* make sure we are not removing the default HB preset */
4234                 if ([[[UserPresets objectAtIndex:i] objectForKey:@"Default"] intValue] != 1) // 1 is HB default
4235                 {
4236                         [[UserPresets objectAtIndex:i] setObject:[NSNumber numberWithInt:0] forKey:@"Default"];
4237                 }
4238                 i++;
4239         }
4240         /* Second, go ahead and set the appropriate user specfied preset */
4241         /* we get the chosen preset from the UserPresets array */
4242         if ([[[UserPresets objectAtIndex:[tableView selectedRow]] objectForKey:@"Default"] intValue] != 1) // 1 is HB default
4243         {
4244                 [[UserPresets objectAtIndex:[tableView selectedRow]] setObject:[NSNumber numberWithInt:2] forKey:@"Default"];
4245         }
4246         presetUserDefault = [tableView selectedRow];
4247         
4248         /* We save all of the preset data here */
4249     [self savePreset];
4250         /* We Reload the New Table data for presets */
4251     [tableView reloadData];
4252 }
4253
4254 - (IBAction)selectDefaultPreset:(id)sender
4255 {
4256         /* if there is a user specified default, we use it */
4257         if (presetUserDefault)
4258         {
4259         [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:presetUserDefault] byExtendingSelection:NO];
4260         [self tableViewSelected:NULL];
4261         }
4262         else if (presetHbDefault) //else we use the built in default presetHbDefault
4263         {
4264         [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:presetHbDefault] byExtendingSelection:NO];
4265         [self tableViewSelected:NULL];
4266         }
4267 }
4268
4269 - (IBAction)tableViewSelected:(id)sender
4270 {
4271     /* Since we cannot disable the presets tableView in terms of clickability
4272            we will use the enabled state of the add presets button to determine whether
4273            or not clicking on a preset will do anything */
4274         if ([fPresetsAdd isEnabled])
4275         {
4276                 if ([tableView selectedRow] >= 0)
4277                 {       
4278                         /* we get the chosen preset from the UserPresets array */
4279                         chosenPreset = [UserPresets objectAtIndex:[tableView selectedRow]];
4280                         curUserPresetChosenNum = [sender selectedRow];
4281                         /* we set the preset display field in main window here */
4282                         [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@",[chosenPreset valueForKey:@"PresetName"]]];
4283                         if ([[chosenPreset objectForKey:@"Default"] intValue] == 1)
4284                         {
4285                                 [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@ (Default)",[chosenPreset valueForKey:@"PresetName"]]];
4286                         }
4287                         else
4288                         {
4289                                 [fPresetSelectedDisplay setStringValue: [NSString stringWithFormat: @"%@",[chosenPreset valueForKey:@"PresetName"]]];
4290                         }
4291                         /* File Format */
4292                         [fDstFormatPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"FileFormat"]]];
4293                         [self formatPopUpChanged: NULL];
4294                         
4295                         /* Chapter Markers*/
4296                         [fCreateChapterMarkers setState:[[chosenPreset objectForKey:@"ChapterMarkers"] intValue]];
4297                         /* Allow Mpeg4 64 bit formatting +4GB file sizes */
4298                         [fDstMpgLargeFileCheck setState:[[chosenPreset objectForKey:@"Mp4LargeFile"] intValue]];
4299                         /* Codecs */
4300                         [fDstCodecsPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"FileCodecs"]]];
4301                         [self codecsPopUpChanged: NULL];
4302                         /* Video encoder */
4303                         [fVidEncoderPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoEncoder"]]];
4304                         
4305                         /* We can show the preset options here in the gui if we want to
4306                                 so we check to see it the user has specified it in the prefs */
4307                         [fAdvancedOptions setOptions: [NSString stringWithFormat:[chosenPreset valueForKey:@"x264Option"]]];
4308                         
4309                         /* Lets run through the following functions to get variables set there */
4310                         [self encoderPopUpChanged: NULL];
4311                         
4312                         [self calculateBitrate: NULL];
4313                         
4314                         /* Video quality */
4315                         [fVidQualityMatrix selectCellAtRow:[[chosenPreset objectForKey:@"VideoQualityType"] intValue] column:0];
4316                         
4317                         [fVidTargetSizeField setStringValue: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoTargetSize"]]];
4318                         [fVidBitrateField setStringValue: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoAvgBitrate"]]];
4319                         
4320                         [fVidQualitySlider setFloatValue: [[chosenPreset valueForKey:@"VideoQualitySlider"] floatValue]];
4321                         [self videoMatrixChanged: NULL];
4322                         
4323                         /* Video framerate */
4324                         [fVidRatePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"VideoFramerate"]]];
4325                         
4326                         /* GrayScale */
4327                         [fVidGrayscaleCheck setState:[[chosenPreset objectForKey:@"VideoGrayScale"] intValue]];
4328                         
4329                         /* 2 Pass Encoding */
4330                         [fVidTwoPassCheck setState:[[chosenPreset objectForKey:@"VideoTwoPass"] intValue]];
4331                         [self twoPassCheckboxChanged: NULL];
4332                         /* Turbo 1st pass for 2 Pass Encoding */
4333                         [fVidTurboPassCheck setState:[[chosenPreset objectForKey:@"VideoTurboTwoPass"] intValue]];
4334                         
4335                         /*Audio*/
4336                         
4337                         /* Audio Sample Rate*/
4338                         [fAudRatePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"AudioSampleRate"]]];
4339                         /* Audio Bitrate Rate*/
4340                         [fAudBitratePopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"AudioBitRate"]]];
4341                         /*Subtitles*/
4342                         [fSubPopUp selectItemWithTitle: [NSString stringWithFormat:[chosenPreset valueForKey:@"Subtitles"]]];
4343                         
4344                         /* Picture Settings */
4345                         /* Look to see if we apply these here in objectForKey:@"UsesPictureSettings"] */
4346                         if ([[chosenPreset objectForKey:@"UsesPictureSettings"]  intValue] > 0)
4347                         {
4348                                 hb_job_t * job = fTitle->job;
4349                                 /* Check to see if we should use the max picture setting for the current title*/
4350                                 if ([[chosenPreset objectForKey:@"UsesPictureSettings"]  intValue] == 2 || [[chosenPreset objectForKey:@"UsesMaxPictureSettings"]  intValue] == 1)
4351                                 {
4352                                         /* Use Max Picture settings for whatever the dvd is.*/
4353                                         [self revertPictureSizeToMax: NULL];
4354                                         job->keep_ratio = [[chosenPreset objectForKey:@"PictureKeepRatio"]  intValue];
4355                                         if (job->keep_ratio == 1)
4356                                         {
4357                                                 hb_fix_aspect( job, HB_KEEP_WIDTH );
4358                                                 if( job->height > fTitle->height )
4359                                                 {
4360                                                         job->height = fTitle->height;
4361                                                         hb_fix_aspect( job, HB_KEEP_HEIGHT );
4362                                                 }
4363                                         }
4364                                         job->pixel_ratio = [[chosenPreset objectForKey:@"PicturePAR"]  intValue];
4365                                 }
4366                                 else // Apply picture settings that were in effect at the time the preset was saved
4367                                 {
4368                                         job->width = [[chosenPreset objectForKey:@"PictureWidth"]  intValue];
4369                                         job->height = [[chosenPreset objectForKey:@"PictureHeight"]  intValue];
4370                                         job->keep_ratio = [[chosenPreset objectForKey:@"PictureKeepRatio"]  intValue];
4371                                         if (job->keep_ratio == 1)
4372                                         {
4373                                                 hb_fix_aspect( job, HB_KEEP_WIDTH );
4374                                                 if( job->height > fTitle->height )
4375                                                 {
4376                                                         job->height = fTitle->height;
4377                                                         hb_fix_aspect( job, HB_KEEP_HEIGHT );
4378                                                 }
4379                                         }
4380                                         job->pixel_ratio = [[chosenPreset objectForKey:@"PicturePAR"]  intValue];
4381                     [fPictureController setDeinterlace:[[chosenPreset objectForKey:@"PictureDeinterlace"] intValue]];
4382                                         
4383                                         if ([chosenPreset objectForKey:@"PictureDetelecine"])
4384                                         {
4385                         [fPictureController setDetelecine:[[chosenPreset objectForKey:@"PictureDetelecine"] intValue]];
4386                                         }
4387                                         if ([chosenPreset objectForKey:@"PictureDenoise"])
4388                                         {
4389                         [fPictureController setDenoise:[[chosenPreset objectForKey:@"PictureDenoise"] intValue]];
4390                                         }
4391                                         /* If Cropping is set to custom, then recall all four crop values from
4392                                                 when the preset was created and apply them */
4393                                         if ([[chosenPreset objectForKey:@"PictureAutoCrop"]  intValue] == 0)
4394                                         {
4395                         [fPictureController setAutoCrop:NO];
4396                                                 
4397                                                 /* Here we use the custom crop values saved at the time the preset was saved */
4398                                                 job->crop[0] = [[chosenPreset objectForKey:@"PictureTopCrop"]  intValue];
4399                                                 job->crop[1] = [[chosenPreset objectForKey:@"PictureBottomCrop"]  intValue];
4400                                                 job->crop[2] = [[chosenPreset objectForKey:@"PictureLeftCrop"]  intValue];
4401                                                 job->crop[3] = [[chosenPreset objectForKey:@"PictureRightCrop"]  intValue];
4402                                                 
4403                                         }
4404                                         else /* if auto crop has been saved in preset, set to auto and use post scan auto crop */
4405                                         {
4406                         [fPictureController setAutoCrop:YES];
4407                         /* Here we use the auto crop values determined right after scan */
4408                                                 job->crop[0] = AutoCropTop;
4409                                                 job->crop[1] = AutoCropBottom;
4410                                                 job->crop[2] = AutoCropLeft;
4411                                                 job->crop[3] = AutoCropRight;
4412                                                 
4413                                         }
4414                                 }
4415                                 [self calculatePictureSizing: NULL]; 
4416                         }
4417                         
4418                         
4419                         [[fPresetsActionMenu itemAtIndex:0] setEnabled: YES];
4420                         }
4421 }
4422 }
4423
4424
4425
4426 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
4427 {
4428     return [UserPresets count];
4429 }
4430
4431 /* we use this to determine display characteristics for
4432 each table cell based on content currently only used to
4433 show the built in presets in a blue font. */
4434 - (void)tableView:(NSTableView *)aTableView
4435  willDisplayCell:(id)aCell 
4436  forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
4437 {
4438     NSDictionary *userPresetDict = [UserPresets objectAtIndex:rowIndex];
4439         NSFont *txtFont;
4440         NSColor *fontColor;
4441         NSColor *shadowColor;
4442         txtFont = [NSFont systemFontOfSize: [NSFont smallSystemFontSize]];
4443         /* First, we check to see if its a selected row, if so, we use white since its highlighted in blue */
4444         if ([[aTableView selectedRowIndexes] containsIndex:rowIndex] && ([tableView editedRow] != rowIndex))
4445         {
4446                 
4447                 fontColor = [NSColor whiteColor];
4448                 shadowColor = [NSColor colorWithDeviceRed:(127.0/255.0) green:(140.0/255.0) blue:(160.0/255.0) alpha:1.0];
4449         }
4450         else
4451         {
4452                 /* We set the properties of unselected rows */
4453                 /* if built-in preset (defined by "type" == 0) we use a blue font */
4454                 if ([[userPresetDict objectForKey:@"Type"] intValue] == 0)
4455                 {
4456                         fontColor = [NSColor blueColor];
4457                 }
4458                 else // User created preset, use a black font
4459                 {
4460                         fontColor = [NSColor blackColor];
4461                 }
4462                 shadowColor = nil;
4463         }
4464         /* We check to see if this is the HB default, if so, color it appropriately */
4465         if (!presetUserDefault && presetHbDefault && rowIndex == presetHbDefault)
4466         {
4467         txtFont = [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]];
4468         }
4469         /* We check to see if this is the User Specified default, if so, color it appropriately */
4470         if (presetUserDefault && rowIndex == presetUserDefault)
4471         {
4472         txtFont = [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]];
4473         }
4474         
4475         [aCell setTextColor:fontColor];
4476         [aCell setFont:txtFont];
4477         /* this shadow stuff (like mail app) for some reason looks crappy, commented out
4478         temporarily in case we want to resurrect it */
4479         /*
4480         NSShadow *shadow = [[NSShadow alloc] init];
4481         NSSize shadowOffset = { width: 1.0, height: -1.5};
4482         [shadow setShadowOffset:shadowOffset];
4483         [shadow setShadowColor:shadowColor];
4484         [shadow set];
4485         */
4486         
4487 }
4488 /* Method to display tooltip with the description for each preset, if available */
4489 - (NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)aCell 
4490                    rect:(NSRectPointer)aRect tableColumn:(NSTableColumn *)aTableColumn
4491                     row:(int)rowIndex mouseLocation:(NSPoint)aPos
4492 {
4493      /* initialize the tooltip contents variable */
4494          NSString *loc_tip;
4495      /* if there is a description for the preset, we show it in the tooltip */
4496          if ([[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"])
4497          {
4498         loc_tip = [NSString stringWithFormat: @"%@",[[UserPresets objectAtIndex:rowIndex] valueForKey:@"PresetDescription"]];
4499         return (loc_tip);
4500          }
4501          else
4502          {
4503         loc_tip = @"No description available";
4504          }
4505          return (loc_tip);
4506
4507 }
4508
4509 - (id)tableView:(NSTableView *)aTableView
4510       objectValueForTableColumn:(NSTableColumn *)aTableColumn
4511       row:(int)rowIndex
4512 {
4513 id theRecord, theValue;
4514     
4515     theRecord = [UserPresets objectAtIndex:rowIndex];
4516     theValue = [theRecord objectForKey:[aTableColumn identifier]];
4517     return theValue;
4518 }
4519
4520 // NSTableDataSource method that we implement to edit values directly in the table...
4521 - (void)tableView:(NSTableView *)aTableView
4522         setObjectValue:(id)anObject
4523         forTableColumn:(NSTableColumn *)aTableColumn
4524         row:(int)rowIndex
4525 {
4526     id theRecord;
4527     
4528     theRecord = [UserPresets objectAtIndex:rowIndex];
4529     [theRecord setObject:anObject forKey:@"PresetName"];
4530     /* We Sort the Presets By Factory or Custom */
4531         NSSortDescriptor * presetTypeDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"Type" 
4532                                                     ascending:YES] autorelease];
4533                 /* We Sort the Presets Alphabetically by name */
4534         NSSortDescriptor * presetNameDescriptor=[[[NSSortDescriptor alloc] initWithKey:@"PresetName" 
4535                                                     ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease];
4536         NSArray *sortDescriptors=[NSArray arrayWithObjects:presetTypeDescriptor,presetNameDescriptor,nil];
4537     NSArray *sortedArray=[UserPresets sortedArrayUsingDescriptors:sortDescriptors];
4538         [UserPresets setArray:sortedArray];
4539         /* We Reload the New Table data for presets */
4540     [tableView reloadData];
4541    /* We save all of the preset data here */
4542     [self savePreset];
4543 }
4544
4545 - (void)savePreset
4546 {
4547     [UserPresets writeToFile:UserPresetsFile atomically:YES];
4548         /* We get the default preset in case it changed */
4549         [self getDefaultPresets: NULL];
4550
4551 }
4552
4553 - (void) controlTextDidChange: (NSNotification *) notification
4554 {
4555     [self calculateBitrate: NULL];
4556 }
4557
4558 - (IBAction) openHomepage: (id) sender
4559 {
4560     [[NSWorkspace sharedWorkspace] openURL: [NSURL
4561         URLWithString:@"http://handbrake.m0k.org/"]];
4562 }
4563
4564 - (IBAction) openForums: (id) sender
4565 {
4566     [[NSWorkspace sharedWorkspace] openURL: [NSURL
4567         URLWithString:@"http://handbrake.m0k.org/forum/"]];
4568 }
4569 - (IBAction) openUserGuide: (id) sender
4570 {
4571     [[NSWorkspace sharedWorkspace] openURL: [NSURL
4572         URLWithString:@"http://handbrake.m0k.org/trac/wiki/HandBrakeGuide"]];
4573 }
4574
4575 /**
4576  * Shows debug output window.
4577  */
4578 - (IBAction)showDebugOutputPanel:(id)sender
4579 {
4580     [outputPanel showOutputPanel:sender];
4581 }
4582
4583 /**
4584  * Shows preferences window modally.
4585  */
4586 - (IBAction) showPreferencesWindow: (id) sender
4587 {
4588     NSWindow * window = [fPreferencesController window];
4589     if (![window isVisible])
4590         [window center];
4591
4592     [window makeKeyAndOrderFront: nil];
4593 }
4594
4595 /**
4596  * Shows queue window.
4597  */
4598 - (IBAction) showQueueWindow:(id)sender
4599 {
4600     [fQueueController showQueueWindow:sender];
4601 }
4602
4603
4604 - (IBAction) toggleDrawer:(id)sender {
4605     [fPresetDrawer toggle:self];
4606 }
4607
4608 @end