Revision: 29524
          http://sourceforge.net/p/bibdesk/svn/29524
Author:   hofman
Date:     2025-09-03 16:18:21 +0000 (Wed, 03 Sep 2025)
Log Message:
-----------
move downloaded file from NSURLDownload after download to tmp file has 
finished, as the download does not wait for the user/delegate to choose a final 
location

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

Modified: trunk/bibdesk/BDSKDownloadManager.h
===================================================================
--- trunk/bibdesk/BDSKDownloadManager.h 2025-09-03 15:45:30 UTC (rev 29523)
+++ trunk/bibdesk/BDSKDownloadManager.h 2025-09-03 16:18:21 UTC (rev 29524)
@@ -76,6 +76,7 @@
     NSURL *URL;
     NSURL *fileURL;
     BDSKDownloadStatus status;
+    NSURLResponse *response;
     id download;
 }
 
@@ -86,6 +87,7 @@
 @property (nonatomic, readonly) NSURL *URL;
 @property (nonatomic, nullable, strong) NSURL *fileURL;
 @property (nonatomic, nullable, readonly) NSString *fileName;
+@property (nonatomic, nullable, strong) NSURLResponse *response;
 @property (nonatomic) BDSKDownloadStatus status;
 
 - (void)cancel;

Modified: trunk/bibdesk/BDSKDownloadManager.m
===================================================================
--- trunk/bibdesk/BDSKDownloadManager.m 2025-09-03 15:45:30 UTC (rev 29523)
+++ trunk/bibdesk/BDSKDownloadManager.m 2025-09-03 16:18:21 UTC (rev 29524)
@@ -207,8 +207,37 @@
     [self notifyUpdate];
 }
 
-- (void)downloadDidFinish:(id)download {
+- (void)downloadDidFinish:(id)download {log_method();
     BDSKWebDownload *webDownload = [self webDownloadForDownload:download];
+    
+    if ([webDownload response] && [webDownload fileURL]) {
+        // this must be an NSURLDownload, otherwwise 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 = [[NSFileManager defaultManager] 
uniqueFileURL:[sPanel URL]];
+            if ([[NSFileManager defaultManager] moveItemAtURL:[webDownload 
fileURL] toURL:fileURL error:NULL])
+                [webDownload setFileURL:fileURL];
+        }
+    }
+    
     [webDownload setStatus:BDSKDownloadStatusFinished];
     
     if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFinishedDownloadsKey] && webDownload)
@@ -234,35 +263,15 @@
     [alert runModal];
 }
 
-- (void)download:(NSURLDownload *)download 
decideDestinationWithSuggestedFilename:(NSString *)filename {
-       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];
-       
-    // we need to do this modally, not using a sheet, as the download may 
otherwise finish on Leopard before the sheet is done
-    NSInteger returnCode = [sPanel runModal];
-    if (returnCode == NSModalResponseOK)
-        [download setDestination:[[sPanel URL] path] allowOverwrite:YES];
-    else
-        [download cancel];
+- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse 
*)response {log_method();
+    [[self webDownloadForDownload:download] setResponse:response];
 }
 
-- (void)download:(NSURLDownload *)download didCreateDestination:(NSString 
*)path {
+// -download:decideDestinationWithSuggestedFilename: is not functional anymore
+// it returns after the destination is already returned and downloading to 
disk has already started
+
+- (void)download:(NSURLDownload *)download didCreateDestination:(NSString 
*)path {log_method();
     [[self webDownloadForDownload:download] setFileURL:[NSURL 
fileURLWithPath:path isDirectory:NO]];
-    [self notifyUpdate];
 }
 
 - (BOOL)download:(NSURLDownload *)download 
shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType {
@@ -329,7 +338,7 @@
 
 @implementation BDSKWebDownload
 
-@synthesize download, uniqueID, URL, fileURL, status;
+@synthesize download, uniqueID, URL, fileURL, status, response;
 @dynamic fileName;
 
 static NSUInteger currentUniqueID = 0;
@@ -342,6 +351,7 @@
         fileURL = nil;
         status = BDSKDownloadStatusDownloading;
         download = aDownload;
+        response = nil;
     }
     return self;
 }
@@ -363,8 +373,10 @@
 - (void)setStatus:(BDSKDownloadStatus)newStatus {
     if (status != newStatus) {
         status = newStatus;
-        if (status != BDSKDownloadStatusDownloading)
+        if (status != BDSKDownloadStatusDownloading) {
             download = nil;
+            response = nil;
+        }
         if (status == BDSKDownloadStatusFailed)
             [self setFileURL:nil];
     }

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