From http://www.jwz.org/xscreensaver/xscreensaver-5.25.tar.gz
[xscreensaver] / OSX / Updater.m
1 /* xscreensaver, Copyright (c) 2013 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  *
11  * XScreenSaverUpdater.app -- downloads and installs XScreenSaver updates
12  * via Sparkle.framework.
13  *
14  * Created: 7-Dec-2013
15  *
16  * NOTE: This does not work with Sparkle 1.5b6 -- it requires the "HEAD"
17  *       version 4-Dec-2013 or later.
18  */
19
20 #import "Updater.h"
21 #import "Sparkle/SUUpdater.h"
22
23 @implementation XScreenSaverUpdater : NSObject
24
25 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
26 {
27   SUUpdater *updater = [SUUpdater updaterForBundle:
28                                     [NSBundle bundleForClass:[self class]]];
29   NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
30   [defs registerDefaults:UPDATER_DEFAULTS];
31
32   [updater setDelegate:self];
33
34   // Launch the updater thread.
35   [updater checkForUpdatesInBackground];
36
37   // Now we need to wait for the Sparkle thread to finish before we can
38   // exit, so just poll waiting for it.
39   [NSTimer scheduledTimerWithTimeInterval:1
40            target:self
41            selector:@selector(exitWhenDone:)
42            userInfo:updater
43            repeats:YES];
44 }
45
46 // Delegate method that lets us append extra info to the system-info URL.
47 //
48 - (NSArray *) feedParametersForUpdater:(SUUpdater *)updater
49                   sendingSystemProfile:(BOOL)sending
50 {
51   // Get the name of the saver that invoked us, and include that in the
52   // system info.
53   NSString *saver = [[[NSProcessInfo 
54                         processInfo]environment]objectForKey:
55                         @"XSCREENSAVER_CLASSPATH"];
56   if (! saver) return nil;
57   NSString *head = @"org.jwz.xscreensaver.";
58   if ([saver hasPrefix:head])
59     saver = [saver substringFromIndex:[head length]];
60
61   return @[ @{ @"key":          @"saver",
62                @"value":        saver,
63                @"displayKey":   @"Current Saver",
64                @"displayValue": saver
65              }
66           ];
67 }
68
69
70 - (void) exitWhenDone:(NSTimer *)timer
71 {
72   SUUpdater *updater = [timer userInfo];
73   if (![updater updateInProgress])
74     [[NSApplication sharedApplication] terminate:self];
75 }
76
77 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app
78 {
79   return YES;
80 }
81
82 @end