From b515c15f9b444f090cd0b542bb2480e486023a5e Mon Sep 17 00:00:00 2001 From: Andy Matuschak Date: Tue, 4 Oct 2011 20:39:09 -0700 Subject: [PATCH] We have this check box that says "Automatically download and install updates in the future." But we only download them automatically. We still ask permission again before installing them. With this patch, we make good on the description of that box: updates are automatically installed when the app is quit. No user intervention involved whatsoever if they checked that box. If the user doesn't quit the app for a day after the update is downloaded, we'll show the dialog we've always used for automatically downloaded updates (Install Now / Install on Quit / Skip This Update). --- SUAutomaticUpdateDriver.h | 3 ++- SUAutomaticUpdateDriver.m | 44 ++++++++++++++++++++++++++++++++++----- SUBasicUpdateDriver.m | 2 -- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/SUAutomaticUpdateDriver.h b/SUAutomaticUpdateDriver.h index 266f6b69..c3d811ff 100644 --- a/SUAutomaticUpdateDriver.h +++ b/SUAutomaticUpdateDriver.h @@ -16,8 +16,9 @@ @interface SUAutomaticUpdateDriver : SUBasicUpdateDriver { @private - BOOL postponingInstallation, showErrors; + BOOL showErrors; SUAutomaticUpdateAlert *alert; + NSTimer *showUpdateAlertTimer; } @end diff --git a/SUAutomaticUpdateDriver.m b/SUAutomaticUpdateDriver.m index 19a3c418..7bd9d05b 100644 --- a/SUAutomaticUpdateDriver.m +++ b/SUAutomaticUpdateDriver.m @@ -12,9 +12,12 @@ #import "SUHost.h" #import "SUConstants.h" +// If the user hasn't quit in a day, ask them if they want to relaunch to get the latest bits. It doesn't matter that this measure of "one day" is imprecise. +static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 24; + @implementation SUAutomaticUpdateDriver -- (void)unarchiverDidFinish:(SUUnarchiver *)ua +- (void)showUpdateAlert { alert = [[SUAutomaticUpdateAlert alloc] initWithAppcastItem:updateItem host:host delegate:self]; @@ -33,6 +36,39 @@ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp]; } +- (void)unarchiverDidFinish:(SUUnarchiver *)ua +{ + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; + + showUpdateAlertTimer = [[NSTimer scheduledTimerWithTimeInterval:SUAutomaticUpdatePromptImpatienceTimer target:self selector:@selector(showUpdateAlert) userInfo:nil repeats:NO] retain]; +} + +- (void)stopUpdatingOnTermination +{ + [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; +} + +- (void)invalidateShowUpdateAlertTimer +{ + [showUpdateAlertTimer invalidate]; + [showUpdateAlertTimer release]; + showUpdateAlertTimer = nil; +} + +- (void)dealloc +{ + [self invalidateShowUpdateAlertTimer]; + [alert release]; + [super dealloc]; +} + +- (void)abortUpdate +{ + [self stopUpdatingOnTermination]; + [self invalidateShowUpdateAlertTimer]; + [super abortUpdate]; +} + - (void)applicationDidBecomeActive:(NSNotification *)aNotification { [[alert window] makeKeyAndOrderFront:self]; @@ -44,12 +80,12 @@ switch (choice) { case SUInstallNowChoice: + [self stopUpdatingOnTermination]; [self installWithToolAndRelaunch:YES]; break; case SUInstallLaterChoice: - postponingInstallation = YES; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; + // No-op: we're already waiting on quit. break; case SUDoNotInstallChoice: @@ -59,8 +95,6 @@ } } -- (BOOL)shouldInstallSynchronously { return postponingInstallation; } - - (void)installWithToolAndRelaunch:(BOOL)relaunch { showErrors = YES; diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 158d271b..637aba78 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -252,8 +252,6 @@ [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:[NSDictionary dictionaryWithObject:SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil) forKey:NSLocalizedDescriptionKey]]]; } -- (BOOL)shouldInstallSynchronously { return NO; } - - (void)installWithToolAndRelaunch:(BOOL)relaunch { if (![updater mayUpdateAndRestart])