From cec916695662f2bd238774529cf73bb2b74fdc9b Mon Sep 17 00:00:00 2001 From: Matt Stevens Date: Tue, 22 Jul 2008 20:24:12 -0600 Subject: [PATCH] Improved notification handling for otherwise UI-less background applications. --- SUBasicUpdateDriver.h | 2 ++ SUBasicUpdateDriver.m | 3 ++- SUUIBasedUpdateDriver.h | 2 ++ SUUIBasedUpdateDriver.m | 27 +++++++++++++++++++++------ SUUpdatePermissionPrompt.m | 7 ++++++- SUUserInitiatedUpdateDriver.m | 9 ++++++++- 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/SUBasicUpdateDriver.h b/SUBasicUpdateDriver.h index ddafed6c..74dd3aff 100644 --- a/SUBasicUpdateDriver.h +++ b/SUBasicUpdateDriver.h @@ -21,6 +21,8 @@ NSString *downloadPath; NSString *relaunchPath; + + BOOL isBackgroundApplication; } - (void)checkForUpdatesAtURL:(NSURL *)appcastURL hostBundle:(NSBundle *)hb; diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 21299159..f2ed18e5 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -14,6 +14,7 @@ - (void)checkForUpdatesAtURL:(NSURL *)appcastURL hostBundle:(NSBundle *)hb { hostBundle = [hb retain]; + isBackgroundApplication = ([[hostBundle objectForInfoDictionaryKey:@"LSUIElement"] doubleValue]); if ([hostBundle isRunningFromDiskImage]) { @@ -165,7 +166,7 @@ { // We don't want the download system to extract our gzips. // Note that we use a substring matching here instead of direct comparison because the docs say "application/gzip" but the system *uses* "application/x-gzip". This is a documentation bug. - return ([encodingType rangeOfString:@"gzip"].origin == NSNotFound); + return ([encodingType rangeOfString:@"gzip"].location == NSNotFound); } - (void)extractUpdate diff --git a/SUUIBasedUpdateDriver.h b/SUUIBasedUpdateDriver.h index d3459801..09a96472 100644 --- a/SUUIBasedUpdateDriver.h +++ b/SUUIBasedUpdateDriver.h @@ -18,6 +18,8 @@ SUUpdateAlert *updateAlert; } +- (void)showModalAlert:(NSAlert *)alert; + @end #endif diff --git a/SUUIBasedUpdateDriver.m b/SUUIBasedUpdateDriver.m index b7196dc0..19bdd9fe 100644 --- a/SUUIBasedUpdateDriver.m +++ b/SUUIBasedUpdateDriver.m @@ -16,8 +16,15 @@ updateAlert = [[SUUpdateAlert alloc] initWithAppcastItem:updateItem hostBundle:hostBundle]; [updateAlert setDelegate:self]; - // If the app is a menubar app or the like, we need to focus it first: - if ([[hostBundle objectForInfoDictionaryKey:@"LSUIElement"] doubleValue]) { [NSApp activateIgnoringOtherApps:YES]; } + // If the app is a menubar app or the like, we need to focus it first and alter the + // update prompt to behave like a normal window. Otherwise if the window were hidden + // there may be no way for the application to be activated to make it visible again. + if (isBackgroundApplication) + { + [[updateAlert window] setHidesOnDeactivate:NO]; + [[updateAlert window] setLevel:NSNormalWindowLevel]; + [NSApp activateIgnoringOtherApps:YES]; + } // Only show the update alert if the app is active; otherwise, we'll wait until it is. if ([NSApp isActive]) @@ -31,8 +38,7 @@ if ([delegate respondsToSelector:@selector(didNotFindUpdateToHostBundle:)]) [delegate didNotFindUpdateToHostBundle:hostBundle]; 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), [hostBundle name], [hostBundle displayVersion]]; - [alert setIcon:[hostBundle icon]]; - [alert runModal]; + [self showModalAlert:alert]; [self abortUpdate]; } @@ -138,8 +144,7 @@ - (void)abortUpdateWithError:(NSError *)error { NSAlert *alert = [NSAlert alertWithMessageText:SULocalizedString(@"Update Error!", nil) defaultButton:SULocalizedString(@"Cancel Update", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:[error localizedDescription]]; - [alert setIcon:[hostBundle icon]]; - [alert runModal]; + [self showModalAlert:alert]; [super abortUpdateWithError:error]; } @@ -153,4 +158,14 @@ [super abortUpdate]; } +- (void)showModalAlert:(NSAlert *)alert +{ + // When showing a modal alert we need to ensure that background applications + // are focused to inform the user since there is no dock icon to notify them. + if (isBackgroundApplication) { [NSApp activateIgnoringOtherApps:YES]; } + + [alert setIcon:[hostBundle icon]]; + [alert runModal]; +} + @end diff --git a/SUUpdatePermissionPrompt.m b/SUUpdatePermissionPrompt.m index b954d435..1a474c7c 100644 --- a/SUUpdatePermissionPrompt.m +++ b/SUUpdatePermissionPrompt.m @@ -32,7 +32,12 @@ + (void)promptWithHostBundle:(NSBundle *)hb delegate:(id)d { - id prompt = [[[self class] alloc] initWithHostBundle:hb delegate:d]; + // If this is a background application we need to focus it in order to bring the prompt + // to the user's attention. Otherwise the prompt would be hidden behind other applications and + // the user would not know why the application was paused. + if ([[hb objectForInfoDictionaryKey:@"LSUIElement"] doubleValue]) { [NSApp activateIgnoringOtherApps:YES]; } + + id prompt = [[[self class] alloc] initWithHostBundle:hb delegate:d]; [NSApp runModalForWindow:[prompt window]]; } diff --git a/SUUserInitiatedUpdateDriver.m b/SUUserInitiatedUpdateDriver.m index 5a019f0d..f0b60051 100644 --- a/SUUserInitiatedUpdateDriver.m +++ b/SUUserInitiatedUpdateDriver.m @@ -12,13 +12,20 @@ @implementation SUUserInitiatedUpdateDriver - (void)checkForUpdatesAtURL:(NSURL *)appcastURL hostBundle:(NSBundle *)hb -{ +{ checkingController = [[SUStatusController alloc] initWithHostBundle:hb]; [[checkingController window] center]; // Force the checking controller to load its window. [checkingController beginActionWithTitle:SULocalizedString(@"Checking for updates\u2026", nil) maxProgressValue:0 statusText:nil]; [checkingController setButtonTitle:SULocalizedString(@"Cancel", nil) target:self action:@selector(cancelCheckForUpdates:) isDefault:NO]; [checkingController showWindow:self]; [super checkForUpdatesAtURL:appcastURL hostBundle:hb]; + + // For background applications, obtain focus. + // Useful if the update check is requested from another app like System Preferences. + if (isBackgroundApplication) + { + [NSApp activateIgnoringOtherApps:YES]; + } } - (void)closeCheckingWindow