diff --git a/SUAppcastItem.h b/SUAppcastItem.h index d6f9c64f..3007e38e 100644 --- a/SUAppcastItem.h +++ b/SUAppcastItem.h @@ -29,7 +29,7 @@ NSDictionary *deltaUpdates; NSDictionary *propertiesDictionary; - + NSURL *infoURL; // UK 2007-08-31 } @@ -49,6 +49,7 @@ - (NSString *)maximumSystemVersion; - (NSDictionary *)deltaUpdates; - (BOOL)isDeltaUpdate; +- (BOOL)isCriticalUpdate; // Returns the dictionary provided in initWithDictionary; this might be useful later for extensions. - (NSDictionary *)propertiesDictionary; diff --git a/SUAppcastItem.m b/SUAppcastItem.m index 525d3fef..a330f682 100644 --- a/SUAppcastItem.m +++ b/SUAppcastItem.m @@ -138,6 +138,11 @@ return [[propertiesDictionary objectForKey:@"enclosure"] objectForKey:@"sparkle:deltaFrom"] != nil; } +- (BOOL)isCriticalUpdate +{ + return [[propertiesDictionary objectForKey:@"sparkle:tags"] containsObject:@"sparkle:criticalUpdate"]; +} + - initWithDictionary:(NSDictionary *)dict { return [self initWithDictionary:dict failureReason:nil]; diff --git a/SUAutomaticUpdateAlert.m b/SUAutomaticUpdateAlert.m index f055f48d..65f84bed 100644 --- a/SUAutomaticUpdateAlert.m +++ b/SUAutomaticUpdateAlert.m @@ -60,12 +60,26 @@ - (NSString *)titleText { - return [NSString stringWithFormat:SULocalizedString(@"A new version of %@ is ready to install!", nil), [host name]]; + if ([updateItem isCriticalUpdate]) + { + return [NSString stringWithFormat:SULocalizedString(@"An important update to %@ is ready to install", nil)]; + } + else + { + return [NSString stringWithFormat:SULocalizedString(@"A new version of %@ is ready to install!", nil), [host name]]; + } } - (NSString *)descriptionText { - return [NSString stringWithFormat:SULocalizedString(@"%1$@ %2$@ has been downloaded and is ready to use! Would you like to install it and relaunch %1$@ now?", nil), [host name], [updateItem displayVersionString]]; + if ([updateItem isCriticalUpdate]) + { + return [NSString stringWithFormat:SULocalizedString(@"%1$@ %2$@ has been downloaded and is ready to use! This is an important update; would you like to install it and relaunch %1$@ now?", nil), [host name], [updateItem displayVersionString]]; + } + else + { + return [NSString stringWithFormat:SULocalizedString(@"%1$@ %2$@ has been downloaded and is ready to use! Would you like to install it and relaunch %1$@ now?", nil), [host name], [updateItem displayVersionString]]; + } } @end diff --git a/SUAutomaticUpdateDriver.h b/SUAutomaticUpdateDriver.h index 266f6b69..678f62b7 100644 --- a/SUAutomaticUpdateDriver.h +++ b/SUAutomaticUpdateDriver.h @@ -17,7 +17,9 @@ { @private BOOL postponingInstallation, showErrors; + BOOL willUpdateOnTermination; SUAutomaticUpdateAlert *alert; + NSTimer *showUpdateAlertTimer; } @end diff --git a/SUAutomaticUpdateDriver.m b/SUAutomaticUpdateDriver.m index 19a3c418..60d4192c 100644 --- a/SUAutomaticUpdateDriver.m +++ b/SUAutomaticUpdateDriver.m @@ -12,10 +12,14 @@ #import "SUHost.h" #import "SUConstants.h" +// If the user hasn't quit in a week, 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 * 7; + @implementation SUAutomaticUpdateDriver -- (void)unarchiverDidFinish:(SUUnarchiver *)ua +- (void)showUpdateAlert { + isInterruptible = NO; alert = [[SUAutomaticUpdateAlert alloc] initWithAppcastItem:updateItem host:host delegate:self]; // If the app is a menubar app or the like, we need to focus it first and alter the @@ -33,6 +37,83 @@ [[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]; + + // Sudden termination is available on 10.6+ + NSProcessInfo *processInfo = [NSProcessInfo processInfo]; + if ([processInfo respondsToSelector:@selector(disableSuddenTermination)]) { + [processInfo disableSuddenTermination]; + } + + willUpdateOnTermination = YES; + + if ([[updater delegate] respondsToSelector:@selector(updater:willInstallUpdateOnQuit:immediateInstallationInvocation:)]) + { + BOOL relaunch = YES; + BOOL showUI = NO; + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(installWithToolAndRelaunch:displayingUserInterface:)]]; + [invocation setSelector:@selector(installWithToolAndRelaunch:displayingUserInterface:)]; + [invocation setArgument:&relaunch atIndex:2]; + [invocation setArgument:&showUI atIndex:3]; + [invocation setTarget:self]; + + [[updater delegate] updater:updater willInstallUpdateOnQuit:updateItem immediateInstallationInvocation:invocation]; + } + + // If this is marked as a critical update, we'll prompt the user to install it right away. + if ([updateItem isCriticalUpdate]) + { + [self showUpdateAlert]; + } + else + { + showUpdateAlertTimer = [[NSTimer scheduledTimerWithTimeInterval:SUAutomaticUpdatePromptImpatienceTimer target:self selector:@selector(showUpdateAlert) userInfo:nil repeats:NO] retain]; + + // At this point the driver is idle, allow it to be interrupted for user-initiated update checks. + isInterruptible = YES; + } +} + +- (void)stopUpdatingOnTermination +{ + if (willUpdateOnTermination) + { + [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; + NSProcessInfo *processInfo = [NSProcessInfo processInfo]; + if ([processInfo respondsToSelector:@selector(enableSuddenTermination)]) { + [processInfo enableSuddenTermination]; + } + willUpdateOnTermination = NO; + + if ([[updater delegate] respondsToSelector:@selector(updater:didCancelInstallUpdateOnQuit:)]) + [[updater delegate] updater:updater didCancelInstallUpdateOnQuit:updateItem]; + } +} + +- (void)invalidateShowUpdateAlertTimer +{ + [showUpdateAlertTimer invalidate]; + [showUpdateAlertTimer release]; + showUpdateAlertTimer = nil; +} + +- (void)dealloc +{ + [self stopUpdatingOnTermination]; + [self invalidateShowUpdateAlertTimer]; + [alert release]; + [super dealloc]; +} + +- (void)abortUpdate +{ + [self stopUpdatingOnTermination]; + [self invalidateShowUpdateAlertTimer]; + [super abortUpdate]; +} + - (void)applicationDidBecomeActive:(NSNotification *)aNotification { [[alert window] makeKeyAndOrderFront:self]; @@ -44,12 +125,15 @@ 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]; + // We're already waiting on quit, just indicate that we're idle. + isInterruptible = YES; break; case SUDoNotInstallChoice: @@ -61,10 +145,13 @@ - (BOOL)shouldInstallSynchronously { return postponingInstallation; } -- (void)installWithToolAndRelaunch:(BOOL)relaunch +- (void)installWithToolAndRelaunch:(BOOL)relaunch displayingUserInterface:(BOOL)showUI { - showErrors = YES; - [super installWithToolAndRelaunch:relaunch]; + if (relaunch) + [self stopUpdatingOnTermination]; + + showErrors = YES; + [super installWithToolAndRelaunch:relaunch displayingUserInterface:showUI]; } - (void)applicationWillTerminate:(NSNotification *)note diff --git a/SUBasicUpdateDriver.h b/SUBasicUpdateDriver.h index 3e10e5af..9928cb41 100644 --- a/SUBasicUpdateDriver.h +++ b/SUBasicUpdateDriver.h @@ -47,6 +47,7 @@ - (void)failedToApplyDeltaUpdate; - (void)installWithToolAndRelaunch:(BOOL)relaunch; +- (void)installWithToolAndRelaunch:(BOOL)relaunch displayingUserInterface:(BOOL)showUI; - (void)installerForHost:(SUHost *)host failedWithError:(NSError *)error; - (void)cleanUpDownload; diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 08d455a4..15b70921 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -299,6 +299,12 @@ - (BOOL)shouldInstallSynchronously { return NO; } - (void)installWithToolAndRelaunch:(BOOL)relaunch +{ + // Perhaps a poor assumption but: if we're not relaunching, we assume we shouldn't be showing any UI either. Because non-relaunching installations are kicked off without any user interaction, we shouldn't be interrupting them. + [self installWithToolAndRelaunch:relaunch displayingUserInterface:relaunch]; +} + +- (void)installWithToolAndRelaunch:(BOOL)relaunch displayingUserInterface:(BOOL)showUI { #if !ENDANGER_USERS_WITH_INSECURE_UPDATES if (![self validateUpdateDownloadedToPath:downloadPath extractedToPath:tempDir DSASignature:[updateItem DSASignature] publicDSAKey:[host publicDSAKey]]) @@ -364,7 +370,14 @@ if ([[updater delegate] respondsToSelector:@selector(pathToRelaunchForUpdater:)]) pathToRelaunch = [[updater delegate] pathToRelaunchForUpdater:updater]; NSString *relaunchToolPath = [relaunchPath stringByAppendingPathComponent: @"/Contents/MacOS/finish_installation"]; - [NSTask launchedTaskWithLaunchPath: relaunchToolPath arguments:[NSArray arrayWithObjects:[host bundlePath], pathToRelaunch, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], tempDir, relaunch ? @"1" : @"0", nil]]; + [NSTask launchedTaskWithLaunchPath: relaunchToolPath arguments:[NSArray arrayWithObjects: + [host bundlePath], + pathToRelaunch, + [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], + tempDir, + relaunch ? @"1" : @"0", + showUI ? @"1" : @"0", + nil]]; [NSApp terminate:self]; } diff --git a/SUUpdateDriver.h b/SUUpdateDriver.h index 9e8a73e2..8e83e3a9 100644 --- a/SUUpdateDriver.h +++ b/SUUpdateDriver.h @@ -21,11 +21,13 @@ extern NSString * const SUUpdateDriverFinishedNotification; NSURL *appcastURL; BOOL finished; + BOOL isInterruptible; } - initWithUpdater:(SUUpdater *)updater; - (void)checkForUpdatesAtURL:(NSURL *)URL host:(SUHost *)host; - (void)abortUpdate; +- (BOOL)isInterruptible; - (BOOL)finished; - (SUHost*)host; - (void)setHost:(SUHost*)newHost; diff --git a/SUUpdateDriver.m b/SUUpdateDriver.m index e1bae4ee..51b365da 100644 --- a/SUUpdateDriver.m +++ b/SUUpdateDriver.m @@ -33,6 +33,8 @@ NSString * const SUUpdateDriverFinishedNotification = @"SUUpdateDriverFinished"; [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdateDriverFinishedNotification object:self]; } +- (BOOL)isInterruptible { return isInterruptible; } + - (BOOL)finished { return finished; } - (void)dealloc diff --git a/SUUpdater.h b/SUUpdater.h index 1557c60b..929d863a 100644 --- a/SUUpdater.h +++ b/SUUpdater.h @@ -152,6 +152,11 @@ extern NSString *const SUUpdaterAppcastNotificationKey; -(void) updaterWillShowModalAlert:(SUUpdater *)updater; -(void) updaterDidShowModalAlert:(SUUpdater *)updater; +// Called when an update is scheduled to be silently installed on quit. +// The invocation can be used to trigger an immediate silent install and relaunch. +- (void)updater:(SUUpdater *)updater willInstallUpdateOnQuit:(SUAppcastItem *)update immediateInstallationInvocation:(NSInvocation *)invocation; +- (void)updater:(SUUpdater *)updater didCancelInstallUpdateOnQuit:(SUAppcastItem *)update; + @end diff --git a/SUUpdater.m b/SUUpdater.m index c40e9a2b..19162776 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -302,6 +302,9 @@ static NSString * const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefault - (IBAction)checkForUpdates: (id)sender { + if (driver && [driver isInterruptible]) + [driver abortUpdate]; + [self checkForUpdatesWithDriver:[[[SUUserInitiatedUpdateDriver alloc] initWithUpdater:self] autorelease]]; } diff --git a/en.lproj/Sparkle.strings b/en.lproj/Sparkle.strings index 8e01acc3..f91cdca9 100644 --- a/en.lproj/Sparkle.strings +++ b/en.lproj/Sparkle.strings @@ -1,3 +1,6 @@ +/* No comment provided by engineer. */ +"%1$@ %2$@ has been downloaded and is ready to use! This is an important update; would you like to install it and relaunch %1$@ now?" = "%1$@ %2$@ has been downloaded and is ready to use! This is an important update; would you like to install it and relaunch %1$@ now?"; + /* No comment provided by engineer. */ "%1$@ %2$@ has been downloaded and is ready to use! Would you like to install it and relaunch %1$@ now?" = "%1$@ %2$@ has been downloaded and is ready to use! Would you like to install it and relaunch %1$@ now?"; @@ -40,6 +43,9 @@ /* No comment provided by engineer. */ "An error occurred while relaunching %1$@, but the new version will be available next time you run %1$@." = "An error occurred while relaunching %1$@, but the new version will be available next time you run %1$@."; +/* No comment provided by engineer. */ +"An important update to %@ is ready to install" = "An important update to %@ is ready to install"; + /* the unit for bytes */ "B" = "B"; diff --git a/finish_installation.m b/finish_installation.m index dc07c16e..a8e1f9ab 100644 --- a/finish_installation.m +++ b/finish_installation.m @@ -24,6 +24,7 @@ NSTimer *longInstallationTimer; SUHost *host; BOOL shouldRelaunch; + BOOL shouldShowUI; } - (void) parentHasQuit; @@ -38,8 +39,7 @@ @implementation TerminationListener -- (id) initWithHostPath:(const char *)inhostpath executablePath:(const char *)execpath parentProcessId:(pid_t)ppid folderPath: (const char*)infolderpath shouldRelaunch:(BOOL)relaunch - selfPath: (NSString*)inSelfPath +- (id) initWithHostPath:(const char *)inhostpath executablePath:(const char *)execpath parentProcessId:(pid_t)ppid folderPath:(const char*)infolderpath shouldRelaunch:(BOOL)relaunch shouldShowUI:(BOOL)showUI selfPath:(NSString*)inSelfPath { if( !(self = [super init]) ) return nil; @@ -50,6 +50,7 @@ folderpath = infolderpath; selfPath = [inSelfPath retain]; shouldRelaunch = relaunch; + shouldShowUI = showUI; BOOL alreadyTerminated = (getppid() == 1); // ppid is launchd (1) => parent terminated already @@ -144,8 +145,7 @@ host = [[SUHost alloc] initWithBundle: theBundle]; installationPath = [[host installationPath] copy]; - // Perhaps a poor assumption but: if we're not relaunching, we assume we shouldn't be showing any UI either. Because non-relaunching installations are kicked off without any user interaction, we shouldn't be interrupting them. - if (shouldRelaunch) { + if (shouldShowUI) { SUStatusController* statusCtl = [[SUStatusController alloc] initWithHost: host]; // We quit anyway after we've installed, so leak this for now. [statusCtl setButtonTitle: SULocalizedString(@"Cancel Update",@"") target: nil action: Nil isDefault: NO]; [statusCtl beginActionWithTitle: SULocalizedString(@"Installing update...",@"") @@ -168,8 +168,7 @@ - (void) installerForHost:(SUHost *)host failedWithError:(NSError *)error { - // Perhaps a poor assumption but: if we're not relaunching, we assume we shouldn't be showing any UI either. Because non-relaunching installations are kicked off without any user interaction, we shouldn't be interrupting them. - if (shouldRelaunch) + if (shouldShowUI) NSRunAlertPanel( @"", @"%@", @"OK", @"", @"", [error localizedDescription] ); exit(EXIT_FAILURE); } @@ -178,14 +177,13 @@ int main (int argc, const char * argv[]) { - if( argc < 5 || argc > 6 ) + if( argc < 5 || argc > 7 ) return EXIT_FAILURE; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //ProcessSerialNumber psn = { 0, kCurrentProcess }; //TransformProcessType( &psn, kProcessTransformToForegroundApplication ); - [[NSApplication sharedApplication] activateIgnoringOtherApps: YES]; #if 0 // Cmdline tool NSString* selfPath = nil; @@ -200,12 +198,19 @@ int main (int argc, const char * argv[]) NSString* selfPath = [[NSBundle mainBundle] bundlePath]; #endif + BOOL shouldShowUI = (argc > 6) ? atoi(argv[6]) : 1; + if (shouldShowUI) + { + [[NSApplication sharedApplication] activateIgnoringOtherApps: YES]; + } + [NSApplication sharedApplication]; [[[TerminationListener alloc] initWithHostPath: (argc > 1) ? argv[1] : NULL executablePath: (argc > 2) ? argv[2] : NULL parentProcessId: (argc > 3) ? atoi(argv[3]) : 0 folderPath: (argc > 4) ? argv[4] : NULL shouldRelaunch: (argc > 5) ? atoi(argv[5]) : 1 + shouldShowUI: shouldShowUI selfPath: selfPath] autorelease]; [[NSApplication sharedApplication] run];