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; }