diff --git a/Sparkle/SUAppcast.h b/Sparkle/SUAppcast.h index 9b4c677b..9e9acd5d 100644 --- a/Sparkle/SUAppcast.h +++ b/Sparkle/SUAppcast.h @@ -12,23 +12,15 @@ #import #import "SUExport.h" -@protocol SUAppcastDelegate; - @class SUAppcastItem; -SU_EXPORT @interface SUAppcast : NSObject +SU_EXPORT @interface SUAppcast : NSObject -@property (weak) id delegate; @property (copy) NSString *userAgentString; @property (copy) NSDictionary *httpHeaders; -- (void)fetchAppcastFromURL:(NSURL *)url; +- (void)fetchAppcastFromURL:(NSURL *)url completionBlock:(void (^)(NSError *))err; @property (readonly, copy) NSArray *items; @end -@protocol SUAppcastDelegate -- (void)appcastDidFinishLoading:(SUAppcast *)appcast; -- (void)appcast:(SUAppcast *)appcast failedToLoadWithError:(NSError *)error; -@end - #endif diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index a075200e..98d2e899 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -33,6 +33,7 @@ @end @interface SUAppcast () +@property (strong) void (^completionBlock)(NSError *); @property (copy) NSString *downloadFilename; @property (strong) NSURLDownload *download; @property (copy) NSArray *items; @@ -43,14 +44,16 @@ @implementation SUAppcast @synthesize downloadFilename; -@synthesize delegate; +@synthesize completionBlock; @synthesize userAgentString; @synthesize httpHeaders; @synthesize download; @synthesize items; -- (void)fetchAppcastFromURL:(NSURL *)url +- (void)fetchAppcastFromURL:(NSURL *)url completionBlock:(void (^)(NSError *))block { + self.completionBlock = block; + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]; if (self.userAgentString) { [request setValue:self.userAgentString forHTTPHeaderField:@"User-Agent"]; @@ -210,27 +213,23 @@ } } - if ([appcastItems count]) - { + if ([appcastItems count]) { NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; [appcastItems sortUsingDescriptors:@[sort]]; self.items = appcastItems; } - if (failed) - { + if (failed) { [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastParseError userInfo:@{ NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while parsing the update feed.", nil) }]]; - } - else if ([self.delegate respondsToSelector:@selector(appcastDidFinishLoading:)]) - { - [self.delegate appcastDidFinishLoading:self]; + } else { + self.completionBlock(nil); + self.completionBlock = nil; } } - (void)download:(NSURLDownload *)__unused aDownload didFailWithError:(NSError *)error { - if (self.downloadFilename) - { + if (self.downloadFilename) { [[NSFileManager defaultManager] removeItemAtPath:self.downloadFilename error:nil]; } self.downloadFilename = nil; @@ -245,10 +244,11 @@ - (void)reportError:(NSError *)error { - if ([self.delegate respondsToSelector:@selector(appcast:failedToLoadWithError:)]) - { - [self.delegate appcast:self failedToLoadWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastError userInfo:@{NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred in retrieving update information. Please try again later.", nil), NSLocalizedFailureReasonErrorKey: [error localizedDescription]}]]; - } + self.completionBlock([NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastError userInfo:@{ + NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred in retrieving update information. Please try again later.", nil), + NSLocalizedFailureReasonErrorKey: [error localizedDescription] + }]); + self.completionBlock = nil; } - (NSXMLNode *)bestNodeInNodes:(NSArray *)nodes diff --git a/Sparkle/SUAutomaticUpdateAlert.h b/Sparkle/SUAutomaticUpdateAlert.h index 0a2c8610..1fcfcfb7 100644 --- a/Sparkle/SUAutomaticUpdateAlert.h +++ b/Sparkle/SUAutomaticUpdateAlert.h @@ -11,8 +11,6 @@ #import "SUWindowController.h" -@protocol SUAutomaticUpdateAlertDelegate; - typedef NS_ENUM(NSInteger, SUAutomaticInstallationChoice) { SUInstallNowChoice, SUInstallLaterChoice, @@ -22,15 +20,11 @@ typedef NS_ENUM(NSInteger, SUAutomaticInstallationChoice) { @class SUAppcastItem, SUHost; @interface SUAutomaticUpdateAlert : SUWindowController -- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)hostBundle delegate:(id)delegate; +- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)hostBundle completionBlock:(void (^)(SUAutomaticInstallationChoice))c; - (IBAction)installNow:sender; - (IBAction)installLater:sender; - (IBAction)doNotInstall:sender; @end -@protocol SUAutomaticUpdateAlertDelegate -- (void)automaticUpdateAlert:(SUAutomaticUpdateAlert *)aua finishedWithChoice:(SUAutomaticInstallationChoice)choice; -@end - #endif diff --git a/Sparkle/SUAutomaticUpdateAlert.m b/Sparkle/SUAutomaticUpdateAlert.m index 1119b27b..4271e608 100644 --- a/Sparkle/SUAutomaticUpdateAlert.m +++ b/Sparkle/SUAutomaticUpdateAlert.m @@ -11,23 +11,22 @@ #import "SUHost.h" @interface SUAutomaticUpdateAlert () +@property (strong) void(^completionBlock)(SUAutomaticInstallationChoice); @property (strong) SUAppcastItem *updateItem; -@property (weak) id delegate; @property (strong) SUHost *host; @end @implementation SUAutomaticUpdateAlert -@synthesize delegate; @synthesize host; @synthesize updateItem; +@synthesize completionBlock; -- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)aHost delegate:(id)del +- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)aHost completionBlock:(void (^)(SUAutomaticInstallationChoice))block { self = [super initWithWindowNibName:@"SUAutomaticUpdateAlert"]; - if (self) - { + if (self) { self.updateItem = item; - self.delegate = del; + self.completionBlock = block; self.host = aHost; [self setShouldCascadeWindows:NO]; [[self window] center]; @@ -40,19 +39,22 @@ - (IBAction)installNow:(id)__unused sender { [self close]; - [self.delegate automaticUpdateAlert:self finishedWithChoice:SUInstallNowChoice]; + self.completionBlock(SUInstallNowChoice); + self.completionBlock = nil; } - (IBAction)installLater:(id)__unused sender { [self close]; - [self.delegate automaticUpdateAlert:self finishedWithChoice:SUInstallLaterChoice]; + self.completionBlock(SUInstallLaterChoice); + self.completionBlock = nil; } - (IBAction)doNotInstall:(id)__unused sender { [self close]; - [self.delegate automaticUpdateAlert:self finishedWithChoice:SUDoNotInstallChoice]; + self.completionBlock(SUDoNotInstallChoice); + self.completionBlock = nil; } - (NSImage *)applicationIcon diff --git a/Sparkle/SUAutomaticUpdateDriver.h b/Sparkle/SUAutomaticUpdateDriver.h index 9935936e..4dd90e8f 100644 --- a/Sparkle/SUAutomaticUpdateDriver.h +++ b/Sparkle/SUAutomaticUpdateDriver.h @@ -13,7 +13,7 @@ #import "SUBasicUpdateDriver.h" #import "SUAutomaticUpdateAlert.h" -@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver +@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver @end diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 51c445c0..45b3d546 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -42,13 +42,14 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 - (void)showUpdateAlert { self.interruptible = NO; - self.alert = [[SUAutomaticUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host delegate:self]; + self.alert = [[SUAutomaticUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUAutomaticInstallationChoice choice) { + [self automaticUpdateAlertFinishedWithChoice:choice]; + }]; // If the app is a menubar app or the like, we need to focus it first and alter the // update prompt to behave like a normal window. Otherwise if the window were hidden // there may be no way for the application to be activated to make it visible again. - if ([self.host isBackgroundApplication]) - { + if ([self.host isBackgroundApplication]) { [[self.alert window] setHidesOnDeactivate:NO]; [NSApp activateIgnoringOtherApps:YES]; } @@ -138,7 +139,7 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidBecomeActiveNotification object:NSApp]; } -- (void)automaticUpdateAlert:(SUAutomaticUpdateAlert *)__unused aua finishedWithChoice:(SUAutomaticInstallationChoice)choice +- (void)automaticUpdateAlertFinishedWithChoice:(SUAutomaticInstallationChoice)choice { switch (choice) { diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 32e320bc..fee63ede 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -15,7 +15,7 @@ #import "SUAppcast.h" @class SUAppcastItem, SUHost; -@interface SUBasicUpdateDriver : SUUpdateDriver +@interface SUBasicUpdateDriver : SUUpdateDriver @property (strong, readonly) SUAppcastItem *updateItem; @property (strong, readonly) NSURLDownload *download; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index dc34f12f..9ae856d6 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -54,10 +54,15 @@ SUAppcast *appcast = [[SUAppcast alloc] init]; - [appcast setDelegate:self]; [appcast setUserAgentString:[self.updater userAgentString]]; [appcast setHttpHeaders:[self.updater httpHeaders]]; - [appcast fetchAppcastFromURL:URL]; + [appcast fetchAppcastFromURL:URL completionBlock:^(NSError *error) { + if (error) { + [self abortUpdateWithError:error]; + } else { + [self appcastDidFinishLoading:appcast]; + } + }]; } - (id)versionComparator @@ -153,11 +158,6 @@ } } -- (void)appcast:(SUAppcast *)__unused ac failedToLoadWithError:(NSError *)error -{ - [self abortUpdateWithError:error]; -} - - (void)didFindValidUpdate { assert(self.updateItem); diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index fb145072..990446d7 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -15,11 +15,12 @@ @class SUStatusController; -@interface SUUIBasedUpdateDriver : SUBasicUpdateDriver +@interface SUUIBasedUpdateDriver : SUBasicUpdateDriver - (void)showModalAlert:(NSAlert *)alert; - (IBAction)cancelDownload:(id)sender; - (void)installAndRestart:(id)sender; +- (void)appcastDidFinishLoading:(SUAppcast *)ac; @end diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index f755227f..7875dadd 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -28,8 +28,9 @@ - (void)didFindValidUpdate { - self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host]; - [self.updateAlert setDelegate:self]; + self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { + [self updateAlertFinishedWithChoice:choice]; + }]; id versDisp = nil; if ([[self.updater delegate] respondsToSelector:@selector(versionDisplayerForUpdater:)]) { @@ -44,8 +45,7 @@ // If the app is a menubar app or the like, we need to focus it first and alter the // update prompt to behave like a normal window. Otherwise if the window were hidden // there may be no way for the application to be activated to make it visible again. - if ([self.host isBackgroundApplication]) - { + if ([self.host isBackgroundApplication]) { [[self.updateAlert window] setHidesOnDeactivate:NO]; [NSApp activateIgnoringOtherApps:YES]; } @@ -78,12 +78,11 @@ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidBecomeActiveNotification object:NSApp]; } -- (void)updateAlert:(SUUpdateAlert *)__unused alert finishedWithChoice:(SUUpdateAlertChoice)choice +- (void)updateAlertFinishedWithChoice:(SUUpdateAlertChoice)choice { self.updateAlert = nil; [self.host setObject:nil forUserDefaultsKey:SUSkippedVersionKey]; - switch (choice) - { + switch (choice) { case SUInstallUpdateChoice: self.statusController = [[SUStatusController alloc] initWithHost:self.host]; [self.statusController beginActionWithTitle:SULocalizedString(@"Downloading update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; diff --git a/Sparkle/SUUpdateAlert.h b/Sparkle/SUUpdateAlert.h index caf9ccba..2db57bf4 100644 --- a/Sparkle/SUUpdateAlert.h +++ b/Sparkle/SUUpdateAlert.h @@ -24,10 +24,9 @@ typedef NS_ENUM(NSInteger, SUUpdateAlertChoice) { @class WebView, SUAppcastItem, SUHost; @interface SUUpdateAlert : SUWindowController -@property (weak) id delegate; @property (weak) id versionDisplayer; -- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)host; +- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)host completionBlock:(void(^)(SUUpdateAlertChoice))c; - (IBAction)installUpdate:sender; - (IBAction)skipThisVersion:sender; @@ -35,10 +34,4 @@ typedef NS_ENUM(NSInteger, SUUpdateAlertChoice) { @end -@protocol SUUpdateAlertDelegate -- (void)updateAlert:(SUUpdateAlert *)updateAlert finishedWithChoice:(SUUpdateAlertChoice)updateChoice; -@optional -- (void)updateAlert:(SUUpdateAlert *)updateAlert shouldAllowAutoUpdate:(BOOL *)shouldAllowAutoUpdate; -@end - #endif diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index 76c69dda..0b361038 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -21,6 +21,7 @@ @property (strong) SUAppcastItem *updateItem; @property (strong) SUHost *host; +@property (strong) void(^completionBlock)(SUUpdateAlertChoice); @property (strong) NSProgressIndicator *releaseNotesSpinner; @property (assign) BOOL webViewFinishedLoading; @@ -36,7 +37,7 @@ @implementation SUUpdateAlert -@synthesize delegate; +@synthesize completionBlock; @synthesize versionDisplayer; @synthesize updateItem; @@ -52,11 +53,12 @@ @synthesize skipButton; @synthesize laterButton; -- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)aHost +- (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)aHost completionBlock:(void (^)(SUUpdateAlertChoice))block { self = [super initWithWindowNibName:@"SUUpdateAlert"]; if (self) { + self.completionBlock = block; host = aHost; updateItem = item; [self setShouldCascadeWindows:NO]; @@ -79,8 +81,8 @@ [self.releaseNotesView setPolicyDelegate:nil]; [self.releaseNotesView removeFromSuperview]; // Otherwise it gets sent Esc presses (why?!) and gets very confused. [self close]; - if ([self.delegate respondsToSelector:@selector(updateAlert:finishedWithChoice:)]) - [self.delegate updateAlert:self finishedWithChoice:choice]; + self.completionBlock(choice); + self.completionBlock = nil; } - (IBAction)installUpdate:(id)__unused sender @@ -164,10 +166,6 @@ if ([self.host objectForInfoDictionaryKey:SUAllowsAutomaticUpdatesKey]) allowAutoUpdates = [self.host boolForInfoDictionaryKey:SUAllowsAutomaticUpdatesKey]; - // Give delegate a chance to modify this choice: - if (self.delegate && [self.delegate respondsToSelector:@selector(updateAlert:shouldAllowAutoUpdate:)]) - [self.delegate updateAlert:self shouldAllowAutoUpdate:&allowAutoUpdates]; - return allowAutoUpdates; } diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index 5dd66f29..d1e0c9a6 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -78,16 +78,6 @@ [super abortUpdate]; } -- (void)appcast:(SUAppcast *)ac failedToLoadWithError:(NSError *)error -{ - if (self.isCanceled) - { - [self abortUpdate]; - return; - } - [super appcast:ac failedToLoadWithError:error]; -} - - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui { // We don't check to see if this update's been skipped, because the user explicitly *asked* if he had the latest version.