diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 4e7f7ec8..11bbc044 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -41,8 +41,25 @@ @synthesize statusController; @synthesize updateAlert; +- (instancetype)initWithUpdater:(SUUpdater *)anUpdater +{ + if ((self = [super initWithUpdater:anUpdater])) { + self.automaticallyInstallUpdates = NO; + } + return self; +} + - (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; + } + self.updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:self.updateItem host:self.host completionBlock:^(SUUpdateAlertChoice choice) { [self updateAlertFinishedWithChoice:choice]; }]; @@ -53,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. @@ -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/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 { 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; }