Refactored some code in a new method

This commit is contained in:
Damiano Galassi committed 2015-08-29 16:56:19 +02:00
1 parent ee94900e33
commit 28b52e3059
1 file changed
+55 -54
+55 -54
View File
@@ -82,7 +82,7 @@
} }
#pragma mark Read methods #pragma mark - Read methods
+ (BOOL)canConcurrentlyReadDocumentsOfType:(NSString *)type + (BOOL)canConcurrentlyReadDocumentsOfType:(NSString *)type
{ {
@@ -122,7 +122,30 @@
return YES; return YES;
} }
#pragma mark Save methods #pragma mark - Save methods
- (IBAction)cancelSaveOperation:(id)sender
{
[cancelSave setEnabled:NO];
[self.mp4 cancel];
}
- (IBAction)saveAndOptimize:(id)sender
{
_optimize = YES;
[self saveDocument:sender];
}
- (IBAction)sendToExternalApp:(id)sender
{
// Send to itunes after save.
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSString *appPath = [workspace fullPathForApplication:@"iTunes"];
if (appPath) {
[workspace openFile:self.fileURL.path withApplication:appPath];
}
}
- (BOOL)canAsynchronouslyWriteToURL:(NSURL *)url - (BOOL)canAsynchronouslyWriteToURL:(NSURL *)url
ofType:(NSString *)typeName ofType:(NSString *)typeName
@@ -149,14 +172,7 @@
IOReturn io_success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, IOReturn io_success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep,
kIOPMAssertionLevelOn, reasonForActivity, &assertionID); kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
BOOL result = NO; BOOL result = NO;
NSDictionary<NSString *, NSNumber *> *attributes = [self saveAttributes];
NSMutableDictionary<NSString *, NSNumber *> * attributes = [[NSMutableDictionary alloc] init];
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue]) {
attributes[MP42GenerateChaptersPreviewTrack] = @YES;
}
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBOrganizeAlternateGroups"] boolValue]) {
attributes[MP42OrganizeAlternateGroups] = @YES;
}
switch (saveOperation) { switch (saveOperation) {
case NSSaveOperation: case NSSaveOperation:
@@ -167,9 +183,6 @@
case NSSaveAsOperation: case NSSaveAsOperation:
// movie does not exist, create a new one from scratch. // movie does not exist, create a new one from scratch.
if (_64bit_data.state) { attributes[MP4264BitData] = @YES; }
if (_64bit_time.state) { attributes[MP4264BitTime] = @YES; }
result = [self.mp4 writeToUrl:url withAttributes:attributes error:outError]; result = [self.mp4 writeToUrl:url withAttributes:attributes error:outError];
break; break;
@@ -178,8 +191,6 @@
break; break;
} }
[attributes release];
if (result && _optimize) { if (result && _optimize) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[saveOperationName setStringValue:@"Optimizing…"]; [saveOperationName setStringValue:@"Optimizing…"];
@@ -222,6 +233,8 @@
return result; return result;
} }
#pragma mark - Save panel
- (BOOL)prepareSavePanel:(NSSavePanel *)savePanel - (BOOL)prepareSavePanel:(NSSavePanel *)savePanel
{ {
_currentSavePanel = savePanel; _currentSavePanel = savePanel;
@@ -254,7 +267,7 @@
} }
if (self.mp4.dataSize > 4200000000) { if (self.mp4.dataSize > 4200000000) {
[_64bit_data setState:NSOnState]; _64bit_data.state = NSOnState;
} }
return YES; return YES;
@@ -263,7 +276,8 @@
- (IBAction)setSaveFormat:(NSPopUpButton *)sender - (IBAction)setSaveFormat:(NSPopUpButton *)sender
{ {
NSString *requiredFileType = nil; NSString *requiredFileType = nil;
NSInteger index = [sender indexOfSelectedItem]; NSInteger index = sender.indexOfSelectedItem;
switch (index) { switch (index) {
case 0: case 0:
requiredFileType = MP42FileTypeM4V; requiredFileType = MP42FileTypeM4V;
@@ -285,34 +299,27 @@
break; break;
} }
[_currentSavePanel setAllowedFileTypes:@[requiredFileType]]; _currentSavePanel.allowedFileTypes = @[requiredFileType];
[[NSUserDefaults standardUserDefaults] setObject:requiredFileType forKey:@"SBSaveFormat"]; [[NSUserDefaults standardUserDefaults] setObject:requiredFileType forKey:@"SBSaveFormat"];
} }
- (IBAction)cancelSaveOperation:(id)sender - (NSDictionary<NSString *, NSNumber *> *)saveAttributes {
{ NSMutableDictionary<NSString *, NSNumber *> * attributes = [[NSMutableDictionary alloc] init];
[cancelSave setEnabled:NO];
[self.mp4 cancel]; if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue]) {
attributes[MP42GenerateChaptersPreviewTrack] = @YES;
}
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBOrganizeAlternateGroups"] boolValue]) {
attributes[MP42OrganizeAlternateGroups] = @YES;
} }
- (IBAction)saveAndOptimize:(id)sender if (_64bit_data.state) { attributes[MP4264BitData] = @YES; }
{ if (_64bit_time.state) { attributes[MP4264BitTime] = @YES; }
_optimize = YES;
[self saveDocument:sender]; return [attributes autorelease];
} }
- (IBAction)sendToExternalApp:(id)sender #pragma mark - Interface validation
{
// Send to itunes after save.
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSString *appPath = [workspace fullPathForApplication:@"iTunes"];
if (appPath) {
[workspace openFile:self.fileURL.path withApplication:appPath];
}
}
#pragma mark Interface validation
- (void)progressStatus:(CGFloat)progress - (void)progressStatus:(CGFloat)progress
{ {
@@ -398,7 +405,7 @@
return NO; return NO;
} }
#pragma mark table datasource #pragma mark - Table Data Source
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{ {
@@ -550,9 +557,10 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn
NSPasteboard *pboard = [info draggingPasteboard]; NSPasteboard *pboard = [info draggingPasteboard];
NSData *rowData = [pboard dataForType:SublerTableViewDataType]; NSData *rowData = [pboard dataForType:SublerTableViewDataType];
NSIndexSet *rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData]; NSIndexSet *rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
NSInteger dragRow = [rowIndexes firstIndex]; NSInteger dragRow = rowIndexes.firstIndex;
[self.mp4 moveTrackAtIndex:dragRow toIndex:row]; [self.mp4 moveTrackAtIndex:dragRow toIndex:row];
[fileTracksTable reloadData]; [fileTracksTable reloadData];
return YES; return YES;
} }
@@ -562,7 +570,7 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn
return YES; return YES;
} }
/* NSComboBoxCell dataSource */ #pragma mark - NSComboBoxCell dataSource
- (NSInteger)numberOfItemsInComboBoxCell:(NSComboBoxCell *)comboBoxCell - (NSInteger)numberOfItemsInComboBoxCell:(NSComboBoxCell *)comboBoxCell
{ {
@@ -577,7 +585,7 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn
return [languages indexOfObject: string]; return [languages indexOfObject: string];
} }
#pragma mark Various things #pragma mark - Various things
- (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{ {
@@ -603,18 +611,11 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn
[panel beginSheetModalForWindow:documentWindow completionHandler:^(NSInteger result) { [panel beginSheetModalForWindow:documentWindow completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) { if (result == NSFileHandlingPanelOKButton) {
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init]; NSDictionary *attributes = [self saveAttributes];
if (_64bit_data.state) { attributes[MP4264BitData] = @YES; }
if (_64bit_time.state) { attributes[MP4264BitTime] = @YES; }
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue]) {
attributes[MP42GenerateChaptersPreviewTrack] = @YES;
}
SBQueueItem *item = [SBQueueItem itemWithMP4:self.mp4 destinationURL:panel.URL attributes:attributes]; SBQueueItem *item = [SBQueueItem itemWithMP4:self.mp4 destinationURL:panel.URL attributes:attributes];
[queue addItem:item]; [queue addItem:item];
[attributes release];
[self close]; [self close];
} }
}]; }];
@@ -835,12 +836,9 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn
- (IBAction)export:(id)sender - (IBAction)export:(id)sender
{ {
NSString *filename = self.fileURL.URLByDeletingPathExtension.lastPathComponent;
NSSavePanel *panel = [NSSavePanel savePanel]; NSSavePanel *panel = [NSSavePanel savePanel];
panel.canSelectHiddenExtension = YES;
panel.nameFieldStringValue = filename;
NSString *filename = self.fileURL.URLByDeletingPathExtension.lastPathComponent;
NSInteger row = fileTracksTable.selectedRow; NSInteger row = fileTracksTable.selectedRow;
if (row != -1 && [[self.mp4 trackAtIndex:row] isKindOfClass:[MP42SubtitleTrack class]]) { if (row != -1 && [[self.mp4 trackAtIndex:row] isKindOfClass:[MP42SubtitleTrack class]]) {
@@ -852,6 +850,9 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn
panel.allowedFileTypes = @[@"txt"]; panel.allowedFileTypes = @[@"txt"];
} }
panel.canSelectHiddenExtension = YES;
panel.nameFieldStringValue = filename;
[panel beginSheetModalForWindow:documentWindow completionHandler:^(NSInteger result) { [panel beginSheetModalForWindow:documentWindow completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) { if (result == NSFileHandlingPanelOKButton) {