| 1 |
#import "UpdateController.h" |
|---|
| 2 |
|
|---|
| 3 |
#include "SSHKeychain_Prefix.pch" |
|---|
| 4 |
|
|---|
| 5 |
UpdateController *sharedUpdateController; |
|---|
| 6 |
|
|---|
| 7 |
/* This function resides in Controller.m. */ |
|---|
| 8 |
extern NSString *local(NSString *theString); |
|---|
| 9 |
|
|---|
| 10 |
@implementation UpdateController |
|---|
| 11 |
|
|---|
| 12 |
- (id)init |
|---|
| 13 |
{ |
|---|
| 14 |
if(!(self = [super init])) |
|---|
| 15 |
{ |
|---|
| 16 |
return nil; |
|---|
| 17 |
} |
|---|
| 18 |
|
|---|
| 19 |
sharedUpdateController = self; |
|---|
| 20 |
|
|---|
| 21 |
return self; |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
+ (UpdateController *)sharedController |
|---|
| 25 |
{ |
|---|
| 26 |
if(!sharedUpdateController) |
|---|
| 27 |
{ |
|---|
| 28 |
return [[UpdateController alloc] init]; |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
return sharedUpdateController; |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
- (void)checkForUpdatesWithWarnings:(BOOL)warnings |
|---|
| 35 |
{ |
|---|
| 36 |
[NSThread detachNewThreadSelector:@selector(_checkForUpdatesWithWarnings:) toTarget:self withObject:[NSNumber numberWithBool:warnings]]; |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
- (void)_checkForUpdatesWithWarnings:(NSNumber *)warnings |
|---|
| 40 |
{ |
|---|
| 41 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 42 |
NSString *versionPath = [[NSBundle mainBundle] pathForResource:@"version" ofType:@"plist"]; |
|---|
| 43 |
NSDictionary *versionDict = [NSDictionary dictionaryWithContentsOfFile:versionPath]; |
|---|
| 44 |
NSString *remoteVersionURL = [versionDict objectForKey:@"RemoteVersionURL"]; |
|---|
| 45 |
NSDictionary *remoteVersionInfo; |
|---|
| 46 |
|
|---|
| 47 |
remoteVersionInfo = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:remoteVersionURL]]; |
|---|
| 48 |
|
|---|
| 49 |
NSMethodSignature *methodSig = [self methodSignatureForSelector:@selector(_processVersionInfo:withWarnings:)]; |
|---|
| 50 |
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig]; |
|---|
| 51 |
[invocation setSelector:@selector(_processVersionInfo:withWarnings:)]; |
|---|
| 52 |
[invocation setArgument:&remoteVersionInfo atIndex:2]; |
|---|
| 53 |
[invocation setArgument:&warnings atIndex:3]; |
|---|
| 54 |
[invocation retainArguments]; |
|---|
| 55 |
[invocation performSelectorOnMainThread:@selector(invokeWithTarget:) withObject:self waitUntilDone:NO]; |
|---|
| 56 |
|
|---|
| 57 |
[pool release]; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
- (void)_processVersionInfo:(NSDictionary *)remoteVersionInfo withWarnings:(NSNumber *)warnings |
|---|
| 61 |
{ |
|---|
| 62 |
NSString *latestVersion, *currentVersion; |
|---|
| 63 |
NSURL *downloadURL, *changesURL; |
|---|
| 64 |
int r; |
|---|
| 65 |
|
|---|
| 66 |
if(!remoteVersionInfo) |
|---|
| 67 |
{ |
|---|
| 68 |
if([warnings boolValue] == YES) |
|---|
| 69 |
{ |
|---|
| 70 |
[self warningPanelWithTitle:local(@"CheckForUpdates") |
|---|
| 71 |
andMessage:local(@"FailedToRetrieveXMLVersionInfo")]; |
|---|
| 72 |
} |
|---|
| 73 |
return; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
latestVersion = [remoteVersionInfo objectForKey:@"version"]; |
|---|
| 77 |
downloadURL = [NSURL URLWithString:[remoteVersionInfo objectForKey:@"downloadURL"]]; |
|---|
| 78 |
changesURL = [NSURL URLWithString:[remoteVersionInfo objectForKey:@"changesURL"]]; |
|---|
| 79 |
|
|---|
| 80 |
currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; |
|---|
| 81 |
|
|---|
| 82 |
if(currentVersion == nil) |
|---|
| 83 |
{ |
|---|
| 84 |
if([warnings boolValue] == YES) |
|---|
| 85 |
{ |
|---|
| 86 |
[self warningPanelWithTitle:local(@"CheckForUpdates") |
|---|
| 87 |
andMessage:local(@"Can'tFigureOutOwnVersion")]; |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
else if ([latestVersion compare:currentVersion] == NSOrderedDescending) |
|---|
| 92 |
{ |
|---|
| 93 |
if((downloadURL) && (changesURL)) |
|---|
| 94 |
{ |
|---|
| 95 |
[NSApp requestUserAttention:NSCriticalRequest]; |
|---|
| 96 |
[NSApp activateIgnoringOtherApps:YES]; |
|---|
| 97 |
r = NSRunAlertPanel(local(@"NewVersion"), local(@"NewVersionAvailable"), local(@"Download"), local(@"Cancel"), local(@"Changes")); |
|---|
| 98 |
|
|---|
| 99 |
if(r == NSAlertDefaultReturn) |
|---|
| 100 |
{ |
|---|
| 101 |
[[NSWorkspace sharedWorkspace] openURL:downloadURL]; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
else if(r == NSAlertOtherReturn) |
|---|
| 105 |
{ |
|---|
| 106 |
[[NSWorkspace sharedWorkspace] openURL:changesURL]; |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
else |
|---|
| 111 |
{ |
|---|
| 112 |
[self warningPanelWithTitle:local(@"NewVersion") |
|---|
| 113 |
andMessage:local(@"NewVersionAvailable")]; |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
else if([warnings boolValue] == YES) |
|---|
| 118 |
{ |
|---|
| 119 |
[self warningPanelWithTitle:local(@"NewVersion") andMessage:local(@"NoNewVersion")]; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
return; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
- (void)warningPanelWithTitle:(NSString *)title andMessage:(NSString *)message |
|---|
| 126 |
{ |
|---|
| 127 |
[NSApp activateIgnoringOtherApps:YES]; |
|---|
| 128 |
NSRunAlertPanel(title, message, nil, nil, nil); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
@end |
|---|