Revision: 29537
          http://sourceforge.net/p/bibdesk/svn/29537
Author:   hofman
Date:     2025-09-07 09:26:12 +0000 (Sun, 07 Sep 2025)
Log Message:
-----------
separate out some code to choose download destination

Modified Paths:
--------------
    trunk/bibdesk/BDSKDownloadManager.m

Modified: trunk/bibdesk/BDSKDownloadManager.m
===================================================================
--- trunk/bibdesk/BDSKDownloadManager.m 2025-09-05 17:00:18 UTC (rev 29536)
+++ trunk/bibdesk/BDSKDownloadManager.m 2025-09-07 09:26:12 UTC (rev 29537)
@@ -177,6 +177,35 @@
 
 - (NSString *)fontFamilyName { return [[NSFont systemFontOfSize:0.0] 
familyName]; }
 
+#pragma mark Download delegate protocols
+
+- (void)download:(id)download decideDestinationWithSuggestedFilename:(NSString 
*)filename window:(NSWindow *)window completionHandler:(void (^)(NSURL * 
destination))completionHandler {
+    NSString *extension = [filename pathExtension];
+   
+    NSString *downloadsDirectory = [[[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKDownloadsDirectoryKey] stringByExpandingTildeInPath];
+    NSURL *downloadsURL = nil;
+    if (downloadsDirectory)
+        downloadsURL = [NSURL fileURLWithPath:downloadsDirectory 
isDirectory:YES];
+    else
+        downloadsURL = [[NSFileManager defaultManager] downloadFolderURL];
+    
+    NSSavePanel *sPanel = [NSSavePanel savePanel];
+    if (NO == [extension isEqualToString:@""])
+        [sPanel setAllowedFileTypes:@[extension]];
+    [sPanel setAllowsOtherFileTypes:YES];
+    [sPanel setCanSelectHiddenExtension:YES];
+    [sPanel setNameFieldStringValue:filename];
+    [sPanel setDirectoryURL:downloadsURL];
+    
+    void (^handler)(NSModalResponse) = ^(NSModalResponse result){
+        completionHandler(result == NSModalResponseOK ? [sPanel URL] : nil);
+    };
+    if (window)
+        [sPanel beginSheetModalForWindow:window completionHandler:handler];
+    else
+        [sPanel beginWithCompletionHandler:handler];
+}
+
 #pragma mark NSURLDownload delegate protocol
 
 - (void)downloadDidBegin:(NSURLDownload *)download {
@@ -191,39 +220,25 @@
         // this must be an NSURLDownload, otherwise there won't be a response
         // move the file when finished, as the delegate method to choose 
beforehand does not work
         NSString *filename = [[webDownload response] suggestedFilename];
-        NSString *extension = [filename pathExtension];
-       
-        NSString *downloadsDirectory = [[[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKDownloadsDirectoryKey] stringByExpandingTildeInPath];
-        NSURL *downloadsURL = nil;
-        if (downloadsDirectory)
-            downloadsURL = [NSURL fileURLWithPath:downloadsDirectory 
isDirectory:YES];
-        else
-            downloadsURL = [[NSFileManager defaultManager] downloadFolderURL];
         
-        NSSavePanel *sPanel = [NSSavePanel savePanel];
-        if (NO == [extension isEqualToString:@""])
-            [sPanel setAllowedFileTypes:@[extension]];
-        [sPanel setAllowsOtherFileTypes:YES];
-        [sPanel setCanSelectHiddenExtension:YES];
-        [sPanel setNameFieldStringValue:filename];
-        [sPanel setDirectoryURL:downloadsURL];
-        
-        if ([sPanel runModal] == NSModalResponseOK) {
-            NSURL *fileURL = [sPanel URL];
-            NSError *error = nil;
-            if ([fileURL checkResourceIsReachableAndReturnError:NULL])
-                [[NSFileManager defaultManager] removeItemAtURL:fileURL 
error:NULL];
-            if ([[NSFileManager defaultManager] moveItemAtURL:[webDownload 
fileURL] toURL:fileURL error:&error]) {
-                [webDownload setFileURL:fileURL];
-                [self setStatus:BDSKDownloadStatusFinished 
forDownload:webDownload error:nil];
+        [self download:download 
decideDestinationWithSuggestedFilename:filename window:nil 
completionHandler:^(NSURL *destination){
+            if (destination) {
+                NSError *error = nil;
+                if ([destination checkResourceIsReachableAndReturnError:NULL])
+                    [[NSFileManager defaultManager] 
removeItemAtURL:destination error:NULL];
+                if ([[NSFileManager defaultManager] moveItemAtURL:[webDownload 
fileURL] toURL:destination error:&error]) {
+                    [webDownload setFileURL:destination];
+                    [self setStatus:BDSKDownloadStatusFinished 
forDownload:webDownload error:nil];
+                } else {
+                    [self setStatus:BDSKDownloadStatusFailed 
forDownload:webDownload error:error];
+                }
             } else {
+                NSError *error = [NSError errorWithDomain:NSURLErrorDomain 
code:NSURLErrorCancelled userInfo:nil];
                 [self setStatus:BDSKDownloadStatusFailed 
forDownload:webDownload error:error];
             }
-        } else {
-            [self setStatus:BDSKDownloadStatusFailed forDownload:webDownload 
error:nil];
-        }
+        }];
     } else {
-        [self setStatus:BDSKDownloadStatusFinished forDownload:webDownload 
error:nil];
+        [self setStatus:BDSKDownloadStatusFailed forDownload:webDownload 
error:nil];
     }
 }
 
@@ -255,36 +270,16 @@
 
 - (void)download:(BDSKDownload *)download didCompleteWithError:(NSError 
*)error {
     BDSKWebDownload *webDownload = [self webDownloadForDownload:download];
-    
-    if (error) {
+    if (error)
         [self setStatus:BDSKDownloadStatusFailed forDownload:webDownload 
error:error];
-    } else {
+    else
         [self setStatus:BDSKDownloadStatusFinished forDownload:webDownload 
error:nil];
-    }
 }
 
 - (void)download:(BDSKDownload *)download 
decideDestinationWithSuggestedFilename:(NSString *)filename 
completionHandler:(void (^)(NSURL *destinationURL, BOOL 
allowOverwrite))completionHandler {
-    NSString *extension = [filename pathExtension];
-   
-    NSString *downloadsDirectory = [[[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKDownloadsDirectoryKey] stringByExpandingTildeInPath];
-    NSURL *downloadsURL = nil;
-    if (downloadsDirectory)
-        downloadsURL = [NSURL fileURLWithPath:downloadsDirectory 
isDirectory:YES];
-    else
-        downloadsURL = [[NSFileManager defaultManager] downloadFolderURL];
-    
-    NSSavePanel *sPanel = [NSSavePanel savePanel];
-    if (NO == [extension isEqualToString:@""])
-        [sPanel setAllowedFileTypes:@[extension]];
-    [sPanel setAllowsOtherFileTypes:YES];
-    [sPanel setCanSelectHiddenExtension:YES];
-    [sPanel setNameFieldStringValue:filename];
-    [sPanel setDirectoryURL:downloadsURL];
-    
-    if ([sPanel runModal] == NSModalResponseOK)
-        completionHandler([sPanel URL], YES);
-    else
-        completionHandler(nil, NO);
+    [self download:download decideDestinationWithSuggestedFilename:filename 
window:nil completionHandler:^(NSURL *destination){
+        completionHandler(destination, YES);
+    }];
 }
 
 #pragma mark WKDownload delegate protocol
@@ -295,39 +290,14 @@
 }
 
 - (void)download:(WKDownload *)download 
decideDestinationUsingResponse:(NSURLResponse *)response 
suggestedFilename:(NSString *)filename completionHandler:(void (^)(NSURL * 
destination))completionHandler API_AVAILABLE(macos(11.3)) {
-    NSString *extension = [filename pathExtension];
-   
-    NSString *downloadsDirectory = [[[NSUserDefaults standardUserDefaults] 
stringForKey:BDSKDownloadsDirectoryKey] stringByExpandingTildeInPath];
-    NSURL *downloadsURL = nil;
-    if (downloadsDirectory)
-        downloadsURL = [NSURL fileURLWithPath:downloadsDirectory 
isDirectory:YES];
-    else
-        downloadsURL = [[NSFileManager defaultManager] downloadFolderURL];
-    
-    NSSavePanel *sPanel = [NSSavePanel savePanel];
-    if (NO == [extension isEqualToString:@""])
-        [sPanel setAllowedFileTypes:@[extension]];
-    [sPanel setAllowsOtherFileTypes:YES];
-    [sPanel setCanSelectHiddenExtension:YES];
-    [sPanel setNameFieldStringValue:filename];
-    [sPanel setDirectoryURL:downloadsURL];
-    
-    NSWindow *window = [[download webView] window];
-    void (^handler)(NSModalResponse) = ^(NSModalResponse result){
-        if (result == NSModalResponseOK) {
-            NSURL *fileURL = [sPanel URL];
-            if ([fileURL checkResourceIsReachableAndReturnError:NULL])
-                [[NSFileManager defaultManager] removeItemAtURL:fileURL 
error:NULL];
-            completionHandler(fileURL);
-            [[self webDownloadForDownload:download] setFileURL:fileURL];
-        } else {
-            completionHandler(nil);
+    [self download:download decideDestinationWithSuggestedFilename:filename 
window:[[download webView] window] completionHandler:^(NSURL *destination){
+        if (destination) {
+            if ([destination checkResourceIsReachableAndReturnError:NULL])
+                [[NSFileManager defaultManager] removeItemAtURL:destination 
error:NULL];
+            [[self webDownloadForDownload:download] setFileURL:destination];
         }
-    };
-    if (window)
-        [sPanel beginSheetModalForWindow:window completionHandler:handler];
-    else
-        [sPanel beginWithCompletionHandler:handler];
+        completionHandler(destination);
+    }];
 }
 
 @end

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



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to