From 17c51da8af7dd89f31c4554ac3bf832405b87e65 Mon Sep 17 00:00:00 2001 From: Marcus Osobase Date: Tue, 3 May 2016 15:56:09 -0400 Subject: [PATCH] Allow delegate to decide how to proceed with new update --- Sparkle/SUUIBasedUpdateDriver.m | 94 +++++++++++++++++---------------- Sparkle/SUUpdater.h | 26 +++++++++ 2 files changed, 74 insertions(+), 46 deletions(-) diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index a75aace9..078103eb 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -59,41 +59,39 @@ - (void)didFindValidUpdate { id updater = self.updater; - if ([[updater delegate] respondsToSelector:@selector(updater:didFindValidUpdate:)]) { - [[updater delegate] updater:self.updater didFindValidUpdate:self.updateItem]; + if ([[updater delegate] respondsToSelector:@selector(updater:didFindValidUpdate:respondWithChoice:)]) { + [[updater delegate] updater:self.updater didFindValidUpdate:self.updateItem respondWithChoice:^(SUDUpdateAlertChoice choice) { + [self updateAlertFinishedWithChoice:(SUUpdateAlertChoice)choice]; + }]; + + return; + } + else if ([[updater delegate] respondsToSelector:@selector(updater:didFindValidUpdate:)]) { + [[updater delegate] updater:self.updater didFindValidUpdate:self.updateItem]; + } + + self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { + [self updateAlertFinishedWithChoice:choice]; + }]; + + id versDisp = nil; + if ([[updater delegate] respondsToSelector:@selector(versionDisplayerForUpdater:)]) { + versDisp = [[updater delegate] versionDisplayerForUpdater:self.updater]; + } + [self.updateAlert setVersionDisplayer:versDisp]; + + // 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]) { + [[self.updateAlert window] setHidesOnDeactivate:NO]; + [NSApp activateIgnoringOtherApps:YES]; } - if (self.automaticallyInstallUpdates) { - [self updateAlertFinishedWithChoice:SUInstallUpdateChoice]; - return; - } - -// self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { -// [self updateAlertFinishedWithChoice:choice]; -// }]; -// -// id versDisp = nil; -// if ([[updater delegate] respondsToSelector:@selector(versionDisplayerForUpdater:)]) { -// versDisp = [[updater delegate] versionDisplayerForUpdater:self.updater]; -// } -// [self.updateAlert setVersionDisplayer:versDisp]; -// -// // 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 ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { -// [[self.updateAlert window] setHidesOnDeactivate:NO]; -// [NSApp activateIgnoringOtherApps:YES]; -// } -// -// // Only show the update alert if the app is active; otherwise, we'll wait until it is. -// if ([NSApp isActive]) { -// NSWindow *window = [self.updateAlert window]; -// if ([self shouldDisableKeyboardShortcutForInstallButton]) { -// [self.updateAlert disableKeyboardShortcutForInstallButton]; -// } -// [window makeKeyAndOrderFront:self]; -// } else + // Only show the update alert if the app is active; otherwise, we'll wait until it is. +// if ([NSApp isActive]) +// [[self.updateAlert window] makeKeyAndOrderFront:self]; +// else // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; } @@ -135,7 +133,7 @@ break; case SUOpenInfoURLChoice: - [[NSWorkspace sharedWorkspace] openURL:[self.updateItem infoURL]]; + [[NSWorkspace sharedWorkspace] openURL:[self.updateItem releaseNotesURL]]; [self abortUpdate]; break; @@ -152,18 +150,22 @@ - (void)downloadUpdate { - BOOL createdStatusController = NO; - if (self.statusController == nil) { - self.statusController = [[SUStatusController alloc] initWithHost:self.host]; - createdStatusController = YES; - } - - [self.statusController beginActionWithTitle:SULocalizedString(@"Downloading update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; - [self.statusController setButtonTitle:SULocalizedString(@"Cancel", nil) target:self action:@selector(cancelDownload:) isDefault:NO]; - [self.statusController setButtonEnabled:YES]; - - if (createdStatusController) { - [self.statusController showWindow:self]; + id updater = self.updater; + if(![[updater delegate] suppressUI]) + { + BOOL createdStatusController = NO; + if (self.statusController == nil) { + self.statusController = [[SUStatusController alloc] initWithHost:self.host]; + createdStatusController = YES; + } + + [self.statusController beginActionWithTitle:SULocalizedString(@"Downloading update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; + [self.statusController setButtonTitle:SULocalizedString(@"Cancel", nil) target:self action:@selector(cancelDownload:) isDefault:NO]; + [self.statusController setButtonEnabled:YES]; + + if (createdStatusController) { + [self.statusController showWindow:self]; + } } [super downloadUpdate]; diff --git a/Sparkle/SUUpdater.h b/Sparkle/SUUpdater.h index a4744755..623c57d2 100644 --- a/Sparkle/SUUpdater.h +++ b/Sparkle/SUUpdater.h @@ -18,6 +18,13 @@ #import "SUVersionComparisonProtocol.h" #import "SUVersionDisplayProtocol.h" +typedef NS_ENUM(NSInteger, SUDUpdateAlertChoice) { + SUDInstallUpdateChoice, + SUDRemindMeLaterChoice, + SUDSkipThisVersionChoice, + SUDOpenInfoURLChoice +}; + @class SUAppcastItem, SUAppcast; @protocol SUUpdaterDelegate; @@ -53,6 +60,9 @@ SU_EXPORT @interface SUUpdater : NSObject */ - (instancetype)initForBundle:(NSBundle *)bundle; + +@property (nonatomic) BOOL suppressUI; + /*! Explicitly checks for updates and displays a progress dialog while doing so. @@ -117,7 +127,23 @@ SU_EXPORT @interface SUUpdater : NSObject /*! Begins a "probing" check for updates which will not actually offer to update to that version. + + Called when a valid update is found by the update driver and provides delegate ability to respond. + + \param updater The SUUpdater instance. + \param item The appcast item corresponding to the update that is proposed to be installed. + \param responseBlock The block carrying the users response to update. + */ +- (void)updater:(SUUpdater *)updater didFindValidUpdate:(SUAppcastItem *)item respondWithChoice:(void (^)(SUDUpdateAlertChoice))responseBlock; +/*! + Called when a valid update is not found. + + \param updater The SUUpdater instance. + */ +- (void)updaterDidNotFindUpdate:(SUUpdater *)updater; + +/*! However, the delegate methods SUUpdaterDelegate::updater:didFindValidUpdate: and SUUpdaterDelegate::updaterDidNotFindUpdate: will be called,