From b3f39444958a7e0d63c39b22117e8d3fd5af6aca Mon Sep 17 00:00:00 2001 From: Andy Matuschak Date: Wed, 16 Jul 2008 23:51:39 -0700 Subject: [PATCH] Fixes 244857 Made a first crack at redifining the SUUpdater delegate protocol. Still not sure about a couple things, so please don't change your delegate methods to conform until this gets merged into trunk. Also made the SUUpdateDrivers not have the updater's delegate as their delegate (that was really icky). Now they're just coupled with the SUUpdater, which is icky as well, but less icky, at least. --- SUBasicUpdateDriver.m | 38 ++++++++++++++++++++++---------------- SUUIBasedUpdateDriver.m | 7 +++---- SUUpdateDriver.h | 3 --- SUUpdateDriver.m | 2 -- SUUpdater.h | 27 +++++++++++++-------------- SUUpdater.m | 26 ++++++-------------------- 6 files changed, 44 insertions(+), 59 deletions(-) diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 0199eb10..c5332f39 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -34,9 +34,10 @@ { id comparator = nil; + SUUpdater *updater = [SUUpdater updaterForBundle:[host bundle]]; // Give the delegate a chance to provide a custom version comparator - if ([delegate respondsToSelector:@selector(versionComparatorForHost:)]) - comparator = [delegate versionComparatorForHost:host]; + if ([[updater delegate] respondsToSelector:@selector(versionComparatorForUpdater:)]) + comparator = [[updater delegate] versionComparatorForUpdater:updater]; // If we don't get a comparator from the delegate, use the default comparator if (!comparator) @@ -70,13 +71,14 @@ - (void)appcastDidFinishLoading:(SUAppcast *)ac { - if ([delegate respondsToSelector:@selector(appcastDidFinishLoading:forHost:)]) - [delegate appcastDidFinishLoading:ac forHost:host]; + SUUpdater *updater = [SUUpdater updaterForBundle:[host bundle]]; + if ([[updater delegate] respondsToSelector:@selector(updater:didFinishLoadingAppcast:)]) + [[updater delegate] updater:updater didFinishLoadingAppcast:ac]; // Now we have to find the best valid update in the appcast. - if ([delegate respondsToSelector:@selector(bestValidUpdateInAppcast:forHost:)]) // Does the delegate want to handle it? + if ([[updater delegate] respondsToSelector:@selector(bestValidUpdateInAppcast:forUpdater:)]) // Does the delegate want to handle it? { - updateItem = [delegate bestValidUpdateInAppcast:ac forHost:host]; + updateItem = [[updater delegate] bestValidUpdateInAppcast:ac forUpdater:updater]; } else // If not, we'll take care of it ourselves. { @@ -105,15 +107,17 @@ - (void)didFindValidUpdate { - if ([delegate respondsToSelector:@selector(didFindValidUpdate:toHost:)]) - [delegate didFindValidUpdate:updateItem toHost:host]; + SUUpdater *updater = [SUUpdater updaterForBundle:[host bundle]]; + if ([[updater delegate] respondsToSelector:@selector(updater:didFindValidUpdate:)]) + [[updater delegate] updater:updater didFindValidUpdate:updateItem]; [self downloadUpdate]; } - (void)didNotFindUpdate { - if ([delegate respondsToSelector:@selector(didNotFindUpdateToHost:)]) - [delegate didNotFindUpdateToHost:host]; + SUUpdater *updater = [SUUpdater updaterForBundle:[host bundle]]; + if ([[updater delegate] respondsToSelector:@selector(updaterDidNotFindUpdate:)]) + [[updater delegate] updaterDidNotFindUpdate:updater]; [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUNoUpdateError userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:SULocalizedString(@"You already have the newest version of %@.", nil), [host name]] forKey:NSLocalizedDescriptionKey]]]; } @@ -206,8 +210,9 @@ - (void)installUpdate { - if ([delegate respondsToSelector:@selector(updateWillInstall:toHost:)]) - [delegate updateWillInstall:updateItem toHost:host]; + SUUpdater *updater = [SUUpdater updaterForBundle:[host bundle]]; + if ([[updater delegate] respondsToSelector:@selector(updater:willInstallUpdate:)]) + [[updater delegate] updater:updater willInstallUpdate: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]]; @@ -229,21 +234,22 @@ { // Give the host app an opportunity to postpone the relaunch. static BOOL postponedOnce = NO; - if (!postponedOnce && [delegate respondsToSelector:@selector(shouldPostponeRelaunchForUpdate:toHost:untilInvoking:)]) + SUUpdater *updater = [SUUpdater updaterForBundle:[host bundle]]; + if (!postponedOnce && [[updater delegate] respondsToSelector:@selector(updater: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 toHost:host untilInvoking:invocation]) + if ([[updater delegate] updater:updater shouldPostponeRelaunchForUpdate:updateItem untilInvoking:invocation]) return; } [self cleanUp]; // Clean up the download and extracted files. [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterWillRestartNotification object:self]; - if ([delegate respondsToSelector:@selector(updaterWillRelaunchApplication)]) - [delegate updaterWillRelaunchApplication]; + if ([[updater delegate] respondsToSelector:@selector(updaterWillRelaunchApplication)]) + [[updater delegate] updaterWillRelaunchApplication:updater]; if(!relaunchPath || ![[NSFileManager defaultManager] fileExistsAtPath:relaunchPath]) { diff --git a/SUUIBasedUpdateDriver.m b/SUUIBasedUpdateDriver.m index c712d223..23b69e0c 100644 --- a/SUUIBasedUpdateDriver.m +++ b/SUUIBasedUpdateDriver.m @@ -28,8 +28,9 @@ - (void)didNotFindUpdate { - if ([delegate respondsToSelector:@selector(didNotFindUpdateToHost:)]) - [delegate didNotFindUpdateToHost:host]; + SUUpdater *updater = [SUUpdater updaterForBundle:[host bundle]]; + if ([[updater delegate] respondsToSelector:@selector(updaterDidNotFindUpdate:)]) + [[updater delegate] updaterDidNotFindUpdate:updater]; NSAlert *alert = [NSAlert alertWithMessageText:SULocalizedString(@"You're up to date!", nil) defaultButton:SULocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:SULocalizedString(@"%@ %@ is currently the newest version available.", nil), [host name], [host displayVersion]]; [alert setIcon:[host icon]]; [alert runModal]; @@ -45,8 +46,6 @@ - (void)updateAlert:(SUUpdateAlert *)alert finishedWithChoice:(SUUpdateAlertChoice)choice { [updateAlert release]; updateAlert = nil; - if ([delegate respondsToSelector:@selector(userChoseAction:forUpdate:toHost:)]) - [delegate userChoseAction:choice forUpdate:updateItem toHost:host]; [host setObject:nil forUserDefaultsKey:SUSkippedVersionKey]; switch (choice) { diff --git a/SUUpdateDriver.h b/SUUpdateDriver.h index 7c9485d2..16057b57 100644 --- a/SUUpdateDriver.h +++ b/SUUpdateDriver.h @@ -19,14 +19,11 @@ extern NSString *SUUpdateDriverFinishedNotification; SUHost *host; BOOL finished; - id delegate; } - (void)checkForUpdatesAtURL:(NSURL *)appcastURL host:(SUHost *)hb; - (void)abortUpdate; - (BOOL)finished; -- delegate; -- (void)setDelegate:delegate; @end #endif diff --git a/SUUpdateDriver.m b/SUUpdateDriver.m index d6c6d9e1..5f568514 100644 --- a/SUUpdateDriver.m +++ b/SUUpdateDriver.m @@ -23,6 +23,4 @@ NSString *SUUpdateDriverFinishedNotification = @"SUUpdateDriverFinished"; } - (BOOL)finished { return finished; } -- delegate { return delegate; } -- (void)setDelegate:del { delegate = del; } @end diff --git a/SUUpdater.h b/SUUpdater.h index 1381a497..cf9cb4dc 100644 --- a/SUUpdater.h +++ b/SUUpdater.h @@ -23,8 +23,10 @@ + (SUUpdater *)sharedUpdater; + (SUUpdater *)updaterForBundle:(NSBundle *)bundle; +- (NSBundle *)hostBundle; - (void)setDelegate:(id)delegate; +- delegate; // This IBAction is meant for a main menu item. Hook up any menu item to this action, // and Sparkle will check for updates and report back its findings verbosely. @@ -47,40 +49,37 @@ @interface NSObject (SUUpdaterDelegateInformalProtocol) // 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 *)feedParametersForHost:(SUHost *)bundle sendingSystemProfile:(BOOL)sendingProfile; +// Sparkle will also be sending along the system profile. This method should return an array of dictionaries with keys: "key" and "value". +- (NSArray *)feedParametersForUpdater:(SUUpdater *)updater sendingSystemProfile:(BOOL)sendingProfile; // Use this to override the default behavior for Sparkle prompting the user about automatic update checks. -- (BOOL)shouldPromptForPermissionToCheckForUpdatesToHost:(SUHost *)bundle; +- (BOOL)updaterShouldPromptForPermissionToCheckForUpdates:(SUUpdater *)bundle; // Implement this if you want to do some special handling with the appcast once it finishes loading. -- (void)appcastDidFinishLoading:(SUAppcast *)appcast forHost:(SUHost *)bundle; +- (void)updater:(SUUpdater *)updater didFinishLoadingAppcast:(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 forHost:(SUHost *)bundle; +- (SUAppcastItem *)bestValidUpdateInAppcast:(SUAppcast *)appcast forUpdater:(SUUpdater *)bundle; // Sent when a valid update is found by the update driver. -- (void)didFindValidUpdate:(SUAppcastItem *)update toHost:(SUHost *)bundle; +- (void)updater:(SUUpdater *)updater didFindValidUpdate:(SUAppcastItem *)update; // Sent when a valid update is not found. -- (void)didNotFindUpdateToHost:(SUHost *)hb; - -// 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 toHost:(SUHost *)bundle; +- (void)updaterDidNotFindUpdate:(SUUpdater *)update; // Sent immediately before installing the specified update. -- (void)updateWillInstall:(SUAppcastItem *)update toHost:(SUHost *)bundle; +- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update; // Return YES to delay the relaunch until you do some processing; invoke the given NSInvocation to continue. -- (BOOL)shouldPostponeRelaunchForUpdate:(SUAppcastItem *)update toHost:(SUHost *)hostBundle untilInvoking:(NSInvocation *)invocation; +- (BOOL)updater:(SUUpdater *)updater shouldPostponeRelaunchForUpdate:(SUAppcastItem *)update untilInvoking:(NSInvocation *)invocation; // Called immediately before relaunching. -- (void)updaterWillRelaunchApplication; +- (void)updaterWillRelaunchApplication:(SUUpdater *)updater; // This method allows you to provide a custom version comparator. // If you don't implement this method or return nil, the standard version comparator will be used. -- (id )versionComparatorForHost:(SUHost *)hb; +- (id )versionComparatorForUpdater:(SUUpdater *)updater; @end diff --git a/SUUpdater.m b/SUUpdater.m index b9659b7a..1984b5a6 100644 --- a/SUUpdater.m +++ b/SUUpdater.m @@ -82,9 +82,9 @@ static NSString *SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaultsObserv shouldPrompt = NO; } // Does the delegate want to take care of the logic for when we should ask permission to update? - else if ([delegate respondsToSelector:@selector(shouldPromptForPermissionToCheckForUpdatesToHostBundle:)]) + else if ([delegate respondsToSelector:@selector(updaterShouldPromptForPermissionToCheckForUpdates:)]) { - shouldPrompt = [delegate shouldPromptForPermissionToCheckForUpdatesToHost:host]; + shouldPrompt = [delegate updaterShouldPromptForPermissionToCheckForUpdates:self]; } // Has he been asked already? And don't ask if the host has a default value set in its Info.plist. else if ([host objectForUserDefaultsKey:SUEnableAutomaticChecksKey] == nil && @@ -175,7 +175,6 @@ static NSString *SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaultsObserv [NSException raise:@"SUNoFeedURL" format:@"You must specify the URL of the appcast as the SUFeedURLKey in either the Info.plist or the user defaults!"]; driver = [d retain]; - if ([driver delegate] == nil) { [driver setDelegate:delegate]; } [driver checkForUpdatesAtURL:[self feedURL] host:host]; } @@ -271,8 +270,8 @@ static NSString *SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaultsObserv // Determine all the parameters we're attaching to the base feed URL. BOOL sendingSystemProfile = ([host boolForUserDefaultsKey:SUSendProfileInfoKey] == YES); NSArray *parameters = [NSArray array]; - if ([delegate respondsToSelector:@selector(feedParametersForHostBundle:sendingSystemProfile:)]) - parameters = [parameters arrayByAddingObjectsFromArray:[delegate feedParametersForHost:host sendingSystemProfile:sendingSystemProfile]]; + if ([delegate respondsToSelector:@selector(feedParametersForUpdater:sendingSystemProfile:)]) + parameters = [parameters arrayByAddingObjectsFromArray:[delegate feedParametersForUpdater:self sendingSystemProfile:sendingSystemProfile]]; if (sendingSystemProfile) parameters = [parameters arrayByAddingObjectsFromArray:[host systemProfile]]; if (parameters == nil || [parameters count] == 0) { return baseFeedURL; } @@ -329,20 +328,7 @@ static NSString *SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaultsObserv return driver && ([driver finished] == NO); } -// Redirect driver delegate methods to our delegate -/* -- (void)appcastDidFinishLoading:(SUAppcast *)appcast forHost:(SUHost *)bundle -{ - if ([delegate respondsToSelector:@selector(appcastDidFinishLoading:forHost:)]) - [delegate appcastDidFinishLoading:ac forHost:bundle]; -} - -- (SUAppcastItem *)bestValidUpdateInAppcast:(SUAppcast *)appcast forHost:(SUHost *)bundle -{ - if ([delegate respondsToSelector:@selector(bestValidUpdateInAppcast:forHost:)]) // Does the delegate want to handle it? - return [delegate bestValidUpdateInAppcast:ac forHost:bundle]; - else - return nil; -}*/ +- delegate { return delegate; } +- (NSBundle *)hostBundle { return [host bundle]; } @end