1 #import "ExpressController.h"
2 #import "DriveDetector.h"
4 #define INSERT_STRING @"Insert a DVD"
6 @interface ExpressController (Private)
8 - (void) openUpdateDrives: (NSDictionary *) drives;
9 - (void) openBrowseDidEnd: (NSOpenPanel *) sheet returnCode: (int)
10 returnCode contextInfo: (void *) contextInfo;
11 - (void) openEnable: (BOOL) b;
12 - (void) openTimer: (NSTimer *) timer;
15 - (void) convertEnable: (BOOL) b;
16 - (void) convertTimer: (NSTimer *) timer;
20 @implementation ExpressController
22 /***********************************************************************
23 * Application delegate methods
24 **********************************************************************/
27 NSEnumerator * enumerator;
29 /* Show the "Open DVD" interface */
30 fDriveDetector = [[DriveDetector alloc] initWithCallback: self
31 selector: @selector( openUpdateDrives: )];
33 [self openEnable: YES];
34 [fWindow setContentSize: [fOpenView frame].size];
35 [fWindow setContentView: fOpenView];
37 [fWindow makeKeyAndOrderFront: nil];
39 /* NSTableView initializations */
40 NSButtonCell * buttonCell;
41 NSTableColumn * tableColumn;
42 enumerator = [[fConvertTableView tableColumns] objectEnumerator];
43 while( ( tableColumn = [enumerator nextObject] ) )
45 [tableColumn setEditable: NO];
47 tableColumn = [fConvertTableView tableColumnWithIdentifier: @"Check"];
48 buttonCell = [[[NSButtonCell alloc] initTextCell: @""] autorelease];
49 [buttonCell setEditable: YES];
50 [buttonCell setButtonType: NSSwitchButton];
51 [tableColumn setDataCell: buttonCell];
54 fConvertFolderString = [@"~/Movies" stringByExpandingTildeInPath];
55 [fConvertFolderString retain];
58 - (void) applicationWillFinishLaunching: (NSNotification *) n
60 fHandle = hb_init_express( HB_DEBUG_NONE, 0 );
61 fList = hb_get_titles( fHandle );
64 - (void) applicationWillTerminate: (NSNotification *) n
69 /***********************************************************************
70 * Tableview datasource methods
71 **********************************************************************/
72 - (int) numberOfRowsInTableView: (NSTableView *) t
77 return hb_list_count( fList );
80 - (id) tableView:(NSTableView *) t objectValueForTableColumn:
81 (NSTableColumn *) col row: (int) row
83 if( [[col identifier] isEqualToString: @"Check"] )
85 return [fConvertCheckArray objectAtIndex: row];
89 hb_title_t * title = hb_list_item( fList, row );
90 if( [[col identifier] isEqualToString: @"Title"] )
92 return [@"Title " stringByAppendingFormat: @"%d",
95 else if( [[col identifier] isEqualToString: @"Duration"] )
97 if( title->hours > 0 )
99 return [NSString stringWithFormat:
100 @"%d hour%s %d min%s", title->hours,
101 title->hours > 1 ? "s" : "", title->minutes,
102 title->minutes > 1 ? "s": ""];
104 else if( title->minutes > 0 )
106 return [NSString stringWithFormat:
107 @"%d min%s %d sec%s", title->minutes,
108 title->minutes > 1 ? "s" : "", title->seconds,
109 title->seconds > 1 ? "s": ""];
113 return [NSString stringWithFormat: @"%d seconds",
121 - (void) tableView: (NSTableView *) t setObjectValue: (id) object
122 forTableColumn: (NSTableColumn *) col row: (int) row
124 if( [[col identifier] isEqualToString: @"Check"] )
126 [fConvertCheckArray replaceObjectAtIndex: row withObject: object];
130 /***********************************************************************
131 * User events methods
132 **********************************************************************/
133 - (void) openShow: (id) sender
135 NSRect frame = [fWindow frame];
136 float offset = [fConvertView frame].size.height -
137 [fOpenView frame].size.height;
139 frame.origin.y += offset;
140 frame.size.height -= offset;
141 [fWindow setContentView: fEmptyView];
142 [fWindow setFrame: frame display: YES animate: YES];
143 [fWindow setContentView: fOpenView];
145 [fDriveDetector run];
148 - (void) openMatrixChanged: (id) sender
150 [self openEnable: YES];
151 if( [fOpenMatrix selectedRow] )
153 [self openBrowse: self];
157 - (void) openBrowse: (id) sender
159 NSOpenPanel * panel = [NSOpenPanel openPanel];
160 [panel setAllowsMultipleSelection: NO];
161 [panel setCanChooseFiles: YES];
162 [panel setCanChooseDirectories: YES ];
163 [panel beginSheetForDirectory: nil file: nil types: nil
164 modalForWindow: fWindow modalDelegate: self
165 didEndSelector: @selector( openBrowseDidEnd:returnCode:contextInfo: )
169 - (void) openGo: (id) sender
171 [self openEnable: NO];
172 [fOpenIndicator setIndeterminate: YES];
173 [fOpenIndicator startAnimation: nil];
174 [fOpenProgressField setStringValue: @"Opening..."];
175 [fDriveDetector stop];
177 if( [fOpenMatrix selectedRow] )
179 hb_scan( fHandle, [fOpenFolderString UTF8String], 0 );
183 hb_scan( fHandle, [[fDrives objectForKey: [fOpenPopUp
184 titleOfSelectedItem]] UTF8String], 0 );
187 NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 2.0
188 target: self selector: @selector( openTimer: ) userInfo: nil
192 - (void) convertGo: (id) sender
196 for( i = 0; i < hb_list_count( fList ); i++ )
198 if( ![[fConvertCheckArray objectAtIndex: i] boolValue] )
201 hb_title_t * title = hb_list_item( fList, i );
202 hb_job_t * job = title->job;
205 int aspect = title->aspect;
206 if( [fConvertAspectPopUp indexOfSelectedItem] == 1)
208 aspect = 4 * HB_ASPECT_BASE / 3;
212 job->vbitrate = 1000;
213 if( [fConvertMaxWidthPopUp indexOfSelectedItem] == 1)
218 job->deinterlace = 1;
222 hb_set_size( job, aspect, pixels );
224 } while(job->width > maxwidth);
226 if( [fConvertFormatPopUp indexOfSelectedItem] == 0 )
229 job->mux = HB_MUX_IPOD;
230 job->vcodec = HB_VCODEC_X264;
231 job->h264_level = 30;
233 else if( [fConvertFormatPopUp indexOfSelectedItem] == 1 )
236 job->mux = HB_MUX_MP4;
237 job->vcodec = HB_VCODEC_FFMPEG;
242 job->mux = HB_MUX_PSP;
243 job->vrate = 27000000;
244 job->vrate_base = 900900; /* 29.97 fps */
245 job->vcodec = HB_VCODEC_FFMPEG;
250 aspect = 16 * HB_ASPECT_BASE / 9;
252 if( [fConvertAspectPopUp indexOfSelectedItem] )
257 hb_set_size( job, aspect, pixels );
260 job->vquality = -1.0;
264 /* Audio selection */
266 lang = [[fConvertAudioPopUp titleOfSelectedItem] UTF8String];
268 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
270 /* Choose the first track that matches the language */
271 audio = hb_list_item( title->list_audio, j );
272 if( !strcmp( lang, audio->lang_simple ) )
278 if( job->audios[0] == -1 )
280 /* If the language isn't available in this title, choose
286 /* Subtitle selection */
287 hb_subtitle_t * subtitle;
288 lang = [[fConvertSubtitlePopUp titleOfSelectedItem] UTF8String];
290 for( j = 0; j < hb_list_count( title->list_subtitle ); j++ )
292 /* Choose the first track that matches the language */
293 subtitle = hb_list_item( title->list_subtitle, j );
294 if( !strcmp( lang, subtitle->lang ) )
301 job->file = strdup( [[NSString stringWithFormat:
302 @"%@/%s - Title %d.m4v", fConvertFolderString,
303 title->name, title->index] UTF8String] );
304 hb_add( fHandle, job );
309 NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval: 2.0
310 target: self selector: @selector( convertTimer: ) userInfo: nil
313 [self convertEnable: NO];
316 - (void) convertCancel: (id) sender
319 [self convertEnable: YES];
324 /***********************************************************************
326 **********************************************************************/
328 @implementation ExpressController (Private)
330 - (void) openUpdateDrives: (NSDictionary *) drives
336 fDrives = [[NSDictionary alloc] initWithDictionary: drives];
339 NSEnumerator * enumerator = [fDrives keyEnumerator];
340 [fOpenPopUp removeAllItems];
341 while( ( device = [enumerator nextObject] ) )
343 [fOpenPopUp addItemWithTitle: device];
346 if( ![fOpenPopUp numberOfItems] )
348 [fOpenPopUp addItemWithTitle: INSERT_STRING];
350 [fOpenPopUp selectItemAtIndex: 0];
351 if( [fOpenMatrix isEnabled] )
353 [self openEnable: YES];
357 - (void) openBrowseDidEnd: (NSOpenPanel *) sheet returnCode: (int)
358 returnCode contextInfo: (void *) contextInfo
360 if( returnCode != NSOKButton )
363 if( fOpenFolderString )
364 [fOpenFolderString release];
365 fOpenFolderString = [[[sheet filenames] objectAtIndex: 0] retain];
366 [fOpenFolderField setStringValue: [fOpenFolderString lastPathComponent]];
370 - (void) openEnable: (BOOL) b
372 [fOpenMatrix setEnabled: b];
373 [fOpenPopUp setEnabled: b];
374 [fOpenFolderField setEnabled: b];
375 [fOpenBrowseButton setEnabled: b];
376 [fOpenGoButton setEnabled: b];
380 if( [fOpenMatrix selectedRow] )
382 [fOpenPopUp setEnabled: NO];
386 [fOpenFolderField setEnabled: NO];
387 [fOpenBrowseButton setEnabled: NO];
388 if( [[fOpenPopUp titleOfSelectedItem]
389 isEqualToString: INSERT_STRING] )
391 [fOpenGoButton setEnabled: NO];
397 - (void) openTimer: (NSTimer *) timer
400 hb_get_state( fHandle, &s );
403 #define p s.param.scanning
404 case HB_STATE_SCANNING:
405 [fOpenIndicator setIndeterminate: NO];
406 [fOpenIndicator setDoubleValue: 100.0 *
407 ( (float) p.title_cur - 0.5 ) / p.title_count];
408 [fOpenProgressField setStringValue: [NSString
409 stringWithFormat: @"Scanning title %d of %d...",
410 p.title_cur, p.title_count]];
414 case HB_STATE_SCANDONE:
417 [fOpenIndicator setIndeterminate: NO];
418 [fOpenIndicator setDoubleValue: 0.0];
419 [self openEnable: YES];
421 if( hb_list_count( fList ) )
427 [fDriveDetector run];
440 fConvertCheckArray = [[NSMutableArray alloc] initWithCapacity:
441 hb_list_count( fList )];
442 [fConvertAudioPopUp removeAllItems];
443 [fConvertSubtitlePopUp removeAllItems];
444 [fConvertSubtitlePopUp addItemWithTitle: @"None"];
445 for( i = 0; i < hb_list_count( fList ); i++ )
447 /* Default is to convert titles longer than 30 minutes. */
448 hb_title_t * title = hb_list_item( fList, i );
449 [fConvertCheckArray addObject: [NSNumber numberWithBool:
450 ( 60 * title->hours + title->minutes > 30 )]];
452 /* Update audio popup */
454 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
456 audio = hb_list_item( title->list_audio, j );
457 [fConvertAudioPopUp addItemWithTitle:
458 [NSString stringWithUTF8String: audio->lang_simple]];
460 [fConvertAudioPopUp selectItemWithTitle: @"English"];
462 /* Update subtitle popup */
463 hb_subtitle_t * subtitle;
464 for( j = 0; j < hb_list_count( title->list_subtitle ); j++ )
466 subtitle = hb_list_item( title->list_subtitle, j );
467 [fConvertSubtitlePopUp addItemWithTitle:
468 [NSString stringWithUTF8String: subtitle->lang]];
471 [fConvertTableView reloadData];
473 NSRect frame = [fWindow frame];
474 float offset = [fConvertView frame].size.height -
475 [fOpenView frame].size.height;
476 frame.origin.y -= offset;
477 frame.size.height += offset;
478 [fWindow setContentView: fEmptyView];
479 [fWindow setFrame: frame display: YES animate: YES];
480 [fWindow setContentView: fConvertView];
483 NSMenuItem * item = [fConvertFolderPopUp itemAtIndex: 0];
484 [item setTitle: [fConvertFolderString lastPathComponent]];
485 NSImage * image32 = [[NSWorkspace sharedWorkspace] iconForFile:
486 fConvertFolderString];
487 NSImage * image16 = [[NSImage alloc] initWithSize:
490 [[NSGraphicsContext currentContext]
491 setImageInterpolation: NSImageInterpolationHigh];
492 [image32 drawInRect: NSMakeRect(0,0,16,16)
493 fromRect: NSMakeRect(0,0,32,32) operation: NSCompositeCopy
495 [image16 unlockFocus];
496 [item setImage: image16];
499 [self convertEnable: YES];
502 - (void) convertEnable: (BOOL) b
504 [fConvertTableView setEnabled: b];
505 [fConvertFolderPopUp setEnabled: b];
506 [fConvertFormatPopUp setEnabled: b];
507 [fConvertAspectPopUp setEnabled: b];
508 [fConvertMaxWidthPopUp setEnabled: b];
509 [fConvertAudioPopUp setEnabled: b];
510 [fConvertSubtitlePopUp setEnabled: b];
511 [fConvertOpenButton setEnabled: b];
514 [fConvertGoButton setTitle: @"Convert"];
515 [fConvertGoButton setAction: @selector(convertGo:)];
519 [fConvertGoButton setTitle: @"Cancel"];
520 [fConvertGoButton setAction: @selector(convertCancel:)];
524 /***********************************************************************
526 ***********************************************************************
527 * Shows a progression bar on the dock icon, filled according to
528 * 'progress' (0.0 <= progress <= 1.0).
529 * Called with progress < 0.0 or progress > 1.0, restores the original
531 **********************************************************************/
532 - (void) UpdateDockIcon: (float) progress
536 NSBitmapImageRep * bmp;
538 uint32_t black = htonl( 0x000000FF );
539 uint32_t red = htonl( 0xFF0000FF );
540 uint32_t white = htonl( 0xFFFFFFFF );
541 int row_start, row_end;
544 /* Get application original icon */
545 icon = [NSImage imageNamed: @"NSApplicationIcon"];
547 if( progress < 0.0 || progress > 1.0 )
549 [NSApp setApplicationIconImage: icon];
553 /* Get it in a raw bitmap form */
554 tiff = [icon TIFFRepresentationUsingCompression:
555 NSTIFFCompressionNone factor: 1.0];
556 bmp = [NSBitmapImageRep imageRepWithData: tiff];
558 /* Draw the progression bar */
559 /* It's pretty simple (ugly?) now, but I'm no designer */
561 row_start = 3 * (int) [bmp size].height / 4;
562 row_end = 7 * (int) [bmp size].height / 8;
564 for( i = row_start; i < row_start + 2; i++ )
566 pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
567 for( j = 0; j < (int) [bmp size].width; j++ )
572 for( i = row_start + 2; i < row_end - 2; i++ )
574 pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
577 for( j = 2; j < (int) [bmp size].width - 2; j++ )
579 if( j < 2 + (int) ( ( [bmp size].width - 4.0 ) * progress ) )
591 for( i = row_end - 2; i < row_end; i++ )
593 pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
594 for( j = 0; j < (int) [bmp size].width; j++ )
600 /* Now update the dock icon */
601 tiff = [bmp TIFFRepresentationUsingCompression:
602 NSTIFFCompressionNone factor: 1.0];
603 icon = [[NSImage alloc] initWithData: tiff];
604 [NSApp setApplicationIconImage: icon];
608 - (void) convertTimer: (NSTimer *) timer
611 hb_get_state( fHandle, &s );
614 #define p s.param.working
615 case HB_STATE_WORKING:
617 float progress_total = ( p.progress + p.job_cur - 1 ) / p.job_count;
618 NSMutableString * string = [NSMutableString
619 stringWithFormat: @"Converting: %.1f %%",
620 100.0 * progress_total];
623 [string appendFormat: @" (%.1f fps, ", p.rate_avg];
626 [string appendFormat: @"%d hour%s %d min%s",
627 p.hours, p.hours == 1 ? "" : "s",
628 p.minutes, p.minutes == 1 ? "" : "s"];
630 else if( p.minutes > 0 )
632 [string appendFormat: @"%d min%s %d sec%s",
633 p.minutes, p.minutes == 1 ? "" : "s",
634 p.seconds, p.seconds == 1 ? "" : "s"];
638 [string appendFormat: @"%d second%s",
639 p.seconds, p.seconds == 1 ? "" : "s"];
641 [string appendString: @" left)"];
643 [fConvertInfoString setStringValue: string];
644 [fConvertIndicator setIndeterminate: NO];
645 [fConvertIndicator setDoubleValue: 100.0 * progress_total];
646 [self UpdateDockIcon: progress_total];
651 #define p s.param.muxing
652 case HB_STATE_MUXING:
654 NSMutableString * string = [NSMutableString
655 stringWithFormat: @"Muxing..."];
656 [fConvertInfoString setStringValue: string];
657 [fConvertIndicator setIndeterminate: YES];
658 [fConvertIndicator startAnimation: nil];
659 [self UpdateDockIcon: 1.0];
664 case HB_STATE_WORKDONE:
667 [fConvertIndicator setIndeterminate: NO];
668 [fConvertIndicator setDoubleValue: 0.0];
669 [self UpdateDockIcon: -1.0];
670 [self convertEnable: YES];
672 #define p s.param.workdone
676 [fConvertInfoString setStringValue: @"Done."];
678 case HB_ERROR_CANCELED:
679 [fConvertInfoString setStringValue: @"Canceled."];
681 case HB_ERROR_UNKNOWN:
682 [fConvertInfoString setStringValue: @"Unknown Error."];
688 while( ( job = hb_job( fHandle, 0 ) ) )
690 hb_rem( fHandle, job );