From 494f2f537edb02a32a4c241fa1e7a24d0e70bb91 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Wed, 12 Mar 2014 20:08:39 +0100 Subject: [PATCH] Added back the progress info in the queue, and refactored the SBQueueItem creation method. Added an edit contextual menu item. --- Classes/SBQueue.h | 4 +- Classes/SBQueue.m | 82 ++++++++++++++++++------------------- Classes/SBQueueController.h | 3 -- Classes/SBQueueController.m | 55 +++++++++++++++++-------- Classes/SBQueueItem.h | 3 +- English.lproj/Queue.xib | 7 ++++ 6 files changed, 89 insertions(+), 65 deletions(-) diff --git a/Classes/SBQueue.h b/Classes/SBQueue.h index 8ba14cb..434c42c 100644 --- a/Classes/SBQueue.h +++ b/Classes/SBQueue.h @@ -32,9 +32,9 @@ extern NSString *SBQueueCancelledNotification; SBQueueStatus _status; MP42File *_currentMP4; + NSUInteger _currentIndex; NSMutableArray *_items; NSURL *_URL; - NSURL *_destination; IOPMAssertionID _assertionID; IOReturn _io_success; @@ -44,8 +44,6 @@ extern NSString *SBQueueCancelledNotification; } @property (atomic, readonly) SBQueueStatus status; - -@property (atomic, copy) NSURL *destination; @property (atomic) BOOL optimize; - (instancetype)initWithURL:(NSURL *)queueURL; diff --git a/Classes/SBQueue.m b/Classes/SBQueue.m index 994bfbc..648e7ff 100644 --- a/Classes/SBQueue.m +++ b/Classes/SBQueue.m @@ -16,8 +16,6 @@ #import #import -static NSString *fileType = @"mp4"; - NSString *SBQueueWorkingNotification = @"SBQueueWorkingNotification"; NSString *SBQueueCompletedNotification = @"SBQueueCompletedNotification"; NSString *SBQueueFailedNotification = @"SBQueueFailedNotification"; @@ -30,14 +28,18 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; @property (nonatomic, copy) NSURL *URL; @property (nonatomic, retain) NSMutableArray *items; +@property (atomic, retain) MP42File *currentMP4; +@property (nonatomic) NSUInteger currentIndex; + @property (nonatomic) dispatch_queue_t queue; @end @implementation SBQueue -@synthesize status = _status, destination = _destination; +@synthesize status = _status; @synthesize optimize = _optimize; +@synthesize currentMP4 = _currentMP4, currentIndex = _currentIndex; @synthesize items = _items, URL = _URL, queue = _queue; - (instancetype)initWithURL:(NSURL *)queueURL { @@ -151,6 +153,9 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; } } +/* + * Starts the queue. + */ - (void)start { if (self.status == SBQueueStatusWorking) { return; @@ -169,6 +174,11 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; @autoreleasepool { __block SBQueueItem *item = nil; + // Save the queue + dispatch_async(dispatch_get_main_queue(), ^{ + [self saveQueueToDisk]; + }); + // Get the first item available in the queue dispatch_sync(dispatch_get_main_queue(), ^{ item = [[self firstItemInQueue] retain]; @@ -179,7 +189,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; break; } - [self handleSBStatusWorking]; + [self handleSBStatusWorking:0]; noErr = [self processItem:item error:&outError]; // Check results @@ -190,7 +200,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; break; } else if (noErr) { if (self.optimize) { - noErr = [_currentMP4 optimize]; + noErr = [self.currentMP4 optimize]; } } @@ -198,18 +208,9 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; item.status = SBQueueItemStatusCompleted; } else { item.status = SBQueueItemStatusFailed; - if (outError) { - dispatch_async(dispatch_get_main_queue(), ^{ - [NSApp presentError:outError]; - }); - } + [self handleSBStatusFailed:outError]; } - // Save the queue - dispatch_async(dispatch_get_main_queue(), ^{ - [self saveQueueToDisk]; - }); - [item release]; } @@ -242,40 +243,33 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; BOOL noErr = YES; - _currentMP4 = [item mp4File]; - [_currentMP4 setDelegate:self]; - - // Set the destination url - if (![item destURL]) { - if (!_currentMP4 && self.destination /*&& customDestination*/) { - item.destURL = [[[self.destination URLByAppendingPathComponent:[item.URL lastPathComponent]] URLByDeletingPathExtension] URLByAppendingPathExtension:fileType]; - } else { - item.destURL = [[item.URL URLByDeletingPathExtension] URLByAppendingPathExtension:fileType]; - } - } + self.currentMP4 = [item mp4File]; + self.currentIndex = [self.items indexOfObject:item]; // The file has been added directly to the queue if (!_currentMP4 && item.URL) { [item prepareItem:outError]; - _currentMP4 = item.mp4File; + self.currentMP4 = item.mp4File; } + self.currentMP4.delegate = self; + NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[_currentMP4.URL path] error:NULL]; NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize]; if (freeSpace && [_currentMP4 dataSize] > [freeSpace longLongValue]) { NSLog(@"Not enough disk space"); } - // We have an existing mp4 file, update it if (!_cancelled) { - if ([_currentMP4 hasFileRepresentation]) - noErr = [_currentMP4 updateMP4FileWithAttributes:attributes error:outError]; - // Write the new file to disk - else if (_currentMP4 && item.destURL) { - [attributes addEntriesFromDictionary:[item attributes]]; - noErr = [_currentMP4 writeToUrl:[item destURL] - withAttributes:attributes - error:outError]; + if ([self.currentMP4 hasFileRepresentation]) { + // We have an existing mp4 file, update it + noErr = [self.currentMP4 updateMP4FileWithAttributes:attributes error:outError]; + } else if (self.currentMP4 && item.destURL) { + // Write the new file to disk + [attributes addEntriesFromDictionary:item.attributes]; + noErr = [self.currentMP4 writeToUrl:item.destURL + withAttributes:attributes + error:outError]; } } @@ -288,20 +282,26 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; return noErr; } +/** + * Stops the queue and abort the current work. + */ - (void)stop { _cancelled = YES; + [self.currentMP4 cancel]; } - (void)progressStatus:(CGFloat)progress { - NSLog(@"%f", progress); + [self handleSBStatusWorking:progress]; } /** * Processes SBQueueStatusWorking state information. Current implementation just * sends SBQueueWorkingNotification. */ -- (void)handleSBStatusWorking { - [[NSNotificationCenter defaultCenter] postNotificationName:SBQueueWorkingNotification object:self]; +- (void)handleSBStatusWorking:(CGFloat)progress { + NSString *info = [NSString stringWithFormat:@"Processing file %ld of %lu.",(long)self.currentIndex + 1, (unsigned long)[self.items count]]; + [[NSNotificationCenter defaultCenter] postNotificationName:SBQueueWorkingNotification object:self userInfo:@{@"ProgressString": info, + @"Progress": @(progress)}]; } /** @@ -317,9 +317,9 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; * Processes SBQueueStatusFailed state information. Current implementation just * sends SBQueueFailedNotification. */ -- (void)handleSBStatusFailed { +- (void)handleSBStatusFailed:(NSError *)error { self.status = SBQueueStatusFailed; - [[NSNotificationCenter defaultCenter] postNotificationName:SBQueueFailedNotification object:self]; + [[NSNotificationCenter defaultCenter] postNotificationName:SBQueueFailedNotification object:self userInfo:@{@"Error" : error}]; } /** diff --git a/Classes/SBQueueController.h b/Classes/SBQueueController.h index 2192775..0af97a1 100755 --- a/Classes/SBQueueController.h +++ b/Classes/SBQueueController.h @@ -46,9 +46,6 @@ + (SBQueueController *)sharedManager; -- (void)start:(id)sender; -- (void)stop:(id)sender; - - (void)addItem:(SBQueueItem *)item; - (BOOL)saveQueueToDisk; diff --git a/Classes/SBQueueController.m b/Classes/SBQueueController.m index 4d483b0..8b14081 100755 --- a/Classes/SBQueueController.m +++ b/Classes/SBQueueController.m @@ -13,6 +13,7 @@ #import +static NSString *fileType = @"mp4"; #define SublerBatchTableViewDataType @"SublerBatchTableViewDataType" #define kOptionsPanelHeight 88 @@ -21,6 +22,9 @@ @property (readonly) SBQueue *queue; +- (void)start:(id)sender; +- (void)stop:(id)sender; + - (void)updateUI; - (void)updateDockTile; - (NSURL *)queueURL; @@ -86,16 +90,21 @@ [_tableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, SublerBatchTableViewDataType, nil]]; + // Register to the queue notifications NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueWorkingNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) { - [_countLabel setStringValue:@"Working"]; + NSDictionary *info = [note userInfo]; + [_countLabel setStringValue:[info valueForKey:@"ProgressString"]]; + [_progressIndicator setIndeterminate:NO]; + [_progressIndicator setDoubleValue:[[info valueForKey:@"Progress"] doubleValue]]; }]; [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueCompletedNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) { [_progressIndicator setHidden:YES]; [_progressIndicator stopAnimation:self]; [_progressIndicator setDoubleValue:0]; + [_progressIndicator setIndeterminate:YES]; [_start setTitle:@"Start"]; [_countLabel setStringValue:@"Done"]; @@ -103,6 +112,11 @@ [self updateUI]; }]; + [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueFailedNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) { + NSDictionary *info = [note userInfo]; + [NSApp presentError:[info valueForKey:@"Error"]]; + }]; + [self updateUI]; } @@ -278,15 +292,7 @@ NSMutableArray *items = [[NSMutableArray alloc] init]; for (NSURL *url in [panel URLs]) { - SBQueueItem *item = [SBQueueItem itemWithURL:url]; - if ([_metadataOption state] == NSOnState) { - [item addAction:[[[SBQueueMetadataAction alloc] init] autorelease]]; - [item addAction:[[[SBQueueSubtitlesAction alloc] init] autorelease]]; - } - if ([_organizeGroupsOption state] == NSOnState) { - [item addAction:[[[SBQueueOrganizeGroupsAction alloc] init] autorelease]]; - } - + SBQueueItem *item = [self createItemWithURL:url]; [items addObject:item]; } @@ -412,20 +418,19 @@ } - (IBAction)edit:(id)sender { - /*SBQueueItem *item = [[self.queue itemAtIndex:[tableView clickedRow]] retain]; + SBQueueItem *item = [[self.queue itemAtIndex:[_tableView clickedRow]] retain]; [self removeItems:[NSArray arrayWithObject:item]]; [self updateUI]; - MP42File *mp4; if (!item.mp4File) - mp4 = [self prepareQueueItem:item.URL error:NULL]; - else - mp4 = item.mp4File; + [item prepareItem:NULL]; + + MP42File *mp4 = item.mp4File; SBDocument *doc = [[NSDocumentController sharedDocumentController] openUntitledDocumentAndDisplay:YES error:NULL]; [doc setMp4File:mp4]; - [item release];*/ + [item release]; } - (IBAction)showInFinder:(id)sender { @@ -546,7 +551,7 @@ NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init]; for (NSURL *url in items) { - [queueItems addObject:[SBQueueItem itemWithURL:url]]; + [queueItems addObject:[self createItemWithURL:url]]; [indexes addIndex:row]; } @@ -566,6 +571,22 @@ return NO; } +- (SBQueueItem *)createItemWithURL:(NSURL *)url { + SBQueueItem *item = [SBQueueItem itemWithURL:url]; + + if ([_metadataOption state] == NSOnState) { + [item addAction:[[[SBQueueMetadataAction alloc] init] autorelease]]; + [item addAction:[[[SBQueueSubtitlesAction alloc] init] autorelease]]; + } + if ([_organizeGroupsOption state] == NSOnState) { + [item addAction:[[[SBQueueOrganizeGroupsAction alloc] init] autorelease]]; + } + + item.destURL = [[[_destination URLByAppendingPathComponent:[url lastPathComponent]] URLByDeletingPathExtension] URLByAppendingPathExtension:fileType]; + + return item; +} + - (void)addItem:(SBQueueItem *)item { [self addItems:[NSArray arrayWithObject:item] atIndexes:nil]; [self updateUI]; diff --git a/Classes/SBQueueItem.h b/Classes/SBQueueItem.h index 8c694e8..524d985 100755 --- a/Classes/SBQueueItem.h +++ b/Classes/SBQueueItem.h @@ -15,6 +15,7 @@ typedef enum SBQueueItemStatus : NSInteger { SBQueueItemtatusUnknown = 0, SBQueueItemStatusReady, + SBQueueItemStatusEditing, SBQueueItemStatusWorking, SBQueueItemStatusCompleted, SBQueueItemStatusFailed, @@ -34,7 +35,7 @@ typedef enum SBQueueItemStatus : NSInteger { @property (nonatomic, readonly) MP42File *mp4File; @property (nonatomic, readonly) NSDictionary *attributes; @property (nonatomic, readonly) NSURL *URL; -@property (nonatomic, retain, readwrite) NSURL *destURL; +@property (nonatomic, copy, readwrite) NSURL *destURL; @property (nonatomic, readwrite) SBQueueItemStatus status; diff --git a/English.lproj/Queue.xib b/English.lproj/Queue.xib index f721860..d5571b6 100755 --- a/English.lproj/Queue.xib +++ b/English.lproj/Queue.xib @@ -256,6 +256,13 @@ + + + + + + +