-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bigpond_Menu_BarAppDelegate.m
966 lines (837 loc) · 27.2 KB
/
Bigpond_Menu_BarAppDelegate.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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
//
// Bigpond_Menu_BarAppDelegate.m
// Bigpond Menu Bar
//
// Created by Anthony on 11/02/10.
// Copyright 2010 Anthony. All rights reserved.
//
/*
When user clicks "Deny" on keychain, don't try to login with a blank password.
*/
#import "Bigpond_Menu_BarAppDelegate.h"
@implementation BigpondUsageMeterDesktopAppDelegate
@synthesize
window, usedMeter, checkForUpdatesCheckingLabel,timeMeter, usedLabel,
progressIndicator,timeLabel,setShowModePercentageButton,updateWindow,
loginWindow,savePasswordCheckBox, percentOfMonthLabel, usernameField,
/*eckForUpdatesAtStartupCheckBo*/runAtStartupCheckBox,updateMenuItem,
versionLabel, setShowModeIconOnlyButton, userLabel,updatingIndicator,
passwordField, freeLabel, updatePeriodPopUp, downloadButton;
typedef struct tagUsageInfo{
unsigned long long download;
unsigned long long upload;
unsigned long long unmetered;
unsigned long long usage;
unsigned long long time;
} usageInfo;
usageInfo newest;
double updateReady = 0.0;
BOOL canceledUpdate = NO;
BOOL inConnection = NO;
long double currentmonthpercent = 0.0;
int kShowModePercentage = 0;
int kShowModeIconOnly = 1;
int percentage = 0;
BOOL debugMode = NO;
NSTimer * updateTimer;
NSString * kBigpondConnect;
NSString * password_non_keychain = @"";
//NSString *kUpdateServer = @"http://url3.tk/bpver.php";
NSString * kUpdateServerPage = @"http://aantthony.github.com/BigPond-Usage-Meter/";
NSString * kSoftwareLicencePage = @"http://aantthony.github.com/BigPond-Usage-Meter/licence.html";
NSString * kImageResourceDefaultIcon = @"bp";
NSString * kImageResourceFadedIcon = @"fade";
NSString * kImageResourceFailIcon = @"fail";
NSString * kPreferenceKeyNameUpdate = @"Update";
NSString * kPreferenceKeyNameShow = @"Show";
NSString * kPreferenceKeyNameDoNotSave = @"DoNotSave";
NSString * kPreferenceKeyNameUsername = @"Username";
NSString * kBundleVersionKeyName = @"CFBundleVersion";
NSString * kPreferenceKeyNamePeriod = @"Period";
NSString * kPreferenceKeyHasRunBefore = @"HasRunBefore";
char * kKeyChainserverName = "signon.bigpond.com";
UInt16 kKeyChainPort = 443;
char * kKeyChainPath = "";
//NSString *passwordCache=nil;
OSStatus kSecurityErrorNotFound = -25300;
UsageMeter* meter;
- (NSString*) getApplicationFilePath {
return [[NSBundle mainBundle] bundlePath];
}
- (NSString *) timeString{
return [[NSDate date]
descriptionWithCalendarFormat:@"%H:%M"
timeZone:nil
locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
}
CFIndex lastcheckStartupIndexFound=-1;
- (BOOL) doesRunAtStartup{
lastcheckStartupIndexFound=-1;
NSString *thisURL = [self getApplicationFilePath];
OSStatus err;
NSArray * items;
CFIndex itemCount;
items = NULL;
err = LIAECopyLoginItems((CFArrayRef*)&items);
if (err == noErr) {
itemCount = [items count];
for(int i=0;i<itemCount;i++){
NSDictionary *dict;
dict=[items objectAtIndex:i];
NSURL *url = [dict valueForKey:@"URL"];
NSString* str = [url path];
if([thisURL isEqualToString:str]){
lastcheckStartupIndexFound = i;
[items release];
return YES;
}
}
}
[items release];
return NO;
}
- (BOOL)getFSRef:(FSRef *)aFSRef forString:(NSString *)string{
return FSPathMakeRef((const UInt8 *)[string UTF8String], aFSRef,NULL) == noErr;
}
- (BOOL) createStartupEntry{
FSRef item;
if(![self getFSRef:&item forString:[self getApplicationFilePath]]){
[self alert:[NSString stringWithFormat:
@"Error - Couldn't set the app to run at startup. getFSRef:&item forString:%@",
[self getApplicationFilePath]]];
return NO;
}
Boolean hideIt=NO;
int err=0;
if(err=LIAEAddRefAtEnd(&item,hideIt)){
[self alert:[NSString stringWithFormat:
@"Error couldn't set the app to run at startup: LIAEAddRefAtEnd(&item,NO) returned %d"
,err]];
return NO;
}
return YES;
}
- (BOOL) deleteStartupEntry{
if([self doesRunAtStartup]){
if(lastcheckStartupIndexFound!=-1){
OSStatus err = LIAERemove(lastcheckStartupIndexFound);
if(err){
[self alert:[NSString stringWithFormat:
@"Unexpected error from: LIAERemove(%d) = %d",
lastcheckStartupIndexFound,
err]];
}
if([self doesRunAtStartup]){
[self alert:[NSString stringWithFormat:
@"Could not delete startup entry in Login Items. %@ %@(%d)",
@"Please go to Accounts in System Preferences, then click",
@"\"Login Items\" and delete the entry there.\n\nLIAERemove",
lastcheckStartupIndexFound]];
return NO;
}
}
}
return YES;
}
- (IBAction) setAtStartup:(id)sender{
if([runAtStartupCheckBox state]==NSOnState){
if(![self doesRunAtStartup]){
if(![self createStartupEntry]){
[runAtStartupCheckBox setState:NSOffState];
}
}
}else {
if(![self deleteStartupEntry]){
[runAtStartupCheckBox setState:NSOnState];
}
}
}
- (IBAction) loadPreferences:(id)sender{
if(![[NSUserDefaults standardUserDefaults] boolForKey:kPreferenceKeyHasRunBefore]){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kPreferenceKeyNameUpdate];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:kPreferenceKeyNameDoNotSave];
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:kPreferenceKeyNamePeriod];
[[NSUserDefaults standardUserDefaults] setInteger:kShowModePercentage forKey:kPreferenceKeyNameShow];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kPreferenceKeyHasRunBefore];
}
//BOOL SettingCheckForUpdatesAtStartup=[[NSUserDefaults standardUserDefaults] boolForKey:kPreferenceKeyNameUpdate];
BOOL SettingDoNotSavePasswordInKeyChainDataBase=[[NSUserDefaults standardUserDefaults]
boolForKey:kPreferenceKeyNameDoNotSave];
int SetttingUpdatePeriodIndexSetting=[[NSUserDefaults standardUserDefaults]
integerForKey:kPreferenceKeyNamePeriod];
int SettingShowModeIndexSetting=[[NSUserDefaults standardUserDefaults]
integerForKey:kPreferenceKeyNameShow];
[updatePeriodPopUp selectItemAtIndex:SetttingUpdatePeriodIndexSetting];
NSString* SettingUsernameOrEmailAccountForBigpondLogin=[[NSUserDefaults standardUserDefaults]
stringForKey:kPreferenceKeyNameUsername];
if(SettingUsernameOrEmailAccountForBigpondLogin==NULL){
SettingUsernameOrEmailAccountForBigpondLogin=@"";
}
if(SettingShowModeIndexSetting==kShowModeIconOnly){
[setShowModeIconOnlyButton setState:NSOnState];
[setShowModePercentageButton setState:NSOffState];
}else {
[setShowModeIconOnlyButton setState:NSOffState];
[setShowModePercentageButton setState:NSOnState];
}
[runAtStartupCheckBox setState:[self doesRunAtStartup]?NSOnState:NSOffState];
[versionLabel setStringValue:[NSString stringWithFormat:
@"Version: %@",
[[[NSBundle mainBundle] infoDictionary] objectForKey:kBundleVersionKeyName]]];
//[checkForUpdatesAtStartupCheckBox setState:(SettingCheckForUpdatesAtStartup?NSOnState:NSOffState)];
[savePasswordCheckBox setState:(SettingDoNotSavePasswordInKeyChainDataBase?NSOffState:NSOnState)];
[usernameField setStringValue:SettingUsernameOrEmailAccountForBigpondLogin];
if([SettingUsernameOrEmailAccountForBigpondLogin isEqualToString:@""]){
[userLabel setStringValue:@"<nobody>"];
}else {
[userLabel setStringValue:SettingUsernameOrEmailAccountForBigpondLogin];
}
[passwordField setStringValue:@""];
}
/*
- (IBAction) setCheckUpdatesAtStartup:(id)sender{
//Replaced by Sparkle
return;
//[[NSUserDefaults standardUserDefaults] setBool:([checkForUpdatesAtStartupCheckBox state]==NSOnState) forKey:kPreferenceKeyNameUpdate];
}*/
/*
- (BOOL) _docheckupdate:(id)sender{
//do not call this function directly, call update;
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
updateReady=0.0;
canceledUpdate=NO;
NSString *responseString;
NSURLResponse* resp;
NSError *error;
NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:kUpdateServer]];
NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&resp error:&error];
NSString *newest=@"0.0";
if(responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]){
newest=[NSString stringWithFormat:@"%@",responseString];
}
if((canceledUpdate==NO)
&& ([newest floatValue]>[[[[NSBundle mainBundle] infoDictionary]
objectForKey:kBundleVersionKeyName] floatValue])) {
updateReady=[newest floatValue];
}
[responseString release];
[self performSelectorOnMainThread:@selector(checkUpdateDone:)withObject:nil waitUntilDone:NO];
[autoreleasepool drain];
return YES;
}
*/
/*
- (IBAction) checkforupdates:(id)sender{
//replaced by sparkle
return;
[checkForUpdatesCheckingLabel setStringValue:@"Checking for updates..."];
[downloadButton setEnabled:NO];
[updateWindow makeKeyAndOrderFront:self];
[updatingIndicator startAnimation:self];
[self performSelectorInBackground:@selector(_docheckupdate:) withObject:self];
}*/
/*
- (IBAction) silentupdate:(id)sender{
//replaced by sparkle
return;
//[self performSelectorInBackground:@selector(_docheckupdate:) withObject:self];
}*/
/*
-(IBAction) cancelUpdate:(id)sender{
updateReady=0.0;
[self checkUpdateDone:self];
}
*/
- (IBAction) openDownloadPage:(id)sender{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:kUpdateServerPage]];
}
- (IBAction) openLicencePage:(id)sender{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:kSoftwareLicencePage]];
}
/*
-(void) checkUpdateDone:(id)sender{
if(updateReady!=0.0){
[updateWindow makeKeyAndOrderFront:self];
[checkForUpdatesCheckingLabel setStringValue:[NSString stringWithFormat:
@"New version (%0.01f) available for download!",
updateReady]];
[downloadButton setEnabled:YES];
}else {
[downloadButton setEnabled:NO];
[updateWindow close];
}
[updatingIndicator stopAnimation:self];
}
*/
-(BOOL) saveToKeyChain:(NSString *)password forUsername:(NSString *)username{
if([username isEqualToString:@""] || [password isEqualToString:@""]){
return NO;
}
//FIX THIS
/*if(passwordCache){
[passwordCache dealloc];
passwordCache=nil;
}*/
char * passwordData=(char *)[password UTF8String];
char * accountName=(char *)[username UTF8String];
SecKeychainItemRef itemRef;
OSStatus err = SecKeychainFindInternetPassword (
NULL,
strlen(kKeyChainserverName),
kKeyChainserverName,
0,
NULL,
strlen(accountName),
accountName,
strlen(kKeyChainPath),
kKeyChainPath,
kKeyChainPort,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
NULL,
NULL,
&itemRef
);
if(err==kSecurityErrorNotFound){
//the keychain item does not exist
err = SecKeychainAddInternetPassword (
NULL,
strlen(kKeyChainserverName),
kKeyChainserverName,
0,
NULL,
strlen(accountName),
accountName,
strlen(kKeyChainPath),
kKeyChainPath,
kKeyChainPort,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
strlen(passwordData),
passwordData,
NULL
);
if(err){
[self showSecurityError:err];
return NO;
}
return YES;
}
else if(err){
[self showSecurityError:err];
} else if(itemRef) {
err=SecKeychainItemModifyContent (
itemRef,
NULL,
strlen(passwordData),
passwordData
);
if(err){
[self showSecurityError:err];
return NO;
}
return YES;
}
return NO;
}
- (BOOL) showSecurityError:(OSStatus)err{
if(err){
if(err==errKCAuthFailed){
[self alert:@"Keychain Error: Please allow access to the keychain. The username and password are stored in the keychain."];
}
else {
NSString *msg = (NSString *)SecCopyErrorMessageString(err, NULL);
[self alert:[NSString stringWithFormat:@"Keychain Error: %@",msg]];
[msg release];
}
return YES;
}
return NO;
}
- (NSString *)getPasswordFromKeychainItem:(NSString *)username
{
if([username isEqualToString:@""]){
return nil;
}
UInt32 passwordLength;
char * passwordData;
char * accountName=(char *)[username UTF8String];
OSStatus err = SecKeychainFindInternetPassword (
NULL,
strlen(kKeyChainserverName),
kKeyChainserverName,
0,
NULL,
strlen(accountName),
accountName,
strlen(kKeyChainPath),
kKeyChainPath,
kKeyChainPort,
kSecProtocolTypeHTTPS,
kSecAuthenticationTypeHTMLForm,
&passwordLength,
(void**)&passwordData,
NULL
);
NSString *password=@"";
if(err==kSecurityErrorNotFound){
[self receiveError:@"kSecurityErrorNotFound: Line 431"];
[window makeKeyAndOrderFront:self];
[userLabel setStringValue:@"<nobody>"];
[self showLogin:self];
return nil;
//the keychain item does not exist
}
else if(err){
[self showSecurityError:err];
//September 5 10:41
return nil;
} else {
if(passwordData){
/*
This code is wrong because passwordData is not null terminated!
password=[NSString stringWithUTF8String:passwordData];
if(password==nil){
[self receiveError:@"Using deprecated stringWithCString instead of stringWithUTF8String. Will continue anyway."];
password=[NSString stringWithCString:passwordData];
if(password==nil){
[self receiveError:@"NSString stringWithCString is nil. line 451"];
[self receiveError:[NSString stringWithFormat:@"NSString stringWithCString is nil! line 453. password @ %d, passwordData @ %d. Will attempt to display passwordData with percent s.",password,passwordData]];
[self receiveError:[NSString stringWithFormat:@"passwordData: %s",passwordData]];
}
}
*/
NSString *releaseme=[[NSString alloc] initWithBytes:passwordData length:passwordLength encoding:NSUTF8StringEncoding];
password=[NSString stringWithString:releaseme];
[releaseme release];
SecKeychainItemFreeContent(NULL,passwordData);
}
}
//passwordCache=[[NSString stringWithString:password] retain];
return password;
}
- (NSString *) getPasswordForUsername:(NSString *)username{
if(username==nil){
return nil;
}
NSString *password=nil;
if([[NSUserDefaults standardUserDefaults] boolForKey:kPreferenceKeyNameDoNotSave]==YES){
password=password_non_keychain;
}
else {
password=[self getPasswordFromKeychainItem:username];
}
if([password isEqualToString:@""]){
return nil;
}
return password;
}
- (NSString *) getUsername{
NSString *username=[[NSUserDefaults standardUserDefaults] stringForKey:kPreferenceKeyNameUsername];
if(username==nil){
username=@"";
}
if([username isEqualToString:@""]){
[self receiveError:@"Please Login"];
return nil;
}
return username;
}
/*
- (NSString *) getCommandLine{
NSString *username=nil;
NSString *password=nil;
username=[[NSUserDefaults standardUserDefaults] stringForKey:kPreferenceKeyNameUsername];
if(username==nil){
return nil;
}
if([username isEqualToString:@""]){
return nil;
}
if([[NSUserDefaults standardUserDefaults] boolForKey:kPreferenceKeyNameDoNotSave]==YES){
password=password_non_keychain;
}
else {
password=[self getPasswordFromKeychainItem:username];
}
if([password isEqualToString:@""]){
return nil;
}
return [NSString stringWithFormat:
@"%@ %@ %@",
kBigpondConnect,
username,
[password stringByReplacingOccurrencesOfString:@" " withString:@"\\ "]
];
}
*/
- (IBAction) receiveError:(NSString *)errorString{
/*
@"Could Not Connect"
@"No Response"
@"Wrong Password"
@"No Data" //ignore
@"Empty Reply" //ignore
@"Error code" //ignore
@"Account Locked for 20 mins"
@"Wrong Response" //ignore
@"ERR%@"
*/
if(debugMode){
NSLog(@"\"%@\"",errorString);
}
NSString *error = [[errorString stringByReplacingOccurrencesOfString:@"\n" withString:@""] stringByReplacingOccurrencesOfString:@"\r" withString:@""];
[statusItem setImage:[NSImage imageNamed:kImageResourceFailIcon]];
if([error isEqualToString:@"Please Login"]){
[window makeKeyAndOrderFront:self];
[userLabel setStringValue:@"<nobody>"];
[self showLogin:self];
}else if([error isEqualToString:@"Could Not Connect: This computer’s Internet connection appears to be offline."]){
error=@"Connection appears to be offline";
[statusItem setImage:[NSImage imageNamed:kImageResourceFadedIcon]];
}
else if([error isEqualToString:@"Wrong Password"]){
[window makeKeyAndOrderFront:self];
//stop timer
[updateTimer invalidate];
[self showLogin:self];
}else if([error isEqualToString:@"Account Locked for 20 mins"]){
[window makeKeyAndOrderFront:self];
[self alert:
@"Your BigPond Account has been locked from accessing your usage data for 20 minutes after several unsuccessful password attempts."];
} else {
[self alert:[NSString stringWithFormat:
@"An unexpected error occured. %@",error]];
}
[self setStatusText:@""];
[freeLabel setStringValue:@""];
[usedLabel setStringValue:[NSString stringWithFormat:@"Error: %@",error]];
}
- (BOOL) alert:(NSString *)message{
//return NO;
[window makeKeyAndOrderFront:self];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
//[[self window] setAlphaValue:1.0];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:message];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:nil
contextInfo:nil];
return true;
}
UsageMeter *meter;
- (usageInfo) getInfoInternal{
// NO MEMORY LEAKS!!!
usageInfo new;
new.download=0;
new.upload=0;
new.unmetered=0;
new.usage=0;
new.time=0;
NSString *username=[self getUsername];
if(username==nil){
return new;
}
NSString *password=[self getPasswordForUsername:username];
if(password==nil){
if(debugMode){
NSLog(@"Password was equal to nil");
}
return new;
}
NSString *txt=[meter refresh:username withPassword:password];
if(![txt isEqualToString:@""])
{
[self receiveError:txt];
return new;
}
new.time=time(0);
new.usage=[meter usageMB];
new.download=[meter bandwMB];
unsigned long nnn=[meter billpday];
currentmonthpercent=100.0*(double)nnn/(double)30;
return new;
}
- (usageInfo) getInfo{
return [self getInfoInternal];
/*
usageInfo new;
new.download=0;
new.upload=0;
new.unmetered=0;
new.usage=0;
new.time=time(0);
NSString* cmdline=[self getCommandLine];
if(cmdline==nil)
{
NSString * errorString;
[self receiveError:errorString];
new.time=0;
return new;
}
FILE *s=popen([cmdline UTF8String],"r");
char line[512];
line[512]=0;
line[0]=0;
int lineid=0;
int good=1;
if(s){
while ( (lineid < 4) && (fgets( line, sizeof line, s))) {
if(!good){
break;
}
unsigned long long nnn;
switch (lineid) {
case 0:
if(line[0]<48 || line[0]>57){
good=0;
}
sscanf(line, "%llu",&new.usage);
break;
case 1:
sscanf(line, "%llu",&new.download);
break;
case 3:
sscanf(line, "%llu",&nnn);
currentmonthpercent=100.0*(double)nnn/(double)30;
break;
default:
break;
}
lineid++;
}
pclose(s);
}else {
good=0;
}
if(!good){
if(line[0]==0){
[self receiveError:@"Could Not Start Process"];
}else {
[self receiveError:[NSString stringWithUTF8String:line]];
}
new.time=0;//this signals that the entry is invalid.
return new;
}
return new;
*/
}
- (BOOL) _doupdate:(id)sender{
//do not call this function directly, call update;
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
usageInfo justthen=[self getInfo];
[autoreleasepool drain];
if(justthen.time){
newest=justthen;
[self performSelectorOnMainThread:@selector(workDone:)withObject:nil waitUntilDone:NO];
}else {
[self performSelectorOnMainThread:@selector(workFailed:)withObject:nil waitUntilDone:NO];
}
return YES;
}
- (IBAction) setPreferenceUpdatePeriod:(id)sender{
[[NSUserDefaults standardUserDefaults]
setInteger:[updatePeriodPopUp indexOfSelectedItem]
forKey:kPreferenceKeyNamePeriod];
[updateTimer invalidate];
[self configureTimer];
/*updateTimer=[[NSTimer scheduledTimerWithTimeInterval:[self getTimerPeriodInSeconds]
target: self
selector: @selector(update:)
userInfo: nil
repeats: YES] retain];*/
}
- (IBAction) update:(id)sender{
if(!inConnection){
if(![updateTimer isValid]){
//[updateTimer dealloc];
[self configureTimer];
/*updateTimer=[[NSTimer scheduledTimerWithTimeInterval: [self getTimerPeriodInSeconds]
target: self
selector: @selector(update:)
userInfo: nil
repeats: YES] retain];*/
}
[updateMenuItem setTitle:@"Updating..."];
[updateMenuItem setEnabled:NO];
inConnection=YES;
[progressIndicator startAnimation:self];
[self performSelectorInBackground:@selector(_doupdate:) withObject:self];
}
}
- (IBAction)complete:(id)sender{
[loginWindow orderOut:nil];
[NSApp endSheet:loginWindow];
NSString* login_username=[usernameField stringValue];
[userLabel setStringValue:login_username];
NSString* login_password=[passwordField stringValue];
if(![login_password isEqualToString:@""] && ![login_username isEqualToString:@""]){
[[NSUserDefaults standardUserDefaults]
setBool:([savePasswordCheckBox state]==NSOffState)
forKey:kPreferenceKeyNameDoNotSave];
if([[NSUserDefaults standardUserDefaults] boolForKey:kPreferenceKeyNameDoNotSave]==YES){
//Do not save the password in the keychain.
password_non_keychain=login_password;
}else {
password_non_keychain=@"";
[self saveToKeyChain:login_password forUsername:login_username];
}
[[NSUserDefaults standardUserDefaults] setValue:login_username forKey:kPreferenceKeyNameUsername];
[self update:self];
}else {
[self showLogin:self];
}
[statusItem setImage:[NSImage imageNamed:kImageResourceFadedIcon]];
}
- (IBAction)cancelOperation:(id)sender{
[loginWindow orderOut:nil];
[NSApp endSheet:loginWindow];
}
-(IBAction) cancelDialog:(id)sender{
}
-(IBAction) showLogin:(id)sender
{
NSString* SettingUsernameOrEmailAccountForBigpondLogin=[[NSUserDefaults standardUserDefaults]
stringForKey:kPreferenceKeyNameUsername];
if(SettingUsernameOrEmailAccountForBigpondLogin==NULL){
SettingUsernameOrEmailAccountForBigpondLogin=@"";
}
[savePasswordCheckBox setState:([[NSUserDefaults standardUserDefaults]
boolForKey:kPreferenceKeyNameDoNotSave]?NSOffState:NSOnState)];
[usernameField setStringValue:SettingUsernameOrEmailAccountForBigpondLogin];
[passwordField setStringValue:@""];
[NSApp beginSheet:loginWindow modalForWindow:window modalDelegate:self
didEndSelector:NULL contextInfo:nil];
[loginWindow makeFirstResponder:usernameField];
}
NSString *statusItemText=nil;
-(void) setStatusText:(NSString *)text{
if([[NSUserDefaults standardUserDefaults] integerForKey:kPreferenceKeyNameShow]==kShowModeIconOnly){
[statusItem setTitle:@""];
}else {
[statusItem setTitle:text];
}
}
- (IBAction) setShowModeIconOnly:(id)sender{
[[NSUserDefaults standardUserDefaults] setInteger:kShowModeIconOnly forKey:kPreferenceKeyNameShow];
[setShowModeIconOnlyButton setState:NSOnState];
[setShowModePercentageButton setState:NSOffState];
[self setStatusText:@""];
}
- (IBAction) setShowModePercentage:(id)sender{
[[NSUserDefaults standardUserDefaults] setInteger:kShowModePercentage forKey:kPreferenceKeyNameShow];
[setShowModeIconOnlyButton setState:NSOffState];
[setShowModePercentageButton setState:NSOnState];
[self setStatusText:[NSString stringWithFormat:@"%d%%",percentage ]];
}
-(void) workFailed:(id)sender{
inConnection=NO;
[progressIndicator stopAnimation:self];
[updateMenuItem setTitle:[NSString stringWithFormat:@"Update - Last Update Failed"]];
[updateMenuItem setEnabled:YES];
}
-(void) configureTimer{
//int interval=[self getTimerPeriodInSeconds];
[updateTimer invalidate];
[updateTimer release];
updateTimer=[[NSTimer scheduledTimerWithTimeInterval: [self getTimerPeriodInSeconds]
target: self
selector: @selector(update:)
userInfo: nil
repeats: YES] retain];
/*
[updateTimer initWithFireDate:[NSDate dateWithTimeIntervalSinceNow: 60.0]
interval:interval
target:self
selector:@selector(update:)
userInfo:nil
repeats:YES];
*/
}
-(void) workDone:(id)sender
{
if(newest.download){
[usedMeter setDoubleValue:(double)(newest.usage*100)/(double)newest.download];
[timeMeter setDoubleValue:(double)(currentmonthpercent)];
[usedLabel setStringValue:[NSString stringWithFormat:
@"%d MB (%d%%)",
newest.usage,
(int)(round(100.0*newest.usage/(double)newest.download))]];
//Memory Leak
percentage = (round(100.0*(double)newest.usage/(double)newest.download));
[self setStatusText:[NSString stringWithFormat:@"%d%%",percentage]];
[percentOfMonthLabel setStringValue:[NSString stringWithFormat:@"%d%%",(int)(currentmonthpercent)]];
double ccurrentmonthpercent=currentmonthpercent;
[timeLabel setStringValue:[NSString stringWithFormat:
@"%dd remain",
(signed int)(30.0-(ccurrentmonthpercent*30.0/100.0))]];
[freeLabel setStringValue:[NSString stringWithFormat:@"Free: %d MB",
(int)((double)newest.download-(double)newest.usage)]];
}
[statusItem setImage:[NSImage imageNamed:kImageResourceDefaultIcon]];
[progressIndicator stopAnimation:self];
inConnection=NO;
[updateMenuItem setTitle:[NSString stringWithFormat:@"Update Now - Last updated: %@",[self timeString]]];
[updateMenuItem setEnabled:YES];
}
/*
- (void) itIsTimeToCheckForUpdatesNow: (NSTimer *) timer{
[self silentupdate:self];
}
*/
- (int) getTimerPeriodInSeconds{
int SetttingUpdatePeriodIndexSetting=[[NSUserDefaults standardUserDefaults] integerForKey:kPreferenceKeyNamePeriod];
switch (SetttingUpdatePeriodIndexSetting) {
case 0:
return 20 MINUTES;
case 1:
return 30 MINUTES;
case 2:
return 60 MINUTES;
case 3:
return 128 MINUTES;
default:
return 30 MINUTES;
}
}
- (void) setup
{
meter= [[UsageMeter alloc] init];
[self loadPreferences:self];
//set timer to check for updates.
/*
if([[NSUserDefaults standardUserDefaults] boolForKey:kPreferenceKeyNameUpdate]){
[NSTimer scheduledTimerWithTimeInterval: 5 MINUTES
target: self
selector: @selector(itIsTimeToCheckForUpdatesNow:)
userInfo: nil
repeats: NO];
}
*/
updateTimer=[[NSTimer scheduledTimerWithTimeInterval: [self getTimerPeriodInSeconds]
target: self
selector: @selector(update:)
userInfo: nil
repeats: YES] retain];
//TODO: Check if ready. ie first run
[self update:self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[self setup];
}
-(void)awakeFromNib{
kBigpondConnect=[[[NSString stringWithFormat:
@"%@/Contents/Resources/bigpondconnect",
[self getApplicationFilePath]] stringByReplacingOccurrencesOfString:@" " withString:@"\\ "] retain];
//[self alert:kBigpondConnect];
newest.download=newest.upload=newest.unmetered=newest.usage=newest.time=0;
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setMenu:statusMenu];
[self setStatusText:@""];
[statusItem setImage:[NSImage imageNamed:kImageResourceFadedIcon]];
[statusItem setHighlightMode:YES];
}
@end