Revision: 5948
          http://skim-app.svn.sourceforge.net/skim-app/?rev=5948&view=rev
Author:   hofman
Date:     2009-10-26 09:19:38 +0000 (Mon, 26 Oct 2009)

Log Message:
-----------
save skim notes in save delegate method rather than the save method, because 
saving may be delayed when the document wants to ask the user permission to 
e.g. override a file lock

Modified Paths:
--------------
    trunk/SKMainDocument.m

Modified: trunk/SKMainDocument.m
===================================================================
--- trunk/SKMainDocument.m      2009-10-25 20:50:02 UTC (rev 5947)
+++ trunk/SKMainDocument.m      2009-10-26 09:19:38 UTC (rev 5948)
@@ -309,7 +309,7 @@
     return success;
 }
 
-- (void)document:(NSDocument *)doc didSave:(BOOL)didSave contextInfo:(void 
*)contextInfo { 
+- (void)document:(NSDocument *)doc didSaveFromPanel:(BOOL)didSave 
contextInfo:(void *)contextInfo { 
     docFlags.exportUsingPanel = NO;
     if (contextInfo != NULL) {
         NSInvocation *invocation = [(NSInvocation *)contextInfo autorelease];
@@ -333,26 +333,21 @@
         [invocation retainArguments];
     }
     
-    [super runModalSavePanelForSaveOperation:saveOperation delegate:self 
didSaveSelector:@selector(document:didSave:contextInfo:) contextInfo:NULL];
+    [super runModalSavePanelForSaveOperation:saveOperation delegate:self 
didSaveSelector:@selector(document:didSaveFromPanel:contextInfo:) 
contextInfo:invocation];
 }
 
-- (BOOL)saveToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName 
forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError{
-    if (saveOperation == NSSaveOperation || saveOperation == 
NSSaveAsOperation) {
-        [self stopCheckingFileUpdates];
-        docFlags.isSaving = YES;
-    } else if (docFlags.exportUsingPanel) {
-        [[NSUserDefaults standardUserDefaults] setObject:typeName 
forKey:SKLastExportedTypeKey];
-    }
+- (void)document:(NSDocument *)doc didSave:(BOOL)didSave contextInfo:(void  
*)contextInfo {
+    NSDictionary *info = [(id)contextInfo autorelease];
+    NSURL *absoluteURL = [info objectForKey:@"URL"];
+    NSString *typeName = [info objectForKey:@"type"];
+    NSSaveOperationType saveOperation = [[info objectForKey:@"saveOperation"] 
intValue];
     
-    BOOL success = NO;
-    NSError *error = nil;
-    
     // we check for notes and may save a .skim as well:
     if (SKIsPDFDocumentType(typeName) || SKIsPostScriptDocumentType(typeName) 
|| SKIsDVIDocumentType(typeName)) {
         
         NSFileManager *fm = [NSFileManager defaultManager];
         
-        if (success = [super saveToURL:absoluteURL ofType:typeName 
forSaveOperation:saveOperation error:&error]) {
+        if (didSave) {
             
             BOOL saveNotesOK = NO;
             
@@ -404,51 +399,30 @@
             if (options)
                 [[SKNExtendedAttributeManager sharedManager] 
setExtendedAttributeNamed:PRESENTATION_OPTIONS_KEY toPropertyListValue:options 
atPath:[absoluteURL path] options:kSKNXattrDefault error:NULL];
             
-            if (success)
-                [[NSDistributedNotificationCenter defaultCenter]
-                    postNotificationName:SKSkimFileDidSaveNotification 
object:[absoluteURL path]];
+            [[NSDistributedNotificationCenter defaultCenter]
+                postNotificationName:SKSkimFileDidSaveNotification 
object:[absoluteURL path]];
         }
         
     } else if (SKIsPDFBundleDocumentType(typeName)) {
         
         NSFileManager *fm = [NSFileManager defaultManager];
         NSString *path = [absoluteURL path];
-        NSString *tmpPath = nil;
-        BOOL isDir = NO;
-        NSString *file;
+        NSString *tmpPath = [info objectForKey:@"tmpPath"];
         
-        // we move everything that's not ours out of the way, so we can 
preserve version control info
-        if ([fm fileExistsAtPath:path isDirectory:&isDir] && isDir) {
-            NSSet *ourExtensions = [NSSet setWithObjects:@"pdf", @"skim", 
@"fdf", @"txt", @"text", @"rtf", @"plist", nil];
-            for (file in [fm contentsOfDirectoryAtPath:path error:NULL]) {
-                if ([ourExtensions containsObject:[[file pathExtension] 
lowercaseString]] == NO) {
-                    if (tmpPath == nil)
-                        tmpPath = SKUniqueTemporaryDirectory();
-                    [fm moveItemAtPath:[path 
stringByAppendingPathComponent:file] toPath:[tmpPath 
stringByAppendingPathComponent:file] error:NULL];
-                }
-            }
-        }
-        
-        success = [super saveToURL:absoluteURL ofType:typeName 
forSaveOperation:saveOperation error:&error];
-        
         if (tmpPath) {
-            for (file in [fm contentsOfDirectoryAtPath:tmpPath error:NULL])
+            for (NSString *file in [fm contentsOfDirectoryAtPath:tmpPath 
error:NULL])
                 [fm moveItemAtPath:[tmpPath 
stringByAppendingPathComponent:file] toPath:[path 
stringByAppendingPathComponent:file] error:NULL];
             [fm removeItemAtPath:tmpPath error:NULL];
         }
         
-        if (success)
+        if (didSave)
             [[NSDistributedNotificationCenter defaultCenter]
                 postNotificationName:SKSkimFileDidSaveNotification 
object:[absoluteURL path]];
         
-    } else {
-        
-        success = [super saveToURL:absoluteURL ofType:typeName 
forSaveOperation:saveOperation error:&error];
-        
     }
     
     if (saveOperation == NSSaveOperation || saveOperation == 
NSSaveAsOperation) {
-        if (success) {
+        if (didSave) {
             [[self undoManager] removeAllActions];
             [self updateChangeCount:NSChangeCleared];
             docFlags.fileChangedOnDisk = NO;
@@ -459,10 +433,57 @@
         docFlags.isSaving = NO;
     }
     
-    if (success == NO && outError != NULL)
-        *outError = error ? : [NSError errorWithDomain:SKDocumentErrorDomain 
code:SKWriteFileError userInfo:[NSDictionary 
dictionaryWithObjectsAndKeys:NSLocalizedString(@"Unable to write file", @"Error 
description"), NSLocalizedDescriptionKey, nil]];
+    NSInvocation *invocation = [info objectForKey:@"callback"];
+    if (invocation) {
+        [invocation setArgument:&doc atIndex:2];
+        [invocation setArgument:&didSave atIndex:3];
+        [invocation invoke];
+    }
+}
+
+// Don't use -saveToURL:ofType:forSaveOperation:error:, because that may 
return before the actual saving when NSDoucment needs to ask the user for 
permission, for instance to override a file lock
+- (void)saveToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName 
forSaveOperation:(NSSaveOperationType)saveOperation delegate:(id)delegate 
didSaveSelector:(SEL)didSaveSelector contextInfo:(void *)contextInfo {
+    if (saveOperation == NSSaveOperation || saveOperation == 
NSSaveAsOperation) {
+        [self stopCheckingFileUpdates];
+        docFlags.isSaving = YES;
+    } else if (docFlags.exportUsingPanel) {
+        [[NSUserDefaults standardUserDefaults] setObject:typeName 
forKey:SKLastExportedTypeKey];
+    }
     
-    return success;
+    NSMutableDictionary *info = [[NSMutableDictionary alloc] 
initWithObjectsAndKeys:absoluteURL, @"URL", typeName, @"type", [NSNumber 
numberWithInt:saveOperation], @"saveOperation", nil];
+    NSInvocation *invocation = nil;
+    if (delegate && didSaveSelector) {
+        NSMethodSignature *ms = [delegate 
methodSignatureForSelector:didSaveSelector];
+        invocation = [NSInvocation invocationWithMethodSignature:ms];
+        [invocation setTarget:delegate];
+        [invocation setSelector:didSaveSelector];
+        [invocation setArgument:&contextInfo atIndex:4];
+        [invocation retainArguments];
+        [info setObject:invocation forKey:@"callback"];
+    }
+    
+    if (SKIsPDFBundleDocumentType(typeName)) {
+        NSFileManager *fm = [NSFileManager defaultManager];
+        NSString *path = [absoluteURL path];
+        NSString *tmpPath = [info objectForKey:@"tmpPath"];
+        BOOL isDir = NO;
+        
+        // we move everything that's not ours out of the way, so we can 
preserve version control info
+        if ([fm fileExistsAtPath:path isDirectory:&isDir] && isDir) {
+            NSSet *ourExtensions = [NSSet setWithObjects:@"pdf", @"skim", 
@"fdf", @"txt", @"text", @"rtf", @"plist", nil];
+            for (NSString *file in [fm contentsOfDirectoryAtPath:path 
error:NULL]) {
+                if ([ourExtensions containsObject:[[file pathExtension] 
lowercaseString]] == NO) {
+                    if (tmpPath == nil)
+                        tmpPath = SKUniqueTemporaryDirectory();
+                    [fm moveItemAtPath:[path 
stringByAppendingPathComponent:file] toPath:[tmpPath 
stringByAppendingPathComponent:file] error:NULL];
+                }
+            }
+        }
+        if (tmpPath)
+            [info setObject:tmpPath forKey:@"tmpPath"];
+    }
+    
+    [super saveToURL:absoluteURL ofType:typeName 
forSaveOperation:saveOperation delegate:self 
didSaveSelector:@selector(document:didSave:contextInfo:) contextInfo:info];
 }
 
 - (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName 
error:(NSError **)outError{


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to