Use NSDocument's asynchronous saving instead of the the previous ugly hack.

This commit is contained in:
Damiano Galassi committed 2015-05-02 14:51:52 +02:00
1 parent 475efae14c
commit 18af7bce53
1 file changed
+30 -45
+30 -45
View File
@@ -83,7 +83,7 @@
+ (BOOL)canConcurrentlyReadDocumentsOfType:(NSString *)type + (BOOL)canConcurrentlyReadDocumentsOfType:(NSString *)type
{ {
return NO; return YES;
} }
- (BOOL)isEntireFileLoaded - (BOOL)isEntireFileLoaded
@@ -142,50 +142,42 @@
#pragma mark Save methods #pragma mark Save methods
- (BOOL)saveDidComplete:(NSError **)outError URL:(NSURL *)absoluteURL - (BOOL)canAsynchronouslyWriteToURL:(NSURL *)url
ofType:(NSString *)typeName
forSaveOperation:(NSSaveOperationType)saveOperation
{
return YES;
}
- (BOOL)saveDidComplete:(NSError *)outError URL:(NSURL *)absoluteURL
{ {
[NSApp endSheet:savingWindow]; [NSApp endSheet:savingWindow];
[savingWindow orderOut:self]; [savingWindow orderOut:self];
[optBar stopAnimation:self]; [optBar stopAnimation:self];
if (*outError && [*outError code] == 101) { if (outError && [outError code] == 101) {
// Write permission error, don't reload the file // Write permission error, don't reload the file
} else { } else {
[self reloadFile:absoluteURL]; [self reloadFile:absoluteURL];
} }
if (*outError) { if (outError) {
[self presentError:*outError [self presentError:outError
modalForWindow:documentWindow modalForWindow:documentWindow
delegate:nil delegate:nil
didPresentSelector:NULL didPresentSelector:NULL
contextInfo:NULL]; contextInfo:NULL];
[*outError release]; [outError release];
} }
return YES; return YES;
} }
- (void)saveDidEnd:(id)sender {
/* Post an event so our event loop wakes up */
[NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
location:NSZeroPoint
modifierFlags:0
timestamp:0
windowNumber:0
context:NULL
subtype:0
data1:0
data2:0] atStart:NO];
}
- (BOOL)writeSafelyToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName - (BOOL)writeSafelyToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName
forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError; forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError;
{ {
__block BOOL success = NO; NSError *inError = nil;
__block int32_t done = 0;
__block NSError *inError = NULL;
NSMutableDictionary * attributes = [[NSMutableDictionary alloc] init]; NSMutableDictionary * attributes = [[NSMutableDictionary alloc] init];
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue]) if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"chaptersPreviewTrack"] boolValue])
@@ -193,20 +185,23 @@
if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBOrganizeAlternateGroups"] boolValue]) if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"SBOrganizeAlternateGroups"] boolValue])
[attributes setObject:@YES forKey:MP42OrganizeAlternateGroups]; [attributes setObject:@YES forKey:MP42OrganizeAlternateGroups];
dispatch_async(dispatch_get_main_queue(), ^{
[optBar setIndeterminate:YES]; [optBar setIndeterminate:YES];
[optBar startAnimation:self]; [optBar startAnimation:self];
[saveOperationName setStringValue:@"Saving…"]; [saveOperationName setStringValue:@"Saving…"];
[NSApp beginSheet:savingWindow modalForWindow:documentWindow [NSApp beginSheet:savingWindow modalForWindow:documentWindow
modalDelegate:nil didEndSelector:NULL contextInfo:nil]; modalDelegate:nil didEndSelector:NULL contextInfo:nil];
});
[self unblockUserInteraction];
IOPMAssertionID assertionID; IOPMAssertionID assertionID;
// Enable sleep assertion // Enable sleep assertion
CFStringRef reasonForActivity= CFSTR("Subler Save Operation"); CFStringRef reasonForActivity= CFSTR("Subler Save Operation");
IOReturn io_success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, IOReturn io_success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep,
kIOPMAssertionLevelOn, reasonForActivity, &assertionID); kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
BOOL success = NO;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
switch (saveOperation) { switch (saveOperation) {
case NSSaveOperation: case NSSaveOperation:
// movie file already exists, so we'll just update // movie file already exists, so we'll just update
@@ -219,39 +214,29 @@
success = [self.mp4 writeToUrl:absoluteURL withAttributes:attributes error:&inError]; success = [self.mp4 writeToUrl:absoluteURL withAttributes:attributes error:&inError];
break; break;
default: default:
// not implemented NSAssert(NO, @"Unhandled save operation");
break; break;
} }
if (success && _optimize) { if (success && _optimize) {
dispatch_async(dispatch_get_main_queue(), ^{
[saveOperationName setStringValue:@"Optimizing…"]; [saveOperationName setStringValue:@"Optimizing…"];
});
success = [self.mp4 optimize]; success = [self.mp4 optimize];
_optimize = NO; _optimize = NO;
} }
done = 1;
[inError retain];
});
while (!done) { if (io_success == kIOReturnSuccess) {
@autoreleasepool {
@try {
NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
if (event) [NSApp sendEvent:event];
}
@catch (NSException *localException) {
NSLog(@"Exception thrown during save: %@", localException);
}
}
}
if (io_success == kIOReturnSuccess)
IOPMAssertionRelease(assertionID); IOPMAssertionRelease(assertionID);
}
*outError = [inError autorelease];
[*outError retain];
[attributes release]; [attributes release];
[self saveDidComplete:outError URL:absoluteURL];
dispatch_async(dispatch_get_main_queue(), ^{
[self saveDidComplete:inError URL:absoluteURL];
});
return success; return success;
} }