Made SBQueue independent from MP42File, moved the actual item processing to SBQueueItem.

This commit is contained in:
Damiano Galassi committed 2014-03-20 20:56:31 +01:00
1 parent d71d0b48c7
commit 2fef5d5200
4 files changed
+77 -77

No files matched your search

-2
View File
@@ -11,8 +11,6 @@
#import "SBQueueItem.h"
@class MP42File;
typedef NS_ENUM(NSUInteger, SBQueueStatus) {
SBQueueStatusUnknown = 0,
SBQueueStatusWorking,
+4 -67
View File
@@ -8,19 +8,12 @@
#import "SBQueue.h"
#import "MetadataImporter.h"
#import <MP42Foundation/MP42File.h>
#import <MP42Foundation/MP42FileImporter.h>
#import <MP42Foundation/MP42Image.h>
#import <MP42Foundation/MP42Utilities.h>
NSString *SBQueueWorkingNotification = @"SBQueueWorkingNotification";
NSString *SBQueueCompletedNotification = @"SBQueueCompletedNotification";
NSString *SBQueueFailedNotification = @"SBQueueFailedNotification";
NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
@interface SBQueue () <MP42FileDelegate>
@interface SBQueue ()
@property (atomic) SBQueueStatus status;
@@ -199,9 +192,10 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
self.currentIndex = [self.items indexOfObject:self.currentItem];
self.currentItem.status = SBQueueItemStatusWorking;
self.currentItem.delegate = self;
[self handleSBStatusWorking:0 index:self.currentIndex];
noErr = [self processItem:self.currentItem optimize:self.optimize error:&outError];
noErr = [self.currentItem processItem:self.optimize error:&outError];
// Check results
if (self.cancelled) {
@@ -218,6 +212,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
[self handleSBStatusFailed:outError];
}
self.currentItem.delegate = nil;
self.currentItem = nil;
}
@@ -235,64 +230,6 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
});
}
/**
* Processes a SBQueueItem.
*/
- (BOOL)processItem:(SBQueueItem *)item optimize:(BOOL)optimize error:(NSError **)outError {
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue])
[attributes setObject:@YES forKey:MP42GenerateChaptersPreviewTrack];
#ifdef SB_SANDBOX
if([destination respondsToSelector:@selector(startAccessingSecurityScopedResource)])
[destination startAccessingSecurityScopedResource];
#endif
BOOL noErr = YES;
MP42File *file = nil;
// The file has been added directly to the queue
if (!item.mp4File && item.URL) {
[item prepareItem:outError];
}
file = item.mp4File;
file.delegate = self;
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[file.URL path] error:NULL];
NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize];
if (freeSpace && [file dataSize] > [freeSpace longLongValue]) {
NSLog(@"Not enough disk space");
[self stop];
}
if (!self.cancelled) {
if ([file hasFileRepresentation]) {
// We have an existing mp4 file, update it
noErr = [file updateMP4FileWithAttributes:attributes error:outError];
} else if (file && item.destURL) {
// Write the new file to disk
[attributes addEntriesFromDictionary:item.attributes];
noErr = [file writeToUrl:item.destURL
withAttributes:attributes
error:outError];
}
}
if (noErr && optimize) {
[file optimize];
}
#ifdef SB_SANDBOX
if([destination respondsToSelector:@selector(stopAccessingSecurityScopedResource)])
[destination stopAccessingSecurityScopedResource];
#endif
[attributes release];
return noErr;
}
/**
* Stops the queue and abort the current work.
*/
+10 -7
View File
@@ -7,7 +7,6 @@
//
#import <Foundation/Foundation.h>
#import "SBQueueAction.h"
@class MP42File;
@@ -30,29 +29,33 @@ typedef enum SBQueueItemStatus : NSInteger {
NSDictionary *_attributes;
NSMutableArray *_actions;
BOOL _cancelled;
id _delegate;
SBQueueItemStatus _status;
}
@property (nonatomic, readwrite) SBQueueItemStatus status;
@property (nonatomic, readonly) MP42File *mp4File;
@property (nonatomic, readonly) NSDictionary *attributes;
@property (nonatomic, readonly) NSURL *URL;
@property (nonatomic, copy) NSURL *destURL;
@property (nonatomic, readonly) NSMutableArray *actions;
@property (nonatomic, readonly) NSDictionary *attributes;
@property (nonatomic, readwrite) SBQueueItemStatus status;
@property (nonatomic, assign) id delegate;
- (instancetype)initWithURL:(NSURL *)URL;
+ (instancetype)itemWithURL:(NSURL *)URL;
- (instancetype)initWithMP4:(MP42File *)MP4;
- (instancetype)initWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict;
+ (instancetype)itemWithMP4:(MP42File *)MP4;
+ (instancetype)itemWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict;
- (void)addAction:(id<SBQueueActionProtocol>)action;
- (BOOL)prepareItem:(NSError **)outError;
- (BOOL)processItem:(BOOL)optimize error:(NSError **)outError;
@end
+63 -1
View File
@@ -13,10 +13,11 @@
#import <MP42Foundation/MP42FileImporter.h>
#import <MP42Foundation/MP42Utilities.h>
@interface SBQueueItem ()
@interface SBQueueItem () <MP42FileDelegate>
@property (nonatomic, readwrite, retain) MP42File *mp4File;
@property (nonatomic, readwrite) NSMutableArray *actions;
@property (atomic) BOOL cancelled;
@end
@@ -26,11 +27,14 @@
@synthesize attributes = _attributes;
@synthesize actions = _actions;
@synthesize cancelled = _cancelled;
@synthesize URL = _fileURL;
@synthesize destURL = _destURL;
@synthesize mp4File = _mp4File;
@synthesize delegate = _delegate;
- (instancetype)init
{
self = [super init];
@@ -164,6 +168,64 @@
return YES;
}
- (BOOL)processItem:(BOOL)optimize error:(NSError **)outError {
#ifdef SB_SANDBOX
if([destination respondsToSelector:@selector(startAccessingSecurityScopedResource)])
[destination startAccessingSecurityScopedResource];
#endif
BOOL noErr = YES;
// The file has been added directly to the queue
if (!self.mp4File && self.URL) {
[self prepareItem:outError];
}
self.mp4File.delegate = self;
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[self.mp4File.URL path] error:NULL];
NSNumber *freeSpace = [dict objectForKey:NSFileSystemFreeSize];
if (freeSpace && [self.mp4File dataSize] > [freeSpace longLongValue]) {
NSLog(@"Not enough disk space");
[self cancel];
}
if (!self.cancelled) {
if ([self.mp4File hasFileRepresentation]) {
// We have an existing mp4 file, update it
noErr = [self.mp4File updateMP4FileWithAttributes:self.attributes error:outError];
} else if (self.mp4File && self.destURL) {
// Write the new file to disk
noErr = [self.mp4File writeToUrl:self.destURL
withAttributes:self.attributes
error:outError];
}
}
if (noErr && optimize) {
noErr = [self.mp4File optimize];
}
#ifdef SB_SANDBOX
if([destination respondsToSelector:@selector(stopAccessingSecurityScopedResource)])
[destination stopAccessingSecurityScopedResource];
#endif
self.mp4File = nil;
[self.actions removeAllObjects];
return noErr;
}
- (void)cancel {
self.cancelled = YES;
[self.mp4File cancel];
}
- (void)progressStatus:(CGFloat)progress {
[self.delegate progressStatus:progress];
}
- (void)dealloc {
[_attributes release];
[_actions release];