From 9f01fd303cb0a88ba99e927cb981efebd3ede898 Mon Sep 17 00:00:00 2001 From: George Nachman Date: Thu, 21 Feb 2019 22:19:31 -0800 Subject: [PATCH 1/2] Add delegate methods to suppress update alert. Some applications may prefer to show their own unobtrusive notification that an update is available when using the scheduled update driver. This commit adds new delegate methods to enable that behavior. The delegate may suppress the alert; if the alert is shown, the delegate is informed when the user is not interested in a particular update so they can remove their own notification. --- Sparkle/SUScheduledUpdateDriver.m | 11 +++++++++++ Sparkle/SUUIBasedUpdateDriver.h | 1 + Sparkle/SUUIBasedUpdateDriver.m | 32 +++++++++++++++++++++++++++---- Sparkle/SUUpdaterDelegate.h | 17 ++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Sparkle/SUScheduledUpdateDriver.m b/Sparkle/SUScheduledUpdateDriver.m index a7a840db..c11e3ef6 100644 --- a/Sparkle/SUScheduledUpdateDriver.m +++ b/Sparkle/SUScheduledUpdateDriver.m @@ -47,4 +47,15 @@ return YES; } +- (BOOL)shouldShowUpdateAlert { + id updater = self.updater; + id updaterDelegate = [updater delegate]; + + if ([updaterDelegate respondsToSelector:@selector(updaterShouldShowUpdateAlertForScheduledUpdate:)]) { + return [updaterDelegate updaterShouldShowUpdateAlertForScheduledUpdate:self.updater]; + } + + return [super shouldShowUpdateAlert]; +} + @end diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index 0c9f3914..ee80a9b2 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -20,6 +20,7 @@ - (void)showAlert:(NSAlert *)alert; - (IBAction)cancelDownload:(id)sender; - (void)installAndRestart:(id)sender; +- (BOOL)shouldShowUpdateAlert; @end diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 6b0092bc..687a741d 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -56,6 +56,10 @@ return self; } +- (BOOL)shouldShowUpdateAlert { + return YES; +} + - (void)didFindValidUpdate { id updater = self.updater; @@ -64,12 +68,18 @@ } if (self.automaticallyInstallUpdates) { - [self updateAlertFinishedWithChoice:SUInstallUpdateChoice]; + [self updateAlertFinishedWithChoice:SUInstallUpdateChoice forItem:nil]; return; } - self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { - [self updateAlertFinishedWithChoice:choice]; + if (![self shouldShowUpdateAlert]) { + [self abortUpdate]; + return; + } + + SUAppcastItem *updateItem = self.updateItem; + self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { + [self updateAlertFinishedWithChoice:choice forItem:updateItem]; }]; id versDisp = nil; @@ -125,26 +135,40 @@ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidBecomeActiveNotification object:NSApp]; } -- (void)updateAlertFinishedWithChoice:(SUUpdateAlertChoice)choice +- (void)didDismissAlertPermanently:(BOOL)permanently forItem:(SUAppcastItem *)item { + if (item == nil) { + return; + } + id updater = self.updater; + if ([updater.delegate respondsToSelector:@selector(updater:didDismissUpdateAlertPermanently:forItem:)]) { + [updater.delegate updater:self.updater didDismissUpdateAlertPermanently:permanently forItem:item]; + } +} + +- (void)updateAlertFinishedWithChoice:(SUUpdateAlertChoice)choice forItem:(SUAppcastItem *)item { self.updateAlert = nil; [self.host setObject:nil forUserDefaultsKey:SUSkippedVersionKey]; switch (choice) { case SUInstallUpdateChoice: + [self didDismissAlertPermanently:NO forItem:item]; [self downloadUpdate]; break; case SUOpenInfoURLChoice: + [self didDismissAlertPermanently:NO forItem:item]; [[NSWorkspace sharedWorkspace] openURL:[self.updateItem infoURL]]; [self abortUpdate]; break; case SUSkipThisVersionChoice: + [self didDismissAlertPermanently:YES forItem:item]; [self.host setObject:[self.updateItem versionString] forUserDefaultsKey:SUSkippedVersionKey]; [self abortUpdate]; break; case SURemindMeLaterChoice: + [self didDismissAlertPermanently:NO forItem:item]; [self abortUpdate]; break; } diff --git a/Sparkle/SUUpdaterDelegate.h b/Sparkle/SUUpdaterDelegate.h index 86d1eb9e..0ff73b6f 100644 --- a/Sparkle/SUUpdaterDelegate.h +++ b/Sparkle/SUUpdaterDelegate.h @@ -117,6 +117,23 @@ SU_EXPORT extern NSString *const SUUpdaterAppcastNotificationKey; */ - (void)updater:(SUUpdater *)updater didFindValidUpdate:(SUAppcastItem *)item; +/*! + Called just before the scheduled update driver prompts the user to install an update. + + \param updater The SUUpdater instance. + + \return YES to allow the update prompt to be shown (the default behavior), or NO to suppress it. + */ +- (BOOL)updaterShouldShowUpdateAlertForScheduledUpdate:(SUUpdater *)updater; + +/*! + Called after the user dismisses the update alert. + + \param updater The SUUpdater instance. + \param permanently YES if the alert will not appear again for this update; NO if it may reappear. + */ +- (void)updater:(SUUpdater *)updater didDismissUpdateAlertPermanently:(BOOL)permanently forItem:(SUAppcastItem *)item; + /*! Called when a valid update is not found. From 9b6346516e63461a5cd57ecc656a240f368e00c4 Mon Sep 17 00:00:00 2001 From: George Nachman Date: Sat, 9 Mar 2019 14:58:08 -0800 Subject: [PATCH 2/2] Pass item to updaterShouldShowUpdateAlertForScheduledUpdate delegate method --- Sparkle/SUScheduledUpdateDriver.m | 8 ++++---- Sparkle/SUUIBasedUpdateDriver.h | 2 +- Sparkle/SUUIBasedUpdateDriver.m | 6 +++--- Sparkle/SUUpdaterDelegate.h | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Sparkle/SUScheduledUpdateDriver.m b/Sparkle/SUScheduledUpdateDriver.m index c11e3ef6..01e094bf 100644 --- a/Sparkle/SUScheduledUpdateDriver.m +++ b/Sparkle/SUScheduledUpdateDriver.m @@ -47,15 +47,15 @@ return YES; } -- (BOOL)shouldShowUpdateAlert { +- (BOOL)shouldShowUpdateAlertForItem:(SUAppcastItem *)item { id updater = self.updater; id updaterDelegate = [updater delegate]; - if ([updaterDelegate respondsToSelector:@selector(updaterShouldShowUpdateAlertForScheduledUpdate:)]) { - return [updaterDelegate updaterShouldShowUpdateAlertForScheduledUpdate:self.updater]; + if ([updaterDelegate respondsToSelector:@selector(updaterShouldShowUpdateAlertForScheduledUpdate:forItem:)]) { + return [updaterDelegate updaterShouldShowUpdateAlertForScheduledUpdate:self.updater forItem:item]; } - return [super shouldShowUpdateAlert]; + return [super shouldShowUpdateAlertForItem:item]; } @end diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index ee80a9b2..1b91132b 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -20,7 +20,7 @@ - (void)showAlert:(NSAlert *)alert; - (IBAction)cancelDownload:(id)sender; - (void)installAndRestart:(id)sender; -- (BOOL)shouldShowUpdateAlert; +- (BOOL)shouldShowUpdateAlertForItem:(SUAppcastItem *)item; @end diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 687a741d..55acd66a 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -56,7 +56,7 @@ return self; } -- (BOOL)shouldShowUpdateAlert { +- (BOOL)shouldShowUpdateAlertForItem:(SUAppcastItem *)__unused item { return YES; } @@ -72,12 +72,12 @@ return; } - if (![self shouldShowUpdateAlert]) { + SUAppcastItem *updateItem = self.updateItem; + if (![self shouldShowUpdateAlertForItem:updateItem]) { [self abortUpdate]; return; } - SUAppcastItem *updateItem = self.updateItem; self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { [self updateAlertFinishedWithChoice:choice forItem:updateItem]; }]; diff --git a/Sparkle/SUUpdaterDelegate.h b/Sparkle/SUUpdaterDelegate.h index 0ff73b6f..a09b0777 100644 --- a/Sparkle/SUUpdaterDelegate.h +++ b/Sparkle/SUUpdaterDelegate.h @@ -124,7 +124,7 @@ SU_EXPORT extern NSString *const SUUpdaterAppcastNotificationKey; \return YES to allow the update prompt to be shown (the default behavior), or NO to suppress it. */ -- (BOOL)updaterShouldShowUpdateAlertForScheduledUpdate:(SUUpdater *)updater; +- (BOOL)updaterShouldShowUpdateAlertForScheduledUpdate:(SUUpdater *)updater forItem:(SUAppcastItem *)item; /*! Called after the user dismisses the update alert.