Revision: 23945
          http://sourceforge.net/p/bibdesk/svn/23945
Author:   hofman
Date:     2019-07-05 11:31:36 +0000 (Fri, 05 Jul 2019)
Log Message:
-----------
use notifications rather than delegate for item download updates

Modified Paths:
--------------
    trunk/bibdesk/BDSKEditor.h
    trunk/bibdesk/BDSKEditor.m
    trunk/bibdesk/BDSKStringConstants.h
    trunk/bibdesk/BDSKStringConstants.m
    trunk/bibdesk/BibItem.h
    trunk/bibdesk/BibItem.m

Modified: trunk/bibdesk/BDSKEditor.h
===================================================================
--- trunk/bibdesk/BDSKEditor.h  2019-07-05 09:38:50 UTC (rev 23944)
+++ trunk/bibdesk/BDSKEditor.h  2019-07-05 11:31:36 UTC (rev 23945)
@@ -57,7 +57,7 @@
     @abstract WindowController for the edit window
     @discussion Subclass of the NSWindowController class, This handles making, 
reversing and keeping track of changes to the BibItem, and displaying a nice 
GUI.
 */
-@interface BDSKEditor : NSWindowController <NSWindowDelegate, 
NSTableViewDelegate, NSTableViewDataSource, NSSplitViewDelegate, 
NSControlTextEditingDelegate, BDSKStatusBarDelegate, 
BDSKComplexStringFormatterDelegate, BDSKCitationFormatterDelegate, 
FVFileViewDelegate, FVFileViewDataSource, BDSKItemProgressDelegate, 
QLPreviewPanelDelegate, QLPreviewPanelDataSource, NSTouchBarDelegate> {
+@interface BDSKEditor : NSWindowController <NSWindowDelegate, 
NSTableViewDelegate, NSTableViewDataSource, NSSplitViewDelegate, 
NSControlTextEditingDelegate, BDSKStatusBarDelegate, 
BDSKComplexStringFormatterDelegate, BDSKCitationFormatterDelegate, 
FVFileViewDelegate, FVFileViewDataSource, QLPreviewPanelDelegate, 
QLPreviewPanelDataSource, NSTouchBarDelegate> {
        IBOutlet NSSplitView *mainSplitView;
        IBOutlet NSSplitView *fileSplitView;
     IBOutlet NSPopUpButton *bibTypeButton;

Modified: trunk/bibdesk/BDSKEditor.m
===================================================================
--- trunk/bibdesk/BDSKEditor.m  2019-07-05 09:38:50 UTC (rev 23944)
+++ trunk/bibdesk/BDSKEditor.m  2019-07-05 11:31:36 UTC (rev 23945)
@@ -88,6 +88,7 @@
 #import "NSTextView_BDSKExtensions.h"
 #import "NSError_BDSKExtensions.h"
 #import "NSPasteboard_BDSKExtensions.h"
+#import "BDSKItemDownload.h"
 
 #define WEAK_NULL NULL
 
@@ -285,7 +286,21 @@
     [fileView setEditable:editorFlags.isEditable];
     [fileView setAllowsDownloading:editorFlags.isEditable];
     
-    [publication setProgressDelegate:self];
+    for (BDSKItemDownload *download in [publication downloads]) {
+        CGFloat progress = [download progress];
+        if ([download index] == NSNotFound) {
+            [statusBar 
setProgressIndicatorStyle:BDSKProgressIndicatorSpinningStyle];
+            if (progress < 0.0) {
+                [[statusBar progressIndicator] setIndeterminate:YES];
+            } else {
+                [[statusBar progressIndicator] setIndeterminate:NO];
+                [[statusBar progressIndicator] setDoubleValue:progress];
+            }
+            [statusBar startAnimation:nil];
+        } else {
+            [fileView updateProgressIndicator:[download progress] 
forURL:[download URL] atIndex:[download index]];
+        }
+    }
 }
 
 - (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName{
@@ -1815,34 +1830,6 @@
     }
 }
 
-#pragma mark BDSKItemProgressDelegate
-
-- (void)item:(BibItem *)item updateProgress:(CGFloat)progress forURL:(NSURL 
*)url atIndex:(NSUInteger)index {
-    if (index != NSNotFound) {
-        [fileView updateProgressIndicator:progress forURL:url atIndex:index];
-    } else {
-        if ([statusBar progressIndicatorStyle] != 
BDSKProgressIndicatorSpinningStyle) {
-            [statusBar 
setProgressIndicatorStyle:BDSKProgressIndicatorSpinningStyle];
-            [statusBar startAnimation:nil];
-        }
-        if (progress < 0.0) {
-            [[statusBar progressIndicator] setIndeterminate:YES];
-        } else {
-            [[statusBar progressIndicator] setIndeterminate:NO];
-            [[statusBar progressIndicator] setDoubleValue:progress];
-        }
-    }
-}
-
-- (void)item:(BibItem *)item removeProgressForURL:(NSURL *)url 
atIndex:(NSUInteger)index {
-    if (index != NSNotFound) {
-        [fileView removeProgressIndicatorForURL:url atIndex:index];
-    } else {
-        [statusBar stopAnimation:nil];
-        [statusBar setProgressIndicatorStyle:BDSKProgressIndicatorNone];
-    }
-}
-
 #pragma mark KVO
 
 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context {
@@ -2584,6 +2571,37 @@
     editorFlags.controllingFVPreviewPanel = NO;
 }
 
+- (void)downloadUpdated:(NSNotification *)notification {
+    BDSKItemDownload *download = [[notification userInfo] 
objectForKey:BDSKItemDownloadKey];
+    NSUInteger index = [download index];
+    CGFloat progress = [download progress];
+    if (index != NSNotFound) {
+        [fileView updateProgressIndicator:progress forURL:[download URL] 
atIndex:index];
+    } else {
+        if ([statusBar progressIndicatorStyle] != 
BDSKProgressIndicatorSpinningStyle) {
+            [statusBar 
setProgressIndicatorStyle:BDSKProgressIndicatorSpinningStyle];
+            [statusBar startAnimation:nil];
+        }
+        if (progress < 0.0) {
+            [[statusBar progressIndicator] setIndeterminate:YES];
+        } else {
+            [[statusBar progressIndicator] setIndeterminate:NO];
+            [[statusBar progressIndicator] setDoubleValue:progress];
+        }
+    }
+}
+
+- (void)downloadFinished:(NSNotification *)notification {
+    BDSKItemDownload *download = [[notification userInfo] 
objectForKey:BDSKItemDownloadKey];
+    NSUInteger index = [download index];
+    if (index != NSNotFound) {
+        [fileView removeProgressIndicatorForURL:[download URL] atIndex:index];
+    } else {
+        [statusBar stopAnimation:nil];
+        [statusBar setProgressIndicatorStyle:BDSKProgressIndicatorNone];
+    }
+}
+
 #pragma mark document interaction
        
 - (void)bibWillBeRemoved:(NSNotification *)notification{
@@ -3010,8 +3028,6 @@
     if (editorFlags.isEditing && [self commitEditing] == NO)
         [self discardEditing];
        
-    [publication setProgressDelegate:nil];
-    
     // close so it's not hanging around by itself; this works if the doc 
window closes, also
     [complexStringEditor close];
     
@@ -3681,6 +3697,14 @@
                selector:@selector(fileURLDidChange:)
                    name:BDSKDocumentFileURLDidChangeNotification
                  object:[self document]];
+        [nc addObserver:self
+               selector:@selector(downloadUpdated:)
+                   name:BDSKItemDownloadUpdatedNotification
+                 object:publication];
+        [nc addObserver:self
+               selector:@selector(downloadFinished:)
+                   name:BDSKItemDownloadFinishedNotification
+                 object:publication];
     }else {
         [nc addObserver:self
                selector:@selector(bibWasAddedOrRemoved:)

Modified: trunk/bibdesk/BDSKStringConstants.h
===================================================================
--- trunk/bibdesk/BDSKStringConstants.h 2019-07-05 09:38:50 UTC (rev 23944)
+++ trunk/bibdesk/BDSKStringConstants.h 2019-07-05 11:31:36 UTC (rev 23945)
@@ -296,6 +296,8 @@
 extern NSString *BDSKGroupTableSelectionChangedNotification;
 extern NSString *BDSKBibItemChangedNotification;
 extern NSString *BDSKNeedsToBeFiledChangedNotification;
+extern NSString *BDSKItemDownloadUpdatedNotification;
+extern NSString *BDSKItemDownloadFinishedNotification;
 extern NSString *BDSKDocumentDidAddItemNotification;
 extern NSString *BDSKDocumentWillRemoveItemNotification;
 extern NSString *BDSKDocumentDidRemoveItemNotification;

Modified: trunk/bibdesk/BDSKStringConstants.m
===================================================================
--- trunk/bibdesk/BDSKStringConstants.m 2019-07-05 09:38:50 UTC (rev 23944)
+++ trunk/bibdesk/BDSKStringConstants.m 2019-07-05 11:31:36 UTC (rev 23945)
@@ -306,6 +306,8 @@
 NSString *BDSKGroupTableSelectionChangedNotification = 
@"BDSKGroupTableSelectionChangedNotification";
 NSString *BDSKBibItemChangedNotification = @"BDSKBibItemChangedNotification";
 NSString *BDSKNeedsToBeFiledChangedNotification = 
@"BDSKBibItemNeedsToBeFiledChangedNotification";
+NSString *BDSKItemDownloadUpdatedNotification = 
@"BDSKItemDownloadUpdatedNotification";
+NSString *BDSKItemDownloadFinishedNotification = 
@"BDSKItemDownloadFinishedNotification";
 NSString *BDSKDocumentDidAddItemNotification = 
@"BDSKDocumentDidAddItemNotification";
 NSString *BDSKDocumentWillRemoveItemNotification = 
@"BDSKDocumentWillRemoveItemNotification";
 NSString *BDSKDocumentDidRemoveItemNotification = 
@"BDSKDocumentDidRemoveItemNotification";

Modified: trunk/bibdesk/BibItem.h
===================================================================
--- trunk/bibdesk/BibItem.h     2019-07-05 09:38:50 UTC (rev 23944)
+++ trunk/bibdesk/BibItem.h     2019-07-05 11:31:36 UTC (rev 23945)
@@ -50,6 +50,8 @@
 extern NSString *BDSKBibItemIdentifierURLKey;
 extern NSString *BDSKBibItemURLsKey;
 
+extern NSString *BDSKItemDownloadKey;
+
 extern NSString *BDSKBibItemURLScheme;
 
 enum {
@@ -81,7 +83,6 @@
 
 @class BibDocument, BDSKCategoryGroup, BibAuthor, BDSKFieldCollection, 
BDSKTemplate, BDSKPublicationsArray, BDSKMacroResolver;
 @protocol BDSKParseableItem, BDSKOwner;
-@protocol BDSKItemProgressDelegate;
 
 /*!
 @class BibItem
@@ -114,7 +115,6 @@
     NSMutableArray *files;
     NSUInteger colorLabel;
     NSMutableArray *downloads;
-    id<BDSKItemProgressDelegate> progressDelegate;
 }
 
 + (NSString *)defaultCiteKey;
@@ -139,6 +139,8 @@
 - (NSArray *)existingLocalFiles;
 - (NSArray *)remoteURLs;
 
+- (NSArray *)downloads;
+   
 - (NSArray *)usedMacros;
 - (NSArray *)usedLocalMacros;
 
@@ -819,9 +821,6 @@
 
 - (NSString *)fieldValueForFileURL:(NSURL *)aURL withFormat:(NSInteger)format;
 
-- (id<BDSKItemProgressDelegate>)progressDelegate;
-- (void)setProgressDelegate:(id<BDSKItemProgressDelegate>)delegate;
-
 - (void)downloadURLForField:(NSString *)field;
 - (void)downloadLinkedFile:(BDSKLinkedFile *)linkedFile replace:(BOOL)replace;
 
@@ -862,10 +861,3 @@
 
 extern const CFSetCallBacks kBDSKBibItemEqualitySetCallBacks;
 extern const CFSetCallBacks kBDSKBibItemEquivalenceSetCallBacks;
-
-@protocol BDSKItemProgressDelegate <NSObject>
-
-- (void)item:(BibItem *)item updateProgress:(CGFloat)progress forURL:(NSURL 
*)url atIndex:(NSUInteger)index;
-- (void)item:(BibItem *)item removeProgressForURL:(NSURL *)url 
atIndex:(NSUInteger)index;
-
-@end

Modified: trunk/bibdesk/BibItem.m
===================================================================
--- trunk/bibdesk/BibItem.m     2019-07-05 09:38:50 UTC (rev 23944)
+++ trunk/bibdesk/BibItem.m     2019-07-05 11:31:36 UTC (rev 23945)
@@ -90,6 +90,8 @@
 NSString *BDSKBibItemIdentifierURLKey = @"identifierURL";
 NSString *BDSKBibItemURLsKey = @"urls";
 
+NSString *BDSKItemDownloadKey = @"download";
+
 NSString *BDSKBibItemURLScheme = @"x-bdsk";
 
 #define BDSKFieldsToWriteIfEmptyKey @"BDSKFieldsToWriteIfEmpty"
@@ -2705,7 +2707,8 @@
     BDSKItemDownload *download = [self downloadForLinkedFile:file];
     if (download) {
         [download cancel];
-        [progressDelegate item:self removeProgressForURL:[download URL] 
atIndex:[download index]];
+        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:download 
forKey:BDSKItemDownloadKey];
+        [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKItemDownloadFinishedNotification object:self 
userInfo:userInfo];
         [downloads removeObject:download];
     }
     
@@ -3186,29 +3189,17 @@
 
 #pragma mark Download
 
-- (id<BDSKItemProgressDelegate>)progressDelegate {
-    return progressDelegate;
-}
+- (NSArray *)downloads { return downloads; }
 
-- (void)setProgressDelegate:(id<BDSKItemProgressDelegate>)delegate {
-    BOOL wasNil = progressDelegate == nil;
-    progressDelegate = delegate;
-    if (progressDelegate && wasNil && [downloads count]) {
-        for (BDSKItemDownload *download in downloads) {
-            NSUInteger idx = [download index];
-            NSURL *url = [download URL];
-            [progressDelegate item:self updateProgress:[download progress] 
forURL:url atIndex:idx];
-        }
-    }
-}
-
 - (void)itemDownload:(BDSKItemDownload *)download 
updateWithProgress:(CGFloat)progress {
-    [progressDelegate item:self updateProgress:progress forURL:[download URL] 
atIndex:[download index]];
+    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:download 
forKey:BDSKItemDownloadKey];
+    [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKItemDownloadUpdatedNotification object:self 
userInfo:userInfo];
 }
 
 - (void)itemDownload:(BDSKItemDownload *)download didFinishWithURL:(NSURL 
*)fileURL {
     NSUInteger idx = [download index];
-    [progressDelegate item:self removeProgressForURL:[download URL] 
atIndex:idx];
+    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:download 
forKey:BDSKItemDownloadKey];
+    [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKItemDownloadFinishedNotification object:self 
userInfo:userInfo];
     if (fileURL) {
         BOOL useLocalUrl = [[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKUseLocalUrlAndUrlKey];
         BOOL autoFile = [[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKFilePapersAutomaticallyKey];
@@ -4137,7 +4128,8 @@
         NSUInteger realIdx = [files indexOfObject:linkedFile];
         if (realIdx != NSNotFound && realIdx != idx) {
             [download setIndex:realIdx];
-            [progressDelegate item:self updateProgress:[download progress] 
forURL:[download URL] atIndex:realIdx];
+            NSDictionary *userInfo = [NSDictionary 
dictionaryWithObject:download forKey:BDSKItemDownloadKey];
+            [[NSNotificationCenter defaultCenter] 
postNotificationName:BDSKItemDownloadUpdatedNotification object:self 
userInfo:userInfo];
         }
     }
 }

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