From aa4cc6a7dcb85befaec67a81d333650d65a4a5bd Mon Sep 17 00:00:00 2001 From: Zorg Date: Thu, 24 Sep 2015 23:07:49 -0400 Subject: [PATCH] Don't install automatic updates if the system might shut off --- Sparkle/SUAutomaticUpdateDriver.m | 11 +++++++++++ Sparkle/SUErrors.h | 5 ++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 45b3d546..562f2d05 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -63,6 +63,7 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 - (void)unarchiverDidFinish:(SUUnarchiver *)__unused ua { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; + [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(systemWillPowerOff:) name:NSWorkspaceWillPowerOffNotification object:nil]; // Sudden termination is available on 10.6+ NSProcessInfo *processInfo = [NSProcessInfo processInfo]; @@ -103,6 +104,8 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 if (self.willUpdateOnTermination) { [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; + [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceWillPowerOffNotification object:nil]; + NSProcessInfo *processInfo = [NSProcessInfo processInfo]; [processInfo enableSuddenTermination]; @@ -151,6 +154,7 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 case SUInstallLaterChoice: self.postponingInstallation = YES; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; + [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(systemWillPowerOff:) name:NSWorkspaceWillPowerOffNotification object:nil]; // We're already waiting on quit, just indicate that we're idle. self.interruptible = YES; break; @@ -172,6 +176,13 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 [super installWithToolAndRelaunch:relaunch displayingUserInterface:showUI]; } +- (void)systemWillPowerOff:(NSNotification *)__unused note +{ + [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUSystemPowerOffError userInfo:@{ + NSLocalizedDescriptionKey: SULocalizedString(@"The update will not be installed because the user requested for the system to power off", nil) + }]]; +} + - (void)applicationWillTerminate:(NSNotification *)__unused note { [self installWithToolAndRelaunch:NO]; diff --git a/Sparkle/SUErrors.h b/Sparkle/SUErrors.h index e624bb05..d73aadba 100644 --- a/Sparkle/SUErrors.h +++ b/Sparkle/SUErrors.h @@ -38,7 +38,10 @@ typedef NS_ENUM(OSStatus, SUError) { SUMissingInstallerToolError = 4003, SURelaunchError = 4004, SUInstallationError = 4005, - SUDowngradeError = 4006 + SUDowngradeError = 4006, + + // System phase errors + SUSystemPowerOffError = 5000 }; #endif