-
Notifications
You must be signed in to change notification settings - Fork 0
/
MacPADSocket.m
398 lines (353 loc) · 12.8 KB
/
MacPADSocket.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
// MacPADSocket.m
// MacPAD Version Check
//
// Created by Kevin Ballard on Sun Dec 07 2003.
// Copyright (c) 2003-2004 TildeSoft. All rights reserved.
//
#import "MacPADSocket.h"
// Constant strings
NSString *MacPADErrorCode = @"MacPADErrorCode";
NSString *MacPADErrorMessage = @"MacPADErrorMessage";
NSString *MacPADNewVersionAvailable = @"MacPADNewVersionAvailable";
// NSNotifications
NSString *MacPADErrorOccurredNotification = @"MacPADErrorOccurredNotification";
NSString *MacPADCheckFinishedNotification = @"MacPADCheckFinishedNotification";
enum {
kNumberType,
kStringType,
kPeriodType
};
@implementation MacPADSocket
// Code
- (id)init
{
if (self = [super init]) {
_fileHandle = nil;
_fileURL = nil;
_currentVersion = nil;
_newVersion = nil;
_releaseNotes = nil;
_productPageURL = nil;
_productDownloadURLs = nil;
_buffer = nil;
}
return self;
}
- (void)initiateCheck:(id)sender;
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:_fileURL];
[self processDictionary:dict];
[pool release];
}
- (void)performCheck:(NSURL *)url withVersion:(NSString *)version
{
// Make sure we were actually *given* stuff
if (url == nil || version == nil) {
// Bah
[self returnError:kMacPADResultMissingValues message:@"URL or version was nil"];
return;
}
// Save the current version and URL
[_currentVersion release];
_currentVersion = [version copy];
[_fileURL release];
_fileURL = [url copy];
[NSThread detachNewThreadSelector:@selector(initiateCheck:) toTarget:self withObject:nil];
}
- (void)performCheckWithVersion:(NSString *)version
{
// This method makes use of the MacPAD.url file inside the application bundle
// If this file isn't there, or it's not in the correct format, this will return
// error kMacPADResultMissingValues with an appropriate message
// If it is there, it calls performCheck:withVersion: with the URL
NSString *path = [[NSBundle mainBundle] pathForResource:@"MacPAD" ofType:@"url"];
if (path == nil) {
// File is missing
[self returnError:kMacPADResultMissingValues message:@"MacPAD.url file was not found"];
return;
}
NSError * error;
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (contents == nil) {
// The file can't be opened
NSLog(@"Got error %@ trying to open the MacPAD url file", error);
[self returnError:kMacPADResultMissingValues message:@"The MacPAD.url file can't be opened"];
return;
}
NSString *urlString;
NSRange range = [contents rangeOfString:@"URL="];
if (range.location != NSNotFound) {
// We have a URL= prefix
range.location += range.length;
range.length = [contents length] - range.location;
urlString = [contents substringWithRange:range];
} else {
// The file is the URL
urlString = contents;
}
// Strip whitespace
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Perform the check
[self performCheck:[NSURL URLWithString:urlString] withVersion:version];
}
- (void)performCheckWithURL:(NSURL *)url
{
// Gets the version from the Info.plist file and calls performCheck:withVersion:
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
[self performCheck:url withVersion:version];
}
- (void)performCheck
{
// Gets the version from the Info.plist file and calls performCheckWithVersion:
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
[self performCheckWithVersion:version];
}
- (void)setDelegate:(id)delegate
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
if (_delegate != nil) {
// Unregister with the notification center
[nc removeObserver:_delegate name:MacPADErrorOccurredNotification object:self];
[nc removeObserver:_delegate name:MacPADCheckFinishedNotification object:self];
[_delegate autorelease];
}
_delegate = [delegate retain];
// Register the new MacPADSocketNotification methods for the delegate
// Only register if the delegate implements it, though
if ([_delegate respondsToSelector:@selector(macPADErrorOccurred:)]) {
[nc addObserver:_delegate selector:@selector(macPADErrorOccurred:)
name:MacPADErrorOccurredNotification object:self];
}
if ([_delegate respondsToSelector:@selector(macPADCheckFinished:)]) {
[nc addObserver:_delegate selector:@selector(macPADCheckFinished:)
name:MacPADCheckFinishedNotification object:self];
}
}
- (NSString *)releaseNotes
{
if (_releaseNotes == nil) {
return @"";
} else {
return [[_releaseNotes copy] autorelease];
}
}
- (NSString *)newVersion
{
if (_newVersion == nil) {
return @"";
} else {
return [[_newVersion copy] autorelease];
}
}
- (NSString *)productPageURL
{
if (_productPageURL == nil) {
return @"";
} else {
return [[_productPageURL copy] autorelease];
}
}
- (NSString *)productDownloadURL
{
if (_productDownloadURLs != nil && [_productDownloadURLs count] >= 1) {
return [_productDownloadURLs objectAtIndex:0];
} else {
return @"";
}
}
- (NSArray *)productDownloadURLs
{
if (_productDownloadURLs == nil) {
return [NSArray array];
} else {
return [[_productDownloadURLs copy] autorelease];
}
}
- (void)returnError:(MacPADResultCode)code message:(NSString *)msg
{
NSNumber *yesno = [NSNumber numberWithBool:(code == kMacPADResultNewVersion)];
NSNumber *errorCode = [NSNumber numberWithInt:code];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:yesno, MacPADNewVersionAvailable,
msg, MacPADErrorMessage, errorCode, MacPADErrorCode, nil];
if (code == 0 || code == 5) {
// Not an error
[self performSelectorOnMainThread:@selector(returnSuccess:) withObject:userInfo waitUntilDone:NO];
} else {
// It's an error
[self performSelectorOnMainThread:@selector(returnFailure:) withObject:userInfo waitUntilDone:NO];
}
}
- (void)returnSuccess:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:MacPADCheckFinishedNotification
object:self userInfo:userInfo];
}
- (void)returnFailure:(NSDictionary *)userInfo
{
[[NSNotificationCenter defaultCenter] postNotificationName:MacPADErrorOccurredNotification
object:self userInfo:userInfo];
}
- (void)processDictionary:(NSDictionary *)dict
{
if (dict == nil) {
[self returnError:kMacPADResultInvalidURL message:@"Remote file or URL was invalid"];
return;
}
_newVersion = [[dict objectForKey:@"productVersion"] copy];
if (_newVersion == nil) {
// File is missing version information
[self returnError:kMacPADResultBadSyntax message:@"Product version information missing"];
return;
}
// Get release notes
_releaseNotes = [[dict objectForKey:@"productReleaseNotes"] copy];
// Get product page URL
_productPageURL = [[dict objectForKey:@"productPageURL"] copy];
// Get the first product download URL
_productDownloadURLs = [[dict objectForKey:@"productDownloadURL"] copy];
// Compare versions
if ([self compareVersion:_newVersion toVersion:_currentVersion] == NSOrderedAscending) {
// It's a new version
[self returnError:kMacPADResultNewVersion message:@"New version available"];
} else {
[self returnError:kMacPADResultNoNewVersion message:@"No new version available"];
}
// We're done
}
- (void)dealloc
{
// Unregister the delegate with the notification center
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:_delegate name:MacPADErrorOccurredNotification object:self];
[nc removeObserver:_delegate name:MacPADCheckFinishedNotification object:self];
[nc removeObserver:self];
// Release objects
[_delegate release];
[_fileHandle release];
[_currentVersion release];
[_buffer release];
[_newVersion release];
[_releaseNotes release];
[_productPageURL release];
[_productDownloadURLs release];
[super dealloc];
}
- (NSComparisonResult)compareVersion:(NSString *)versionA toVersion:(NSString *)versionB
{
NSArray *partsA = [self splitVersion:versionA];
NSArray *partsB = [self splitVersion:versionB];
NSString *partA, *partB;
int i, typeA, typeB, intA, intB;
NSUInteger n = MIN([partsA count], [partsB count]);
for (i = 0; i < n; ++i) {
partA = [partsA objectAtIndex:i];
partB = [partsB objectAtIndex:i];
typeA = [self getCharType:partA];
typeB = [self getCharType:partB];
// Compare types
if (typeA == typeB) {
// Same type; we can compare
if (typeA == kNumberType) {
intA = [partA intValue];
intB = [partB intValue];
if (intA > intB) {
return NSOrderedAscending;
} else if (intA < intB) {
return NSOrderedDescending;
}
} else if (typeA == kStringType) {
NSComparisonResult result = [partA compare:partB];
if (result != NSOrderedSame) {
return result;
}
}
} else {
// Not the same type? Now we have to do some validity checking
if (typeA != kStringType && typeB == kStringType) {
// typeA wins
return NSOrderedAscending;
} else if (typeA == kStringType && typeB != kStringType) {
// typeB wins
return NSOrderedDescending;
} else {
// One is a number and the other is a period. The period is invalid
if (typeA == kNumberType) {
return NSOrderedAscending;
} else {
return NSOrderedDescending;
}
}
}
}
// The versions are equal up to the point where they both still have parts
// Lets check to see if one is larger than the other
if ([partsA count] != [partsB count]) {
// Yep. Lets get the next part of the larger
// n holds the value we want
NSString *missingPart;
int missingType, shorterResult, largerResult;
if ([partsA count] > [partsB count]) {
missingPart = [partsA objectAtIndex:n];
shorterResult = NSOrderedDescending;
largerResult = NSOrderedAscending;
} else {
missingPart = [partsB objectAtIndex:n];
shorterResult = NSOrderedAscending;
largerResult = NSOrderedDescending;
}
missingType = [self getCharType:missingPart];
// Check the type
if (missingType == kStringType) {
// It's a string. Shorter version wins
return shorterResult;
} else {
// It's a number/period. Larger version wins
return largerResult;
}
}
// The 2 strings are identical
return NSOrderedSame;
}
- (NSArray *)splitVersion:(NSString *)version
{
NSString *character;
NSMutableString *s;
int i, oldType, newType;
NSMutableArray *parts = [NSMutableArray array];
if ([version length] == 0) {
// Nothing to do here
return parts;
}
s = [[[version substringToIndex:1] mutableCopy] autorelease];
oldType = [self getCharType:s];
NSUInteger n = [version length] - 1;
for (i = 1; i <= n; ++i) {
character = [version substringWithRange:NSMakeRange(i, 1)];
newType = [self getCharType:character];
if (oldType != newType || oldType == kPeriodType) {
// We've reached a new segment
[parts addObject:[s copy]];
[s setString:character];
} else {
// Add character to string and continue
[s appendString:character];
}
oldType = newType;
}
// Add the last part onto the array
[parts addObject:[s copy]];
return parts;
}
- (int)getCharType:(NSString *)character
{
if ([character isEqualToString:@"."]) {
return kPeriodType;
} else if ([character isEqualToString:@"0"] || [character intValue] != 0) {
return kNumberType;
} else {
return kStringType;
}
}
@end