From f63771a4e00c2c438d194831886213fad98e27ae Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Wed, 12 Mar 2014 09:36:57 +0100 Subject: [PATCH] First part of a new effort to rationalize the queue and to remove the / dichotomy. ue --- Classes/SBQueue.h | 4 - Classes/SBQueue.m | 183 ++--------------------------- Classes/SBQueueAction.h | 24 ++++ Classes/SBQueueAction.m | 155 +++++++++++++++++++++++++ Classes/SBQueueController.h | 12 +- Classes/SBQueueController.m | 92 ++++++++------- Classes/SBQueueItem.h | 28 +++-- Classes/SBQueueItem.m | 192 ++++++++++++++++++++----------- English.lproj/Queue.xib | 7 +- Subler.xcodeproj/project.pbxproj | 26 ++++- 10 files changed, 410 insertions(+), 313 deletions(-) create mode 100644 Classes/SBQueueAction.h create mode 100644 Classes/SBQueueAction.m diff --git a/Classes/SBQueue.h b/Classes/SBQueue.h index 0578690..8ba14cb 100644 --- a/Classes/SBQueue.h +++ b/Classes/SBQueue.h @@ -41,16 +41,12 @@ extern NSString *SBQueueCancelledNotification; BOOL _cancelled; BOOL _optimize; - BOOL _fetchMetadata; - BOOL _organizeGroups; } @property (atomic, readonly) SBQueueStatus status; @property (atomic, copy) NSURL *destination; @property (atomic) BOOL optimize; -@property (atomic) BOOL fetchMetadata; -@property (atomic) BOOL organizeGroups; - (instancetype)initWithURL:(NSURL *)queueURL; diff --git a/Classes/SBQueue.m b/Classes/SBQueue.m index 03fc12a..994bfbc 100644 --- a/Classes/SBQueue.m +++ b/Classes/SBQueue.m @@ -37,7 +37,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; @implementation SBQueue @synthesize status = _status, destination = _destination; -@synthesize optimize = _optimize, fetchMetadata = _fetchMetadata, organizeGroups = _organizeGroups; +@synthesize optimize = _optimize; @synthesize items = _items, URL = _URL, queue = _queue; - (instancetype)initWithURL:(NSURL *)queueURL { @@ -137,177 +137,6 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; return [indexes autorelease]; } -#pragma mark - item preprocessing - -- (NSArray *)loadSubtitles:(NSURL *)url { - NSError *outError; - NSMutableArray *tracksArray = [[NSMutableArray alloc] init]; - NSArray *directory = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[url URLByDeletingLastPathComponent] - includingPropertiesForKeys:nil - options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | - NSDirectoryEnumerationSkipsHiddenFiles | - NSDirectoryEnumerationSkipsPackageDescendants - error:nil]; - - for (NSURL *dirUrl in directory) { - if ([[dirUrl pathExtension] isEqualToString:@"srt"]) { - NSComparisonResult result; - NSString *movieFilename = [[url URLByDeletingPathExtension] lastPathComponent]; - NSString *subtitleFilename = [[dirUrl URLByDeletingPathExtension] lastPathComponent]; - NSRange range = { 0, [movieFilename length] }; - - if ([movieFilename length] <= [subtitleFilename length]) { - result = [subtitleFilename compare:movieFilename options:NSCaseInsensitiveSearch range:range]; - - if (result == NSOrderedSame) { - MP42FileImporter *fileImporter = [[[MP42FileImporter alloc] initWithURL:dirUrl - error:&outError] autorelease]; - - for (MP42Track *track in fileImporter.tracks) { - [tracksArray addObject:track]; - } - } - } - } - } - - return [tracksArray autorelease]; -} - -- (MP42Image *)loadArtwork:(NSURL *)url { - NSData *artworkData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; - if (artworkData && [artworkData length]) { - MP42Image *artwork = [[MP42Image alloc] initWithData:artworkData type:MP42_ART_JPEG]; - if (artwork != nil) { - return [artwork autorelease]; - } - } - - return nil; -} - -- (MP42Metadata *)searchMetadataForFile:(NSURL *)url { - id currentSearcher = nil; - MP42Metadata *metadata = nil; - - // Parse FileName and search for metadata - NSDictionary *parsed = [MetadataImporter parseFilename:[url lastPathComponent]]; - NSString *type = (NSString *)[parsed valueForKey:@"type"]; - if ([@"movie" isEqualToString:type]) { - currentSearcher = [MetadataImporter defaultMovieProvider]; - NSString *language = [MetadataImporter defaultMovieLanguage]; - NSArray *results = [currentSearcher searchMovie:[parsed valueForKey:@"title"] language:language]; - if ([results count]) - metadata = [currentSearcher loadMovieMetadata:[results objectAtIndex:0] language:language]; - } else if ([@"tv" isEqualToString:type]) { - currentSearcher = [MetadataImporter defaultTVProvider]; - NSString *language = [MetadataImporter defaultTVLanguage]; - NSArray *results = [currentSearcher searchTVSeries:[parsed valueForKey:@"seriesName"] - language:language seasonNum:[parsed valueForKey:@"seasonNum"] - episodeNum:[parsed valueForKey:@"episodeNum"]]; - if ([results count]) - metadata = [currentSearcher loadTVMetadata:[results objectAtIndex:0] language:language]; - } - - if (metadata.artworkThumbURLs && [metadata.artworkThumbURLs count]) { - NSURL *artworkURL = nil; - if ([type isEqualToString:@"movie"]) { - artworkURL = [metadata.artworkFullsizeURLs objectAtIndex:0]; - } else if ([type isEqualToString:@"tv"]) { - if ([metadata.artworkFullsizeURLs count] > 1) { - int i = 0; - for (NSString *artworkProviderName in metadata.artworkProviderNames) { - NSArray *a = [artworkProviderName componentsSeparatedByString:@"|"]; - if ([a count] > 1 && ![[a objectAtIndex:1] isEqualToString:@"episode"]) { - artworkURL = [metadata.artworkFullsizeURLs objectAtIndex:i]; - break; - } - i++; - } - } else { - artworkURL = [metadata.artworkFullsizeURLs objectAtIndex:0]; - } - } - - MP42Image *artwork = [self loadArtwork:artworkURL]; - - if (artwork) - [metadata.artworks addObject:artwork]; - } - - return metadata; -} - -- (MP42File *)prepareQueueItem:(NSURL*)url error:(NSError**)outError { - NSString *type; - MP42File *mp4File = nil; - - [url getResourceValue:&type forKey:NSURLTypeIdentifierKey error:outError]; - - if ([type isEqualToString:@"com.apple.m4a-audio"] || [type isEqualToString:@"com.apple.m4v-video"] || [type isEqualToString:@"public.mpeg-4"]) { - mp4File = [[MP42File alloc] initWithExistingFile:url andDelegate:self]; - } else { - mp4File = [[MP42File alloc] initWithDelegate:self]; - MP42FileImporter *fileImporter = [[MP42FileImporter alloc] initWithURL:url - error:outError]; - - for (MP42Track *track in fileImporter.tracks) { - if ([track.format isEqualToString:MP42AudioFormatAC3]) { - if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBAudioConvertAC3"] boolValue]) { - if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBAudioKeepAC3"] boolValue] || - ![(MP42AudioTrack *)track fallbackTrack]) { - MP42AudioTrack *copy = [track copy]; - [copy setNeedConversion:YES]; - [copy setMixdownType:SBDolbyPlIIMixdown]; - - [(MP42AudioTrack *)track setFallbackTrack:copy]; - - [mp4File addTrack:copy]; - - [copy release]; - } else { - track.needConversion = YES; - } - } - } - - if ([track.format isEqualToString:MP42AudioFormatDTS] || - ([track.format isEqualToString:MP42SubtitleFormatVobSub] && [[[NSUserDefaults standardUserDefaults] valueForKey:@"SBSubtitleConvertBitmap"] boolValue])) - track.needConversion = YES; - - if (isTrackMuxable(track.format) || trackNeedConversion(track.format)) - [mp4File addTrack:track]; - } - [fileImporter release]; - } - - // Search for external subtitles files - NSArray *subtitles = [self loadSubtitles:url]; - for (MP42SubtitleTrack *subTrack in subtitles) { - [mp4File addTrack:subTrack]; - } - - // Search for metadata - if (self.fetchMetadata) { - MP42Metadata *metadata = [self searchMetadataForFile:url]; - - for (MP42Track *track in mp4File.tracks) - if ([track isKindOfClass:[MP42VideoTrack class]]) { - int hdVideo = isHdVideo([((MP42VideoTrack *) track) trackWidth], [((MP42VideoTrack *) track) trackHeight]); - - if (hdVideo) - [mp4File.metadata setTag:@(hdVideo) forKey:@"HD Video"]; - } - - [[mp4File metadata] mergeMetadata:metadata]; - } - - if (self.organizeGroups) - [mp4File organizeAlternateGroups]; - - return [mp4File autorelease]; -} - #pragma mark - Queue control - (void)disableSleep { @@ -322,8 +151,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; } } -- (void)start -{ +- (void)start { if (self.status == SBQueueStatusWorking) { return; } else { @@ -428,7 +256,8 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; // The file has been added directly to the queue if (!_currentMP4 && item.URL) { - _currentMP4 = [self prepareQueueItem:item.URL error:outError]; + [item prepareItem:outError]; + _currentMP4 = item.mp4File; } NSDictionary *dict = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[_currentMP4.URL path] error:NULL]; @@ -463,6 +292,10 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; _cancelled = YES; } +- (void)progressStatus:(CGFloat)progress { + NSLog(@"%f", progress); +} + /** * Processes SBQueueStatusWorking state information. Current implementation just * sends SBQueueWorkingNotification. diff --git a/Classes/SBQueueAction.h b/Classes/SBQueueAction.h new file mode 100644 index 0000000..7a5aacc --- /dev/null +++ b/Classes/SBQueueAction.h @@ -0,0 +1,24 @@ +// +// SBQueueAction.h +// Subler +// +// Created by Damiano Galassi on 12/03/14. +// +// + +#import + +@class SBQueueItem; + +@protocol SBQueueActionProtocol +- (void)runAction:(SBQueueItem *)item; +@end + +@interface SBQueueMetadataAction : NSObject +@end + +@interface SBQueueSubtitlesAction : NSObject +@end + +@interface SBQueueOrganizeGroupsAction : NSObject +@end \ No newline at end of file diff --git a/Classes/SBQueueAction.m b/Classes/SBQueueAction.m new file mode 100644 index 0000000..813cf6e --- /dev/null +++ b/Classes/SBQueueAction.m @@ -0,0 +1,155 @@ +// +// SBQueueAction.m +// Subler +// +// Created by Damiano Galassi on 12/03/14. +// +// + +#import "SBQueueAction.h" + +#import "SBQueueItem.h" +#import "MetadataImporter.h" + +#import +#import +#import +#import + +@implementation SBQueueSubtitlesAction + +- (NSArray *)loadSubtitles:(NSURL *)url { + NSError *outError; + NSMutableArray *tracksArray = [[NSMutableArray alloc] init]; + NSArray *directory = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[url URLByDeletingLastPathComponent] + includingPropertiesForKeys:nil + options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | + NSDirectoryEnumerationSkipsHiddenFiles | + NSDirectoryEnumerationSkipsPackageDescendants + error:nil]; + + for (NSURL *dirUrl in directory) { + if ([[dirUrl pathExtension] isEqualToString:@"srt"]) { + NSComparisonResult result; + NSString *movieFilename = [[url URLByDeletingPathExtension] lastPathComponent]; + NSString *subtitleFilename = [[dirUrl URLByDeletingPathExtension] lastPathComponent]; + NSRange range = { 0, [movieFilename length] }; + + if ([movieFilename length] <= [subtitleFilename length]) { + result = [subtitleFilename compare:movieFilename options:NSCaseInsensitiveSearch range:range]; + + if (result == NSOrderedSame) { + MP42FileImporter *fileImporter = [[[MP42FileImporter alloc] initWithURL:dirUrl + error:&outError] autorelease]; + + for (MP42Track *track in fileImporter.tracks) { + [tracksArray addObject:track]; + } + } + } + } + } + + return [tracksArray autorelease]; +} + +- (void)runAction:(SBQueueItem *)item { + // Search for external subtitles files + NSArray *subtitles = [self loadSubtitles:item.URL]; + for (MP42SubtitleTrack *subTrack in subtitles) { + [item.mp4File addTrack:subTrack]; + } +} + +@end + +@implementation SBQueueMetadataAction + +- (MP42Image *)loadArtwork:(NSURL *)url { + NSData *artworkData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; + if (artworkData && [artworkData length]) { + MP42Image *artwork = [[MP42Image alloc] initWithData:artworkData type:MP42_ART_JPEG]; + if (artwork != nil) { + return [artwork autorelease]; + } + } + + return nil; +} + +- (MP42Metadata *)searchMetadataForFile:(NSURL *)url { + id currentSearcher = nil; + MP42Metadata *metadata = nil; + + // Parse FileName and search for metadata + NSDictionary *parsed = [MetadataImporter parseFilename:[url lastPathComponent]]; + NSString *type = (NSString *)[parsed valueForKey:@"type"]; + if ([@"movie" isEqualToString:type]) { + currentSearcher = [MetadataImporter defaultMovieProvider]; + NSString *language = [MetadataImporter defaultMovieLanguage]; + NSArray *results = [currentSearcher searchMovie:[parsed valueForKey:@"title"] language:language]; + if ([results count]) + metadata = [currentSearcher loadMovieMetadata:[results objectAtIndex:0] language:language]; + } else if ([@"tv" isEqualToString:type]) { + currentSearcher = [MetadataImporter defaultTVProvider]; + NSString *language = [MetadataImporter defaultTVLanguage]; + NSArray *results = [currentSearcher searchTVSeries:[parsed valueForKey:@"seriesName"] + language:language seasonNum:[parsed valueForKey:@"seasonNum"] + episodeNum:[parsed valueForKey:@"episodeNum"]]; + if ([results count]) + metadata = [currentSearcher loadTVMetadata:[results objectAtIndex:0] language:language]; + } + + if (metadata.artworkThumbURLs && [metadata.artworkThumbURLs count]) { + NSURL *artworkURL = nil; + if ([type isEqualToString:@"movie"]) { + artworkURL = [metadata.artworkFullsizeURLs objectAtIndex:0]; + } else if ([type isEqualToString:@"tv"]) { + if ([metadata.artworkFullsizeURLs count] > 1) { + int i = 0; + for (NSString *artworkProviderName in metadata.artworkProviderNames) { + NSArray *a = [artworkProviderName componentsSeparatedByString:@"|"]; + if ([a count] > 1 && ![[a objectAtIndex:1] isEqualToString:@"episode"]) { + artworkURL = [metadata.artworkFullsizeURLs objectAtIndex:i]; + break; + } + i++; + } + } else { + artworkURL = [metadata.artworkFullsizeURLs objectAtIndex:0]; + } + } + + MP42Image *artwork = [self loadArtwork:artworkURL]; + + if (artwork) + [metadata.artworks addObject:artwork]; + } + + return metadata; +} + +- (void)runAction:(SBQueueItem *)item { + // Search for metadata + MP42Metadata *metadata = [self searchMetadataForFile:item.URL]; + + for (MP42Track *track in item.mp4File.tracks) + if ([track isKindOfClass:[MP42VideoTrack class]]) { + int hdVideo = isHdVideo([((MP42VideoTrack *) track) trackWidth], [((MP42VideoTrack *) track) trackHeight]); + + if (hdVideo) + [item.mp4File.metadata setTag:@(hdVideo) forKey:@"HD Video"]; + } + + [[item.mp4File metadata] mergeMetadata:metadata]; +} + +@end + +@implementation SBQueueOrganizeGroupsAction + +- (void)runAction:(SBQueueItem *)item { + [item.mp4File organizeAlternateGroups]; +} + +@end diff --git a/Classes/SBQueueController.h b/Classes/SBQueueController.h index 22cd415..2192775 100755 --- a/Classes/SBQueueController.h +++ b/Classes/SBQueueController.h @@ -29,15 +29,15 @@ IBOutlet NSBox *_optionsBox; BOOL _optionsStatus; - IBOutlet NSScrollView *tableScrollView; - IBOutlet SBTableView *tableView; + IBOutlet NSScrollView *_tableScrollView; + IBOutlet SBTableView *_tableView; - IBOutlet NSPopUpButton *destButton; + IBOutlet NSPopUpButton *_destButton; - NSImage *docImg; + NSImage *_docImg; - NSURL *destination; - BOOL customDestination; + NSURL *_destination; + BOOL _customDestination; SBQueue *_queue; } diff --git a/Classes/SBQueueController.m b/Classes/SBQueueController.m index 39f7f5e..4d483b0 100755 --- a/Classes/SBQueueController.m +++ b/Classes/SBQueueController.m @@ -10,7 +10,6 @@ #import "SBQueueItem.h" #import "SBDocument.h" #import "SBTableView.h" -#import "MetadataImporter.h" #import @@ -72,20 +71,20 @@ frame.size.height -= kOptionsPanelHeight; frame.origin.y += kOptionsPanelHeight; - [tableScrollView setAutoresizingMask:NSViewNotSizable | NSViewMinYMargin]; + [_tableScrollView setAutoresizingMask:NSViewNotSizable | NSViewMinYMargin]; [_optionsBox setAutoresizingMask:NSViewNotSizable | NSViewMinYMargin]; [[self window] setFrame:frame display:YES animate:NO]; - [tableScrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + [_tableScrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [_optionsBox setAutoresizingMask:NSViewWidthSizable | NSViewMaxYMargin]; - docImg = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode('MOOV')] retain]; - [docImg setSize:NSMakeSize(16, 16)]; + _docImg = [[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode('MOOV')] retain]; + [_docImg setSize:NSMakeSize(16, 16)]; [self prepareDestPopup]; - [tableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, SublerBatchTableViewDataType, nil]]; + [_tableView registerForDraggedTypes: [NSArray arrayWithObjects: NSFilenamesPboardType, SublerBatchTableViewDataType, nil]]; NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; @@ -144,7 +143,7 @@ NSMenuItem *folderItem = nil; if ([[NSUserDefaults standardUserDefaults] valueForKey:@"SBQueueDestination"]) { - destination = [[NSURL fileURLWithPath:[[NSUserDefaults standardUserDefaults] valueForKey:@"SBQueueDestination"]] retain]; + _destination = [[NSURL fileURLWithPath:[[NSUserDefaults standardUserDefaults] valueForKey:@"SBQueueDestination"]] retain]; #ifdef SB_SANDBOX if ([[NSUserDefaults standardUserDefaults] valueForKey:@"SBQueueDestinationBookmark"]) { @@ -161,35 +160,35 @@ error:&error] retain]; } #endif - if (![[NSFileManager defaultManager] fileExistsAtPath:[destination path] isDirectory:nil]) - destination = nil; + if (![[NSFileManager defaultManager] fileExistsAtPath:[_destination path] isDirectory:nil]) + _destination = nil; } - if (!destination) { + if (!_destination) { NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSMoviesDirectory, NSUserDomainMask, YES); if ([allPaths count]) - destination = [[NSURL fileURLWithPath:[allPaths lastObject]] retain];; + _destination = [[NSURL fileURLWithPath:[allPaths lastObject]] retain];; } - folderItem = [self prepareDestPopupItem:destination]; + folderItem = [self prepareDestPopupItem:_destination]; - [[destButton menu] insertItem:[NSMenuItem separatorItem] atIndex:0]; - [[destButton menu] insertItem:folderItem atIndex:0]; + [[_destButton menu] insertItem:[NSMenuItem separatorItem] atIndex:0]; + [[_destButton menu] insertItem:folderItem atIndex:0]; if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBQueueDestinationSelected"] boolValue]) { - [destButton selectItem:folderItem]; - customDestination = YES; + [_destButton selectItem:folderItem]; + _customDestination = YES; } } - (IBAction)destination:(id)sender { if ([sender tag] == 10) { - customDestination = YES; + _customDestination = YES; [[NSUserDefaults standardUserDefaults] setValue:@"YES" forKey:@"SBQueueDestinationSelected"]; } else { - customDestination = NO; + _customDestination = NO; [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"SBQueueDestinationSelected"]; } } @@ -204,7 +203,7 @@ } - (void)updateUI { - [tableView reloadData]; + [_tableView reloadData]; if (self.queue.status != SBQueueStatusWorking) { [_countLabel setStringValue:[NSString stringWithFormat:@"%lu files in queue.", (unsigned long)[self.queue count]]]; [self updateDockTile]; @@ -247,8 +246,7 @@ if (_optionsStatus) { value = -kOptionsPanelHeight; _optionsStatus = NO; - } - else { + } else { value = kOptionsPanelHeight; _optionsStatus = YES; } @@ -257,12 +255,12 @@ frame.size.height += value; frame.origin.y -= value; - [tableScrollView setAutoresizingMask:NSViewNotSizable | NSViewMinYMargin]; + [_tableScrollView setAutoresizingMask:NSViewNotSizable | NSViewMinYMargin]; [_optionsBox setAutoresizingMask:NSViewNotSizable | NSViewMinYMargin]; [[self window] setFrame:frame display:YES animate:YES]; - [tableScrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + [_tableScrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [_optionsBox setAutoresizingMask:NSViewWidthSizable | NSViewMaxYMargin]; } @@ -279,8 +277,18 @@ if (result == NSFileHandlingPanelOKButton) { NSMutableArray *items = [[NSMutableArray alloc] init]; - for (NSURL *url in [panel URLs]) - [items addObject:[SBQueueItem itemWithURL:url]]; + 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]]; + } + + [items addObject:item]; + } [self addItems:items atIndexes:nil]; [items release]; @@ -303,15 +311,15 @@ [panel setPrompt:@"Select"]; [panel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) { if (result == NSFileHandlingPanelOKButton) { - destination = [[panel URL] retain]; + _destination = [[panel URL] retain]; NSMenuItem *folderItem = [self prepareDestPopupItem:[panel URL]]; - [[destButton menu] removeItemAtIndex:0]; - [[destButton menu] insertItem:folderItem atIndex:0]; + [[_destButton menu] removeItemAtIndex:0]; + [[_destButton menu] insertItem:folderItem atIndex:0]; - [destButton selectItem:folderItem]; - customDestination = YES; + [_destButton selectItem:folderItem]; + _customDestination = YES; #ifdef SB_SANDBOX NSData *bookmark = nil; @@ -331,7 +339,7 @@ [[NSUserDefaults standardUserDefaults] setValue:@"YES" forKey:@"SBQueueDestinationSelected"]; } else - [destButton selectItemAtIndex:2]; + [_destButton selectItemAtIndex:2]; }]; } @@ -354,7 +362,7 @@ else if (batchStatus == SBQueueItemStatusFailed) return [NSImage imageNamed:@"EncodeCanceled"]; else - return docImg; + return _docImg; } return nil; @@ -421,12 +429,12 @@ } - (IBAction)showInFinder:(id)sender { - SBQueueItem *item = [self.queue itemAtIndex:[tableView clickedRow]]; + SBQueueItem *item = [self.queue itemAtIndex:[_tableView clickedRow]]; [[NSWorkspace sharedWorkspace] selectFile:[item.destURL path] inFileViewerRootedAtPath:nil]; } - (IBAction)removeSelectedItems:(id)sender { - [self _deleteSelectionFromTableView:tableView]; + [self _deleteSelectionFromTableView:_tableView]; } - (IBAction)removeCompletedItems:(id)sender { @@ -435,12 +443,12 @@ if ([indexes count]) { if ([NSTableView instancesRespondToSelector:@selector(beginUpdates)]) { #if __MAC_OS_X_VERSION_MAX_ALLOWED > 1060 - [tableView beginUpdates]; - [tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationEffectFade]; - [tableView endUpdates]; + [_tableView beginUpdates]; + [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationEffectFade]; + [_tableView endUpdates]; #endif } else { - [tableView reloadData]; + [_tableView reloadData]; } if (self.queue.status != SBQueueStatusWorking) { @@ -454,12 +462,12 @@ SEL action = [anItem action]; if (action == @selector(removeSelectedItems:)) - if ([tableView selectedRow] != -1 || [tableView clickedRow] != -1) + if ([_tableView selectedRow] != -1 || [_tableView clickedRow] != -1) return YES; if (action == @selector(showInFinder:)) { - if ([tableView clickedRow] != -1) { - SBQueueItem *item = [self.queue itemAtIndex:[tableView clickedRow]]; + if ([_tableView clickedRow] != -1) { + SBQueueItem *item = [self.queue itemAtIndex:[_tableView clickedRow]]; if ([item status] == SBQueueItemStatusCompleted) return YES; } @@ -512,7 +520,7 @@ { NSPasteboard *pboard = [info draggingPasteboard]; - if (tableView == [info draggingSource]) { // From self + if (_tableView == [info draggingSource]) { // From self NSData *rowData = [pboard dataForType:SublerBatchTableViewDataType]; NSIndexSet *rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData]; NSUInteger i = [rowIndexes countOfIndexesInRange:NSMakeRange(0, row)]; diff --git a/Classes/SBQueueItem.h b/Classes/SBQueueItem.h index 3af9cc1..8c694e8 100755 --- a/Classes/SBQueueItem.h +++ b/Classes/SBQueueItem.h @@ -8,6 +8,8 @@ #import +#import "SBQueueAction.h" + @class MP42File; typedef enum SBQueueItemStatus : NSInteger { @@ -20,21 +22,21 @@ typedef enum SBQueueItemStatus : NSInteger { } SBQueueItemStatus; @interface SBQueueItem : NSObject { - MP42File *mp4File; - NSURL *fileURL; - NSURL *destURL; - NSDictionary *attributes; + MP42File *_mp4File; + NSURL *_fileURL; + NSURL *_destURL; + NSDictionary *_attributes; + NSMutableArray *_actions; - SBQueueItemStatus status; - BOOL humanEdited; + SBQueueItemStatus _status; } -@property (atomic, readonly) MP42File *mp4File; -@property (atomic, readonly) NSDictionary *attributes; -@property (atomic, readonly) NSURL *URL; -@property (atomic, retain, readwrite) NSURL *destURL; +@property (nonatomic, readonly) MP42File *mp4File; +@property (nonatomic, readonly) NSDictionary *attributes; +@property (nonatomic, readonly) NSURL *URL; +@property (nonatomic, retain, readwrite) NSURL *destURL; -@property (atomic, readwrite) SBQueueItemStatus status; +@property (nonatomic, readwrite) SBQueueItemStatus status; - (instancetype)initWithURL:(NSURL *)URL; + (instancetype)itemWithURL:(NSURL *)URL; @@ -45,4 +47,8 @@ typedef enum SBQueueItemStatus : NSInteger { + (instancetype)itemWithMP4:(MP42File *)MP4; + (instancetype)itemWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict; +- (void)addAction:(id)action; + +- (BOOL)prepareItem:(NSError **)outError; + @end diff --git a/Classes/SBQueueItem.m b/Classes/SBQueueItem.m index 869ebdf..b1e7b9a 100755 --- a/Classes/SBQueueItem.m +++ b/Classes/SBQueueItem.m @@ -7,134 +7,194 @@ // #import "SBQueueItem.h" +#import "SBQueueAction.h" + #import +#import +#import + +@interface SBQueueItem () + +@property (nonatomic, readwrite, retain) MP42File *mp4File; + +@property (nonatomic, readwrite) NSMutableArray *actions; + +@end @implementation SBQueueItem -@synthesize attributes; -@synthesize URL = fileURL; -@synthesize destURL; -@synthesize mp4File; +@synthesize status = _status; -- (instancetype)initWithURL:(NSURL *)URL { +@synthesize attributes = _attributes; +@synthesize actions = _actions; + +@synthesize URL = _fileURL; +@synthesize destURL = _destURL; +@synthesize mp4File = _mp4File; + +- (instancetype)init +{ self = [super init]; if (self) { - fileURL = [URL retain]; + _actions = [[NSMutableArray alloc] init]; + } + return self; +} + +- (instancetype)initWithURL:(NSURL *)URL { + self = [self init]; + if (self) { + _fileURL = [URL retain]; NSFileManager *fileManager = [NSFileManager defaultManager]; - unsigned long long originalFileSize = [[[fileManager attributesOfItemAtPath:[fileURL path] error:nil] valueForKey:NSFileSize] unsignedLongLongValue]; + unsigned long long originalFileSize = [[[fileManager attributesOfItemAtPath:[_fileURL path] error:nil] valueForKey:NSFileSize] unsignedLongLongValue]; if (originalFileSize > 4200000000) { - attributes = [[NSDictionary alloc] initWithObjectsAndKeys:@YES, MP4264BitData, nil]; + _attributes = [[NSDictionary alloc] initWithObjectsAndKeys:@YES, MP4264BitData, nil]; } } return self; } -+ (instancetype)itemWithURL:(NSURL *)URL -{ ++ (instancetype)itemWithURL:(NSURL *)URL { return [[[SBQueueItem alloc] initWithURL:URL] autorelease]; } - (id)initWithMP4:(MP42File*)MP4 { - self = [super init]; + self = [self init]; if (self) { - mp4File = [MP4 retain]; + _mp4File = [MP4 retain]; - if ([MP4 URL]) - fileURL = [[MP4 URL] retain]; - else { - for (NSUInteger i = 0; i < [mp4File tracksCount]; i++) { - MP42Track *track = [mp4File trackAtIndex:i]; + if ([MP4 URL]) { + _fileURL = [[MP4 URL] retain]; + } else { + for (NSUInteger i = 0; i < [_mp4File tracksCount]; i++) { + MP42Track *track = [_mp4File trackAtIndex:i]; if ([track sourceURL]) { - fileURL = [[track sourceURL] retain]; + _fileURL = [[track sourceURL] retain]; break; } } } - status = SBQueueItemStatusReady; + _status = SBQueueItemStatusReady; } return self; } -+ (instancetype)itemWithMP4:(MP42File *)MP4 -{ ++ (instancetype)itemWithMP4:(MP42File *)MP4 { return [[[SBQueueItem alloc] initWithMP4:MP4] autorelease]; } -- (id)initWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict -{ - if (self = [super init]) - { - mp4File = [MP4 retain]; - fileURL = [URL retain]; - destURL = [URL retain]; - attributes = [dict retain]; +- (id)initWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict { + if (self = [self init]) { + _mp4File = [MP4 retain]; + _fileURL = [URL copy]; + _destURL = [URL copy]; + _attributes = [dict copy]; - status = SBQueueItemStatusReady; + _status = SBQueueItemStatusReady; } return self; } -+ (instancetype)itemWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict -{ ++ (instancetype)itemWithMP4:(MP42File *)MP4 url:(NSURL *)URL attributes:(NSDictionary *)dict { return [[[SBQueueItem alloc] initWithMP4:MP4 url:URL attributes:dict] autorelease]; } -- (SBQueueItemStatus)status -{ - return status; -} - -- (void)setStatus:(SBQueueItemStatus)itemStatus -{ - status = itemStatus; - if (status == SBQueueItemStatusCompleted) { - if (mp4File) { - [mp4File release]; - mp4File = nil; +- (void)setStatus:(SBQueueItemStatus)itemStatus { + _status = itemStatus; + if (_status == SBQueueItemStatusCompleted) { + if (self.mp4File) { + self.mp4File = nil; } } } -- (void)dealloc -{ - [attributes release]; - [fileURL release]; - [destURL release]; - [mp4File release]; +- (void)addAction:(id)action { + [self.actions addObject:action]; +} + +- (BOOL)prepareItem:(NSError **)outError { + NSString *type; + [self.URL getResourceValue:&type forKey:NSURLTypeIdentifierKey error:outError]; + + if ([type isEqualToString:@"com.apple.m4a-audio"] || [type isEqualToString:@"com.apple.m4v-video"] || [type isEqualToString:@"public.mpeg-4"]) { + self.mp4File = [[[MP42File alloc] initWithExistingFile:self.URL andDelegate:nil] autorelease]; + } else { + self.mp4File = [[[MP42File alloc] initWithDelegate:nil] autorelease]; + MP42FileImporter *fileImporter = [[MP42FileImporter alloc] initWithURL:self.URL error:outError]; + + for (MP42Track *track in fileImporter.tracks) { + if ([track.format isEqualToString:MP42AudioFormatAC3]) { + if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBAudioConvertAC3"] boolValue]) { + if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBAudioKeepAC3"] boolValue] || + ![(MP42AudioTrack *)track fallbackTrack]) { + MP42AudioTrack *copy = [track copy]; + [copy setNeedConversion:YES]; + [copy setMixdownType:SBDolbyPlIIMixdown]; + + [(MP42AudioTrack *)track setFallbackTrack:copy]; + + [self.mp4File addTrack:copy]; + + [copy release]; + } else { + track.needConversion = YES; + } + } + } + + if ([track.format isEqualToString:MP42AudioFormatDTS] || + ([track.format isEqualToString:MP42SubtitleFormatVobSub] && [[[NSUserDefaults standardUserDefaults] valueForKey:@"SBSubtitleConvertBitmap"] boolValue])) + track.needConversion = YES; + + if (isTrackMuxable(track.format) || trackNeedConversion(track.format)) + [self.mp4File addTrack:track]; + } + [fileImporter release]; + } + + for (id action in self.actions) { + [action runAction:self]; + } + + return YES; +} + +- (void)dealloc { + [_attributes release]; + [_actions release]; + [_fileURL release]; + [_destURL release]; + [_mp4File release]; [super dealloc]; } -- (void)encodeWithCoder:(NSCoder *)coder -{ +- (void)encodeWithCoder:(NSCoder *)coder { [coder encodeInt:1 forKey:@"SBQueueItemTagEncodeVersion"]; - [coder encodeObject:mp4File forKey:@"SBQueueItemMp4File"]; - [coder encodeObject:fileURL forKey:@"SBQueueItemFileURL"]; - [coder encodeObject:destURL forKey:@"SBQueueItemDestURL"]; - [coder encodeObject:attributes forKey:@"SBQueueItemAttributes"]; + [coder encodeObject:_mp4File forKey:@"SBQueueItemMp4File"]; + [coder encodeObject:_fileURL forKey:@"SBQueueItemFileURL"]; + [coder encodeObject:_destURL forKey:@"SBQueueItemDestURL"]; + [coder encodeObject:_attributes forKey:@"SBQueueItemAttributes"]; - [coder encodeInt:status forKey:@"SBQueueItemStatus"]; - [coder encodeInt:humanEdited forKey:@"SBQueueItemHumanEdited"]; + [coder encodeInt:_status forKey:@"SBQueueItemStatus"]; } -- (id)initWithCoder:(NSCoder *)decoder -{ +- (id)initWithCoder:(NSCoder *)decoder { self = [super init]; - mp4File = [[decoder decodeObjectForKey:@"SBQueueItemMp4File"] retain]; + _mp4File = [[decoder decodeObjectForKey:@"SBQueueItemMp4File"] retain]; - fileURL = [[decoder decodeObjectForKey:@"SBQueueItemFileURL"] retain]; - destURL = [[decoder decodeObjectForKey:@"SBQueueItemDestURL"] retain]; - attributes = [[decoder decodeObjectForKey:@"SBQueueItemAttributes"] retain]; + _fileURL = [[decoder decodeObjectForKey:@"SBQueueItemFileURL"] retain]; + _destURL = [[decoder decodeObjectForKey:@"SBQueueItemDestURL"] retain]; + _attributes = [[decoder decodeObjectForKey:@"SBQueueItemAttributes"] retain]; - status = [decoder decodeIntForKey:@"SBQueueItemStatus"]; - humanEdited = [decoder decodeIntForKey:@"SBQueueItemHumanEdited"]; + _status = [decoder decodeIntForKey:@"SBQueueItemStatus"]; return self; } diff --git a/English.lproj/Queue.xib b/English.lproj/Queue.xib index 50bb1ea..f721860 100755 --- a/English.lproj/Queue.xib +++ b/English.lproj/Queue.xib @@ -9,6 +9,7 @@ + @@ -16,9 +17,9 @@ + + - - @@ -185,7 +186,7 @@ - + diff --git a/Subler.xcodeproj/project.pbxproj b/Subler.xcodeproj/project.pbxproj index c4568bb..ce5ec6e 100755 --- a/Subler.xcodeproj/project.pbxproj +++ b/Subler.xcodeproj/project.pbxproj @@ -44,6 +44,7 @@ A9AB486617F0AEFD000AA72E /* MovieView.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9AB485E17F0AEFD000AA72E /* MovieView.xib */; }; A9AB486717F0AEFD000AA72E /* SoundView.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9AB486017F0AEFD000AA72E /* SoundView.xib */; }; A9AB486817F0AEFD000AA72E /* VideoView.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9AB486217F0AEFD000AA72E /* VideoView.xib */; }; + A9AF6A7518D0496400FAAB08 /* SBQueueAction.m in Sources */ = {isa = PBXBuildFile; fileRef = A9AF6A7418D0496400FAAB08 /* SBQueueAction.m */; }; A9B46A8C12B79B240040517A /* SBFileImport.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B46A7F12B79B240040517A /* SBFileImport.m */; }; A9B46A8E12B79B240040517A /* SBPrefsController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B46A8312B79B240040517A /* SBPrefsController.m */; }; A9B46A8F12B79B240040517A /* SBTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B46A8512B79B240040517A /* SBTableView.m */; }; @@ -183,6 +184,8 @@ A9AB485F17F0AEFD000AA72E /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = Classes/PropertyView/English.lproj/MovieView.xib; sourceTree = ""; }; A9AB486117F0AEFD000AA72E /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = Classes/PropertyView/English.lproj/SoundView.xib; sourceTree = ""; }; A9AB486317F0AEFD000AA72E /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = Classes/PropertyView/English.lproj/VideoView.xib; sourceTree = ""; }; + A9AF6A7318D0496400FAAB08 /* SBQueueAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBQueueAction.h; path = Classes/SBQueueAction.h; sourceTree = ""; }; + A9AF6A7418D0496400FAAB08 /* SBQueueAction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBQueueAction.m; path = Classes/SBQueueAction.m; sourceTree = ""; }; A9B3411B1679CA3E00E6EE0C /* QTKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QTKit.framework; path = System/Library/Frameworks/QTKit.framework; sourceTree = SDKROOT; }; A9B46A7E12B79B240040517A /* SBFileImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBFileImport.h; path = Classes/SBFileImport.h; sourceTree = ""; }; A9B46A7F12B79B240040517A /* SBFileImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBFileImport.m; path = Classes/SBFileImport.m; sourceTree = ""; }; @@ -312,12 +315,7 @@ children = ( A9B46ABD12B79D1C0040517A /* SBDocument.h */, A9B46ABE12B79D1C0040517A /* SBDocument.m */, - A961475F13F567E10068F161 /* SBQueueController.h */, - A961476013F567E10068F161 /* SBQueueController.m */, - A9D73C5018BF3F1100C6FFA6 /* SBQueue.h */, - A9D73C5118BF3F1100C6FFA6 /* SBQueue.m */, - A982D0BE140AB66400B92357 /* SBQueueItem.h */, - A982D0BF140AB66400B92357 /* SBQueueItem.m */, + A9AF6A7818D0537F00FAAB08 /* Queue */, A961476113F567E10068F161 /* SBPresetManager.h */, A961476213F567E10068F161 /* SBPresetManager.m */, A9B46A7E12B79B240040517A /* SBFileImport.h */, @@ -418,6 +416,21 @@ name = NIBs; sourceTree = ""; }; + A9AF6A7818D0537F00FAAB08 /* Queue */ = { + isa = PBXGroup; + children = ( + A961475F13F567E10068F161 /* SBQueueController.h */, + A961476013F567E10068F161 /* SBQueueController.m */, + A9D73C5018BF3F1100C6FFA6 /* SBQueue.h */, + A9D73C5118BF3F1100C6FFA6 /* SBQueue.m */, + A982D0BE140AB66400B92357 /* SBQueueItem.h */, + A982D0BF140AB66400B92357 /* SBQueueItem.m */, + A9AF6A7318D0496400FAAB08 /* SBQueueAction.h */, + A9AF6A7418D0496400FAAB08 /* SBQueueAction.m */, + ); + name = Queue; + sourceTree = ""; + }; A9FD5D150F3C692400817716 /* PropertyView */ = { isa = PBXGroup; children = ( @@ -666,6 +679,7 @@ A961476313F567E10068F161 /* SBQueueController.m in Sources */, A961476413F567E10068F161 /* SBPresetManager.m in Sources */, A982D0C0140AB66400B92357 /* SBQueueItem.m in Sources */, + A9AF6A7518D0496400FAAB08 /* SBQueueAction.m in Sources */, CDC06FCE1747494D003C1D0A /* iTunesStore.m in Sources */, CD03087417576A550093A0BE /* TheMovieDB3.m in Sources */, CD03087917576AE50093A0BE /* MetadataImporter.m in Sources */,