Revision: 29631
          http://sourceforge.net/p/bibdesk/svn/29631
Author:   hofman
Date:     2025-09-26 16:40:07 +0000 (Fri, 26 Sep 2025)
Log Message:
-----------
Allow save panel sheet for BDSKDownload destination, move file to tmp location 
when the deldegate method finishes before the completionHandler is called

Modified Paths:
--------------
    trunk/bibdesk/BDSKDownloader.m
    trunk/bibdesk/BDSKTextImportController.m

Modified: trunk/bibdesk/BDSKDownloader.m
===================================================================
--- trunk/bibdesk/BDSKDownloader.m      2025-09-26 16:25:11 UTC (rev 29630)
+++ trunk/bibdesk/BDSKDownloader.m      2025-09-26 16:40:07 UTC (rev 29631)
@@ -169,11 +169,15 @@
             filename = disposition;
     }
     
+    __block NSURL *locationURL = location;
+    
     [download _decideDestinationWithSuggestedFilename:filename 
completionHandler:^(NSURL *destinationURL){
         if (destinationURL == nil) {
             NSError *error = [NSError errorWithDomain:NSURLErrorDomain 
code:NSURLErrorCancelled userInfo:nil];
             [download _didCompleteWithError:error];
             [self cleanupDownload:download];
+            if (locationURL != location)
+                [[NSFileManager defaultManager] removeItemAtURL:locationURL 
error:NULL];
         } else {
             NSError *error = nil;
             NSFileManager *fm = [NSFileManager defaultManager];
@@ -180,7 +184,7 @@
             if ([[destinationURL URLByDeletingLastPathComponent] 
checkResourceIsReachableAndReturnError:NULL] == NO) {
                 [fm createDirectoryAtPath:[[destinationURL 
URLByDeletingLastPathComponent] path] withIntermediateDirectories:YES 
attributes:nil error:NULL];
             }
-            if ([fm moveItemAtURL:location toURL:destinationURL error:&error]) 
{
+            if ([fm moveItemAtURL:locationURL toURL:destinationURL 
error:&error]) {
                 [download _didCompleteWithError:nil];
             } else {
                 [download _didCompleteWithError:error];
@@ -187,7 +191,15 @@
                 [self cleanupDownload:download];
             }
         }
+        locationURL = nil;
     }];
+    
+    if (locationURL) {
+        // finish before completionHandler was called, move the file to a tmp 
location or it will be deleted
+        locationURL = [[NSFileManager defaultManager] 
uniqueTemporaryDirectoryURLWithBasename:filename];
+        if ([[NSFileManager defaultManager] moveItemAtURL:location 
toURL:locationURL error:NULL] == NO)
+            locationURL = location;
+    }
 }
 
 - (void)URLSession:(NSURLSession *)aSession 
downloadTask:(NSURLSessionDownloadTask *)task 
didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten 
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {

Modified: trunk/bibdesk/BDSKTextImportController.m
===================================================================
--- trunk/bibdesk/BDSKTextImportController.m    2025-09-26 16:25:11 UTC (rev 
29630)
+++ trunk/bibdesk/BDSKTextImportController.m    2025-09-26 16:40:07 UTC (rev 
29631)
@@ -1255,17 +1255,21 @@
     [sPanel setCanSelectHiddenExtension:YES];
     [sPanel setNameFieldStringValue:destinationName];
     
-    // we need to do this modally, not using a sheet, as the download may 
otherwise delete the downloaded file before the sheet is done
-    NSURL *fileURL = nil;
-    if ([sPanel runModal] == NSModalResponseOK) {
-        fileURL = [sPanel URL];
-        if ([fileURL checkResourceIsReachableAndReturnError:NULL])
-            [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
-        downloadFileName = [fileURL path];
-    }
-    completionHandler(fileURL);
+    [sPanel beginSheetModalForWindow:[self window] 
completionHandler:^(NSModalResponse result){
+        NSURL *fileURL = nil;
+        if (result == NSModalResponseOK) {
+            fileURL = [sPanel URL];
+            if ([fileURL checkResourceIsReachableAndReturnError:NULL])
+                [[NSFileManager defaultManager] removeItemAtURL:fileURL 
error:nil];
+        }
+        completionHandler(fileURL);
+    }];
 }
 
+- (void)download:(BDSKDownload *)aDownload didCreateDestination:(NSURL 
*)destinationURL {
+       downloadFileName = [[destinationURL path] copy];
+}
+
 - (void)download:(BDSKDownload *)aDownload didCompleteWithError:(NSError 
*)error {
     [self setDownloading:NO];
     if (error == 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