-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacBiff.m
1325 lines (1092 loc) · 34 KB
/
MacBiff.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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: MacBiff.m 181 2012-02-12 18:39:45Z lhagan $
*
* Copyright (c) 2004 Branden J. Moore.
*
* This file is part of MacBiff, and is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* MacBiff is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with MacBiff; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
*/
#ifdef USE_GROWL
#undef USE_GROWL
#endif
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#ifdef USE_GROWL
#include "Growl.framework/Headers/GrowlApplicationBridge.h"
#endif
#import "MacBiff.h"
#if 0
#define EBUG 1
#endif
#include "activity.h"
#include "debug.h"
#include "imap.h"
#include "comms.h"
#include "version.h"
volatile sig_atomic_t user_pressed_stop = 0;
static MacBiff * macBiff;
#ifdef USE_GROWL
static NSString *appName = @"MacBiff";
static NSString *newMailNotificationName = @"New Mail";
//these are keys into the Localizable.strings. there are two to handle plurals.
static NSString *growlDescriptionFormats[] = {
@"Growl notification description (one message)",
@"Growl notification description (multiple messages, one folder)",
@"Growl notification description (multiple messages, multiple folders)",
};
#endif
static void sigUSR1( int sig )
{
/* this trick allows a another process (say, fetchmail) that has no
* connection to the window server to tell macBiff to check the mail
* status "now". With that you can even set the email check delay to
* several hours, since it's the mail fetching process that schedule
* the checking.
*/
if (macBiff) {
[macBiff performSelectorOnMainThread:@selector(refresh:)
withObject: macBiff
waitUntilDone: NO];
}
}
static void sigUSR2( int sig )
{
dprintf("Received SIGUSR2\n");
alert("Stopping Check. Received SIGUSR2\n");
user_pressed_stop = 1;
}
@implementation MacBiff
- (IBAction) checknow: (id) sender
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
CheckNow = ![actWin isOpen];
if ( [prefs boolForKey: @"Show Activity"] ) {
[actWin performSelectorOnMainThread: @selector(display:)
withObject: self
waitUntilDone: YES];
}
[self refresh: self];
}
- (IBAction) stopcheck: (id) sender
{
dprintf("User pressed STOP\n");
if ( checking_thread ) {
pthread_kill( checking_thread, SIGUSR2 );
} else {
kill( getpid(), SIGUSR2 );
}
}
- (id) init
{
if ( self = [super init] ) {
goRed = NO;
CheckNow = NO;
mainMenu = Nil;
checkStatus = Nil;
lock = [[NSLock alloc] init];
timer = Nil;
servers = [[NSMutableArray alloc] initWithCapacity:5];
ICcurServer = -1;
macBiff = self;
struct sigaction usr1act, usr2act;
memset(&usr1act, 0, sizeof(struct sigaction));
memset(&usr2act, 0, sizeof(struct sigaction));
sigemptyset(&(usr1act.sa_mask));
sigemptyset(&(usr2act.sa_mask));
usr1act.sa_handler = sigUSR1;
usr1act.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &usr1act, NULL);
usr2act.sa_handler = sigUSR2;
sigaction(SIGUSR2, &usr2act, NULL);
#ifdef USE_GROWL
NSImage *myIcon = [NSImage imageNamed:appName];
iconData = [[myIcon TIFFRepresentation] retain];
notificationNames = [[NSArray alloc] initWithObjects:
newMailNotificationName, nil];
// commented out old Growl notification code lh 2009-01-12
//
/*//register with Growl.
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
appName, GROWL_APP_NAME,
iconData, GROWL_APP_ICON,
notificationNames, GROWL_NOTIFICATIONS_DEFAULT,
notificationNames, GROWL_NOTIFICATIONS_ALL,
nil];
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:GROWL_APP_REGISTRATION
object:nil
userInfo:userInfo];*/
[GrowlApplicationBridge setGrowlDelegate:self];
#endif
}
return (self);
}
- (void) dealloc
{
[mainMenu release];
mainMenu = nil;
[systemBar release];
systemBar = nil;
[lock unlock];
[lock release];
lock = nil;
[timer invalidate];
[timer release];
timer = nil;
[servers removeAllObjects];
[servers release];
servers = nil;
[iconData release];
[notificationNames release];
[super dealloc];
}
- (void) awakeFromNib
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[CserverTbl setTarget: self];
[CserverTbl setDoubleAction: @selector(editServer:)];
if ( ![prefs integerForKey: @"Server Count"] ) {
[self registerDefaultPreferences];
[self openPrefs: self];
} else {
[self loadServers];
[self setupMenuBar];
if ( [prefs integerForKey: @"checkDelay"] >= 1 ) {
timer = [NSTimer scheduledTimerWithTimeInterval:
60 * [prefs integerForKey: @"checkDelay"]
target: self
selector: @selector (refresh:)
userInfo: self
repeats: YES];
/* Check Now */
[timer fire];
}
}
}
- (void)applicationDidFinishLaunching
{
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
[[workspace notificationCenter] addObserver: self
selector: @selector(wakeUp:)
name: NSWorkspaceDidWakeNotification
object: workspace];
}
/*
* About Box Control
*/
- (IBAction)openAbout:(id)sender
{
[MacBiffVersion setStringValue:
[NSString stringWithFormat: @"MacBiff version %s",
VERSION]];
[aboutWindow makeKeyAndOrderFront: self];
[NSApp activateIgnoringOtherApps: YES];
}
- (IBAction)canelAbout:(id)sender
{
[aboutWindow close];
}
- (IBAction)openURL:(id)sender
{
[[NSWorkspace sharedWorkspace]
openURL: [NSURL URLWithString:
@"http://www.forkit.org/macbiff/macbiff.php"]];
}
/*
* Preferences Control
*/
- (NSArray*) getSounds
{
NSSet *aiffSet = [NSSet setWithObject: @"aiff"];
NSMutableArray *soundNames = [[NSMutableArray alloc]
initWithCapacity: 15];
/* Generate listing of all library directories */
NSArray *tarray = NSSearchPathForDirectoriesInDomains(
NSLibraryDirectory,
NSAllDomainsMask,
YES );
NSEnumerator *libEnum = [tarray objectEnumerator];
NSString *libPath;
NSDirectoryEnumerator *dirEnum;
NSString *fp;
NSString *sp;
while ( (libPath = [libEnum nextObject]) ) {
/* Append 'Sounds' to the library path */
sp = [libPath stringByAppendingFormat: @"/%@", @"Sounds"];
dirEnum = [[NSFileManager defaultManager]
enumeratorAtPath: sp];
while ( (fp = [dirEnum nextObject]) ) {
if ( [aiffSet containsObject: [fp pathExtension]] ) {
[soundNames addObject:
[[[sp stringByAppendingFormat: @"/%@", fp]
stringByDeletingPathExtension]
lastPathComponent]];
}
}
}
return (soundNames);
}
- (IBAction)openPrefs:(id)sender
{
NSArray *sounds;
int i;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// int delay = [prefs integerForKey: @"checkDelay"];
int delay = (int)[prefs integerForKey: @"checkDelay"];
if ( delay > 0 ) {
[CdelayText setIntValue: delay];
[CdelayStep setIntValue: delay];
} else {
[CdelayText setIntValue: 1];
[CdelayStep setIntValue: 1];
}
[CcountIgnores setState:
([prefs boolForKey: @"Ignore Ignores"]) ?
NSControlStateValueOff : NSControlStateValueOn];
[CcheckHeaders setState:
([prefs boolForKey: @"Fetch Unread Headers"]) ?
NSControlStateValueOn : NSControlStateValueOff];
[CcheckIgnHeaders setState:
([prefs boolForKey: @"Fetch Ignored Headers"]) ?
NSControlStateValueOn : NSControlStateValueOff];
[CchColorBut setState:
([prefs boolForKey: @"Alert Color"]) ?
NSControlStateValueOn : NSControlStateValueOff];
[CdoSoundBut setState:
([prefs boolForKey: @"Alert Sound"]) ?
NSControlStateValueOn : NSControlStateValueOff];
[CuseGrowl setState:
([prefs boolForKey: @"Notify with Growl"]) ?
NSControlStateValueOn : NSControlStateValueOff];
#ifndef USE_GROWL
//this version of MacBiff was not compiled with Growl support.
//disable the checkbox to toggle Growl notifications, and set the tool-tip
// to inform the user of the binary's lack of the Growl nature.
[CuseGrowl setEnabled: NO];
[CuseGrowl setToolTip: @"This version of MacBiff does not support Growl."];
#endif
[CshowText setState:
(![prefs boolForKey: @"Hide Text"]) ?
NSControlStateValueOn : NSControlStateValueOff];
[CshowTotBut setState:
(![prefs boolForKey: @"Hide Total"]) ?
NSControlStateValueOn : NSControlStateValueOff];
[CshowBrackets setState:
(![prefs boolForKey: @"Hide Brackets"]) ?
NSControlStateValueOn : NSControlStateValueOff];
[CshowIcon setState:
([prefs boolForKey: @"Show Icon"]) ?
NSControlStateValueOn : NSControlStateValueOff];
[CchColorBut setEnabled: ([CshowText state] == NSControlStateValueOn) ];
[CshowTotBut setEnabled: ([CshowText state] == NSControlStateValueOn) ];
[CshowBrackets setEnabled: ([CshowText state] == NSControlStateValueOn) ];
[CsoundChoicePop setEnabled: ([CdoSoundBut state] == NSControlStateValueOn) ];
[CcheckIgnHeaders setEnabled: ([CcheckHeaders state] == NSControlStateValueOn) ];
[CshowActivity setState:
([prefs boolForKey: @"Show Activity"]) ?
NSControlStateValueOn : NSControlStateValueOff];
/* Look at sound */
[CsoundChoicePop removeAllItems];
[CsoundChoicePop addItemWithTitle: @"System Beep"];
sounds = [self getSounds];
for ( i = 0 ; i < [sounds count] ; ++i ) {
[CsoundChoicePop addItemWithTitle: [sounds objectAtIndex: i]];
}
[sounds release];
[CsoundChoicePop selectItemWithTitle:
[prefs stringForKey: @"Sound Name"]];
/* Set up the Server Table */
[CserverDS replaceServers: servers];
[CserverTbl reloadData];
[CmailAppText setStringValue: [prefs stringForKey: @"Mail App"]];
if ([prefs stringForKey: @"New Unread Mail Command"])
[CunreadMailCommand setStringValue:
[prefs stringForKey: @"New Unread Mail Command"]];
[prefsWindow makeKeyAndOrderFront: self];
[NSApp activateIgnoringOtherApps: YES];
}
- (IBAction)savePrefs:(id)sender
{
int i;
NSMutableArray *serverNames = [NSMutableArray arrayWithCapacity: 5];
[prefsWindow close];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
dprintf("In %s\n", __FUNCTION__);
[prefs setInteger: [servers count] forKey: @"Server Count"];
// make sure delay is not zero or negative
int delay = 1;
if ([CdelayText intValue] >= 1) {
delay = [CdelayText intValue];
}
[prefs setInteger: delay forKey: @"checkDelay"];
[prefs setBool: ( [CcountIgnores state] == NSControlStateValueOff )
forKey: @"Ignore Ignores"];
[prefs setBool: ( [CcheckHeaders state] == NSControlStateValueOn )
forKey: @"Fetch Unread Headers"];
[prefs setBool: ( [CcheckIgnHeaders state] == NSControlStateValueOn )
forKey: @"Fetch Ignored Headers"];
[prefs setBool: ( [CshowBrackets state] != NSControlStateValueOn )
forKey: @"Hide Brackets"];
[prefs setBool: ( [CshowIcon state] == NSControlStateValueOn )
forKey: @"Show Icon"];
[prefs setBool: ( [CshowText state] != NSControlStateValueOn )
forKey: @"Hide Text"];
[prefs setBool: ( [CshowTotBut state] != NSControlStateValueOn )
forKey: @"Hide Total"];
[prefs setBool: ( [CchColorBut state] == NSControlStateValueOn )
forKey: @"Alert Color"];
[prefs setBool: ( [CdoSoundBut state] == NSControlStateValueOn )
forKey: @"Alert Sound"];
[prefs setBool: ([CuseGrowl state] == NSControlStateValueOn)
forKey: @"Notify with Growl"];
[prefs setObject: [CsoundChoicePop titleOfSelectedItem]
forKey: @"Sound Name"];
[prefs setBool: ( [CshowActivity state] == NSControlStateValueOn )
forKey: @"Show Activity"];
[prefs setObject: [CmailAppText stringValue] forKey: @"Mail App"];
[prefs setObject: [CunreadMailCommand stringValue]
forKey: @"New Unread Mail Command"];
for ( i = 0 ; i < [servers count] ; ++i ) {
[[servers objectAtIndex: i] storePrefs];
[serverNames addObject: [[servers objectAtIndex: i] name]];
}
[prefs removeObjectForKey: @"Server Names"];
[prefs setObject: serverNames forKey: @"Server Names"];
dprintf("Doing syncronize\n");
if ( ![prefs synchronize] ) {
fprintf(stderr, "Unable to syncronize\n");
}
if ( timer ) {
[timer invalidate];
}
if ( [CdelayText intValue] >= 1 ) {
timer = [NSTimer scheduledTimerWithTimeInterval:
60 * [CdelayText intValue]
target: self
selector: @selector (refresh:)
userInfo: self
repeats: YES];
}
[self refresh: self];
dprintf("Leaving %s\n", __FUNCTION__);
}
- (IBAction)cancelPrefs:(id)sender
{
[prefsWindow close];
}
- (IBAction) editServer: (id) sender
{
if ( ICcurServer != -1 ) return;
if ( sender == CeditBut || sender == CserverTbl ) {
// ICcurServer = [CserverTbl selectedRow];
ICcurServer = (int)[CserverTbl selectedRow];
} else {
// ICcurServer = [sender tag];
ICcurServer = (int)[sender tag];
}
ICadding = NO;
[self Iconfigure];
}
- (IBAction) addServer: (id) sender
{
imap *server = [[imap alloc] init];
dprintf("Server Count: %d\n", (int)[servers count]);
[servers addObject: server];
// ICcurServer = [servers count]-1;
ICcurServer = (int)[servers count]-1;
ICadding = YES;
dprintf("Server Count: %d\n", (int)[servers count]);
[self Iconfigure];
}
- (IBAction) delServer: (id) sender
{
int result;
if ( ICcurServer != -1 ) return;
if ( [CserverTbl selectedRow] < 0 ) return;
// NSAlert *alert = [NSAlert alertWithMessageText:
// @"Are you sure?"
// defaultButton: @"Nope"
// alternateButton: @"Yep"
// otherButton: nil
// informativeTextWithFormat:
// informativeText:
// @"Are you sure you wish to remove server %@?",
// [[servers objectAtIndex:
// [CserverTbl selectedRow]] name]];
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: @"Nope"];
[alert addButtonWithTitle: @"Yep"];
[alert setMessageText: @"Are you sure?"];
[alert setInformativeText:
[NSString stringWithFormat:@"Are you sure you wish to remove server %@?",
[[servers objectAtIndex: [CserverTbl selectedRow]] name]]];
[alert setAlertStyle: NSAlertStyleInformational];
result = (int)[alert runModal];
if ( result == 1000 ) {
return;
} else {
/* Need to remove */
[self IremoveServer: (int)[CserverTbl selectedRow]];
[CserverDS replaceServers: servers];
[CserverTbl reloadData];
}
}
- (IBAction) stepDelay: (id) sender
{
[CdelayText setIntValue: [CdelayStep intValue]];
}
- (IBAction) editDelay: (id) sender
{
if ([CdelayText intValue] <= 0) {
[CdelayText setIntValue: 1];
}
[CdelayStep setIntValue: [CdelayText intValue]];
}
- (IBAction) selectText: (id) sender
{
[CchColorBut setEnabled: ([CshowText state] == NSControlStateValueOn) ];
[CshowTotBut setEnabled: ([CshowText state] == NSControlStateValueOn) ];
[CshowBrackets setEnabled: ([CshowText state] == NSControlStateValueOn) ];
}
- (IBAction) selectSound: (id) sender
{
[CsoundChoicePop setEnabled: ([CdoSoundBut state] == NSControlStateValueOn) ];
}
- (IBAction) selectUseGrowl: (id) sender
{
//nothing to do here.
}
- (IBAction) soundChange: (id) sender
{
if ( [CsoundChoicePop indexOfSelectedItem] == 0 ) {
//System Beep
NSBeep();
} else {
NSSound *snd = [NSSound soundNamed:
[CsoundChoicePop titleOfSelectedItem]];
[snd play];
}
[CsoundChoicePop synchronizeTitleAndSelectedItem];
}
- (IBAction) selectFetch: (id) sender
{
[CcheckIgnHeaders setEnabled: ([CcheckHeaders state] == NSControlStateValueOn) ];
}
- (IBAction) chooseApp: (id) sender
{
int res;
NSArray *ft = [NSArray arrayWithObject: @"app"];
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowsMultipleSelection: NO];
// res = (int)[op runModalForDirectory: nil
// file: [CmailAppText stringValue]
// types: ft];
[op setDirectoryURL: nil];
[op setRepresentedFilename: [CmailAppText stringValue]];
[op setAllowedFileTypes: ft];
res = (int)[op runModal];
if ( res == NSModalResponseOK ) {
NSArray *files = [op URLs];
[CmailAppText setStringValue: [files objectAtIndex: 0]];
}
}
- (IBAction) launchMail: (id) sender
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[[NSWorkspace sharedWorkspace] launchApplication:
[prefs stringForKey: @"Mail App"]];
}
- (IBAction)openGrowlURL:(id)sender;
{
[[NSWorkspace sharedWorkspace]
openURL: [NSURL URLWithString:
NSLocalizedString(@"Growl URL", /*comment*/ nil)]];
}
- (void) registerDefaultPreferences
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
/*
Register the default sources in prefs storage.
*/
[prefs setInteger: 0 forKey: @"Server Count"];
[prefs setInteger: 5 forKey: @"checkDelay"];
[prefs setBool: NO forKey: @"Ignore Ignores"];
[prefs setBool: NO forKey: @"Fetch Unread Headers"];
[prefs setBool: NO forKey: @"Fetch Ignored Headers"];
[prefs setBool: YES forKey: @"Alert Color"];
[prefs setBool: NO forKey: @"Alert Sound"];
[prefs setBool: YES forKey: @"Hide Text"];
[prefs setBool: NO forKey: @"Hide Total"];
[prefs setBool: YES forKey: @"Hide Brackets"];
[prefs setBool: YES forKey: @"Show Icon"];
[prefs setBool: YES forKey: @"Notify with Growl"];
[prefs setObject: @"System Beep" forKey: @"Sound Name"];
[prefs setObject: @"Mail" forKey: @"Mail App"];
[prefs setObject: @"" forKey: @"New Unread Mail Command"];
}
- (void) loadServers
{
int numServers;
int i;
NSArray* serverNames;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// numServers = [prefs integerForKey: @"Server Count"];
numServers = (int)[prefs integerForKey: @"Server Count"];
serverNames = [prefs stringArrayForKey: @"Server Names"];
if ( numServers != [serverNames count] ) {
alert("Recorded Servers (%d) != Server Count (%d)\n",
// numServers, [serverNames count]);
numServers, (int)serverNames.count);
}
for ( i = 0 ; i < [serverNames count] ; ++i ) {
dprintf("Building Server '%s'\n",
[[serverNames objectAtIndex: i] UTF8String] );
[servers addObject: [[imap alloc]
initFromPrefs: [serverNames objectAtIndex: i]]];
}
}
/*
* IMAP Config
*/
- (void) updateIgnores: (NSMutableArray*) igbox fromBoxes: (NSArray*) boxes
{
int i;
mailbox *box;
for ( i = 0 ; i < [boxes count] ; ++i ) {
box = [boxes objectAtIndex: i];
if ( [box isIgnored] ) {
[igbox addObject: [box fullname]];
}
if ( [[box subBoxes] count] ) {
[self updateIgnores: igbox fromBoxes: [box subBoxes]];
}
}
}
- (void) Iconfigure
{
if ( ICcurServer == -1 )
return;
imap *server = [servers objectAtIndex: ICcurServer];
[ICName setStringValue: [server name]];
[ICServer setStringValue: [server server]];
[ICUsername setStringValue: [server username]];
[ICPasswd setStringValue: @""];
[ICPrefix setStringValue: [server prefix]];
[ICuseSSL setState: ([server mode] == REMOTE) ? NSControlStateValueOff : NSControlStateValueOn];
[ICKeepPW setState: [server savesPW] ? NSControlStateValueOn : NSControlStateValueOff ];
[ICenable setState: [server enabled] ? NSControlStateValueOn : NSControlStateValueOff ];
[ICPort setIntValue: [server port]];
[ICsubscribed setState: ([server subOnly]) ? NSControlStateValueOn : NSControlStateValueOff];
[ICignoreDS setServer: server];
[ICconfigWindow makeKeyAndOrderFront: self];
[NSApp activateIgnoringOtherApps: YES];
}
- (void) IremoveServer: (int) num
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
imap *server = [servers objectAtIndex: num];
NSString *sname = [server name];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"initServer[%@]", sname]];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"server[%@]", sname]];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"username[%@]", sname]];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"prefix[%@]", sname]];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"mode[%@]", sname]];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"storedPW[%@]", sname]];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"server[%@]", sname]];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"ignoredBoxes[%@]", sname]];
[prefs removeObjectForKey:
[NSString stringWithFormat: @"enabled[%@]", sname]];
[servers removeObjectAtIndex: num];
}
- (IBAction) IcancelConfig: (id) sender
{
[ICconfigWindow close];
if ( ICadding ) {
[servers removeObjectAtIndex: ICcurServer];
}
ICcurServer = -1;
}
- (IBAction) IsaveConfig: (id) sender
{
if ( ICcurServer == -1 ) return;
imap *server = [servers objectAtIndex: ICcurServer];
if ( ![[server name] isEqualToString: @""] &&
![[server name] isEqualToString:
[ICName stringValue]] ) {
/* remove old server from preferences */
[self IremoveServer: ICcurServer];
}
[server setName: [ICName stringValue]];
[server setServer: [ICServer stringValue]];
[server setUsername: [ICUsername stringValue]];
[server setPrefix: [ICPrefix stringValue]];
if ( [[ICPasswd stringValue] length] ) {
[server setPassword: [ICPasswd stringValue] andKeep:
([ICKeepPW state] == NSControlStateValueOn) ];
}
[server setMode: ([ICuseSSL state] == NSControlStateValueOn) ? REMOTESSL : REMOTE];
[server setEnabled: ([ICenable state] == NSControlStateValueOn)];
[server setPort: [ICPort intValue]];
[server setSubOnly: ([ICsubscribed state] == NSControlStateValueOn)];
[server storePrefs];
[CserverDS replaceServers: servers];
[CserverTbl reloadData];
[ICconfigWindow close];
ICcurServer = -1;
}
- (IBAction) IchangeMode: (id) sender
{
[ICPort setIntValue: (([ICuseSSL state] == NSControlStateValueOn) ?
993 : 143)];
}
/*
* Ask Password
*/
- (void) askPassForServer: (int) num
{
if ( ICcurServer != -1 ) return;
[PWpasswd setStringValue: @""];
[PWtext setStringValue: [[servers objectAtIndex: num] name]];
[PWkeepPW setState: NSControlStateValueOff];
ICcurServer = num;
[PWpassWindow makeKeyAndOrderFront: self];
[NSApp activateIgnoringOtherApps: YES];
}
- (IBAction) PWcancel: (id) sender
{
[PWpassWindow close];
ICcurServer = -1;
}
- (IBAction) PWOK: (id) sender
{
[PWpasswd validateEditing];
[PWpassWindow close];
[[servers objectAtIndex: ICcurServer] setPassword:
[PWpasswd stringValue] andKeep:
( [PWkeepPW state] == NSControlStateValueOn ) ];
ICcurServer = -1;
[self refresh: self];
}
/*
* Menu Control
*/
- (void) setupMenuBar
{
NSMenu *tmenu = NULL;
/*Create the IMAP status item.*/
systemBar = [[NSStatusBar systemStatusBar]
statusItemWithLength: 65.0];
[systemBar retain];
/*Attach the menu to the status item.*/
tmenu = [self standardMenu];
[systemBar setMenu: tmenu];
/* [systemBar setMenu: mainMenu]; */
systemBar.button.title = @"MacBiff";
// [systemBar setHighlightMode: YES];
systemBar.button.cell.highlighted = YES;
}
- (NSMenu*) standardMenu
{
NSMenu *menu = NULL;
NSMenuItem *menuItem = NULL;
goRed = NO;
menu = [[NSMenu alloc] initWithTitle: @"MacBiff"];
/* Refresh commands*/
if ( checkStatus ) {
[checkStatus release];
}
checkStatus = [menu addItemWithTitle: @"Checking..."
action: NULL
keyEquivalent: @""];
[checkStatus retain];
menuItem = [menu addItemWithTitle: @"Configure"
action: @selector (openPrefs:)
keyEquivalent: @""];
[menuItem setTarget: self];
menuItem = [menu addItemWithTitle: @"Detach"
action: @selector (detachList:)
keyEquivalent: @""];
[menuItem setTarget: self];
menuItem = [menu addItemWithTitle: @"Show Activity"
action: @selector(display:)
keyEquivalent: @""];
[menuItem setTarget: actWin];
menuItem = [menu addItemWithTitle: @"Launch Mail"
action: @selector (launchMail:)
keyEquivalent: @""];
[menuItem setTarget: self];
menuItem = [menu addItemWithTitle: @"About MacBiff"
action: @selector (openAbout:)
keyEquivalent: @""];
[menuItem setTarget: self];
menuItem = [menu addItemWithTitle: @"Quit MacBiff"
action: @selector (terminate:)
keyEquivalent: @""];
[menuItem setTarget: NSApp];
return menu;
}
- (void) checkMail
{
imap *server;
NSMenuItem *title;
int res, i;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ( ![prefs integerForKey: @"Server Count"] ) {
return;
}
total = unread = 0;
if ( mainMenu ) {
[mainMenu release];
}
mainMenu = [self standardMenu];
[actWin performSelectorOnMainThread: @selector(startChecking:)
// withObject: [NSNumber numberWithInt: [servers count]]
withObject: [NSNumber numberWithInt: (int)[servers count]]
waitUntilDone: YES];
/* Check Mail */
for ( i = 0 ; i < [servers count] ; ++i ) {
dprintf("%s starting server # %d\n", __FUNCTION__, i);
server = [servers objectAtIndex: i];
[mainMenu addItem: [NSMenuItem separatorItem]];
title = [mainMenu addItemWithTitle:
[server name]
action: @selector (editServer:)
keyEquivalent: @""];
[title setTarget: self];
[title setTag: i];
[actWin performSelectorOnMainThread: @selector(startServer:)
withObject: [server name]
waitUntilDone: YES];
if ( ![server enabled] || user_pressed_stop ) {
[title setEnabled: NO];
continue;
}
dprintf("%s calling [server checkMail]\n", __FUNCTION__ );
@try {
res = [server checkMail];
}
@catch (NSException *exception) {
alert("Exception thrown!\n");
alert("thrown: '%s'\n", [[exception name] UTF8String]);
if ( ![[exception name] isEqualTo: @"Bad Comms"] ) {
alert("Howdy\n");
@throw(exception);
}
res = 1;
}
@finally {
if (res == 1) {
alert("Exception thrown!\n");
} else {
alert("[server checkMail] ok.\n");
}
}
if ( user_pressed_stop ) {
total = 0;
continue;
}
dprintf("%s back from [server checkMail]\n", __FUNCTION__ );
if ( res ) {
if ( res == EAUTH ) {
/* Get password */
//[self askPassForServer: i];
continue;