-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathChazUpdate.m
327 lines (270 loc) · 8.15 KB
/
ChazUpdate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* The Cheat - The legendary universal game trainer for Mac OS X.
* http://www.brokenzipper.com/trac/wiki/TheCheat
*
* Copyright (c) 2003-2011, Charles McGarvey et al.
*
* Distributable under the terms and conditions of the 2-clause BSD
* license; see the file COPYING for the legal text of the license.
*/
#import "ChazUpdate.h"
// an NSError that an have an error statement associated with it
@interface ChazUpdateError : NSError
{
NSString *_errorString;
}
+ (id)errorWithString:(NSString *)string;
- (id)initWithString:(NSString *)string;
- (void)setErrorString:(NSString *)string;
- (NSString *)localizedDescription;
@end
@implementation ChazUpdateError
+ (id)errorWithString:(NSString *)string
{
return [[[ChazUpdateError alloc] initWithString:string] autorelease];
}
- (id)initWithString:(NSString *)string
{
if ( self = [super initWithDomain:@"ChazUpdateError" code:1 userInfo:nil] ) {
[self setErrorString:string];
}
return self;
}
- (void)setErrorString:(NSString *)string
{
[string retain];
[_errorString release];
_errorString = string;
}
- (NSString *)localizedDescription
{
return _errorString;
}
@end
@interface ChazUpdate : NSObject
{
NSString *_name;
NSURLConnection *_connection;
NSHTTPURLResponse *_response;
NSMutableData *_responseData;
BOOL _verbose;
}
- (id)initWithURL:(id)url name:(NSString *)name verbose:(BOOL)verbose;
- (void)kill;
@end
@implementation ChazUpdate
#pragma mark Initialization
- (id)initWithURL:(id)url name:(NSString *)name verbose:(BOOL)verbose
{
NSURL *theURL;
if ( self = [super init] )
{
// figure out what type of URL was passed
if ( [url isKindOfClass:[NSURL class]] ) {
theURL = url;
}
else if ( [url isKindOfClass:[NSString class]] ) {
theURL = [[[NSURL alloc] initWithString:url] autorelease];
}
else {
if ( verbose ) {
NSRunAlertPanel( @"Update Check Failed",
@"Could not determine the location of the version information file.", @"OK", nil, nil );
}
[self release];
return nil;
}
// save the name
if ( name ) {
_name = [name retain];
}
else {
_name = [[NSString stringWithString:@""] retain];
}
_verbose = verbose;
// create the request
_connection = [[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:theURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30.0] delegate:self] retain];
_response = nil;
_responseData = [[NSMutableData alloc] init];
}
return self;
}
- (void)dealloc
{
// cleanup
[_name release];
[_connection release];
[_response release];
[_responseData release];
[super dealloc];
}
- (void)kill
{
[self release];
self = nil;
}
#pragma mark NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if ( _response ) {
// another response, chop the data
[_responseData setLength:0];
[_response release];
}
_response = [response retain];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// just save the data
[_responseData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *plistDictionary;
NSPropertyListFormat plistFormat;
NSString *plistError;
NSDictionary *appDictionary;
NSString *appRedirect;
NSString *appVersion;
NSString *appOnline;
NSDate *appReleaseDate;
NSString *appDownload;
NSString *appDescription;
// read in the recently downloaded plist
plistDictionary = [NSPropertyListSerialization propertyListFromData:_responseData
mutabilityOption:NSPropertyListImmutable
format:&plistFormat
errorDescription:&plistError];
if ( !plistDictionary ) {
[self connection:connection didFailWithError:[ChazUpdateError errorWithString:plistError]];
return;
}
// make sure the root object is a dictionary
if ( ![NSStringFromClass([plistDictionary class]) isEqualToString:@"NSCFDictionary"] ) {
[self connection:connection didFailWithError:[ChazUpdateError errorWithString:@"the version information file is ill-formatted"]];
return;
}
appDictionary = [plistDictionary objectForKey:_name];
if ( !appDictionary ) {
[self connection:connection didFailWithError:[ChazUpdateError errorWithString:@"no version information for this application"]];
return;
}
// see if the version information for this app is in another file
if ( appRedirect = [appDictionary objectForKey:@"Redirect"] ) {
// recursively follow the redirection
[[ChazUpdate alloc] initWithURL:appRedirect name:_name verbose:_verbose];
[self kill];
return;
}
// read other useful information about the app
appVersion = [appDictionary objectForKey:@"Version"];
appReleaseDate = [NSDate dateWithNaturalLanguageString:[appDictionary objectForKey:@"ReleaseDate"]];
appOnline = [appDictionary objectForKey:@"MoreInfoURL"];
appDownload = [appDictionary objectForKey:@"DownloadURL"];
appDescription = [appDictionary objectForKey:@"Description"];
if ( !appReleaseDate ) {
[self connection:connection didFailWithError:[ChazUpdateError errorWithString:@"missing version information"]];
return;
}
if ( !appVersion ) {
appVersion = @"";
}
if ( [ChazAppBuildDate() compare:appReleaseDate] == NSOrderedAscending ) {
// there is a new version, display message
int choice;
NSString *desc;
NSString *mainBtn;
NSString *alternate;
NSString *other;
if ( appDescription ) {
desc = appDescription;
}
else {
desc = [NSString stringWithFormat:@"A new version of %@ is available! Would you like to learn more about the new version now?", _name];
}
if ( appOnline ) {
mainBtn = @"Learn More";
other = @"Cancel";
}
else {
mainBtn = @"OK";
other = nil;
}
if ( appDownload ) {
alternate = @"Download";
}
else {
alternate = nil;
}
choice = NSRunInformationalAlertPanel( [NSString stringWithFormat:@"New Version: %@ %@", _name, appVersion],
desc, mainBtn, alternate, other );
if ( choice == NSAlertDefaultReturn && appOnline ) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:appOnline]];
}
else if ( choice == NSAlertAlternateReturn ) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:appDownload]];
}
}
else if ( _verbose ) {
// alert that the user has the latest version if we're in verbose mode
NSRunInformationalAlertPanel( [NSString stringWithFormat:@"%@ %@", _name, ChazAppVersion()],
@"You have the latest version of %@.", @"OK", nil, nil, _name );
}
[self kill];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
if ( _verbose )
{
// check failed, display an error
NSRunAlertPanel( @"Update Check Failed", @"Check could not get new version data.\nError code #%i (%@)", @"OK", nil, nil, [error code], [error localizedDescription] );
}
[self kill];
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
{
// handle http redirection
return request;
}
@end
#pragma mark API Functions
void ChazCheckForUpdate( id url, BOOL userRequested )
{
[[ChazUpdate alloc] initWithURL:url name:ChazAppName() verbose:userRequested];
}
void ChazCheckForUpdateWithName( id url, NSString *appName, BOOL userRequested )
{
[[ChazUpdate alloc] initWithURL:url name:appName verbose:userRequested];
}
NSString *ChazAppName()
{
NSString *name;
name = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"];
if ( name && ![name isEqualToString:@""] ) {
return name;
}
name = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
if ( name && ![name isEqualToString:@""] ) {
return name;
}
return @"";
}
NSString *ChazAppVersion()
{
NSString *vers;
vers = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
if ( vers && ![vers isEqualToString:@""] ) {
return vers;
}
vers = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
if ( vers && ![vers isEqualToString:@""] ) {
return vers;
}
return @"";
}
NSDate *ChazAppBuildDate()
{
return [NSDate dateWithNaturalLanguageString:[NSString stringWithFormat:@"%s %s", __TIME__, __DATE__]];
}