Revision: 14918
          http://sourceforge.net/p/skim-app/code/14918
Author:   hofman
Date:     2025-02-05 19:13:09 +0000 (Wed, 05 Feb 2025)
Log Message:
-----------
Add completionHandler to convert notes methods. Suspend convert notes script 
command when waiting and not returning synchronously

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

Modified: trunk/SKMainDocument.m
===================================================================
--- trunk/SKMainDocument.m      2025-02-05 10:20:59 UTC (rev 14917)
+++ trunk/SKMainDocument.m      2025-02-05 19:13:09 UTC (rev 14918)
@@ -1116,7 +1116,7 @@
     }
 }
 
-- (void)convertNotesUsingPDFDocument:(PDFDocument *)pdfDocWithoutNotes {
+- (void)convertNotesUsingPDFDocument:(PDFDocument *)pdfDocWithoutNotes 
completionHandler:(void (^)(void))completionHandler {
     [[self mainWindowController] 
beginProgressSheetWithMessage:[NSLocalizedString(@"Converting notes", @"Message 
for progress sheet") stringByAppendingEllipsis] maxValue:0];
     
     NSMapTable *offsets = nil;
@@ -1176,6 +1176,9 @@
                 [[self mainWindowController] dismissProgressSheet];
                 
                 mdFlags.convertingNotes = 0;
+                
+                if (completionHandler)
+                    completionHandler();
             });
         });
         
@@ -1184,10 +1187,13 @@
         [[self mainWindowController] dismissProgressSheet];
         
         mdFlags.convertingNotes = 0;
+        
+        if (completionHandler)
+            completionHandler();
     }
 }
 
-- (void)beginConvertNotesPasswordSheetForPDFDocument:(PDFDocument *)pdfDoc {
+- (void)beginConvertNotesPasswordSheetForPDFDocument:(PDFDocument *)pdfDoc 
completionHandler:(void (^)(void))completionHandler {
     SKTextFieldSheetController *passwordSheetController = 
[[SKTextFieldSheetController alloc] initWithWindowNibName:@"PasswordSheet"];
     [passwordSheetController setInformativeText:NSLocalizedString(@"The 
document requires a password to be converted", @"Informative text")];
     
@@ -1197,17 +1203,20 @@
                 
                 if (pdfDoc && ([pdfDoc allowsNotes] == NO || [pdfDoc 
allowsSaving] == NO) &&
                     ([pdfDoc unlockWithPassword:[passwordSheetController 
stringValue]] == NO || [pdfDoc allowsNotes] == NO || [pdfDoc allowsSaving] == 
NO)) {
-                    [self beginConvertNotesPasswordSheetForPDFDocument:pdfDoc];
+                    [self beginConvertNotesPasswordSheetForPDFDocument:pdfDoc 
completionHandler:completionHandler];
                 } else {
-                    [self convertNotesUsingPDFDocument:pdfDoc];
+                    [self convertNotesUsingPDFDocument:pdfDoc 
completionHandler:completionHandler];
                 }
             } else {
                 mdFlags.convertingNotes = 0;
+                
+                if (completionHandler)
+                    completionHandler();
             }
         }];
 }
 
-- (void)convertNotes {
+- (void)convertNotesWithCompletionHandler:(void (^)(void))completionHandler {
     mdFlags.convertingNotes = 1;
     
     PDFDocument *pdfDocWithoutNotes = nil;
@@ -1216,11 +1225,11 @@
         pdfDocWithoutNotes = [[PDFDocument alloc] initWithData:pdfData];
         [self tryToUnlockDocument:pdfDocWithoutNotes];
         if ([pdfDocWithoutNotes allowsNotes] == NO || [pdfDocWithoutNotes 
allowsSaving] == NO) {
-            [self 
beginConvertNotesPasswordSheetForPDFDocument:pdfDocWithoutNotes];
+            [self 
beginConvertNotesPasswordSheetForPDFDocument:pdfDocWithoutNotes 
completionHandler:completionHandler];
             return;
         }
     }
-    [self convertNotesUsingPDFDocument:pdfDocWithoutNotes];
+    [self convertNotesUsingPDFDocument:pdfDocWithoutNotes 
completionHandler:completionHandler];
 }
 
 - (BOOL)hasConvertibleAnnotations {
@@ -1249,7 +1258,7 @@
         if (returnCode == NSAlertFirstButtonReturn) {
             // remove the sheet, to make place for either the password or 
progress sheet
             [[alert window] orderOut:nil];
-            [self convertNotes];
+            [self convertNotesWithCompletionHandler:nil];
         }
     }];
 }
@@ -2130,9 +2139,21 @@
     } else if ([self hasConvertibleAnnotations]) {
         NSDictionary *args = [command evaluatedArguments];
         NSNumber *wait = [args objectForKey:@"Wait"];
-        [self convertNotes];
-        if (wait == nil || [wait boolValue])
-            while (mdFlags.convertingNotes == 1 && [[NSRunLoop currentRunLoop] 
runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
+        if (wait == nil || [wait boolValue]) {
+            __block BOOL finished = NO;
+            __block BOOL suspended = NO;
+            [self convertNotesWithCompletionHandler:^{
+                if (suspended)
+                    [command resumeExecutionWithResult:nil];
+                finished = YES;
+            }];
+            if (finished == NO) {
+                [command suspendExecution];
+                suspended = YES;
+            }
+        } else {
+            [self convertNotesWithCompletionHandler:nil];
+        }
     }
 }
 

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



_______________________________________________
Skim-app-commit mailing list
Skim-app-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to