Revision: 9525
          http://sourceforge.net/p/skim-app/code/9525
Author:   hofman
Date:     2017-12-05 17:06:08 +0000 (Tue, 05 Dec 2017)
Log Message:
-----------
Do slow part of convert notes in background on 10.12+

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

Modified: trunk/SKMainDocument.m
===================================================================
--- trunk/SKMainDocument.m      2017-12-05 16:29:57 UTC (rev 9524)
+++ trunk/SKMainDocument.m      2017-12-05 17:06:08 UTC (rev 9525)
@@ -1078,9 +1078,9 @@
     
     PDFDocument *pdfDoc = [self pdfDocument];
     NSInteger i, count = [pdfDoc pageCount];
-    BOOL didConvert = NO;
     NSMapTable *offsets = nil;
     SKPDFView *pdfView = [self pdfView];
+    NSMutableArray *annotations = nil;
     
     for (i = 0; i < count; i++) {
         PDFPage *page = [pdfDoc pageAtIndex:i];
@@ -1088,23 +1088,9 @@
         
         for (PDFAnnotation *annotation in [[[page annotations] copy] 
autorelease]) {
             if ([annotation isSkimNote] == NO && [annotation 
isConvertibleAnnotation]) {
-                NSDictionary *properties = [annotation SkimNoteProperties];
-                if ([[annotation type] isEqualToString:SKNTextString])
-                    properties = [SKNPDFAnnotationNote 
textToNoteSkimNoteProperties:properties];
-                PDFAnnotation *newAnnotation = [[PDFAnnotation alloc] 
initSkimNoteWithProperties:properties];
-                if (newAnnotation) {
-                    // this is only to make sure markup annotations generate 
the lineRects, for thread safety
-                    [newAnnotation boundsOrder];
-                    PDFAnnotation *popup = [annotation popup];
-                    if (popup)
-                        [pdfView removeAnnotation:popup];
-                    [pdfView removeAnnotation:annotation];
-                    [pdfView addAnnotation:newAnnotation toPage:page];
-                    if ([[newAnnotation contents] length] == 0)
-                        [newAnnotation autoUpdateString];
-                    [newAnnotation release];
-                    didConvert = YES;
-                }
+                if (annotations == nil)
+                    annotations = [[NSMutableArray alloc] init];
+                [annotations addObject:annotation];
             }
         }
         
@@ -1117,30 +1103,71 @@
         }
     }
     
-    if (didConvert) {
+    if (annotations) {
+        
         // if pdfDocWithoutNotes was nil, the document was not encrypted, so 
no need to try to unlock
         if (pdfDocWithoutNotes == nil)
             pdfDocWithoutNotes = [[[PDFDocument alloc] initWithData:pdfData] 
autorelease];
-        count = [pdfDocWithoutNotes pageCount];
-        for (i = 0; i < count; i++) {
-            PDFPage *page = [pdfDocWithoutNotes pageAtIndex:i];
+        
+        dispatch_queue_t queue = floor(NSAppKitVersionNumber) > 
NSAppKitVersionNumber10_11 ? 
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) : 
dispatch_get_main_queue();
+        
+        dispatch_async(queue, ^{
             
-            for (PDFAnnotation *annotation in [[[page annotations] copy] 
autorelease]) {
-                if ([annotation isSkimNote] == NO && [annotation 
isConvertibleAnnotation])
-                    [page removeAnnotation:annotation];
+            NSInteger i, count = [pdfDocWithoutNotes pageCount];
+            
+            for (i = 0; i < count; i++) {
+                PDFPage *page = [pdfDocWithoutNotes pageAtIndex:i];
+                
+                for (PDFAnnotation *annotation in [[[page annotations] copy] 
autorelease]) {
+                    if ([annotation isSkimNote] == NO && [annotation 
isConvertibleAnnotation])
+                        [page removeAnnotation:annotation];
+                }
             }
-        }
+            
+            NSData *pdfData = [pdfDocWithoutNotes dataRepresentation];
+            
+            [[pdfDocWithoutNotes outlineRoot] clearDocument];
+
+            dispatch_async(dispatch_get_main_queue(), ^{
+                
+                for (PDFAnnotation *annotation in annotations) {
+                    NSDictionary *properties = [annotation SkimNoteProperties];
+                    if ([[annotation type] isEqualToString:SKNTextString])
+                        properties = [SKNPDFAnnotationNote 
textToNoteSkimNoteProperties:properties];
+                    PDFAnnotation *newAnnotation = [[PDFAnnotation alloc] 
initSkimNoteWithProperties:properties];
+                    if (newAnnotation) {
+                        // this is only to make sure markup annotations 
generate the lineRects, for thread safety
+                        [newAnnotation boundsOrder];
+                        PDFAnnotation *popup = [annotation popup];
+                        if (popup)
+                            [pdfView removeAnnotation:popup];
+                        [pdfView addAnnotation:newAnnotation 
toPage:[annotation page]];
+                        [pdfView removeAnnotation:annotation];
+                        if ([[newAnnotation contents] length] == 0)
+                            [newAnnotation autoUpdateString];
+                        [newAnnotation release];
+                    }
+                }
+
+                [self setPDFData:pdfData pageOffsets:offsets];
+                
+                [[self undoManager] setActionName:NSLocalizedString(@"Convert 
Notes", @"Undo action name")];
+                
+                [offsets release];
+                [annotations release];
+                
+                [[self mainWindowController] dismissProgressSheet];
+            });
+        });
         
-        [self setPDFData:[pdfDocWithoutNotes dataRepresentation] 
pageOffsets:offsets];
+    } else {
         
-        [[self undoManager] setActionName:NSLocalizedString(@"Convert Notes", 
@"Undo action name")];
+        [offsets release];
+
+        [[pdfDocWithoutNotes outlineRoot] clearDocument];
+        
+        [[self mainWindowController] dismissProgressSheet];
     }
-    
-    [offsets release];
-
-    [[pdfDocWithoutNotes outlineRoot] clearDocument];
-    
-    [[self mainWindowController] dismissProgressSheet];
 }
 
 - (void)beginConvertNotesPasswordSheetForPDFDocument:(PDFDocument *)pdfDoc {

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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to