Revision: 14737
          http://sourceforge.net/p/skim-app/code/14737
Author:   hofman
Date:     2024-11-22 15:36:50 +0000 (Fri, 22 Nov 2024)
Log Message:
-----------
Method to print documents from URL in document controller subclass, makes 
implementing app delegate method more transparent and avoids wrong callbacks 
from services

Modified Paths:
--------------
    trunk/SKApplicationController.m
    trunk/SKDocumentController.h
    trunk/SKDocumentController.m

Modified: trunk/SKApplicationController.m
===================================================================
--- trunk/SKApplicationController.m     2024-11-21 16:21:37 UTC (rev 14736)
+++ trunk/SKApplicationController.m     2024-11-22 15:36:50 UTC (rev 14737)
@@ -302,39 +302,16 @@
 - (NSApplicationPrintReply)application:(NSApplication *)sender 
printFiles:(NSArray *)fileNames withSettings:(NSDictionary *)printSettings 
showPrintPanels:(BOOL)showPrintPanels {
     didCheckReopen = YES;
     
-    NSUInteger count = [fileNames count];
-    if ([fileNames count] == 0)
-        return NSPrintingFailure;
-    
-    NSURL *fileURL = [NSURL fileURLWithPath:[fileNames firstObject]];
-    NSArray *nextFileNames = count > 1 ? [fileNames 
subarrayWithRange:NSMakeRange(1, count - 1)] : nil;
     // keep track to see whether we finished before this method returns
     __block NSApplicationPrintReply reply = NSNotFound;
+    NSMutableArray *fileURLs = [NSMutableArray array];
+    for (NSString *fileName in fileNames)
+        [fileURLs addObject:[NSURL fileURLWithPath:fileName]];
     
-    [[NSDocumentController sharedDocumentController] 
openDocumentWithContentsOfURL:fileURL display:NO completionHandler:^(NSDocument 
*document, BOOL documentWasAlreadyOpen, NSError *error){
-        if (document) {
-            void (^block)(BOOL) = ^(BOOL success){
-                NSApplicationPrintReply aReply;
-                if (documentWasAlreadyOpen == NO)
-                    [document close];
-                if (success == NO)
-                    aReply = NSPrintingFailure;
-                else if (nextFileNames)
-                    aReply = [self application:NSApp printFiles:nextFileNames 
withSettings:printSettings showPrintPanels:showPrintPanels];
-                else
-                    aReply = NSPrintingSuccess;
-                if (aReply != NSPrintingReplyLater) {
-                    if (reply == NSPrintingReplyLater)
-                        [NSApp replyToOpenOrPrint:aReply == NSPrintingSuccess 
? NSApplicationDelegateReplySuccess : NSApplicationDelegateReplyFailure];
-                    reply = aReply;
-                }
-            };
-            [document printDocumentWithSettings:printSettings 
showPrintPanel:showPrintPanels delegate:self 
didPrintSelector:@selector(document:didPrint:contextInfo:) contextInfo:(void 
*)CFBridgingRetain(block)];
-        } else {
-            if (reply == NSPrintingReplyLater)
-                [NSApp replyToOpenOrPrint:NSApplicationDelegateReplyFailure];
-            reply = NSPrintingFailure;
-        }
+    [[NSDocumentController sharedDocumentController] 
printDocumentsWithContentsOfURLs:fileURLs withSettings:printSettings 
showPrintPanels:showPrintPanels completionHandler:^(BOOL didPrintSuccessfully){
+        if (reply == NSPrintingReplyLater)
+            [NSApp replyToOpenOrPrint:didPrintSuccessfully ? 
NSApplicationDelegateReplySuccess : NSApplicationDelegateReplyFailure];
+        reply = didPrintSuccessfully ? NSPrintingSuccess : NSPrintingFailure;
     }];
     
     // setting this tells the async block that we returned

Modified: trunk/SKDocumentController.h
===================================================================
--- trunk/SKDocumentController.h        2024-11-21 16:21:37 UTC (rev 14736)
+++ trunk/SKDocumentController.h        2024-11-22 15:36:50 UTC (rev 14737)
@@ -81,6 +81,8 @@
 - (void)openDocumentWithBookmark:(SKBookmark *)bookmark 
completionHandler:(void (^)(NSDocument * _Nullable document, BOOL 
documentWasAlreadyOpen, NSError * _Nullable error))completionHandler;
 - (void)openDocumentWithBookmarks:(NSArray<SKBookmark *> *)bookmarks 
completionHandler:(void (^)(NSDocument * _Nullable document, BOOL 
documentWasAlreadyOpen, NSError * _Nullable error))completionHandler;
 
+- (void)printDocumentsWithContentsOfURLs:(NSArray<NSURL *> *)urls 
withSettings:(NSDictionary<NSString *, id> *)printSettings 
showPrintPanels:(BOOL)showPrintPanels completionHandler:(void (^)(BOOL 
didPrintSuccessfully))completionHandler;
+
 - (nullable Class)documentClassForContentsOfURL:(NSURL *)inAbsoluteURL;
 
 @property (nonatomic, readonly) BOOL openedFile;

Modified: trunk/SKDocumentController.m
===================================================================
--- trunk/SKDocumentController.m        2024-11-21 16:21:37 UTC (rev 14736)
+++ trunk/SKDocumentController.m        2024-11-22 15:36:50 UTC (rev 14737)
@@ -651,6 +651,39 @@
     }
 }
 
+- (void)document:(NSDocument *)document didPrint:(BOOL)didPrintSuccessfully 
contextInfo:(void *)contextInfo {
+    if (contextInfo) {
+        void (^block)(BOOL) = (void(^)(BOOL))CFBridgingRelease(contextInfo);
+        block(didPrintSuccessfully);
+    }
+}
+
+- (void)printDocumentsWithContentsOfURLs:(NSArray *)urls 
withSettings:(NSDictionary *)printSettings 
showPrintPanels:(BOOL)showPrintPanels completionHandler:(void (^)(BOOL 
didPrintSuccessfully))completionHandler {
+    NSUInteger count = [urls count];
+    if ([urls count] == 0) {
+        completionHandler(YES);
+        return;
+    }
+    
+    NSURL *fileURL = [urls firstObject];
+    NSArray *nextURLs = count > 1 ? [urls subarrayWithRange:NSMakeRange(1, 
count - 1)] : nil;
+    [self openDocumentWithContentsOfURL:fileURL display:NO 
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError 
*error){
+        if (document) {
+            void (^block)(BOOL) = ^(BOOL success){
+                if (documentWasAlreadyOpen == NO)
+                    [document close];
+                if (success && nextURLs)
+                    [self printDocumentsWithContentsOfURLs:nextURLs 
withSettings:printSettings showPrintPanels:showPrintPanels 
completionHandler:completionHandler];
+                else
+                    completionHandler(success);
+            };
+            [document printDocumentWithSettings:printSettings 
showPrintPanel:showPrintPanels delegate:self 
didPrintSelector:@selector(document:didPrint:contextInfo:) contextInfo:(void 
*)CFBridgingRetain(block)];
+        } else {
+            completionHandler(NO);
+        }
+    }];
+}
+
 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem {
     if ([anItem action] == @selector(newDocumentFromClipboard:)) {
         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
@@ -692,9 +725,8 @@
 
 - (void)printDocumentFromURLOnPboard:(NSPasteboard *)pboard userData:(NSString 
*)userData error:(out NSString * __autoreleasing *)errorString {
     NSURL *theURL = [[NSURL readURLsFromPasteboard:pboard] firstObject];
-    if ([theURL isFileURL] && [[NSApp delegate] 
respondsToSelector:@selector(application:printFiles:withSettings:showPrintPanels:)])
 {
-        [[NSApp delegate] application:NSApp printFiles:@[theURL] 
withSettings:@{} showPrintPanels:[userData boolValue]];
-    }
+    if (theURL)
+        [self printDocumentsWithContentsOfURLs:@[theURL] withSettings:@{} 
showPrintPanels:[userData boolValue] completionHandler:^(BOOL 
didPrintSuccessfully){}];
 }
 
 @end

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



_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to