forked from voidref/orangered
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrangeredAppDelegate.m
827 lines (692 loc) · 25.3 KB
/
OrangeredAppDelegate.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
//
// OrangeredAppDelegate.m
// Orangered
//
// Created by voidref on 6/16/10.
// Copyright 2010 Voidref Software. All rights reserved.
//
#import "OrangeredAppDelegate.h"
#import "Foundation/NSURLConnection.h"
// I can't believe this is working.
@interface NSMenuItem (hiddenpropcat)
@property BOOL hidden;
@end
@implementation OrangeredAppDelegate
@synthesize loginWindow, userentry, passwordentry, savepassword, loginerror, loginProgress;
@synthesize prefWindow, openAtLoginCB, logDiagnosticsCB, autoUpdateCheckCB, redditCheckIntervalTF, appUpdateCheckProgress, appUpdateResultTF;
@synthesize aboutWindow, versionTF, aboutEnvelope, creditsTF, sloganTF, logoTF;
@synthesize status;
@synthesize menu;
@synthesize update;
@synthesize about;
@synthesize currentIcon;
@synthesize noMailIcon;
@synthesize prefs;
static NSString* GreyEnvelope = @"GreyEnvelope";
static NSString* BlackEnvelope = @"BlackEnvelope";
static NSString* BlueEnvelope = @"BlueEnvelope";
static NSString* OrangeredEnvelope = @"OrangeredEnvelope";
static NSString* HighlightEnvelope = @"HighlightEnvelope";
static NSString* ModMailIcon = @"modmail";
// eventually we will use the version string in the info.plist.
static const int AppUpdatePollInterval = (60 * 60 * 24); // 1 day
// Sadly a macro seems the easiest way to do this right now...
#define OrangeLog1(x) if (true == self.prefs.logDiagnostics) { NSLog(x); }
#define OrangeLog(x, y) if (true == self.prefs.logDiagnostics) { NSLog(x, y); }
// --------------------------------------------------------------------------------------------------------------------
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// We don't use this. Must appease the warning gods.
#pragma unused(aNotification)
#if GROWL
[GrowlApplicationBridge setGrowlDelegate: self];
[self registrationDictionaryForGrowl];
#endif
self.prefs = [[Prefs alloc] init];
[self setLoadAtStartup];
hasModMail = NO;
statusData = nil;
loginData = nil;
appUpdateData = nil;
updatePoller = nil;
self.versionTF.stringValue = [NSString stringWithFormat:@"Version %@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
self.creditsTF.stringValue = @"written by Alan Westbrook (voidref)\n\nSpecial Thanks to the following redditors:\n"
"ashleyw\n"
"Condawg\n"
"dawnerd\n"
"despideme\n"
"derekaw\n"
"EthicalReasoning\n"
"giftedmunchkin\n"
"kevinhoagland\n"
"loggedout\n"
"polyGone\n"
"RamenStein\n"
"shinratdr\n"
"sporadicmonster\n";
[self.creditsTF setHidden:YES];
self.loginWindow.collectionBehavior = NSWindowCollectionBehaviorCanJoinAllSpaces;
self.aboutWindow.collectionBehavior = NSWindowCollectionBehaviorCanJoinAllSpaces;
self.prefWindow.collectionBehavior = NSWindowCollectionBehaviorCanJoinAllSpaces;
self.status = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
self.status.menu = self.menu;
self.status.highlightMode = YES;
self.status.alternateImage = [NSImage imageNamed:HighlightEnvelope];
[self setMessageStatus: GreyEnvelope];
self.menu.delegate = self;
self.menu.autoenablesItems = NO;
self.currentIcon = GreyEnvelope;
self.noMailIcon = BlackEnvelope;
// detect first run / empty username
// We have to have an account name in order to check status!
if (nil == prefs.name)
{
[self showLoginWindow:nil];
}
else
{
[self updateStatus:nil];
}
[self setupPollers];
}
// -------------------------------------------------------------------------------------------------------------------
- (void) setupPollers
{
NSInteger interval = self.prefs.redditCheckInterval * 60;
if (60 > interval)
{
interval = 60;
}
[statusPoller invalidate];
[statusPoller release];
statusPoller = [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(updateStatus:)
userInfo:nil
repeats:YES];
// App update poller.
if (YES == self.prefs.autoUpdateCheck)
{
if (nil == updatePoller)
{
updatePoller = [NSTimer scheduledTimerWithTimeInterval:AppUpdatePollInterval
target:self
selector:@selector(checkForAppUpdate:)
userInfo:nil
repeats:YES];
}
}
else if (nil != updatePoller)
{
[updatePoller invalidate];
[updatePoller release];
updatePoller = nil;
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) dealloc
{
[prefs release];
[statusConnection release];
[loginConnection release];
[appUpdateConnection release];
[statusData release];
[loginData release];
[appUpdateData release];
[statusPoller release];
[updatePoller release];
[super dealloc];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) loginChanged:(id)sender
{
#pragma unused(sender)
NSString* uname = [userentry stringValue];
NSString* pword = [passwordentry stringValue];
if ((uname.length < 1) || (pword.length < 1))
{
self.loginerror.stringValue = @"Username and passwrord are required";
return;
}
else
{
self.loginerror.stringValue = @"";
}
self.prefs.name = uname;
self.prefs.savePassword = ([savepassword state] == NSOnState);
self.prefs.password = pword;
[self login];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) login
{
if ((self.prefs.name.length < 1) || (self.prefs.password.length < 1))
{
// show window
[self showLoginWindow:nil];
return;
}
[self.loginProgress startAnimation:nil];
[self.loginProgress setHidden:NO];
OrangeLog(@"Logging in user: %@", self.prefs.name);
NSURL* url = [NSURL URLWithString:@"http://www.reddit.com/api/login"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:self.prefs.timeout];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [[NSString stringWithFormat:@"user=%@&passwd=%@", self.prefs.name, self.prefs.password] dataUsingEncoding:NSUTF8StringEncoding]];
[loginConnection release];
loginConnection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (nil != loginConnection)
{
loginData = [[NSMutableData data] retain];
}
else
{
// Is there a way to find the exact error?
self.loginerror.stringValue = @"Could not estabilsh connection to reddit.";
OrangeLog(@"login error: %@", self.loginerror.stringValue);
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) parseLogin:(NSHTTPURLResponse*) response
{
[self.loginProgress stopAnimation:nil];
[self.loginProgress setHidden:YES];
OrangeLog(@"Response Headers: %@", [response allHeaderFields]);
NSArray* cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields]
forURL:[response URL]];
if (cookies.count == 0)
{
// set a flag for error check?
}
else
{
OrangeLog(@"Setting Cookie Array: %@", cookies);
NSHTTPCookieStorage* cstorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cstorage setCookies:cookies
forURL:[NSURL URLWithString:@"http://www.reddit.com/"]
mainDocumentURL:nil];
self.loginerror.stringValue = @"";
[self.loginWindow close];
[self updateStatus:nil];
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) updateStatus: (NSTimer*)theTimer
{
#pragma unused(theTimer)
OrangeLog1(@"Updating status");
NSURL* url = [NSURL URLWithString:[self userDataUrl]];
NSURLRequest* request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:self.prefs.timeout];
[statusConnection release];
statusConnection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (nil != statusConnection)
{
if (nil == statusData)
{
statusData = [[NSMutableData data] retain];
}
}
else
{
// Is there a way to find the exact error?
self.loginerror.stringValue = @"Could not estabilsh connection to reddit";
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) parseStatus
{
NSString* statusResult = [[[NSString alloc] initWithData:statusData
encoding:NSASCIIStringEncoding] autorelease];
if ([statusResult rangeOfString:@"\"has_mail\": true"].location != NSNotFound )
{
self.loginerror.stringValue = @"";
self.currentIcon = OrangeredEnvelope;
[self growlAlert:@"You've recieved a new message on reddit"
title:@"New reddit message"
type:@"message"];
}
else if ([statusResult rangeOfString:@"\"has_mail\": null"].location != NSNotFound )
{
// We are no longer logged in for some reason.
static int failcounter = 0;
++failcounter;
if ( failcounter > 1 )
{
OrangeLog1(@"failed to log in twice in one update.");
failcounter = 0;
return;
}
OrangeLog1(@"Status Update failed due to not being logged in, attempting re-login");
// Not sure this will actually do anything as we login sync and block the main thread here.
self.currentIcon = GreyEnvelope;
[self setMessageStatus: self.currentIcon];
// Try to log in.
[self login];
// Reset counter after trying a second log in.
--failcounter;
// login as already called us again on success
// Ok, this logic is convoluted, need to fix that.
return;
}
else if ([statusResult rangeOfString:@"\"has_mail\": false"].location != NSNotFound )
{
self.currentIcon = self.noMailIcon;
}
// Mod mail overrides all
if ([statusResult rangeOfString:@"\"has_mod_mail\": true"].location != NSNotFound)
{
self.currentIcon = ModMailIcon;
hasModMail = YES;
}
else
{
hasModMail = NO;
}
OrangeLog(@"CheckResult: %@", statusResult);
[self setMessageStatus: self.currentIcon];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) showLoginWindow:(id)sender
{
#pragma unused(sender)
[savepassword setState:self.prefs.savePassword];
if (nil != self.prefs.name) [userentry setStringValue:self.prefs.name];
if (nil != self.prefs.password) [passwordentry setStringValue:self.prefs.password];
self.appUpdateResultTF.stringValue = @"";
// open window and force to the front
[NSApp activateIgnoringOtherApps:YES];
[self.loginWindow makeKeyAndOrderFront:nil];
[self.loginWindow orderFrontRegardless];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) showPrefsWindow: (id)sender
{
#pragma unused(sender)
[self.autoUpdateCheckCB setState: self.prefs.autoUpdateCheck];
[self.logDiagnosticsCB setState:self.prefs.logDiagnostics];
[self.openAtLoginCB setState: self.prefs.openAtLogin];
[self.redditCheckIntervalTF setStringValue: [NSString stringWithFormat:@"%d", self.prefs.redditCheckInterval]];
self.appUpdateResultTF.stringValue = @"";
// open window and force to the front
[NSApp activateIgnoringOtherApps:YES];
[prefWindow makeKeyAndOrderFront:nil];
[prefWindow orderFrontRegardless];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) donePrefsWindow: (id)sender
{
#pragma unused(sender)
NSInteger minutes = [[self.redditCheckIntervalTF stringValue] integerValue];
// It seems I can't get the validator/formatter to work right, blea.
if (1 > minutes)
{
minutes = 1;
}
bool adjustPollers = false;
if (minutes != self.prefs.redditCheckInterval)
{
self.prefs.redditCheckInterval = minutes;
adjustPollers = true;
}
if (self.prefs.autoUpdateCheck != (BOOL)[self.autoUpdateCheckCB state])
{
self.prefs.autoUpdateCheck = (BOOL)[self.autoUpdateCheckCB state];
adjustPollers = true;
}
if (true == adjustPollers)
{
[self setupPollers];
}
self.prefs.logDiagnostics = (BOOL)[self.logDiagnosticsCB state];
[prefWindow close];
}
// --------------------------------------------------------------------------------------------------------------------
- (NSString*) userDataUrl
{
return [NSString stringWithFormat:@"http://www.reddit.com/user/%@/about.json", self.prefs.name];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) openMailbox:(id)sender
{
#pragma unused(sender)
// Why didn't I do this with enums?
if (NO == hasModMail)
{
if (self.currentIcon == self.noMailIcon)
{
system("open http://www.reddit.com/message/inbox/ &");
}
else
{
system("open http://www.reddit.com/message/unread/ &");
}
}
else
{
system("open http://www.reddit.com/message/moderator/ &");
hasModMail = NO;
}
// Lets assume they don't want to see the modified envelope after they do this or wait for the next check.
self.currentIcon = self.noMailIcon;
[self setMessageStatus: self.currentIcon];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) updateMenuItemClicked: (id)sender
{
(void)sender;
self.appUpdateResultTF.stringValue = @"";
self.update.hidden = YES;
self.about.hidden = NO;
system("open http://www.voidref.com/Site/Orangered.dmg &");
self.noMailIcon = BlackEnvelope;
// We can do this because we probably will not be checking again.
[self updateStatus:nil];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) checkForAppUpdate: (id)sender
{
#pragma unused(sender)
if (NO == self.prefs.autoUpdateCheck) return;
[self.appUpdateCheckProgress startAnimation:nil];
[self.appUpdateCheckProgress setHidden:NO];
self.appUpdateResultTF.stringValue = @"Checking for update...";
NSURL* url = [NSURL URLWithString:@"http://www.voidref.com/Site/orangered_version.txt"];
NSURLRequest* request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:self.prefs.timeout];
[appUpdateConnection release];
appUpdateConnection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (nil != appUpdateConnection)
{
if (nil == appUpdateData)
{
appUpdateData = [[NSMutableData data] retain];
}
}
else
{
// Is there a way to find the exact error?
self.appUpdateResultTF.stringValue = @"Could not estabilsh connection to Orangered! update server.";
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) parseAppUpdateResult
{
NSString* checkResult = [[[NSString alloc] initWithData:appUpdateData
encoding:NSASCIIStringEncoding] autorelease];
NSString* currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
if ([checkResult compare:currentVersion] != NSOrderedSame)
{
self.appUpdateResultTF.stringValue = [NSString stringWithFormat:@"New version available: %@", checkResult];
self.update.hidden = NO;
self.update.title = [NSString stringWithFormat:@"Get Update (%@)", checkResult];
self.noMailIcon = BlueEnvelope;
[self setMessageStatus: self.noMailIcon];
self.about.hidden = YES;
}
else
{
self.appUpdateResultTF.stringValue = [NSString stringWithFormat:@"Orangered! is up to date, Version: %@", currentVersion];
}
[self.appUpdateCheckProgress stopAnimation:nil];
[self.appUpdateCheckProgress setHidden:YES];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) automaticCheckForUpdateClicked: (id)sender
{
self.prefs.autoUpdateCheck = ([sender state] == NSOnState);
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) loadAtStartupClicked: (id)sender
{
self.prefs.openAtLogin = ([sender state] == NSOnState);
[self setLoadAtStartup];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) showAboutWindow: (id)sender
{
#pragma unused(sender)
[self.logoTF setHidden:NO];
[self.sloganTF setHidden:NO];
[self.versionTF setHidden:NO];
[self.aboutEnvelope setHidden:NO];
[self.creditsTF setHidden:YES];
// open window and force to the front
[NSApp activateIgnoringOtherApps:YES];
[aboutWindow makeKeyAndOrderFront:nil];
[aboutWindow orderFrontRegardless];
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) showAboutButtonClicked: (id)sender
{
#pragma unused(sender)
OrangeLog(@"Button: %@", [sender title]);
NSString* url = nil;
switch ([sender tag])
{
case 0:
url = @"http://www.voidref.com/Site/Orangered!.html";
break;
case 1:
url = @"http://www.reddit.com/r/Orangered_app/";
break;
case 2:
url = @"http://www.github.com/voidref/orangered/";
break;
}
if (nil != url)
{
NSString* command = [NSString stringWithFormat:@"open %@ &", url];
system([command UTF8String]);
}
}
// --------------------------------------------------------------------------------------------------------------------
- (IBAction) aboutEnvelopeClicked: (id)sender
{
#pragma unused(sender)
[self.logoTF setHidden:YES];
[self.sloganTF setHidden:YES];
[self.versionTF setHidden:YES];
[self.aboutEnvelope setHidden:YES];
[self.creditsTF setHidden:NO];
}
// --------------------------------------------------------------------------------------------------------------------
- (void) setLoadAtStartup
{
BOOL exists = NO;
NSURL* thePath = [[NSBundle mainBundle] bundleURL];
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems)
{
UInt32 seedValue = 0;
CFArrayRef loginItemsArrayRef = LSSharedFileListCopySnapshot(loginItems, &seedValue);
NSArray* loginItemsArray = (NSArray *)loginItemsArrayRef;
LSSharedFileListItemRef removeItem;
for (id item in loginItemsArray)
{
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
CFURLRef URL = NULL;
if (LSSharedFileListItemResolve(itemRef, 0, &URL, NULL) == noErr)
{
if ([[[(NSURL *)URL path] lastPathComponent] isEqualToString: [[thePath path] lastPathComponent]])
{
exists = YES;
CFRelease(URL);
removeItem = (LSSharedFileListItemRef)item;
break;
}
}
}
CFRelease(loginItemsArrayRef);
BOOL add = self.prefs.openAtLogin;
if (add && !exists)
{
OrangeLog1(@"Adding to startup items.");
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,
kLSSharedFileListItemBeforeFirst,
NULL,
NULL,
(CFURLRef)thePath,
NULL,
NULL);
if (item) CFRelease(item);
}
else if (!add && exists)
{
OrangeLog1(@"Removing from startup items.");
LSSharedFileListItemRemove(loginItems, removeItem);
}
CFRelease(loginItems);
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) setMessageStatus: (NSString*) imageName
{
OrangeLog(@"Setting Status image to: %@", imageName);
self.status.image = [NSImage imageNamed:imageName];
}
#pragma mark NSURLConnection delegate interface
// --------------------------------------------------------------------------------------------------------------------
- (void) connection: (NSURLConnection *)connection
didFailWithError: (NSError *)error
{
OrangeLog(@"connection Error: %@", error);
[self.loginProgress stopAnimation:nil];
[self.loginProgress setHidden:YES];
if ( connection == statusConnection)
{
self.loginerror.stringValue = [NSString stringWithFormat:@"Unable to retrieve status: %@", [error localizedDescription]];
[self setMessageStatus: GreyEnvelope];
}
else if (connection == loginConnection)
{
self.loginerror.stringValue = [NSString stringWithFormat:@"Unable to login: %@", [error localizedDescription]];
[self setMessageStatus: GreyEnvelope];
}
else if (connection == appUpdateConnection)
{
// I bet nobody cares
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) connection: (NSURLConnection *)connection
didReceiveData: (NSData *)data
{
if ( connection == statusConnection)
{
[statusData appendData:data];
}
else if (connection == loginConnection)
{
[loginData appendData:data];
}
else if (connection == appUpdateConnection)
{
[appUpdateData appendData:data];
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) connection: (NSURLConnection *)connection
didReceiveResponse: (NSURLResponse *)response
{
OrangeLog(@"Got response for %@", [[response URL] path]);
// Here we zero out the data to prepare it to accept new data
if ( connection == statusConnection)
{
statusData.length = 0;
}
else if (connection == loginConnection)
{
// Is casting this way the right thing to do?
[self parseLogin:(NSHTTPURLResponse*)response];
loginData.length = 0;
}
else if (connection == appUpdateConnection)
{
appUpdateData.length = 0;
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) connection: (NSURLConnection *)connection
didSendBodyData: (NSInteger)bytesWritten
totalBytesWritten: (NSInteger)totalBytesWritten
totalBytesExpectedToWrite: (NSInteger)totalBytesExpectedToWrite
{
#pragma unused(connection, bytesWritten)
if (totalBytesWritten != totalBytesExpectedToWrite)
{
[self.loginProgress stopAnimation:nil];
[self.loginProgress setHidden:YES];
self.loginerror.stringValue = @"Could not complete login request, connection severed (I think).";
}
}
// --------------------------------------------------------------------------------------------------------------------
- (void) connectionDidFinishLoading: (NSURLConnection *)connection
{
if ( connection == statusConnection)
{
[self parseStatus];
}
else if (connection == loginConnection)
{
NSString* output = [[[NSString alloc] initWithData:loginData
encoding:NSASCIIStringEncoding] autorelease];
OrangeLog(@"Data result: %@", output);
if ([output rangeOfString:@"WRONG_PASSWORD"].location != NSNotFound)
{
self.loginerror.stringValue = @"Could not log in: Wrong password.";
}
else
{
// hmmm.
}
}
else if (connection == appUpdateConnection)
{
[self parseAppUpdateResult];
}
}
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// dawnerd's basic growl integration
#if GROWL
- (NSDictionary *) registrationDictionaryForGrowl
{ /* Only implement this method if you do not plan on just placing a plist with the same data in your app bundle (see growl documentation) */
NSArray *array = [NSArray arrayWithObjects:@"message", @"error", nil]; /* each string represents a notification name that will be valid for us to use in alert methods */
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1], /* growl 0.7 through growl 1.1 use ticket version 1 */
@"TicketVersion", /* Required key in dictionary */
array, /* defines which notification names our application can use, we defined example and error above */
@"AllNotifications", /*Required key in dictionary */
array, /* using the same array sets all notification names on by default */
@"DefaultNotifications", /* Required key in dictionary */
nil];
return dict;
}
#endif
// --------------------------------------------------------------------------------------------------------------------
-(void) growlAlert:(NSString *)message
title:(NSString *)title
type:(NSString *)type
{
#pragma unused(message, title, type)
#if GROWL
[GrowlApplicationBridge notifyWithTitle:title /* notifyWithTitle is a required parameter */
description:message /* description is a required parameter */
notificationName:type /* notification name is a required parameter, and must exist in the dictionary we registered with growl */
iconData:nil /* not required, growl defaults to using the application icon, only needed if you want to specify an icon. */
priority:0 /* how high of priority the alert is, 0 is default */
isSticky:NO /* indicates if we want the alert to stay on screen till clicked */
clickContext:nil]; /* click context is the method we want called when the alert is clicked, nil for none */
#endif
}
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
@end