X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=OSX%2FUpdater.m;h=1bf29fa2e841d99fb9662125ae83b1c520978671;hb=refs%2Fheads%2Fmaster;hp=f7cd8c68097b572a15e2bdec8746e7d049972bdd;hpb=ffcd2e7e3da122dbba5c4188e05d3a63d0ede26e;p=xscreensaver diff --git a/OSX/Updater.m b/OSX/Updater.m index f7cd8c68..1bf29fa2 100644 --- a/OSX/Updater.m +++ b/OSX/Updater.m @@ -24,11 +24,103 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { - SUUpdater *updater = [SUUpdater updaterForBundle: - [NSBundle bundleForClass:[self class]]]; NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; [defs registerDefaults:UPDATER_DEFAULTS]; + // If it's not time to run the updater, then bail immediately. + // I'm not sure why this is necessary, but Sparkle seems to be + // checking too often. + // + if (! [self timeToCheck]) + [[NSApplication sharedApplication] terminate:self]; + + // If the screen saver is not running, then launch the updater now. + // Otherwise, wait until the screen saver deactivates, and then do + // it. This is because if the updater tries to pop up a dialog box + // while the screen saver is active, everything goes to hell and it + // never shows up. You'd expect the dialog to just map below the + // screen saver window, but no. + + if (! [self screenSaverActive]) { + [self runUpdater]; + } else { + // Run the updater when the "screensaver.didstop" notification arrives. + [[NSDistributedNotificationCenter defaultCenter] + addObserver:self + selector:@selector(saverStoppedNotification:) + name:@"com.apple.screensaver.didstop" + object:nil]; + + // But I'm not sure I trust that, so also poll every couple minutes. + timer = [NSTimer scheduledTimerWithTimeInterval: 60 * 2 + target:self + selector:@selector(pollSaverTermination:) + userInfo:nil + repeats:YES]; + } +} + + +- (BOOL) timeToCheck +{ + NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; + NSTimeInterval interval = [defs doubleForKey:@SUScheduledCheckIntervalKey]; + NSDate *last = [defs objectForKey:@SULastCheckTimeKey]; + if (!interval || !last) + return YES; + NSTimeInterval since = [[NSDate date] timeIntervalSinceDate:last]; + return (since > interval); +} + + +// Whether ScreenSaverEngine is currently running, meaning screen is blanked. +// There's no easy way to determine this other than scanning the process table. +// +- (BOOL) screenSaverActive +{ + BOOL found = NO; + NSString *target = @"/ScreenSaverEngine.app"; + ProcessSerialNumber psn = { kNoProcess, kNoProcess }; + while (GetNextProcess(&psn) == noErr) { + CFDictionaryRef cfdict = + ProcessInformationCopyDictionary (&psn, + kProcessDictionaryIncludeAllInformationMask); + if (cfdict) { + NSDictionary *dict = (NSDictionary *) cfdict; + NSString *path = [dict objectForKey:@"BundlePath"]; + if (path && [path hasSuffix:target]) + found = YES; + CFRelease (cfdict); + } + if (found) + break; + } + return found; +} + + +- (void) saverStoppedNotification:(NSNotification *)note +{ + [self runUpdater]; +} + + +- (void) pollSaverTermination:(NSTimer *)t +{ + if (! [self screenSaverActive]) + [self runUpdater]; +} + + +- (void) runUpdater +{ + if (timer) { + [timer invalidate]; + timer = nil; + } + + SUUpdater *updater = [SUUpdater updaterForBundle: + [NSBundle bundleForClass:[self class]]]; [updater setDelegate:self]; // Launch the updater thread. @@ -36,13 +128,15 @@ // Now we need to wait for the Sparkle thread to finish before we can // exit, so just poll waiting for it. + // [NSTimer scheduledTimerWithTimeInterval:1 target:self - selector:@selector(exitWhenDone:) + selector:@selector(pollUpdaterTermination:) userInfo:updater repeats:YES]; } + // Delegate method that lets us append extra info to the system-info URL. // - (NSArray *) feedParametersForUpdater:(SUUpdater *)updater @@ -50,10 +144,9 @@ { // Get the name of the saver that invoked us, and include that in the // system info. - NSString *saver = [[[NSProcessInfo - processInfo]environment]objectForKey: - @"XSCREENSAVER_CLASSPATH"]; - if (! saver) return nil; + NSString *saver = [[[NSProcessInfo processInfo] environment] + objectForKey:@"XSCREENSAVER_CLASSPATH"]; + if (! saver) return @[]; NSString *head = @"org.jwz.xscreensaver."; if ([saver hasPrefix:head]) saver = [saver substringFromIndex:[head length]]; @@ -67,13 +160,14 @@ } -- (void) exitWhenDone:(NSTimer *)timer +- (void) pollUpdaterTermination:(NSTimer *)t { - SUUpdater *updater = [timer userInfo]; + SUUpdater *updater = [t userInfo]; if (![updater updateInProgress]) [[NSApplication sharedApplication] terminate:self]; } + - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app { return YES;