Revision: 29535
          http://sourceforge.net/p/bibdesk/svn/29535
Author:   hofman
Date:     2025-09-05 16:49:45 +0000 (Fri, 05 Sep 2025)
Log Message:
-----------
convenience method to finalize status of download

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

Modified: trunk/bibdesk/BDSKDownloadManager.m
===================================================================
--- trunk/bibdesk/BDSKDownloadManager.m 2025-09-05 16:23:38 UTC (rev 29534)
+++ trunk/bibdesk/BDSKDownloadManager.m 2025-09-05 16:49:45 UTC (rev 29535)
@@ -91,6 +91,27 @@
     [self notifyUpdate];
 }
 
+- (void)setStatus:(BDSKDownloadStatus)status forDownload:(BDSKWebDownload 
*)webDownload error:(NSError *)error {
+    if (webDownload == nil)
+        return;
+    
+    [webDownload setStatus:status];
+    
+    if ((status == BDSKDownloadStatusFailed && [[NSUserDefaults 
standardUserDefaults] boolForKey:BDSKRemoveFailedDownloadsKey]) ||
+        (status == BDSKDownloadStatusFinished && [[NSUserDefaults 
standardUserDefaults] boolForKey:BDSKRemoveFinishedDownloadsKey]))
+        [downloads removeObject:webDownload];
+    
+    [self notifyUpdate];
+    
+    if (status == BDSKDownloadStatusFailed && error && ([[error domain] 
isEqualToString:NSURLErrorDomain] == NO || [error code] != 
NSURLErrorCancelled)) {
+        NSString *errorDescription = [error localizedDescription] ?: 
NSLocalizedString(@"An error occured during download.", @"Informative text in 
alert dialog");
+        NSAlert *alert = [[NSAlert alloc] init];
+        [alert setMessageText:NSLocalizedString(@"Download Failed", @"Message 
in alert dialog when download failed")];
+        [alert setInformativeText:errorDescription];
+        [alert runModal];
+    }
+}
+
 - (BDSKWebDownload *)webDownloadForDownload:(id)download {
     for (BDSKWebDownload *webDownload in downloads) {
         if ([webDownload download] == download)
@@ -189,55 +210,26 @@
         
         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:NULL]) {
+            if ([[NSFileManager defaultManager] moveItemAtURL:[webDownload 
fileURL] toURL:fileURL error:&error]) {
                 [webDownload setFileURL:fileURL];
-                
-                [webDownload setStatus:BDSKDownloadStatusFinished];
-                
-                if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFinishedDownloadsKey] && webDownload)
-                    [downloads removeObject:webDownload];
-                [self notifyUpdate];
+                [self setStatus:BDSKDownloadStatusFinished 
forDownload:webDownload error:nil];
             } else {
-                [webDownload setStatus:BDSKDownloadStatusFailed];
-                
-                if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFailedDownloadsKey] && webDownload)
-                    [downloads removeObject:webDownload];
-                [self notifyUpdate];
+                [self setStatus:BDSKDownloadStatusFailed 
forDownload:webDownload error:error];
             }
         } else {
-            [webDownload setStatus:BDSKDownloadStatusFailed];
-            
-            if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFailedDownloadsKey] && webDownload)
-                [downloads removeObject:webDownload];
-            [self notifyUpdate];
+            [self setStatus:BDSKDownloadStatusFailed forDownload:webDownload 
error:nil];
         }
     } else {
-        [webDownload setStatus:BDSKDownloadStatusFinished];
-        
-        if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFinishedDownloadsKey] && webDownload)
-            [downloads removeObject:webDownload];
-        [self notifyUpdate];
+        [self setStatus:BDSKDownloadStatusFinished forDownload:webDownload 
error:nil];
     }
 }
 
 - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error {
     BDSKWebDownload *webDownload = [self webDownloadForDownload:download];
-    [webDownload setStatus:BDSKDownloadStatusFailed];
-    
-    if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFailedDownloadsKey] && webDownload)
-        [downloads removeObject:webDownload];
-    [self notifyUpdate];
-    
-    if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == 
NSURLErrorCancelled)
-        return;
-    
-    NSString *errorDescription = [error localizedDescription] ?: 
NSLocalizedString(@"An error occured during download.", @"Informative text in 
alert dialog");
-    NSAlert *alert = [[NSAlert alloc] init];
-    [alert setMessageText:NSLocalizedString(@"Download Failed", @"Message in 
alert dialog when download failed")];
-    [alert setInformativeText:errorDescription];
-    [alert runModal];
+    [self setStatus:BDSKDownloadStatusFailed forDownload:webDownload 
error:error];
 }
 
 - (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse 
*)response {
@@ -265,27 +257,9 @@
     BDSKWebDownload *webDownload = [self webDownloadForDownload:download];
     
     if (error) {
-        [webDownload setStatus:BDSKDownloadStatusFailed];
-        
-        if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFailedDownloadsKey] && webDownload)
-            [downloads removeObject:webDownload];
-        [self notifyUpdate];
-        
-        if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] 
== NSURLErrorCancelled)
-            return;
-        
-        NSString *errorDescription = [error localizedDescription] ?: 
NSLocalizedString(@"An error occured during download.", @"Informative text in 
alert dialog");
-        NSAlert *alert = [[NSAlert alloc] init];
-        [alert setMessageText:NSLocalizedString(@"Download Failed", @"Message 
in alert dialog when download failed")];
-        [alert setInformativeText:errorDescription];
-        [alert runModal];
+        [self setStatus:BDSKDownloadStatusFailed forDownload:webDownload 
error:error];
     } else {
-        [webDownload setStatus:BDSKDownloadStatusFinished];
-        
-        if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFinishedDownloadsKey] && webDownload)
-            [downloads removeObject:webDownload];
-        [self notifyUpdate];
-
+        [self setStatus:BDSKDownloadStatusFinished forDownload:webDownload 
error:nil];
     }
 }
 
@@ -317,20 +291,7 @@
 
 - (void)download:(WKDownload *)download didFailWithError:(NSError *)error 
resumeData:(NSData *)resumeData API_AVAILABLE(macos(11.3)) {
     BDSKWebDownload *webDownload = [self webDownloadForDownload:download];
-    [webDownload setStatus:BDSKDownloadStatusFailed];
-    
-    if ([[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKRemoveFailedDownloadsKey] && webDownload)
-        [downloads removeObject:webDownload];
-    [self notifyUpdate];
-    
-    if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == 
NSURLErrorCancelled)
-        return;
-    
-    NSString *errorDescription = [error localizedDescription] ?: 
NSLocalizedString(@"An error occured during download.", @"Informative text in 
alert dialog");
-    NSAlert *alert = [[NSAlert alloc] init];
-    [alert setMessageText:NSLocalizedString(@"Download Failed", @"Message in 
alert dialog when download failed")];
-    [alert setInformativeText:errorDescription];
-    [alert runModal];
+    [self setStatus:BDSKDownloadStatusFailed forDownload:webDownload 
error:error];
 }
 
 - (void)download:(WKDownload *)download 
decideDestinationUsingResponse:(NSURLResponse *)response 
suggestedFilename:(NSString *)filename completionHandler:(void (^)(NSURL * 
destination))completionHandler API_AVAILABLE(macos(11.3)) {

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



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to