Added back the progress info in the queue, and refactored the SBQueueItem creation method. Added an edit contextual menu item.

This commit is contained in:
Damiano Galassi committed 2014-03-12 20:08:39 +01:00
1 parent 49954c92f7
commit 494f2f537e
6 files changed
+86 -62

No files matched your search

+1 -3
View File
@@ -32,9 +32,9 @@ extern NSString *SBQueueCancelledNotification;
SBQueueStatus _status; SBQueueStatus _status;
MP42File *_currentMP4; MP42File *_currentMP4;
NSUInteger _currentIndex;
NSMutableArray *_items; NSMutableArray *_items;
NSURL *_URL; NSURL *_URL;
NSURL *_destination;
IOPMAssertionID _assertionID; IOPMAssertionID _assertionID;
IOReturn _io_success; IOReturn _io_success;
@@ -44,8 +44,6 @@ extern NSString *SBQueueCancelledNotification;
} }
@property (atomic, readonly) SBQueueStatus status; @property (atomic, readonly) SBQueueStatus status;
@property (atomic, copy) NSURL *destination;
@property (atomic) BOOL optimize; @property (atomic) BOOL optimize;
- (instancetype)initWithURL:(NSURL *)queueURL; - (instancetype)initWithURL:(NSURL *)queueURL;
+38 -38
View File
@@ -16,8 +16,6 @@
#import <MP42Foundation/MP42Image.h> #import <MP42Foundation/MP42Image.h>
#import <MP42Foundation/MP42Utilities.h> #import <MP42Foundation/MP42Utilities.h>
static NSString *fileType = @"mp4";
NSString *SBQueueWorkingNotification = @"SBQueueWorkingNotification"; NSString *SBQueueWorkingNotification = @"SBQueueWorkingNotification";
NSString *SBQueueCompletedNotification = @"SBQueueCompletedNotification"; NSString *SBQueueCompletedNotification = @"SBQueueCompletedNotification";
NSString *SBQueueFailedNotification = @"SBQueueFailedNotification"; NSString *SBQueueFailedNotification = @"SBQueueFailedNotification";
@@ -30,14 +28,18 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
@property (nonatomic, copy) NSURL *URL; @property (nonatomic, copy) NSURL *URL;
@property (nonatomic, retain) NSMutableArray *items; @property (nonatomic, retain) NSMutableArray *items;
@property (atomic, retain) MP42File *currentMP4;
@property (nonatomic) NSUInteger currentIndex;
@property (nonatomic) dispatch_queue_t queue; @property (nonatomic) dispatch_queue_t queue;
@end @end
@implementation SBQueue @implementation SBQueue
@synthesize status = _status, destination = _destination; @synthesize status = _status;
@synthesize optimize = _optimize; @synthesize optimize = _optimize;
@synthesize currentMP4 = _currentMP4, currentIndex = _currentIndex;
@synthesize items = _items, URL = _URL, queue = _queue; @synthesize items = _items, URL = _URL, queue = _queue;
- (instancetype)initWithURL:(NSURL *)queueURL { - (instancetype)initWithURL:(NSURL *)queueURL {
@@ -151,6 +153,9 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
} }
} }
/*
* Starts the queue.
*/
- (void)start { - (void)start {
if (self.status == SBQueueStatusWorking) { if (self.status == SBQueueStatusWorking) {
return; return;
@@ -169,6 +174,11 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
@autoreleasepool { @autoreleasepool {
__block SBQueueItem *item = nil; __block SBQueueItem *item = nil;
// Save the queue
dispatch_async(dispatch_get_main_queue(), ^{
[self saveQueueToDisk];
});
// Get the first item available in the queue // Get the first item available in the queue
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
item = [[self firstItemInQueue] retain]; item = [[self firstItemInQueue] retain];
@@ -179,7 +189,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
break; break;
} }
[self handleSBStatusWorking]; [self handleSBStatusWorking:0];
noErr = [self processItem:item error:&outError]; noErr = [self processItem:item error:&outError];
// Check results // Check results
@@ -190,7 +200,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
break; break;
} else if (noErr) { } else if (noErr) {
if (self.optimize) { if (self.optimize) {
noErr = [_currentMP4 optimize]; noErr = [self.currentMP4 optimize];
} }
} }
@@ -198,17 +208,8 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
item.status = SBQueueItemStatusCompleted; item.status = SBQueueItemStatusCompleted;
} else { } else {
item.status = SBQueueItemStatusFailed; item.status = SBQueueItemStatusFailed;
if (outError) { [self handleSBStatusFailed:outError];
dispatch_async(dispatch_get_main_queue(), ^{
[NSApp presentError:outError];
});
} }
}
// Save the queue
dispatch_async(dispatch_get_main_queue(), ^{
[self saveQueueToDisk];
});
[item release]; [item release];
} }
@@ -242,38 +243,31 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
BOOL noErr = YES; BOOL noErr = YES;
_currentMP4 = [item mp4File]; self.currentMP4 = [item mp4File];
[_currentMP4 setDelegate:self]; self.currentIndex = [self.items indexOfObject:item];
// 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];
}
}
// The file has been added directly to the queue // The file has been added directly to the queue
if (!_currentMP4 && item.URL) { if (!_currentMP4 && item.URL) {
[item prepareItem:outError]; [item prepareItem:outError];
_currentMP4 = item.mp4File; self.currentMP4 = item.mp4File;
} }
self.currentMP4.delegate = self;
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[_currentMP4.URL path] error:NULL]; NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[_currentMP4.URL path] error:NULL];
NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize]; NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize];
if (freeSpace && [_currentMP4 dataSize] > [freeSpace longLongValue]) { if (freeSpace && [_currentMP4 dataSize] > [freeSpace longLongValue]) {
NSLog(@"Not enough disk space"); NSLog(@"Not enough disk space");
} }
// We have an existing mp4 file, update it
if (!_cancelled) { if (!_cancelled) {
if ([_currentMP4 hasFileRepresentation]) if ([self.currentMP4 hasFileRepresentation]) {
noErr = [_currentMP4 updateMP4FileWithAttributes:attributes error:outError]; // 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 // Write the new file to disk
else if (_currentMP4 && item.destURL) { [attributes addEntriesFromDictionary:item.attributes];
[attributes addEntriesFromDictionary:[item attributes]]; noErr = [self.currentMP4 writeToUrl:item.destURL
noErr = [_currentMP4 writeToUrl:[item destURL]
withAttributes:attributes withAttributes:attributes
error:outError]; error:outError];
} }
@@ -288,20 +282,26 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
return noErr; return noErr;
} }
/**
* Stops the queue and abort the current work.
*/
- (void)stop { - (void)stop {
_cancelled = YES; _cancelled = YES;
[self.currentMP4 cancel];
} }
- (void)progressStatus:(CGFloat)progress { - (void)progressStatus:(CGFloat)progress {
NSLog(@"%f", progress); [self handleSBStatusWorking:progress];
} }
/** /**
* Processes SBQueueStatusWorking state information. Current implementation just * Processes SBQueueStatusWorking state information. Current implementation just
* sends SBQueueWorkingNotification. * sends SBQueueWorkingNotification.
*/ */
- (void)handleSBStatusWorking { - (void)handleSBStatusWorking:(CGFloat)progress {
[[NSNotificationCenter defaultCenter] postNotificationName:SBQueueWorkingNotification object:self]; 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 * Processes SBQueueStatusFailed state information. Current implementation just
* sends SBQueueFailedNotification. * sends SBQueueFailedNotification.
*/ */
- (void)handleSBStatusFailed { - (void)handleSBStatusFailed:(NSError *)error {
self.status = SBQueueStatusFailed; self.status = SBQueueStatusFailed;
[[NSNotificationCenter defaultCenter] postNotificationName:SBQueueFailedNotification object:self]; [[NSNotificationCenter defaultCenter] postNotificationName:SBQueueFailedNotification object:self userInfo:@{@"Error" : error}];
} }
/** /**
-3
View File
@@ -46,9 +46,6 @@
+ (SBQueueController *)sharedManager; + (SBQueueController *)sharedManager;
- (void)start:(id)sender;
- (void)stop:(id)sender;
- (void)addItem:(SBQueueItem *)item; - (void)addItem:(SBQueueItem *)item;
- (BOOL)saveQueueToDisk; - (BOOL)saveQueueToDisk;
+38 -17
View File
@@ -13,6 +13,7 @@
#import <MP42Foundation/MP42Utilities.h> #import <MP42Foundation/MP42Utilities.h>
static NSString *fileType = @"mp4";
#define SublerBatchTableViewDataType @"SublerBatchTableViewDataType" #define SublerBatchTableViewDataType @"SublerBatchTableViewDataType"
#define kOptionsPanelHeight 88 #define kOptionsPanelHeight 88
@@ -21,6 +22,9 @@
@property (readonly) SBQueue *queue; @property (readonly) SBQueue *queue;
- (void)start:(id)sender;
- (void)stop:(id)sender;
- (void)updateUI; - (void)updateUI;
- (void)updateDockTile; - (void)updateDockTile;
- (NSURL *)queueURL; - (NSURL *)queueURL;
@@ -86,16 +90,21 @@
[_tableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, SublerBatchTableViewDataType, nil]]; [_tableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, SublerBatchTableViewDataType, nil]];
// Register to the queue notifications
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:SBQueueWorkingNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) { [[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) { [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueCompletedNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
[_progressIndicator setHidden:YES]; [_progressIndicator setHidden:YES];
[_progressIndicator stopAnimation:self]; [_progressIndicator stopAnimation:self];
[_progressIndicator setDoubleValue:0]; [_progressIndicator setDoubleValue:0];
[_progressIndicator setIndeterminate:YES];
[_start setTitle:@"Start"]; [_start setTitle:@"Start"];
[_countLabel setStringValue:@"Done"]; [_countLabel setStringValue:@"Done"];
@@ -103,6 +112,11 @@
[self updateUI]; [self updateUI];
}]; }];
[[NSNotificationCenter defaultCenter] addObserverForName:SBQueueFailedNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
NSDictionary *info = [note userInfo];
[NSApp presentError:[info valueForKey:@"Error"]];
}];
[self updateUI]; [self updateUI];
} }
@@ -278,15 +292,7 @@
NSMutableArray *items = [[NSMutableArray alloc] init]; NSMutableArray *items = [[NSMutableArray alloc] init];
for (NSURL *url in [panel URLs]) { for (NSURL *url in [panel URLs]) {
SBQueueItem *item = [SBQueueItem itemWithURL:url]; SBQueueItem *item = [self createItemWithURL: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]];
}
[items addObject:item]; [items addObject:item];
} }
@@ -412,20 +418,19 @@
} }
- (IBAction)edit:(id)sender { - (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 removeItems:[NSArray arrayWithObject:item]];
[self updateUI]; [self updateUI];
MP42File *mp4;
if (!item.mp4File) if (!item.mp4File)
mp4 = [self prepareQueueItem:item.URL error:NULL]; [item prepareItem:NULL];
else
mp4 = item.mp4File; MP42File *mp4 = item.mp4File;
SBDocument *doc = [[NSDocumentController sharedDocumentController] openUntitledDocumentAndDisplay:YES error:NULL]; SBDocument *doc = [[NSDocumentController sharedDocumentController] openUntitledDocumentAndDisplay:YES error:NULL];
[doc setMp4File:mp4]; [doc setMp4File:mp4];
[item release];*/ [item release];
} }
- (IBAction)showInFinder:(id)sender { - (IBAction)showInFinder:(id)sender {
@@ -546,7 +551,7 @@
NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init]; NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init];
for (NSURL *url in items) { for (NSURL *url in items) {
[queueItems addObject:[SBQueueItem itemWithURL:url]]; [queueItems addObject:[self createItemWithURL:url]];
[indexes addIndex:row]; [indexes addIndex:row];
} }
@@ -566,6 +571,22 @@
return NO; 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 { - (void)addItem:(SBQueueItem *)item {
[self addItems:[NSArray arrayWithObject:item] atIndexes:nil]; [self addItems:[NSArray arrayWithObject:item] atIndexes:nil];
[self updateUI]; [self updateUI];
+2 -1
View File
@@ -15,6 +15,7 @@
typedef enum SBQueueItemStatus : NSInteger { typedef enum SBQueueItemStatus : NSInteger {
SBQueueItemtatusUnknown = 0, SBQueueItemtatusUnknown = 0,
SBQueueItemStatusReady, SBQueueItemStatusReady,
SBQueueItemStatusEditing,
SBQueueItemStatusWorking, SBQueueItemStatusWorking,
SBQueueItemStatusCompleted, SBQueueItemStatusCompleted,
SBQueueItemStatusFailed, SBQueueItemStatusFailed,
@@ -34,7 +35,7 @@ typedef enum SBQueueItemStatus : NSInteger {
@property (nonatomic, readonly) MP42File *mp4File; @property (nonatomic, readonly) MP42File *mp4File;
@property (nonatomic, readonly) NSDictionary *attributes; @property (nonatomic, readonly) NSDictionary *attributes;
@property (nonatomic, readonly) NSURL *URL; @property (nonatomic, readonly) NSURL *URL;
@property (nonatomic, retain, readwrite) NSURL *destURL; @property (nonatomic, copy, readwrite) NSURL *destURL;
@property (nonatomic, readwrite) SBQueueItemStatus status; @property (nonatomic, readwrite) SBQueueItemStatus status;
+7
View File
@@ -256,6 +256,13 @@
</connections> </connections>
</menuItem> </menuItem>
<menuItem isSeparatorItem="YES" id="U9H-ZG-j7u"/> <menuItem isSeparatorItem="YES" id="U9H-ZG-j7u"/>
<menuItem title="Edit…" id="qHz-Zh-l5L">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="edit:" target="-2" id="4Pr-Wx-RXw"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="NnU-9u-KGQ"/>
<menuItem title="Reveal in Finder" id="8gi-57-i6u"> <menuItem title="Reveal in Finder" id="8gi-57-i6u">
<modifierMask key="keyEquivalentModifierMask"/> <modifierMask key="keyEquivalentModifierMask"/>
<connections> <connections>