summaryrefslogtreecommitdiffstats
path: root/macosx/InstantHandBrake/ExpressController.m
blob: 393ab9df80f6d6bccb4cccc7caf99ec161bd9a99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#import "ExpressController.h"

#define INSERT_STRING       @"Insert a DVD"
#define TOOLBAR_START       @"TOOLBAR_START"
#define TOOLBAR_PAUSE       @"TOOLBAR_PAUSE"
#define TOOLBAR_OPEN        @"TOOLBAR_OPEN"
#define TOOLBAR_ADVANCED    @"TOOLBAR_ADVANCED"

#define p fState->param

@interface ExpressController (Private)

- (void) openUpdateDrives: (NSDictionary *) drives;
- (void) openBrowseDidEnd: (NSOpenPanel *) sheet returnCode: (int)
    returnCode contextInfo: (void *) contextInfo;
- (void) openEnable: (BOOL) b;

- (void) convertShow;
- (void) convertEnable: (BOOL) b;

@end

@implementation ExpressController

/***********************************************************************
 * Application delegate methods
 **********************************************************************/
- (void) awakeFromNib
{
    NSEnumerator * enumerator;
    
    /* NSToolbar initializations */
    fToolbar = [[NSToolbar alloc] initWithIdentifier: @"InstantHandBrake Toolbar"];
    [fToolbar setDelegate: self];
    [fToolbar setAllowsUserCustomization: YES];
    [fToolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
    [fToolbar setVisible:NO];
    [fWindow setToolbar: fToolbar];
        
    /* Show the "Open DVD" interface */
    fDriveDetector = [[DriveDetector alloc] initWithCallback: self
        selector: @selector( openUpdateDrives: )];
    [fDriveDetector run];
    [self openEnable: YES];
    [fWindow setContentSize: [fOpenView frame].size];
    [fWindow setContentView: fOpenView];
    [fWindow center];
    [fWindow makeKeyAndOrderFront: nil];

    /* NSTableView initializations */
     NSButtonCell * buttonCell;
     NSTableColumn * tableColumn;
     enumerator = [[fConvertTableView tableColumns] objectEnumerator];
     while( ( tableColumn = [enumerator nextObject] ) )
     {
         [tableColumn setEditable: NO];
     }
     tableColumn = [fConvertTableView tableColumnWithIdentifier: @"Check"];
     buttonCell = [[[NSButtonCell alloc] initTextCell: @""] autorelease];
    [buttonCell setEditable: YES];
    [buttonCell setButtonType: NSSwitchButton];
    [tableColumn setDataCell: buttonCell];

    /* Preferences */
    fConvertFolderString = [NSString stringWithFormat:@"%@/Desktop", NSHomeDirectory()];
    [fConvertFolderString retain];
}

- (void) applicationWillFinishLaunching: (NSNotification *) n
{
    fCore = [[HBCore alloc] init];
    [fCore openInDebugMode:NO checkForUpdates:NO];
    fHandle = [fCore hb_handle];
    fState = [fCore hb_state];
    fList   = hb_get_titles( fHandle );
    
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(scanningSource:)
    name:@"HBCoreScanningNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(scanDone:)
    name:@"HBCoreScanDoneNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(muxing:)
    name:@"HBCoreMuxingNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(working:)
    name:@"HBCoreWorkingNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(workDone:)
    name:@"HBCoreWorkDoneNotification" object:nil];
}

- (void) applicationWillTerminate: (NSNotification *) n
{
    [fCore close];
}

- (NSToolbarItem *) toolbar: (NSToolbar *) toolbar itemForItemIdentifier: (NSString *) ident
                    willBeInsertedIntoToolbar: (BOOL) flag
{
    NSToolbarItem * item;
    item = [[NSToolbarItem alloc] initWithItemIdentifier: ident];

    if ([ident isEqualToString: TOOLBAR_START])
    {
        [item setLabel: NSLocalizedString(@"Start", "Start")];
        [item setImage: [NSImage imageNamed: @"Play"]];
        [item setTarget: self];
        [item setAction: @selector(convertGo:)];
    }
    else if ([ident isEqualToString: TOOLBAR_PAUSE])
    {
        [item setLabel: NSLocalizedString(@"Pause", "Pause")];
        [item setImage: [NSImage imageNamed: @"Pause"]];
        [item setTarget: self];
        [item setAction: @selector(pauseGo:)];
    }
    else if ([ident isEqualToString: TOOLBAR_OPEN])
    {
        [item setLabel: NSLocalizedString(@"Open", "Open")];
        [item setImage: [NSImage imageNamed: @"pref-audio"]];
        [item setTarget: self];
        [item setAction: @selector(openShow:)];
    }
    else if ([ident isEqualToString: TOOLBAR_ADVANCED])
    {
        [item setLabel: NSLocalizedString(@"Advanced", "Advanced")];
        [item setImage: [NSImage imageNamed: @"pref-advanced"]];
        [item setTarget: self];
        [item setAction: @selector(setPrefView:)];
        [item setAutovalidates: NO];
    }
    else
    {
        [item release];
        return nil;
    }

    return item;
}

- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
{
    return [NSArray arrayWithObjects: TOOLBAR_START, TOOLBAR_PAUSE,
                                        TOOLBAR_OPEN, NSToolbarFlexibleSpaceItemIdentifier, TOOLBAR_ADVANCED, nil];
}

- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
{
    return [NSArray arrayWithObjects: TOOLBAR_START, TOOLBAR_PAUSE,
                                        TOOLBAR_OPEN, TOOLBAR_ADVANCED, nil];
}

/***********************************************************************
 * Tableview datasource methods
 **********************************************************************/
- (int) numberOfRowsInTableView: (NSTableView *) t
{
    if( !fHandle )
        return 0;

    return hb_list_count( fList );
}

- (id) tableView:(NSTableView *) t objectValueForTableColumn:
    (NSTableColumn *) col row: (int) row
{
    if( [[col identifier] isEqualToString: @"Check"] )
    {
        return [fConvertCheckArray objectAtIndex: row];
    }
    else
    {
        hb_title_t * title = hb_list_item( fList, row );
        if( [[col identifier] isEqualToString: @"Title"] )
        {
            return [@"Title " stringByAppendingFormat: @"%d",
                    title->index];
        }
        else if( [[col identifier] isEqualToString: @"Duration"] )
        {
            if( title->hours > 0 )
            {
                return [NSString stringWithFormat:
                    @"%d hour%s %d min%s", title->hours,
                    title->hours > 1 ? "s" : "", title->minutes,
                    title->minutes > 1 ? "s": ""];
            }
            else if( title->minutes > 0 )
            {
                return [NSString stringWithFormat:
                    @"%d min%s %d sec%s", title->minutes,
                    title->minutes > 1 ? "s" : "", title->seconds,
                    title->seconds > 1 ? "s": ""];
            }
            else
            {
                return [NSString stringWithFormat: @"%d seconds",
                        title->seconds];
            }
        }
    }
    return nil;
}

- (void) tableView: (NSTableView *) t setObjectValue: (id) object
    forTableColumn: (NSTableColumn *) col row: (int) row
{
    if( [[col identifier] isEqualToString: @"Check"] )
    {
        [fConvertCheckArray replaceObjectAtIndex: row withObject: object];
    }
}

/***********************************************************************
 * User events methods
 **********************************************************************/
- (void) openShow: (id) sender
{
    NSRect frame  = [fWindow frame];
    float  offset = [fConvertView frame].size.height -
                    [fOpenView frame].size.height;

    frame.origin.y    += offset;
    frame.size.height -= offset;
    [fWindow setContentView: fEmptyView];
    [fWindow setFrame: frame display: YES animate: YES];
    [fToolbar setVisible:NO];
    [fWindow setContentView: fOpenView];

    [fDriveDetector run];
}

- (void) openMatrixChanged: (id) sender
{
    [self openEnable: YES];
    if( [fOpenMatrix selectedRow] )
    {
        [self openBrowse: self];
    }
}

- (void) openBrowse: (id) sender
{
    NSOpenPanel * panel = [NSOpenPanel openPanel];
    [panel setAllowsMultipleSelection: NO];
    [panel setCanChooseFiles: YES];
    [panel setCanChooseDirectories: YES ];
    [panel beginSheetForDirectory: nil file: nil types: nil
        modalForWindow: fWindow modalDelegate: self
        didEndSelector: @selector( openBrowseDidEnd:returnCode:contextInfo: )
        contextInfo: nil];                                                      
}

- (void) openGo: (id) sender
{
    [self openEnable: NO];
    [fOpenIndicator setIndeterminate: YES];
    [fOpenIndicator startAnimation: nil];
    [fOpenProgressField setStringValue: @"Opening..."];
    [fDriveDetector stop];

    if( [fOpenMatrix selectedRow] )
    {
        hb_scan( fHandle, [fOpenFolderString UTF8String], 0 );
    }
    else
    {
        hb_scan( fHandle, [[fDrives objectForKey: [fOpenPopUp
                 titleOfSelectedItem]] UTF8String], 0 );
    }
}

- (void) convertGo: (id) sender
{
    int i, j;

    for( i = 0; i < hb_list_count( fList ); i++ )
    {
        if( ![[fConvertCheckArray objectAtIndex: i] boolValue] )
            continue;

        hb_title_t * title = hb_list_item( fList, i );
        hb_job_t   * job   = title->job;

        int pixels = 307200;
		int aspect = title->aspect;
		if( [fConvertAspectPopUp indexOfSelectedItem] == 1)
		{
            aspect = 4 * HB_ASPECT_BASE / 3;
		}

		int maxwidth = 640;
		job->vbitrate = 1000;
		if( [fConvertMaxWidthPopUp indexOfSelectedItem] == 1)
		{
            maxwidth = 320;
			job->vbitrate = 500;
		}
		//job->deinterlace = 1;
		
		do
		{
			hb_set_size( job, aspect, pixels );
			pixels -= 10;
		} while(job->width > maxwidth);
		
        if( [fConvertFormatPopUp indexOfSelectedItem] == 0 )
        {
            /* iPod / H.264 */
            job->mux      = HB_MUX_IPOD;
            job->vcodec   = HB_VCODEC_X264;
			job->h264_level = 30;
        }
        else if( [fConvertFormatPopUp indexOfSelectedItem] == 1 )
        {
            /* iPod / MPEG-4 */
            job->mux      = HB_MUX_MP4;
            job->vcodec   = HB_VCODEC_FFMPEG;
        }
        else
        {
            /* PSP / MPEG-4 */
            job->mux        = HB_MUX_PSP;
            job->vrate      = 27000000;
            job->vrate_base = 900900;   /* 29.97 fps */
            job->vcodec     = HB_VCODEC_FFMPEG;
            job->vbitrate   = 600;
            pixels          = 76800;
            job->arate      = 24000;
            job->abitrate   = 96;
            aspect          = 16 * HB_ASPECT_BASE / 9;

			if( [fConvertAspectPopUp indexOfSelectedItem] )
			{
				aspect = -1;
			}

			hb_set_size( job, aspect, pixels );
        }

        job->vquality = -1.0;

        const char * lang;

        /* Audio selection */
        hb_audio_t * audio;
        lang = [[fConvertAudioPopUp titleOfSelectedItem] UTF8String];
        job->audios[0] = -1;
        for( j = 0; j < hb_list_count( title->list_audio ); j++ )
        {
            /* Choose the first track that matches the language */
            audio = hb_list_item( title->list_audio, j );
            if( !strcmp( lang, audio->lang_simple ) )
            {
                job->audios[0] = j;
                break;
            }
        }
        if( job->audios[0] == -1 )
        {
            /* If the language isn't available in this title, choose
               the first track */
            job->audios[0] = 0;
        }
        job->audios[1] = -1;

        job->audio_mixdowns[0] = HB_AMIXDOWN_DOLBYPLII;
        
        /* Subtitle selection */
        hb_subtitle_t * subtitle;
        lang = [[fConvertSubtitlePopUp titleOfSelectedItem] UTF8String];
        job->subtitle = -1;
        for( j = 0; j < hb_list_count( title->list_subtitle ); j++ )
        {
            /* Choose the first track that matches the language */
            subtitle = hb_list_item( title->list_subtitle, j );
            if( !strcmp( lang, subtitle->lang ) )
            {
                job->subtitle = j;
                break;
            }
        }
        
        job->file = strdup( [[NSString stringWithFormat:                 
                @"%@/%s - Title %d.m4v", fConvertFolderString,      
                title->name, title->index] UTF8String] );
        hb_add( fHandle, job );
    }

    hb_start( fHandle );

    [self convertEnable: NO];
}

- (void) convertCancel: (id) sender
{
    hb_stop( fHandle );
    [self convertEnable: YES];
}

@end

/***********************************************************************
 * Private methods
 **********************************************************************/

@implementation ExpressController (Private)

- (void) openUpdateDrives: (NSDictionary *) drives
{
    if( fDrives )
    {
        [fDrives release];
    }
    fDrives = [[NSDictionary alloc] initWithDictionary: drives];

    NSString * device;
    NSEnumerator * enumerator = [fDrives keyEnumerator];
    [fOpenPopUp removeAllItems];
    while( ( device = [enumerator nextObject] ) )
    {
        [fOpenPopUp addItemWithTitle: device];
    }

    if( ![fOpenPopUp numberOfItems] )
    {
        [fOpenPopUp addItemWithTitle: INSERT_STRING];
    }
    [fOpenPopUp selectItemAtIndex: 0];
    if( [fOpenMatrix isEnabled] )
    {
        [self openEnable: YES];
    }
}

- (void) openBrowseDidEnd: (NSOpenPanel *) sheet returnCode: (int)
    returnCode contextInfo: (void *) contextInfo
{
    if( returnCode != NSOKButton )
        return;

    if( fOpenFolderString )
        [fOpenFolderString release];
    fOpenFolderString = [[[sheet filenames] objectAtIndex: 0] retain];
    [fOpenFolderField setStringValue: [fOpenFolderString lastPathComponent]];
    [self openGo: self];
}

- (BOOL)validateToolbarItem: (NSToolbarItem *) toolbarItem
{
    NSString * ident = [toolbarItem itemIdentifier];
    
    if ([ident isEqualToString: TOOLBAR_START] && [HBStateWorking isEqualToString:[fCore state]])
    {
        [toolbarItem setAction: @selector(convertCancel:)];
        [toolbarItem setLabel:NSLocalizedString(@"Cancel", @"Cancel")];
        return YES;
    }
    else if ([ident isEqualToString: TOOLBAR_START] && [HBStateWorkDone isEqualToString:[fCore state]])
    {
        [toolbarItem setAction: @selector(convertGo:)];
        [toolbarItem setLabel:NSLocalizedString(@"Convert", @"Convert")];
        return YES;
    }
    else if ([ident isEqualToString: TOOLBAR_OPEN] && [HBStateWorking isEqualToString:[fCore state]])
    {
        return NO;
    }
    
    return YES;
}

- (void) openEnable: (BOOL) b
{
    [fOpenMatrix       setEnabled: b];
    [fOpenPopUp        setEnabled: b];
    [fOpenFolderField  setEnabled: b];
    [fOpenBrowseButton setEnabled: b];

    if( b )
    {
        if( [fOpenMatrix selectedRow] )
        {
            [fOpenPopUp setEnabled: NO];
        }
        else
        {
            [fOpenFolderField  setEnabled: NO];
            [fOpenBrowseButton setEnabled: NO];
            if( [[fOpenPopUp titleOfSelectedItem]
                    isEqualToString: INSERT_STRING] )
            {
            }
        }
    }
}

- (void) scanningSource: (NSNotification *) n
{
    [fOpenIndicator setIndeterminate: NO];
    [fOpenIndicator setDoubleValue: 100.0 *
            ( (float) p.scanning.title_cur - 0.5 ) / p.scanning.title_count];
    [fOpenProgressField setStringValue: [NSString
            stringWithFormat: @"Scanning title %d of %d...",
            p.scanning.title_cur, p.scanning.title_count]];
}

- (void) scanDone: (NSNotification *) n
{    
    [fOpenIndicator setIndeterminate: NO];
    [fOpenIndicator setDoubleValue: 0.0];
   
     [self openEnable: YES];

    if( hb_list_count( fList ) )
    {
        [self convertShow];
    }
    else
    {
        [fDriveDetector run];
    }
}

- (void) convertShow
{
    int i, j;

    fConvertCheckArray = [[NSMutableArray alloc] initWithCapacity:
        hb_list_count( fList )];
    [fConvertAudioPopUp removeAllItems];
    [fConvertSubtitlePopUp removeAllItems];
    [fConvertSubtitlePopUp addItemWithTitle: @"None"];
    for( i = 0; i < hb_list_count( fList ); i++ )
    {
        /* Default is to convert titles longer than 30 minutes. */
        hb_title_t * title = hb_list_item( fList, i );
        [fConvertCheckArray addObject: [NSNumber numberWithBool:
            ( 60 * title->hours + title->minutes > 30 )]];

        /* Update audio popup */
        hb_audio_t * audio;
        for( j = 0; j < hb_list_count( title->list_audio ); j++ )
        {
            audio = hb_list_item( title->list_audio, j );
            [fConvertAudioPopUp addItemWithTitle:
                [NSString stringWithUTF8String: audio->lang_simple]];
        }
		[fConvertAudioPopUp selectItemWithTitle: @"English"];
        
        if ( [fConvertAudioPopUp selectedItem] == nil )
            [fConvertAudioPopUp selectItemWithTitle: @"Unknown"];

        /* Update subtitle popup */
        hb_subtitle_t * subtitle;
        for( j = 0; j < hb_list_count( title->list_subtitle ); j++ )
        {
            subtitle = hb_list_item( title->list_subtitle, j );
            [fConvertSubtitlePopUp addItemWithTitle:
                [NSString stringWithUTF8String: subtitle->lang]];
        }
    }
    [fConvertTableView reloadData];

    NSRect frame  = [fWindow frame];
    float  offset = [fConvertView frame].size.height -
                    [fOpenView frame].size.height;
    float hoffset = [fConvertView frame].size.width -
                    [fOpenView frame].size.width;
    frame.origin.y    -= offset;
    frame.size.height += offset;
    frame.size.width += hoffset;
    [fWindow setContentView: fEmptyView];
    [fWindow setFrame: frame display: YES animate: YES];
    [fToolbar setVisible:YES];
    [fWindow setContentView: fConvertView];

    /* Folder popup */
    NSMenuItem * item = [fConvertFolderPopUp itemAtIndex: 0];
    [item setTitle: [fConvertFolderString lastPathComponent]];
    NSImage * image32 = [[NSWorkspace sharedWorkspace] iconForFile:
        fConvertFolderString];
    NSImage * image16 = [[NSImage alloc] initWithSize:
        NSMakeSize(16,16)];
    [image16 lockFocus];
    [[NSGraphicsContext currentContext]
        setImageInterpolation: NSImageInterpolationHigh];
    [image32 drawInRect: NSMakeRect(0,0,16,16)
        fromRect: NSMakeRect(0,0,32,32) operation: NSCompositeCopy
        fraction: 1.0];
    [image16 unlockFocus];                                                      
    [item setImage: image16];
    [image16 release];

    [self convertEnable: YES];
}

- (void) convertEnable: (BOOL) b
{
    [fConvertTableView setEnabled: b];
    [fConvertFolderPopUp setEnabled: b];
    [fConvertFormatPopUp setEnabled: b];
    [fConvertAspectPopUp setEnabled: b];
    [fConvertMaxWidthPopUp setEnabled: b];
    [fConvertAudioPopUp setEnabled: b];
    [fConvertSubtitlePopUp setEnabled: b];
    [fConvertOpenButton setEnabled: b];
}

/***********************************************************************
* UpdateDockIcon
***********************************************************************
* Shows a progression bar on the dock icon, filled according to
* 'progress' (0.0 <= progress <= 1.0).
* Called with progress < 0.0 or progress > 1.0, restores the original
* icon.
**********************************************************************/
- (void) UpdateDockIcon: (float) progress
{
    NSImage * icon;
    NSData * tiff;
    NSBitmapImageRep * bmp;
    uint32_t * pen;
    uint32_t black = htonl( 0x000000FF );
    uint32_t red   = htonl( 0xFF0000FF );
    uint32_t white = htonl( 0xFFFFFFFF );
    int row_start, row_end;
    int i, j;
	
    /* Get application original icon */
    icon = [NSImage imageNamed: @"NSApplicationIcon"];
	
    if( progress < 0.0 || progress > 1.0 )
    {
        [NSApp setApplicationIconImage: icon];
        return;
    }
	
    /* Get it in a raw bitmap form */
    tiff = [icon TIFFRepresentationUsingCompression:
					   NSTIFFCompressionNone factor: 1.0];
    bmp = [NSBitmapImageRep imageRepWithData: tiff];
    
    /* Draw the progression bar */
    /* It's pretty simple (ugly?) now, but I'm no designer */
	
    row_start = 3 * (int) [bmp size].height / 4;
    row_end   = 7 * (int) [bmp size].height / 8;
	
    for( i = row_start; i < row_start + 2; i++ )
    {
        pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
        for( j = 0; j < (int) [bmp size].width; j++ )
        {
            pen[j] = black;
        }
    }
    for( i = row_start + 2; i < row_end - 2; i++ )
    {
        pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
        pen[0] = black;
        pen[1] = black;
        for( j = 2; j < (int) [bmp size].width - 2; j++ )
        {
            if( j < 2 + (int) ( ( [bmp size].width - 4.0 ) * progress ) )
            {
                pen[j] = red;
            }
            else
            {
                pen[j] = white;
            }
        }
        pen[j]   = black;
        pen[j+1] = black;
    }
    for( i = row_end - 2; i < row_end; i++ )
    {
        pen = (uint32_t *) ( [bmp bitmapData] + i * [bmp bytesPerRow] );
        for( j = 0; j < (int) [bmp size].width; j++ )
        {
            pen[j] = black;
        }
    }
	
    /* Now update the dock icon */
    tiff = [bmp TIFFRepresentationUsingCompression:
					  NSTIFFCompressionNone factor: 1.0];
    icon = [[NSImage alloc] initWithData: tiff];
    [NSApp setApplicationIconImage: icon];
    [icon release];
}

- (void) working: (NSNotification *) n
{
   float progress_total = ( p.working.progress + p.working.job_cur - 1 ) / p.working.job_count;
            NSMutableString * string = [NSMutableString
                stringWithFormat: @"Converting: %.1f %%",
                100.0 * progress_total];
            if( p.working.seconds > -1 )
            {
                [string appendFormat: @" (%.1f fps, ", p.working.rate_avg];
                if( p.working.hours > 0 )
                {
                    [string appendFormat: @"%d hour%s %d min%s",
                        p.working.hours, p.working.hours == 1 ? "" : "s",
                        p.working.minutes, p.working.minutes == 1 ? "" : "s"];
                }
                else if( p.working.minutes > 0 )
                {
                    [string appendFormat: @"%d min%s %d sec%s",
                        p.working.minutes, p.working.minutes == 1 ? "" : "s",
                        p.working.seconds, p.working.seconds == 1 ? "" : "s"];
                }
                else
                {
                    [string appendFormat: @"%d second%s",
                        p.working.seconds, p.working.seconds == 1 ? "" : "s"];
                }
                [string appendString: @" left)"];
            }
            [fConvertInfoString setStringValue: string];
            [fConvertIndicator setIndeterminate: NO];
            [fConvertIndicator setDoubleValue: 100.0 * progress_total];
            [self UpdateDockIcon: progress_total];

}

- (void) muxing: (NSNotification *) n
{
    [fConvertInfoString setStringValue: NSLocalizedString(@"Muxing...",@"Muxing...")];
    [fConvertIndicator setIndeterminate: YES];
    [fConvertIndicator startAnimation: nil];
    [self UpdateDockIcon: 1.0];
}

- (void) workDone: (NSNotification *) n
{    
    [fConvertIndicator setIndeterminate: NO];
    [fConvertIndicator setDoubleValue: 0.0];
    [self UpdateDockIcon: -1.0];
    [self convertEnable: YES];
        
    [fConvertInfoString setStringValue: NSLocalizedString(@"Done.",@"Done.")];

    hb_job_t * job;
    while( ( job = hb_job( fHandle, 0 ) ) )
    {
        hb_rem( fHandle, job );
    }
}

@end