From 5c32832eed47afb75689fad1a352e1419e88482f Mon Sep 17 00:00:00 2001 From: Andy Matuschak Date: Sat, 31 May 2008 00:13:38 -0700 Subject: [PATCH] Fixes bug 228446 Finally adding real delegate support. The following methods are implemented; the comments document them. If you have suggestions for other methods that would be useful, feel free to suggest them. // Use this to override the default behavior for Sparkle prompting the user about automatic update checks. - (BOOL)shouldPromptForPermissionToCheckForUpdates; // Implement this if you want to do some special handling with the appcast once it finishes loading. - (void)appcastDidFinishLoading:(SUAppcast *)appcast; // If you're using special logic or extensions in your appcast, implement this to use your own logic for finding // a valid update, if any, in the given appcast. - (SUAppcastItem *)bestValidUpdateInAppcast:(SUAppcast *)appcast; // Sent when a valid update is found by the update driver. - (void)didFindValidUpdate:(SUAppcastItem *)update; // Sent when the user makes a choice in the update alert dialog (install now / remind me later / skip this version). - (void)userChoseAction:(SUUpdateAlertChoice)action forUpdate:(SUAppcastItem *)update; // Sent immediately before installing the specified update. - (void)updateWillInstall:(SUAppcastItem *)update; // Return YES to delay the relaunch until you do some processing; invoke the given NSInvocation to continue. - (BOOL)shouldPostponeRelaunchForUpdate:(SUAppcastItem *)update untilInvoking:(NSInvocation *)invocation; // Called immediately before relaunching. - (void)updaterWillRelaunchApplication; --- SUAppcastItem.h | 5 +++++ SUAppcastItem.m | 7 +++++++ SUBasicUpdateDriver.m | 45 +++++++++++++++++++++++++++++++++-------- SUUIBasedUpdateDriver.m | 2 ++ SUUpdater.h | 43 +++++++++++++++++++++++++-------------- SUUpdater.m | 8 +++++++- 6 files changed, 86 insertions(+), 24 deletions(-) diff --git a/SUAppcastItem.h b/SUAppcastItem.h index ff3f0219..d0c0523e 100644 --- a/SUAppcastItem.h +++ b/SUAppcastItem.h @@ -23,6 +23,8 @@ NSURL *fileURL; NSString *versionString; NSString *displayVersionString; + + NSDictionary *propertiesDictionary; } // Initializes with data from a dictionary provided by the RSS class. @@ -38,6 +40,9 @@ - (NSString *)DSASignature; - (NSString *)minimumSystemVersion; +// Returns the dictionary provided in initWithDictionary; this might be useful later for extensions. +- (NSDictionary *)propertiesDictionary; + @end #endif diff --git a/SUAppcastItem.m b/SUAppcastItem.m index b5709d87..f04a14e1 100644 --- a/SUAppcastItem.m +++ b/SUAppcastItem.m @@ -97,6 +97,7 @@ self = [super init]; if (self) { + propertiesDictionary = [dict retain]; [self setTitle:[dict objectForKey:@"title"]]; [self setDate:[dict objectForKey:@"pubDate"]]; [self setDescription:[dict objectForKey:@"description"]]; @@ -156,7 +157,13 @@ [self setFileURL:nil]; [self setVersionString:nil]; [self setDisplayVersionString:nil]; + [propertiesDictionary release]; [super dealloc]; } +- (NSDictionary *)propertiesDictionary +{ + return propertiesDictionary; +} + @end diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index d577e885..de5d0b0b 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -58,17 +58,28 @@ - (void)appcastDidFinishLoading:(SUAppcast *)ac { + if ([delegate respondsToSelector:@selector(appcastDidFinishLoading:)]) + [delegate appcastDidFinishLoading:ac]; + NSArray* updates = [ac items]; if ([updates count] > 0) [[SUUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:SULastCheckTimeKey]; - // Find the first update we can actually use. - NSEnumerator *updateEnumerator = [updates objectEnumerator]; - do { - updateItem = [updateEnumerator nextObject]; - } while (updateItem && ![self hostSupportsItem:updateItem]); - - [updateItem retain]; + // Now we have to find the best valid update in the appcast. + if ([delegate respondsToSelector:@selector(bestValidUpdateInAppcast:)]) // Does the delegate want to handle it? + { + updateItem = [delegate bestValidUpdateInAppcast:ac]; + } + else // If not, we'll take care of it ourselves. + { + // Find the first update we can actually use. + NSEnumerator *updateEnumerator = [updates objectEnumerator]; + do { + updateItem = [updateEnumerator nextObject]; + } while (updateItem && ![self hostSupportsItem:updateItem]); + + [updateItem retain]; + } CFRelease(ac); // Remember that we're explicitly managing the memory of the appcast. if (updateItem == nil) { [self didNotFindUpdate]; return; } @@ -86,6 +97,8 @@ - (void)didFindValidUpdate { + if ([delegate respondsToSelector:@selector(didFindValidUpdate:)]) + [delegate didFindValidUpdate:updateItem]; [self downloadUpdate]; } @@ -168,6 +181,8 @@ - (void)installUpdate { + if ([delegate respondsToSelector:@selector(updateWillInstall:)]) + [delegate updateWillInstall:updateItem]; // Copy the relauncher into a temporary directory so we can get to it after the new version's installed. NSString *relaunchPathToCopy = [[NSBundle bundleForClass:[self class]] pathForResource:@"relaunch" ofType:@""]; NSString *targetPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; @@ -188,8 +203,22 @@ - (void)relaunchHostApp { + // Give the host app an opportunity to postpone the relaunch. + static BOOL postponedOnce = NO; + if (!postponedOnce && [delegate respondsToSelector:@selector(shouldPostponeRelaunchForUpdate:untilInvoking:)]) + { + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[[self class] instanceMethodSignatureForSelector:@selector(relaunchHostApp)] retain]]; + [invocation setSelector:@selector(relaunchHostApp)]; + [invocation setTarget:self]; + postponedOnce = YES; + if ([delegate shouldPostponeRelaunchForUpdate:updateItem untilInvoking:invocation]) + return; + } + [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterWillRestartNotification object:self]; - + if ([delegate respondsToSelector:@selector(updaterWillRelaunchApplication)]) + [delegate updaterWillRelaunchApplication]; + @try { if(!relaunchPath || ![[NSFileManager defaultManager] fileExistsAtPath:relaunchPath]) diff --git a/SUUIBasedUpdateDriver.m b/SUUIBasedUpdateDriver.m index 561eadde..6852ee44 100644 --- a/SUUIBasedUpdateDriver.m +++ b/SUUIBasedUpdateDriver.m @@ -43,6 +43,8 @@ - (void)updateAlert:(SUUpdateAlert *)alert finishedWithChoice:(SUUpdateAlertChoice)choice { [updateAlert release]; updateAlert = nil; + if ([delegate respondsToSelector:@selector(userChoseAction:forUpdate:)]) + [delegate userChoseAction:choice forUpdate:updateItem]; [[SUUserDefaults standardUserDefaults] setObject:nil forKey:SUSkippedVersionKey]; switch (choice) { diff --git a/SUUpdater.h b/SUUpdater.h index 5eea61a3..80788728 100644 --- a/SUUpdater.h +++ b/SUUpdater.h @@ -33,22 +33,35 @@ @end @interface NSObject (SUUpdaterDelegateInformalProtocol) -/*! - @method - @abstract Delegate method for host apps to define additional feed parameters - @discussion This method allows you to add extra parameters to the appcast URL, potentially based on whether or not Sparkle will also be sending along the system profile. This method should return an array of dictionaries with the following keys: - - key: The key to be used when reporting data to the server - - visibleKey: Alternate version of key to be used in UI displays of profile information - - value: Value to be used when reporting data to the server - - visibleValue: Alternate version of value to be used in UI displays of profile information. -*/ - - +// This method allows you to add extra parameters to the appcast URL, potentially based on whether or not +// Sparkle will also be sending along the system profile. This method should return an array of dictionaries with the following keys: - (NSArray *)feedParametersForUpdater:(SUUpdater *)updater sendingSystemProfile:(BOOL)sendingProfile; + +// Use this to override the default behavior for Sparkle prompting the user about automatic update checks. +- (BOOL)shouldPromptForPermissionToCheckForUpdates; + +// Implement this if you want to do some special handling with the appcast once it finishes loading. +- (void)appcastDidFinishLoading:(SUAppcast *)appcast; + +// If you're using special logic or extensions in your appcast, implement this to use your own logic for finding +// a valid update, if any, in the given appcast. +- (SUAppcastItem *)bestValidUpdateInAppcast:(SUAppcast *)appcast; + +// Sent when a valid update is found by the update driver. +- (void)didFindValidUpdate:(SUAppcastItem *)update; + +// Sent when the user makes a choice in the update alert dialog (install now / remind me later / skip this version). +- (void)userChoseAction:(SUUpdateAlertChoice)action forUpdate:(SUAppcastItem *)update; + +// Sent immediately before installing the specified update. +- (void)updateWillInstall:(SUAppcastItem *)update; + +// Return YES to delay the relaunch until you do some processing; invoke the given NSInvocation to continue. +- (BOOL)shouldPostponeRelaunchForUpdate:(SUAppcastItem *)update untilInvoking:(NSInvocation *)invocation; + +// Called immediately before relaunching. +- (void)updaterWillRelaunchApplication; + @end // Define some minimum intervals to avoid DOS-like checking attacks. These are in seconds. diff --git a/SUUpdater.m b/SUUpdater.m index f1e53462..6ac26a7d 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -55,8 +55,14 @@ static SUUpdater *sharedUpdater = nil; if ([[SUUserDefaults standardUserDefaults] objectForKey:SUEnableAutomaticChecksKey] && [[SUUserDefaults standardUserDefaults] boolForKey:SUEnableAutomaticChecksKey] == NO) { return; } + // Does the delegate want to take care of the logic for when we should ask permission to update? + if ([delegate respondsToSelector:@selector(shouldPromptForPermissionToCheckForUpdates)]) + { + if ([delegate shouldPromptForPermissionToCheckForUpdates]) + [SUUpdatePermissionPrompt promptWithHostBundle:hostBundle delegate:self]; + } // Has he been asked already? And don't ask if the host has a default value set in its Info.plist. - if ([[SUUserDefaults standardUserDefaults] objectForKey:SUEnableAutomaticChecksKey] == nil && + else if ([[SUUserDefaults standardUserDefaults] objectForKey:SUEnableAutomaticChecksKey] == nil && [hostBundle objectForInfoDictionaryKey:SUEnableAutomaticChecksKey] == nil) { if ([[SUUserDefaults standardUserDefaults] objectForKey:SUEnableAutomaticChecksKeyOld])