From a92c23bb267a039c74d8a037df9478a3cc614c4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ian=20Langworth=20=E2=98=A0?= Date: Fri, 18 Sep 2015 15:25:24 -0700 Subject: [PATCH 1/3] Add automatically install updates option --- Sparkle/SUUIBasedUpdateDriver.h | 2 ++ Sparkle/SUUIBasedUpdateDriver.m | 38 +++++++++++++++++++++++++++------ Sparkle/SUUpdater.h | 17 +++++++++++++++ Sparkle/SUUpdater.m | 11 ++++++++++ 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index 65f37443..cf55f3b3 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -17,6 +17,8 @@ @interface SUUIBasedUpdateDriver : SUBasicUpdateDriver +@property (nonatomic, assign) BOOL automaticallyInstallUpdates; + - (void)showModalAlert:(NSAlert *)alert; - (IBAction)cancelDownload:(id)sender; - (void)installAndRestart:(id)sender; diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 4e7f7ec8..7dac7751 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -41,8 +41,21 @@ @synthesize statusController; @synthesize updateAlert; +- (instancetype)initWithUpdater:(SUUpdater *)anUpdater +{ + if ((self = [super initWithUpdater:anUpdater])) { + self.automaticallyInstallUpdates = NO; + } + return self; +} + - (void)didFindValidUpdate { + if (self.automaticallyInstallUpdates) { + [self updateAlertFinishedWithChoice:SUInstallUpdateChoice]; + return; + } + self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { [self updateAlertFinishedWithChoice:choice]; }]; @@ -78,12 +91,14 @@ [[self.updater delegate] updaterDidNotFindUpdate:self.updater]; [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterDidNotFindUpdateNotification object:self.updater]; - NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = SULocalizedString(@"You're up-to-date!", "Status message shown when the user checks for updates but is already current or the feed doesn't contain any updates."); - alert.informativeText = [NSString stringWithFormat:SULocalizedString(@"%@ %@ is currently the newest version available.", nil), [self.host name], [self.host displayVersion]]; - [alert addButtonWithTitle:SULocalizedString(@"OK", nil)]; - [self showModalAlert:alert]; - [self abortUpdate]; + if (!self.automaticallyInstallUpdates) { + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = SULocalizedString(@"You're up-to-date!", "Status message shown when the user checks for updates but is already current or the feed doesn't contain any updates."); + alert.informativeText = [NSString stringWithFormat:SULocalizedString(@"%@ %@ is currently the newest version available.", nil), [self.host name], [self.host displayVersion]]; + [alert addButtonWithTitle:SULocalizedString(@"OK", nil)]; + [self showModalAlert:alert]; + [self abortUpdate]; + } } - (void)applicationDidBecomeActive:(NSNotification *)__unused aNotification @@ -163,8 +178,12 @@ - (IBAction)cancelDownload:(id)__unused sender { - if (self.download) + if (self.download) { [self.download cancel]; + if ([[self.updater delegate] respondsToSelector:@selector(userDidCancelDownload:)]) { + [[self.updater delegate] userDidCancelDownload:self.updater]; + } + } [self abortUpdate]; } @@ -187,6 +206,11 @@ - (void)unarchiverDidFinish:(SUUnarchiver *)__unused ua { + if (self.automaticallyInstallUpdates) { + [self installWithToolAndRelaunch:YES]; + return; + } + [self.statusController beginActionWithTitle:SULocalizedString(@"Ready to Install", nil) maxProgressValue:1.0 statusText:nil]; [self.statusController setProgressValue:1.0]; // Fill the bar. [self.statusController setButtonEnabled:YES]; diff --git a/Sparkle/SUUpdater.h b/Sparkle/SUUpdater.h index ce07a142..ed0072af 100644 --- a/Sparkle/SUUpdater.h +++ b/Sparkle/SUUpdater.h @@ -77,6 +77,16 @@ SU_EXPORT @interface SUUpdater : NSObject */ - (void)checkForUpdatesInBackground; +/*! + Checks for updates and, if available, immediately downloads and installs them. + A progress dialog is shown but the user will never be prompted to read the + release notes. + + You may want to respond to the userDidCancelDownload delegate method in case + the user clicks the "Cancel" button while the update is downloading. + */ +- (void)installUpdatesIfAvailable; + /*! Returns the date of last update check. @@ -228,6 +238,13 @@ SU_EXPORT extern NSString *const SUUpdaterAppcastNotificationKey; */ - (void)updater:(SUUpdater *)updater failedToDownloadUpdate:(SUAppcastItem *)item error:(NSError *)error; +/*! + Called when the user clicks the cancel button while and update is being downloaded. + + \param updater The SUUpdater instance. + */ +- (void)userDidCancelDownload:(SUUpdater *)updater; + /*! Called immediately before installing the specified update. diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 7a824f3c..2e0390b3 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -333,6 +333,17 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults [self checkForUpdatesWithDriver:[[SUProbingUpdateDriver alloc] initWithUpdater:self]]; } +- (void)installUpdatesIfAvailable +{ + if (self.driver && [self.driver isInterruptible]) { + [self.driver abortUpdate]; + } + + SUUIBasedUpdateDriver *theUpdateDriver = [[SUUserInitiatedUpdateDriver alloc] initWithUpdater:self]; + theUpdateDriver.automaticallyInstallUpdates = YES; + [self checkForUpdatesWithDriver:theUpdateDriver]; +} + - (void)checkForUpdatesWithDriver:(SUUpdateDriver *)d { if ([self updateInProgress]) { return; } From 7ccf706a1dc1bed042354182a4f40105525ca4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Thu, 8 Oct 2015 23:24:45 +0100 Subject: [PATCH 2/3] Synthesize automaticallyInstallUpdates --- Sparkle/SUUIBasedUpdateDriver.h | 2 -- Sparkle/SUUpdateDriver.h | 1 + Sparkle/SUUpdateDriver.m | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index cf55f3b3..65f37443 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -17,8 +17,6 @@ @interface SUUIBasedUpdateDriver : SUBasicUpdateDriver -@property (nonatomic, assign) BOOL automaticallyInstallUpdates; - - (void)showModalAlert:(NSAlert *)alert; - (IBAction)cancelDownload:(id)sender; - (void)installAndRestart:(id)sender; diff --git a/Sparkle/SUUpdateDriver.h b/Sparkle/SUUpdateDriver.h index 5575994d..12f93558 100644 --- a/Sparkle/SUUpdateDriver.h +++ b/Sparkle/SUUpdateDriver.h @@ -24,6 +24,7 @@ extern NSString *const SUUpdateDriverFinishedNotification; - (void)abortUpdate; @property (getter=isInterruptible, readonly) BOOL interruptible; @property (readonly) BOOL finished; +@property BOOL automaticallyInstallUpdates; @end diff --git a/Sparkle/SUUpdateDriver.m b/Sparkle/SUUpdateDriver.m index cee31648..6de580ca 100644 --- a/Sparkle/SUUpdateDriver.m +++ b/Sparkle/SUUpdateDriver.m @@ -26,6 +26,7 @@ NSString *const SUUpdateDriverFinishedNotification = @"SUUpdateDriverFinished"; @synthesize interruptible; @synthesize finished; @synthesize appcastURL; +@synthesize automaticallyInstallUpdates; - (instancetype)initWithUpdater:(SUUpdater *)anUpdater { From d1a62bba89fc9d0567d10f827e8c0ce66c200696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ian=20Langworth=20=E2=98=A0?= Date: Thu, 8 Oct 2015 15:33:00 -0700 Subject: [PATCH 3/3] Fire didFindValidUpdate even if automatically installing updates --- Sparkle/SUUIBasedUpdateDriver.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 7dac7751..11bbc044 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -51,6 +51,10 @@ - (void)didFindValidUpdate { + if ([[self.updater delegate] respondsToSelector:@selector(updater:didFindValidUpdate:)]) { + [[self.updater delegate] updater:self.updater didFindValidUpdate:self.updateItem]; + } + if (self.automaticallyInstallUpdates) { [self updateAlertFinishedWithChoice:SUInstallUpdateChoice]; return; @@ -66,10 +70,6 @@ } [self.updateAlert setVersionDisplayer:versDisp]; - if ([[self.updater delegate] respondsToSelector:@selector(updater:didFindValidUpdate:)]) { - [[self.updater delegate] updater:self.updater didFindValidUpdate:self.updateItem]; - } - // 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.