diff --git a/Sparkle/SPUUpdater.h b/Sparkle/SPUUpdater.h index 6eadbead..ce285835 100644 --- a/Sparkle/SPUUpdater.h +++ b/Sparkle/SPUUpdater.h @@ -251,6 +251,18 @@ SU_EXPORT @interface SPUUpdater : NSObject */ @property (nonatomic) BOOL automaticallyDownloadsUpdates; +/** + A property indicating whether or not the *option* to automatically download updates in the background can be turned on. + + This property can be used to determine whether an option to automatically download/install updates should be enabled. + + Its value depends on `automaticallyChecksForUpdates`, or the `SUAllowsAutomaticUpdates`in the host bundle's Info.plist if specified. + Don't set `SUAllowsAutomaticUpdates` in the Info.plist unless you need custom behavior. + + This property is KVO compliant. This property must be called on the main thread. + */ +@property (nonatomic, readonly) BOOL allowsAutomaticUpdates; + /** The URL of the appcast used to download update information. diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index de71e65f..056c85a4 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -1056,6 +1056,25 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification return NO; } +- (BOOL)allowsAutomaticUpdates +{ + if (![NSThread isMainThread]) { + SULog(SULogLevelError, @"Error: -[SPUUpdater allowsAutomaticUpdates] must be called on the main thread."); + } + + return [_updaterSettings allowsAutomaticUpdates]; +} + ++ (NSSet *)keyPathsForValuesAffectingAllowsAutomaticUpdates +{ + return [NSSet setWithObject:@"updaterSettings.allowsAutomaticUpdates"]; +} + ++ (BOOL)automaticallyNotifiesObserversOfAllowsAutomaticUpdates +{ + return NO; +} + - (void)setFeedURL:(NSURL * _Nullable)feedURL { if (![NSThread isMainThread]) { diff --git a/Sparkle/SPUUpdaterSettings.h b/Sparkle/SPUUpdaterSettings.h index 9e343ce6..16323f5e 100644 --- a/Sparkle/SPUUpdaterSettings.h +++ b/Sparkle/SPUUpdaterSettings.h @@ -48,25 +48,12 @@ SU_EXPORT @interface SPUUpdaterSettings : NSObject */ @property (nonatomic) NSTimeInterval updateCheckInterval; -/** - * The impatient update check interval. - * - * If an update has already been downloaded automatically in the background, Sparkle may not notify users of the update immediately, - * and tries to install the update siliently on quit without notifying the user. - * - * Sparkle uses this long impatient update check interval to decide when to notify the user of the update if they haven't quit the app for a long time. - * By default this check interval is set to 604800 seconds (which is 1 week). This interval must be bigger than the `updateCheckInterval`. - */ -@property (nonatomic, readonly) NSTimeInterval impatientUpdateCheckInterval; - -/** - * Indicates whether or not automatically downloading updates is allowed to be turned on by the user. - * If this value is nil, the developer has not explicitly specified this option. - */ -@property (readonly, nonatomic, nullable) NSNumber *allowsAutomaticUpdatesOption; - /** * Indicates whether or not automatically downloading updates is allowed to be turned on by the user. + * + * This property is determined by checking `automaticallyChecksForUpdates` and `allowsAutomaticUpdatesOption`. + * + * This property is KVO compliant. This property must be called on the main thread. */ @property (readonly, nonatomic) BOOL allowsAutomaticUpdates; @@ -80,6 +67,25 @@ SU_EXPORT @interface SPUUpdaterSettings : NSObject */ @property (nonatomic) BOOL automaticallyDownloadsUpdates; +/** + * Indicates whether or not the developer allows turning on updates being automatically downloaded and installed. + * If this value is nil, the developer has not explicitly specified this option (which is the default). + * + * Please prefer to use `allowsAutomaticUpdates` instead. + */ +@property (readonly, nonatomic, nullable) NSNumber *allowsAutomaticUpdatesOption; + +/** + * The impatient update check interval. + * + * If an update has already been downloaded automatically in the background, Sparkle may not notify users of the update immediately, + * and tries to install the update siliently on quit without notifying the user. + * + * Sparkle uses this long impatient update check interval to decide when to notify the user of the update if they haven't quit the app for a long time. + * By default this check interval is set to 604800 seconds (which is 1 week). This interval must be bigger than the `updateCheckInterval`. + */ +@property (nonatomic, readonly) NSTimeInterval impatientUpdateCheckInterval; + /** * Indicates whether or not anonymous system profile information is sent when checking for updates. * diff --git a/Sparkle/SPUUpdaterSettings.m b/Sparkle/SPUUpdaterSettings.m index dc3bcb69..17c88c09 100644 --- a/Sparkle/SPUUpdaterSettings.m +++ b/Sparkle/SPUUpdaterSettings.m @@ -19,6 +19,8 @@ static NSString *SUUpdateCheckIntervalKeyPath = @"updateCheckInterval"; static NSString *SUImpatientUpdateCheckIntervalKeyPath = @"impatientUpdateCheckInterval"; static NSString *SUAutomaticallyDownloadsUpdatesKeyPath = @"automaticallyDownloadsUpdates"; static NSString *SUSendsSystemProfileKeyPath = @"sendsSystemProfile"; +static NSString *SUAllowsAutomaticUpdatesOptionKeyPath = @"allowsAutomaticUpdatesOption"; +static NSString *SUAllowsAutomaticUpdatesKeyPath = @"allowsAutomaticUpdates"; @implementation SPUUpdaterSettings { @@ -34,6 +36,8 @@ static NSString *SUSendsSystemProfileKeyPath = @"sendsSystemProfile"; @synthesize impatientUpdateCheckInterval = _impatientUpdateCheckInterval; @synthesize automaticallyDownloadsUpdates = _automaticallyDownloadsUpdates; @synthesize sendsSystemProfile = _sendsSystemProfile; +@synthesize allowsAutomaticUpdatesOption = _allowsAutomaticUpdatesOption; +@synthesize allowsAutomaticUpdates = _allowsAutomaticUpdates; - (instancetype)initWithHostBundle:(NSBundle *)hostBundle { @@ -50,6 +54,8 @@ static NSString *SUSendsSystemProfileKeyPath = @"sendsSystemProfile"; _automaticallyChecksForUpdates = [self currentAutomaticallyChecksForUpdates]; _updateCheckInterval = [self currentUpdateCheckInterval]; _impatientUpdateCheckInterval = [self currentImpatientUpdateCheckInterval]; + _allowsAutomaticUpdatesOption = [self currentAllowsAutomaticUpdatesOption]; + _allowsAutomaticUpdates = [self currentAllowsAutomaticUpdates]; _automaticallyDownloadsUpdates = [self currentAutomaticallyDownloadsUpdates]; _sendsSystemProfile = [self currentSendsSystemProfile]; @@ -93,6 +99,9 @@ static NSString *SUSendsSystemProfileKeyPath = @"sendsSystemProfile"; _automaticallyChecksForUpdates = currentValue; [self didChangeValueForKey:updatedKeyPath]; + + [self processAllowsAutomaticUpdates]; + [self processAutomaticallyDownloadsUpdates]; } } @@ -126,6 +135,36 @@ static NSString *SUSendsSystemProfileKeyPath = @"sendsSystemProfile"; } } +- (void)processAllowsAutomaticUpdatesOption SPU_OBJC_DIRECT +{ + NSNumber *currentValue = [self currentAllowsAutomaticUpdatesOption]; + + if (((currentValue != nil) != (_allowsAutomaticUpdatesOption != nil)) || (currentValue.boolValue != _allowsAutomaticUpdatesOption.boolValue)) { + NSString *updatedKeyPath = SUAllowsAutomaticUpdatesOptionKeyPath; + + [self willChangeValueForKey:updatedKeyPath]; + + _allowsAutomaticUpdatesOption = currentValue; + + [self didChangeValueForKey:updatedKeyPath]; + } +} + +- (void)processAllowsAutomaticUpdates SPU_OBJC_DIRECT +{ + BOOL currentValue = [self currentAllowsAutomaticUpdates]; + + if (currentValue != _allowsAutomaticUpdates) { + NSString *updatedKeyPath = SUAllowsAutomaticUpdatesKeyPath; + + [self willChangeValueForKey:updatedKeyPath]; + + _allowsAutomaticUpdates = currentValue; + + [self didChangeValueForKey:updatedKeyPath]; + } +} + - (void)processAutomaticallyDownloadsUpdates SPU_OBJC_DIRECT { BOOL currentValue = [self currentAutomaticallyDownloadsUpdates]; @@ -172,6 +211,8 @@ static NSString *SUSendsSystemProfileKeyPath = @"sendsSystemProfile"; [self processCurrentAutomaticallyChecksForUpdates]; [self processUpdateCheckInterval]; [self processImpatientUpdateCheckInterval]; + [self processAllowsAutomaticUpdatesOption]; + [self processAllowsAutomaticUpdates]; [self processAutomaticallyDownloadsUpdates]; [self processSendsSystemProfile]; } @@ -201,6 +242,9 @@ static NSString *SUSendsSystemProfileKeyPath = @"sendsSystemProfile"; } else { [NSNotificationCenter.defaultCenter postNotificationName:SUUpdateAutomaticCheckSettingChangedNotification object:nil userInfo:@{SUUpdateBundlePathUserInfoKey: _host.bundlePath}]; } + + [self processAllowsAutomaticUpdates]; + [self processAutomaticallyDownloadsUpdates]; } + (BOOL)automaticallyNotifiesObserversOfAutomaticallyChecksForUpdates @@ -255,22 +299,32 @@ static NSString *SUSendsSystemProfileKeyPath = @"sendsSystemProfile"; return NO; } -// For allowing automatic downloaded updates to be turned on or off -- (NSNumber * _Nullable)allowsAutomaticUpdatesOption +- (NSNumber * _Nullable)currentAllowsAutomaticUpdatesOption SPU_OBJC_DIRECT { NSNumber *developerAllowsAutomaticUpdates = [_host boolNumberForInfoDictionaryKey:SUAllowsAutomaticUpdatesKey]; return developerAllowsAutomaticUpdates; } -- (BOOL)allowsAutomaticUpdates ++ (BOOL)automaticallyNotifiesObserversOfAllowsAutomaticUpdatesOption { - NSNumber *developerAllowsAutomaticUpdates = [self allowsAutomaticUpdatesOption]; - return (developerAllowsAutomaticUpdates == nil || developerAllowsAutomaticUpdates.boolValue); + return NO; } +// This depends on currentAllowsAutomaticUpdatesOption and currentAutomaticallyChecksForUpdates and must be processed afterwards +- (BOOL)currentAllowsAutomaticUpdates +{ + return (_allowsAutomaticUpdatesOption == nil) ? _automaticallyChecksForUpdates : _allowsAutomaticUpdatesOption.boolValue; +} + ++ (BOOL)automaticallyNotifiesObserversOfAllowsAutomaticUpdates +{ + return NO; +} + +// This depends on currentAllowsAutomaticUpdates and must be processed afterwards - (BOOL)currentAutomaticallyDownloadsUpdates SPU_OBJC_DIRECT { - return [self allowsAutomaticUpdates] && [_host boolForKey:SUAutomaticallyUpdateKey]; + return _allowsAutomaticUpdates && [_host boolForKey:SUAutomaticallyUpdateKey]; } - (void)setAutomaticallyDownloadsUpdates:(BOOL)automaticallyDownloadsUpdates diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index 4cd2a82c..bd530749 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -32,6 +32,7 @@ #import "SPUUserUpdateState.h" static NSString *const SUUpdateAlertTouchBarIdentifier = @"" SPARKLE_BUNDLE_IDENTIFIER ".SUUpdateAlert"; +static NSString *const SUAllowsAutomaticUpdatesKeyPath = @"allowsAutomaticUpdates"; static const CGFloat SUUpdateAlertGroupElementSpacing = 12.0; @@ -60,7 +61,6 @@ static const CGFloat SUUpdateAlertGroupElementSpacing = 12.0; void (^_didBecomeKeyBlock)(void); void(^_completionBlock)(SPUUserUpdateChoice, NSRect, BOOL); - BOOL _allowsAutomaticUpdates; BOOL _windowLoadedAndShowsReleaseNotes; } @@ -78,17 +78,6 @@ static const CGFloat SUUpdateAlertGroupElementSpacing = 12.0; _updaterSettings = updaterSettings; - BOOL allowsAutomaticUpdates; - NSNumber *allowsAutomaticUpdatesOption = _updaterSettings.allowsAutomaticUpdatesOption; - if (item.informationOnlyUpdate) { - allowsAutomaticUpdates = NO; - } else if (allowsAutomaticUpdatesOption == nil) { - allowsAutomaticUpdates = _updaterSettings.automaticallyChecksForUpdates; - } else { - allowsAutomaticUpdates = allowsAutomaticUpdatesOption.boolValue; - } - _allowsAutomaticUpdates = allowsAutomaticUpdates; - [self setShouldCascadeWindows:NO]; } else { assert(false); @@ -96,6 +85,13 @@ static const CGFloat SUUpdateAlertGroupElementSpacing = 12.0; return self; } +- (void)dealloc +{ + if (self.windowLoaded) { + [_updaterSettings removeObserver:self forKeyPath:SUAllowsAutomaticUpdatesKeyPath]; + } +} + - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], _host.bundlePath]; @@ -338,6 +334,15 @@ static const CGFloat SUUpdateAlertGroupElementSpacing = 12.0; } } +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if ([keyPath isEqualToString:SUAllowsAutomaticUpdatesKeyPath]) { + _automaticallyInstallUpdatesButton.superview.hidden = !_updaterSettings.allowsAutomaticUpdates; + } else { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + } +} + - (void)windowDidLoad { NSWindow *window = self.window; @@ -395,8 +400,6 @@ static const CGFloat SUUpdateAlertGroupElementSpacing = 12.0; [_installButton setTitle:SULocalizedStringFromTableInBundle(@"Learn More…", SPARKLE_TABLE, sparkleBundle, @"Alternate title for 'Install Update' button when there's no download in RSS feed.")]; [_installButton setAction:@selector(openInfoURL:)]; } - - BOOL allowsAutomaticUpdates = _allowsAutomaticUpdates; if (showReleaseNotes) { [self displayReleaseNotesSpinner]; @@ -409,9 +412,7 @@ static const CGFloat SUUpdateAlertGroupElementSpacing = 12.0; // NOTE: The code below for deciding what buttons to hide is complex! Due to array of feature configurations :) - if (!allowsAutomaticUpdates) { - _automaticallyInstallUpdatesButton.superview.hidden = YES; - } + [_updaterSettings addObserver:self forKeyPath:SUAllowsAutomaticUpdatesKeyPath options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew) context:NULL]; if (_state.stage == SPUUserUpdateStageInstalling) { // We're going to be relaunching pretty instantaneously diff --git a/Sparkle/SUUpdateAlert.xib b/Sparkle/SUUpdateAlert.xib index c66c41d8..70c543a5 100644 --- a/Sparkle/SUUpdateAlert.xib +++ b/Sparkle/SUUpdateAlert.xib @@ -1,8 +1,8 @@ - + - + @@ -26,17 +26,17 @@ - + - + - + @@ -96,7 +96,7 @@ - + @@ -107,10 +107,10 @@ - + - + + - + + - + + - + + - +