forked from wafflesoftware/growlergn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PrefsController.m
784 lines (654 loc) · 32.7 KB
/
PrefsController.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
/*
BSD License
Growler for Google Notifier
Copyright (c) 2005-2011
Jesper (waffle software)
googlegrowl@wafflesoftware.net
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Growler for Google Notifier or waffle software,
nor the names of Growler for Google Notifier's contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Gmail, Google Mail, Google Calendar and Google are owned by Google, Inc.
Growl is owned by the Growl Development Team.
Likewise, the logos of those services are owned and copyrighted to their owners.
No ownership of any of these is assumed or implied, and no infringement is intended.
For more info on this products or on the technologies on which it builds:
Growl: <http://growl.info/>
Gmail: <http://gmail.com>
Gmail Notifier: <http://toolbar.google.com/gmail-helper/index.html>
Growler for Google Notifier: <http://wafflesoftware.net/googlegrowl/>
*/
//
// PrefsController.m
// GMNGrowl
//
// Created by Jesper on 2005-09-28.
//
#import "PrefsController.h"
#import "GGPluginProtocol.h"
#define GMNGrowlNotificationFormatTag 2010
#define GMNGrowlNotificationFormatTextTag 2020
#define GMNGrowlDontUseABIconsTag 2050
#define GMNGrowlDontShowOnClickTag 2060
#define GMNGrowlShowInWhichBrowserTag 2070
#define GMNGrowlMaxNotificationsCapTag 2090
#define GMNGrowlEventNotificationFormatTag 2110
#define GMNGrowlEventNotificationFormatTextTag 2120
#define GMNGrowlEventDontShowOnClickTag 2160
#define GMNGrowlEventShowInWhichBrowserTag 2170
#define GMNGrowlEventMaxNotificationsCapTag 2190
#define GMNGrowlDisabledTag 3000
#define GMNGrowlMailEnabledTag 4010
#define GMNGrowlEventEnabledTag 4020
#define GMNGrowlLookupNumber(x) [NSNumber numberWithInt:x]
#define GMNGrowlLookupDict [NSDictionary dictionaryWithObjectsAndKeys:\
GMNGrowlNotificationFormatUDK, GMNGrowlLookupNumber(GMNGrowlNotificationFormatTag),\
GMNGrowlNotificationTextFormatUDK, GMNGrowlLookupNumber(GMNGrowlNotificationFormatTextTag),\
GMNGrowlDontUseABIconsUDK, GMNGrowlLookupNumber(GMNGrowlDontUseABIconsTag),\
GMNGrowlDontShowOnClickUDK, GMNGrowlLookupNumber(GMNGrowlDontShowOnClickTag),\
GMNGrowlMaxNotificationsCapUDK, GMNGrowlLookupNumber(GMNGrowlMaxNotificationsCapTag),\
GMNGrowlShowInWhichBrowserUDK, GMNGrowlLookupNumber(GMNGrowlShowInWhichBrowserTag),\
\
GMNGrowlEventNotificationFormatUDK, GMNGrowlLookupNumber(GMNGrowlEventNotificationFormatTag),\
GMNGrowlEventNotificationTextFormatUDK, GMNGrowlLookupNumber(GMNGrowlEventNotificationFormatTextTag),\
GMNGrowlEventDontShowOnClickUDK, GMNGrowlLookupNumber(GMNGrowlEventDontShowOnClickTag),\
GMNGrowlEventMaxNotificationsCapUDKUDK, GMNGrowlLookupNumber(GMNGrowlEventMaxNotificationsCapTag),\
GMNGrowlEventShowInWhichBrowserUDK, GMNGrowlLookupNumber(GMNGrowlEventShowInWhichBrowserTag),\
\
\
GMNGrowlEventEnabledUDK, GMNGrowlLookupNumber(GMNGrowlEventEnabledTag),\
GMNGrowlMailEnabledUDK, GMNGrowlLookupNumber(GMNGrowlMailEnabledTag),\
nil]
#define GMNGrowlAllMailPlaceholders MAKEPLACEHOLDER(GmailMessageDictAuthorEmailKey), MAKEPLACEHOLDER(GmailMessageDictAuthorNameKey), MAKEPLACEHOLDER(GmailMessageDictTitleKey), MAKEPLACEHOLDER(GmailMessageDictSummaryKey), MAKEPLACEHOLDER(GmailMessageDictDateIssuedKey), MAKEPLACEHOLDER(GmailMessageDictDateModifiedKey)
#define GMNGrowlMailPlaceholders [NSArray arrayWithObjects: GMNGrowlAllMailPlaceholders, nil]
#define GMNGrowlAllEventPlaceholders MAKEPLACEHOLDER(GCalEventDictAuthorEmailKey), MAKEPLACEHOLDER(GCalEventDictAuthorNameKey), MAKEPLACEHOLDER(GCalEventDictDateIssuedKey), MAKEPLACEHOLDER(GCalEventDictDateModifiedKey), MAKEPLACEHOLDER(GCalEventDictTitleKey), MAKEPLACEHOLDER(GCalEventDictEventStatusKey), MAKEPLACEHOLDER(GCalEventDictWhereKey), MAKEPLACEHOLDER(GCalEventDictSummaryKey), MAKEPLACEHOLDER(GCalEventAutoEventTimeKey), MAKEPLACEHOLDER(GCalEventAutoWhereSummaryKey), MAKEPLACEHOLDER(GCalEventAutoSummaryWhereKey)
#define GMNGrowlEventPlaceholders [NSArray arrayWithObjects: GMNGrowlAllEventPlaceholders, nil]
#define GMNCFReleaseUnlessNull(x) { if (x != NULL) CFRelease(x); }
@implementation PrefsController
static int CompareBundleIDAppDisplayNames(id a, id b, void *context) {
NSURL* appURLa = nil;
NSURL* appURLb = nil;
if ((LSFindApplicationForInfo(kLSUnknownCreator, (CFStringRef)a, NULL, NULL, (CFURLRef*)&appURLa) == noErr) &&
(LSFindApplicationForInfo(kLSUnknownCreator, (CFStringRef)b, NULL, NULL, (CFURLRef*)&appURLb) == noErr)) {
NSString *aName = [[NSFileManager defaultManager] displayNameAtPath:[appURLa path]];
NSString *bName = [[NSFileManager defaultManager] displayNameAtPath:[appURLb path]];
return [aName compare:bName];
}
return NSOrderedSame;
}
- (IBAction)doUninstall:(id)sender {
}
+ (PrefsController *)sharedPreferencesWindowController {
static PrefsController *_sharedPreferencesWindowController = nil;
if (!_sharedPreferencesWindowController) {
_sharedPreferencesWindowController = [[self alloc] initWithWindowNibName:@"Preferences"];
}
return _sharedPreferencesWindowController;
}
- (IBAction)launchGrowlPrefPane:(id)sender {
NSArray *lookThrough = [NSArray arrayWithObjects:@"/Library/PreferencePanes/Growl.prefPane", @"~/Library/PreferencePanes/Growl.prefPane", @"/System/Library/PreferencePanes/Growl.prefPane", @"/Network/Library/PreferencePanes/Growl.prefPane", nil];
NSEnumerator *pathEnumerator = [lookThrough objectEnumerator];
NSString *path;
while (path = [pathEnumerator nextObject]) {
BOOL isDir;
path = [path stringByExpandingTildeInPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir] && isDir) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:path]];
return;
}
}
NSBeep();
}
- (void)openURL:(NSURL *)url {
[[NSWorkspace sharedWorkspace] openURL:url];
}
- (IBAction)websiteClicked:(id)sender {
[self openURL:[NSURL URLWithString:@"http://wafflesoftware.net/googlegrowl/"]];
}
- (IBAction)relatedWebsiteClicked:(id)sender {
switch ([sender tag]) {
case 1000:
[self openURL:[NSURL URLWithString:@"http://www.gmail.com/"]];
break;
case 1050:
[self openURL:[NSURL URLWithString:@"http://calendar.google.com/"]];
break;
case 2000:
[self openURL:[NSURL URLWithString:@"http://toolbar.google.com/gmail-helper/"]];
break;
case 3000:
[self openURL:[NSURL URLWithString:@"http://growl.info/"]];
break;
}
}
- (void) awakeFromNib {
NSBundle *gg = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"GoogleGrowl" ofType:@"plugin" inDirectory:@"GoogleGrowl"]];
[gg load];
Class ggClass = [gg principalClass];
[ggClass performSelector:@selector(setIsRunningFromUtility)];
[ggClass pluginLoaded];
ggForTesting = [[ggClass alloc] init];
// //NSLog(@"gg: %@", ggForTesting);
// [titleFormatToken setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
[titleFormatToken sendActionOn:(NSLeftMouseDownMask | NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSPeriodicMask)];
[[titleFormatToken cell] setWraps:NO];
[bodyFormatToken setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
[bodyFormatToken sendActionOn:(NSLeftMouseDownMask | NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSPeriodicMask)];
[[bodyFormatToken cell] setWraps:NO];
[eventTitleFormatToken setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
[eventTitleFormatToken sendActionOn:(NSLeftMouseDownMask | NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSPeriodicMask)];
[[eventTitleFormatToken cell] setWraps:NO];
[eventBodyFormatToken setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
[eventBodyFormatToken sendActionOn:(NSLeftMouseDownMask | NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSPeriodicMask)];
[[eventBodyFormatToken cell] setWraps:NO];
[templatesToken setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
[[templatesToken cell] setWraps:YES];
[templatesToken setFrame:[templatesResizeTo frame]];
[templatesToken setAutoresizingMask:[templatesResizeTo autoresizingMask]];
[templatesToken setBezeled:NO];
[templatesToken setBordered:NO];
[templatesToken setBackgroundColor:[NSColor clearColor]];
[templatesToken setDrawsBackground:NO];
[templatesToken setEditable:NO];
[templatesToken setSelectable:YES];
[templatesToken setFocusRingType:NSFocusRingTypeNone];
[templatesToken setObjectValue:GMNGrowlMailPlaceholders];
[eventTemplatesToken setTokenizingCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@""]];
[[eventTemplatesToken cell] setWraps:YES];
[eventTemplatesToken setFrame:[eventTemplatesResizeTo frame]];
[eventTemplatesToken setAutoresizingMask:[eventTemplatesResizeTo autoresizingMask]];
[eventTemplatesToken setBezeled:NO];
[eventTemplatesToken setBordered:NO];
[eventTemplatesToken setBackgroundColor:[NSColor clearColor]];
[eventTemplatesToken setDrawsBackground:NO];
[eventTemplatesToken setEditable:NO];
[eventTemplatesToken setSelectable:YES];
[eventTemplatesToken setFocusRingType:NSFocusRingTypeNone];
[eventTemplatesToken setObjectValue:GMNGrowlEventPlaceholders];
[self updateFields];
[self constructBrowserMenu];
[NSTimer scheduledTimerWithTimeInterval:40.0 target:self selector:@selector(constructBrowserMenu) userInfo:nil repeats:YES];
//NSLog(@"prefsView: %@", prefsView);
// [self showWindow:nil];
}
- (IBAction)previewEventNotification:(id)sender {
NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:
@"example@example.com", GCalEventDictAuthorEmailKey,
@"John Doe", GCalEventDictAuthorNameKey,
@"confirmed", GCalEventDictEventStatusKey,
@"http://example.com/", GCalEventDictEventUUIDKey,
[NSDate date], GCalEventDictDateIssuedKey,
[NSDate date], GCalEventDictDateModifiedKey,
@"http://example.com/", GCalEventDictLinkKey,
[NSDate date], GCalEventDictNotifyTimeKey,
[NSDate date], GCalEventDictStartTimeKey,
[[NSDate date] addTimeInterval:60.0*60.0], GCalEventDictStopTimeKey,
@"Lorem ipsum summary", GCalEventDictSummaryKey,
@"Testing events (title)", GCalEventDictTitleKey,
@"Location", GCalEventDictWhereKey,
nil];
[ggForTesting newMessagesReceived:[NSArray arrayWithObject:event] fullCount:1];
// //NSLog(@"preview event notification");
}
- (IBAction)previewMailNotification:(id)sender {
NSDictionary *mail = [NSDictionary dictionaryWithObjectsAndKeys:
@"example@example.com", GmailMessageDictAuthorEmailKey,
@"John Doe", GmailMessageDictAuthorNameKey,
@"http://example.com/", GmailMessageDictMailUUIDKey,
[NSDate date], GmailMessageDictDateIssuedKey,
[NSDate date], GmailMessageDictDateModifiedKey,
@"http://example.com/", GmailMessageDictLinkKey,
@"Mail Text", GmailMessageDictSummaryKey,
@"Mail Subject", GmailMessageDictTitleKey,
nil];
[ggForTesting newMessagesReceived:[NSArray arrayWithObject:mail] fullCount:1];
// //NSLog(@"preview mail notification");
}
- (void)controlTextDidChange:(NSNotification *)aNotification {
[self prefsChanged:titleFormatToken];
[self prefsChanged:bodyFormatToken];
[self prefsChanged:eventTitleFormatToken];
[self prefsChanged:eventBodyFormatToken];
}
- (NSTokenStyle)tokenField:(NSTokenField *)tokenField styleForRepresentedObject:(id)representedObject {
//NSLog(@"style for object: %@", representedObject);
if ([representedObject isKindOfClass:[NSString class]]) {
NSString *rps = (NSString *)representedObject;
if (ISPLACEHOLDER(rps)) {
return NSRoundedTokenStyle;
}
}
return NSPlainTextTokenStyle;
}
- (NSString *)tokenField:(NSTokenField *)tokenField editingStringForRepresentedObject:(id)representedObject {
//NSLog(@"editing string for repr object: --'%@'--", representedObject);
return [representedObject description];
}
- (NSString *)tokenField:(NSTokenField *)tokenField displayStringForRepresentedObject:(id)representedObject {
NSString *ret = [NSString stringWithFormat:@"%@", representedObject];
BOOL isMailField = [tokenField tag] < 2100;
if ([representedObject isKindOfClass:[NSString class]]) {
NSString *rps = (NSString *)representedObject;
#define ForPlaceholderKeyRetIsString(X, Y) {\
if ([rps isEqualToString:MAKEPLACEHOLDER(X)]) {\
ret = Y;\
}\
}
if (isMailField) {
// mail
ForPlaceholderKeyRetIsString(GmailMessageDictAuthorEmailKey, @"Mail author's email");
ForPlaceholderKeyRetIsString(GmailMessageDictAuthorNameKey, @"Mail author's name");
ForPlaceholderKeyRetIsString(GmailMessageDictDateIssuedKey, @"Date sent (first mail)");
ForPlaceholderKeyRetIsString(GmailMessageDictDateModifiedKey, @"Date sent (latest mail)");
ForPlaceholderKeyRetIsString(GmailMessageDictSummaryKey, @"Message");
ForPlaceholderKeyRetIsString(GmailMessageDictTitleKey, @"Message subject");
} else {
// event
ForPlaceholderKeyRetIsString(GCalEventDictAuthorEmailKey, @"Event author's email");
ForPlaceholderKeyRetIsString(GCalEventDictAuthorNameKey, @"Event author's name");
ForPlaceholderKeyRetIsString(GCalEventDictDateIssuedKey, @"Date created");
ForPlaceholderKeyRetIsString(GCalEventDictDateModifiedKey, @"Date modified");
ForPlaceholderKeyRetIsString(GCalEventDictTitleKey, @"Title");
ForPlaceholderKeyRetIsString(GCalEventDictEventStatusKey, @"Event status");
ForPlaceholderKeyRetIsString(GCalEventDictWhereKey, @"Event location");
ForPlaceholderKeyRetIsString(GCalEventDictSummaryKey, @"Event summary");
ForPlaceholderKeyRetIsString(GCalEventAutoEventTimeKey, @"Event time");
ForPlaceholderKeyRetIsString(GCalEventAutoWhereSummaryKey, @"Event location+summary");
ForPlaceholderKeyRetIsString(GCalEventAutoSummaryWhereKey, @"Event summary+location");
}
}
return ret;
}
- (NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)tokens atIndex:(unsigned)index {
//NSLog(@"should add objects? %@ at index? %i", tokens, index);
NSString *t = [tokens componentsJoinedByString:@""];
[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(saveTokenFields:)
userInfo:nil
repeats:NO];
NSArray *retval = [self separateStringIntoTokens:t tokenField:tokenField];
//NSLog(@"retval: %@", retval);
return retval;
}
- (void)saveTokenFields:(NSTimer*)theTimer {
[self prefsChanged:titleFormatToken];
[self prefsChanged:bodyFormatToken];
[self prefsChanged:eventTitleFormatToken];
[self prefsChanged:eventBodyFormatToken];
}
- (BOOL)tokenField:(NSTokenField *)tokenField writeRepresentedObjects:(NSArray *)objects toPasteboard:(NSPasteboard *)pboard {
//NSLog(@"token field: %d, write represented objects: %@ to pasteboard: %@", [tokenField tag], objects, pboard);
[pboard setString:[objects componentsJoinedByString:@""] forType:NSStringPboardType];
NSArray *types = [pboard types];
NSEnumerator *typeEnumerator = [types objectEnumerator];
NSString *type;
while (type = [typeEnumerator nextObject]) {
//NSLog(@"pboard has type %@, value %@", type, [pboard stringForType:type]);
}
return YES;
}
- (NSArray *)tokenField:(NSTokenField *)tokenField readFromPasteboard:(NSPasteboard *)pboard {
//NSLog(@"tokenField: %d, read from pasteboard: %@", [tokenField tag], pboard);
NSArray *types = [pboard types];
NSEnumerator *typeEnumerator = [types objectEnumerator];
NSString *type;
while (type = [typeEnumerator nextObject]) {
//NSLog(@"pboard has type %@, value %@", type, [pboard stringForType:type]);
}
return [self separateStringIntoTokens:[pboard stringForType:NSStringPboardType] tokenField:tokenField];
}
- (NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(int)tokenIndex indexOfSelectedItem:(int *)selectedIndex {
BOOL isMailField = ([tokenField tag] < 2100);
NSEnumerator *phEnumerator = [(isMailField ? GMNGrowlMailPlaceholders : GMNGrowlEventPlaceholders) objectEnumerator];
NSString *ph;
NSMutableArray *comps = [NSMutableArray array];
while (ph = [phEnumerator nextObject]) {
if ([ph hasPrefix:substring]) {
[comps addObject:ph];
}
}
return comps;
}
- (id)tokenField:(NSTokenField *)tokenField representedObjectForEditingString:(NSString *)editingString {
//NSLog(@"repr object for editing string: %@", editingString);
return editingString;
}
- (NSString *)replace:(NSString *)r with:(NSString *)w inString:(NSString *)str {
//NSLog(@"replace: --'%@'-- with --'%@'-- in --'%@'--", r, w, str);
return [[str componentsSeparatedByString:r] componentsJoinedByString:w];
}
- (NSArray *)separateStringIntoTokens:(NSString *)str tokenField:(NSTokenField *)tokenField {
//NSLog(@"separate string: --'%@'-- into tokens", str);
BOOL isMailField = ([tokenField tag] < 2100);
NSEnumerator *phEnumerator = [(isMailField ? GMNGrowlMailPlaceholders : GMNGrowlEventPlaceholders) objectEnumerator];
NSString *ph;
NSString *sepString = @"~~~#\t#~~~";
NSRange r;
do {
sepString = [sepString stringByAppendingString:@"1"];
r = [str rangeOfString:sepString];
} while (!NSEqualRanges(r,NSMakeRange(NSNotFound,0)));
while (ph = [phEnumerator nextObject]) {
str = [self replace:[NSString stringWithFormat:@"%@", ph]
with:[NSString stringWithFormat:@"%@%@%@", sepString, ph, sepString]
inString:str];
}
return [str componentsSeparatedByString:sepString];
}
CF_RETURNS_RETAINED
- (CFPropertyListRef)preferenceWithKey:(NSString *)preferenceKey {
CFStringRef cfkey = (CFStringRef)preferenceKey;
CFPropertyListRef propListPreference = CFPreferencesCopyValue(cfkey, kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
return propListPreference;
}
- (BOOL)boolPreferenceWithKey:(NSString *)preferenceKey fallback:(BOOL)fallback {
CFPropertyListRef propListPreference = [self preferenceWithKey:preferenceKey];
if (propListPreference == NULL) {
return fallback;
}
if (CFGetTypeID(propListPreference) != CFBooleanGetTypeID()) { // wrong type?!
CFRelease(propListPreference);
return fallback;
} else {
CFBooleanRef boo = (CFBooleanRef)propListPreference;
BOOL wasTrue = (boo == kCFBooleanTrue);
CFRelease(propListPreference); // not strictly needed, but let's uphold the pattern
return wasTrue;
}
}
- (NSString *)stringPreferenceWithKey:(NSString *)preferenceKey fallback:(NSString *)fallback {
CFPropertyListRef propListPreference = [self preferenceWithKey:preferenceKey];
NSString *actualFallback = (fallback ? [fallback copy] : nil);
NSString *returnString = actualFallback;
if (propListPreference == NULL) {
return actualFallback;
}
if (CFGetTypeID(propListPreference) == CFStringGetTypeID()) {
CFStringRef cfstr = (CFStringRef)propListPreference;
NSString *str = [(NSString *)cfstr copy]; // this way, CFRelease can be used uniformly and under GC
returnString = [str autorelease];
}
CFRelease(propListPreference);
return returnString;
}
- (NSNumber *)numberPreferenceWithKey:(NSString *)preferenceKey fallback:(NSNumber *)fallback {
CFPropertyListRef propListPreference = [self preferenceWithKey:preferenceKey];
NSNumber *actualFallback = (fallback ? [fallback copy] : nil);
NSNumber *returnNumber = actualFallback;
if (propListPreference == NULL) {
return actualFallback;
}
if (CFGetTypeID(propListPreference) == CFNumberGetTypeID()) {
CFNumberRef cfnum = (CFNumberRef)propListPreference;
NSNumber *num = [(NSNumber *)cfnum copy]; // this way, CFRelease can be used uniformly and under GC
returnNumber = [num autorelease];
}
CFRelease(propListPreference);
return returnNumber;
}
- (void) updateFields {
// [self syncDisableEnable];
#define ON_IF_YES(x) (x ? NSOnState : NSOffState)
#define ON_IF_NO(x) (x ? NSOffState : NSOnState)
[isMailEnabled setState:ON_IF_YES([self boolPreferenceWithKey:GMNGrowlMailEnabledUDK fallback:YES])];
[isEventEnabled setState:ON_IF_YES([self boolPreferenceWithKey:GMNGrowlEventEnabledUDK fallback:YES])];
[abIcons setState:ON_IF_NO([self boolPreferenceWithKey:GMNGrowlDontUseABIconsUDK fallback:YES])];
BOOL showOnClick = !([self boolPreferenceWithKey:GMNGrowlDontShowOnClickUDK fallback:YES]);
[showMess setState:ON_IF_YES(showOnClick)];
[chosenBrowser setEnabled:showOnClick];
BOOL evtShowOnClick = !([self boolPreferenceWithKey:GMNGrowlEventDontShowOnClickUDK fallback:YES]);
[showEvent setState:ON_IF_YES(evtShowOnClick)];
[eventChosenBrowser setEnabled:evtShowOnClick];
NSString *messageTitle = [self stringPreferenceWithKey:GMNGrowlNotificationFormatUDK fallback:nil];
NSString *messageBody = [self stringPreferenceWithKey:GMNGrowlNotificationTextFormatUDK fallback:nil];
[titleFormatToken setObjectValue:[self separateStringIntoTokens:((messageTitle != nil) ? messageTitle : GMNGrowlNotificationFormat) tokenField:titleFormatToken]];
[bodyFormatToken setObjectValue:[self separateStringIntoTokens:((messageBody != nil) ? messageBody : GMNGrowlNotificationTextFormat) tokenField:bodyFormatToken]];
NSString *eventTitle = [self stringPreferenceWithKey:GMNGrowlEventNotificationFormatUDK fallback:nil];
NSString *eventBody = [self stringPreferenceWithKey:GMNGrowlEventNotificationTextFormatUDK fallback:nil];
[eventTitleFormatToken setObjectValue:[self separateStringIntoTokens:((eventTitle != nil) ? eventTitle : GMNGrowlEventNotificationFormat) tokenField:eventTitleFormatToken]];
[eventBodyFormatToken setObjectValue:[self separateStringIntoTokens:((eventBody != nil) ? eventBody : GMNGrowlEventNotificationTextFormat) tokenField:eventBodyFormatToken]];
NSNumber *n = [self numberPreferenceWithKey:GMNGrowlMaxNotificationsCapUDK fallback:[NSNumber numberWithInt:0]];
int rn;
int iv = [n intValue];
if ((iv < 1) || (iv > 20))
rn = 6;
else
rn = iv;
[maxNot setIntValue:rn];
[maxNotT takeIntValueFrom:maxNot];
n = [self numberPreferenceWithKey:GMNGrowlEventMaxNotificationsCapUDKUDK fallback:[NSNumber numberWithInt:0]];
iv = [n intValue];
if ((iv < 1) || (iv > 20))
rn = 6;
else
rn = iv;
[eventMaxNot setIntValue:rn];
[eventMaxNotT takeIntValueFrom:eventMaxNot];
}
- (NSString *)browserToUse {
NSString *browserident = [self stringPreferenceWithKey:GMNGrowlShowInWhichBrowserUDK fallback:nil];
return (browserident != nil ? browserident : [self identifierForBundle:[self urlToDefaultBrowser]]);
}
- (NSString *)eventBrowserToUse {
NSString *browserident = [self stringPreferenceWithKey:GMNGrowlEventShowInWhichBrowserUDK fallback:nil];
return (browserident != nil ? browserident : [self identifierForBundle:[self urlToDefaultBrowser]]);
}
/*
- (void)toggleDisableEnable {
CFBooleanRef boo = (CFBooleanRef)CFPreferencesCopyValue((CFStringRef)GMNGrowlDisabledUDK,kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost);
BOOL bo = (boo == kCFBooleanTrue);
bo = !bo;
CFPreferencesSetValue((CFStringRef)GMNGrowlDisabledUDK,(bo ? kCFBooleanTrue : kCFBooleanFalse),kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost);
if (CFPreferencesSynchronize(kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost))
GMNLog(@"Preferences synchronized.");
[self syncDisableEnable];
}
- (void)syncDisableEnable {
CFBooleanRef boo = (CFBooleanRef)CFPreferencesCopyValue((CFStringRef)GMNGrowlDisabledUDK,kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost);
BOOL bo = (boo == kCFBooleanTrue);
[enableDisableDesc setStringValue:[NSString stringWithFormat:@"Growler for Google Notifier is currently %@", (bo ? @"disabled!" : @"enabled.")]];
[enableDisable setTitle:(bo ? @"Enable" : @"Disable")];
}*/
- (IBAction)prefsChanged:(id)sender
{
NSString *key = nil; CFPropertyListRef value; BOOL proceed = NO;
switch ([sender tag]) {
case GMNGrowlDontUseABIconsTag:
case GMNGrowlDontShowOnClickTag:
case GMNGrowlEventDontShowOnClickTag:
value = (([(NSButton *)sender state] == NSOffState) ? kCFBooleanTrue : kCFBooleanFalse);
proceed = YES;
if ([sender tag] == GMNGrowlDontShowOnClickTag) {
[chosenBrowser setEnabled:([(NSButton *)sender state] == NSOnState)];
}
if ([sender tag] == GMNGrowlEventDontShowOnClickTag) {
[eventChosenBrowser setEnabled:([(NSButton *)sender state] == NSOnState)];
}
break;
case GMNGrowlEventEnabledTag:
case GMNGrowlMailEnabledTag:
value = (([(NSButton *)sender state] == NSOnState) ? kCFBooleanTrue : kCFBooleanFalse);
// //NSLog(@"%d changed, value: %@", [sender tag], (value == kCFBooleanTrue) ? @"YES" : @"NO");
proceed = YES;
break;
case GMNGrowlNotificationFormatTag:
case GMNGrowlNotificationFormatTextTag:
case GMNGrowlEventNotificationFormatTag:
case GMNGrowlEventNotificationFormatTextTag:
value = (CFStringRef)[(NSTextField *)sender stringValue];
if ([[sender objectValue] isKindOfClass:[NSArray class]])
value = (CFStringRef)[(NSArray *)[sender objectValue] componentsJoinedByString:@""];
// //NSLog(@"string value: %@", (NSString *)value);
proceed = YES;
break;
/*
case GMNGrowlDisabledTag:
value = (([(NSButton *)sender state] == NSOffState) ? kCFBooleanTrue : kCFBooleanFalse);
[self toggleDisableEnable];
break;*/
case GMNGrowlMaxNotificationsCapTag:
case GMNGrowlEventMaxNotificationsCapTag:
value = (CFNumberRef)[NSNumber numberWithInt:[(NSStepper *)sender intValue]];
[(([sender tag] == GMNGrowlEventMaxNotificationsCapTag) ? eventMaxNotT : maxNotT) takeIntValueFrom:sender];
proceed = YES;
break;
}
if (proceed) {
key = (NSString *)[GMNGrowlLookupDict objectForKey:GMNGrowlLookupNumber([sender tag])];
// //NSLog(@"key: %@ (metakey: %@)", key, GMNGrowlLookupNumber([sender tag]));
CFPreferencesSetValue((CFStringRef)key,value,kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost);
if (CFPreferencesSynchronize(kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost))
GMNLog(@"Preferences synchronized.");
}
}
- (IBAction)pickEventBrowser:(id)sender {
NSString *value = (NSString *)[(NSMenuItem *)sender representedObject];
// //NSLog(@"pick event browser: %@", value);
NSString *key = (NSString *)[GMNGrowlLookupDict objectForKey:GMNGrowlLookupNumber([eventChosenBrowser tag])];
CFPreferencesSetValue((CFStringRef)key,(CFStringRef)value,kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost);
if (CFPreferencesSynchronize(kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost))
GMNLog(@"Browser picked; Preferences synchronized.");
}
- (IBAction)pickBrowser:(id)sender {
NSString *value = (NSString *)[(NSMenuItem *)sender representedObject];
// //NSLog(@"pick mail browser: %@", value);
NSString *key = (NSString *)[GMNGrowlLookupDict objectForKey:GMNGrowlLookupNumber([chosenBrowser tag])];
CFPreferencesSetValue((CFStringRef)key,(CFStringRef)value,kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost);
if (CFPreferencesSynchronize(kCFPreferencesAnyApplication,kCFPreferencesCurrentUser,kCFPreferencesAnyHost))
GMNLog(@"Browser picked; Preferences synchronized.");
}
- (void)constructBrowserMenu {
NSMutableArray *list = [[[self allBrowsers] allObjects] mutableCopy];
[list sortUsingFunction:&CompareBundleIDAppDisplayNames context:NULL];
NSMenu *m = [[NSMenu alloc] initWithTitle:@"Mail Browsers"];
NSMenu *em = [[NSMenu alloc] initWithTitle:@"Event Browsers"];
NSMenuItem *mi = [[NSMenuItem alloc] initWithTitle:@"Default Browser" action:@selector(pickBrowser:) keyEquivalent:@""];
NSString *selectedIdentMail = [self browserToUse];
NSMenuItem *selectedMail = nil;
NSString *selectedIdentCal = [self eventBrowserToUse];
NSMenuItem *selectedCal = nil;
NSURL *urlToDef = [self urlToDefaultBrowser];
NSString *ident = [self identifierForBundle:urlToDef];
NSString *defaultIdent = ident;
if ((ident != nil) && ([[NSNull null] isNotEqualTo:ident])) {
mi = [self menuItemForBrowserAtURL:urlToDef];
[mi setTitle:[NSString stringWithFormat:@"%@ (system browser)", [mi title]]];
if ([selectedIdentMail isEqualToString:ident])
selectedMail = mi;
if ([selectedIdentCal isEqualToString:ident])
selectedCal = mi;
[m addItem:mi];
[m addItem:[NSMenuItem separatorItem]];
mi = [mi copy];
[mi setAction:@selector(pickEventBrowser:)];
[em addItem:mi];
[em addItem:[NSMenuItem separatorItem]];
}
NSEnumerator *browserEnumerator = [list objectEnumerator];
while (ident = [browserEnumerator nextObject]) {
if (([ident isNotEqualTo:defaultIdent]) && (ident != nil) && ([[NSNull null] isNotEqualTo:ident])) {
mi = [self menuItemForBrowserAtURL:[NSURL fileURLWithPath:[[NSBundle bundleWithIdentifier:ident] bundlePath]]];
[m addItem:mi];
if ([selectedIdentMail isEqualToString:ident])
selectedMail = mi;
mi = [mi copy];
[mi setAction:@selector(pickEventBrowser:)];
[em addItem:mi];
if ([selectedIdentCal isEqualToString:ident])
selectedCal = mi;
}
}
[list release];
[chosenBrowser setMenu:[m autorelease]];
[eventChosenBrowser setMenu:[em autorelease]];
if (nil == selectedMail)
[chosenBrowser selectItemAtIndex:0];
else
[chosenBrowser selectItemWithTitle:[selectedMail title]];
// //NSLog(@"selected mail browser: %@, selected cal browser: %@", selectedIdentMail, selectedIdentCal);
// //NSLog(@"selected mail browser: %@, selected cal browser: %@", selectedMail, selectedCal);
if (nil == selectedCal)
[eventChosenBrowser selectItemAtIndex:0];
else
[eventChosenBrowser selectItemWithTitle:[selectedCal title]];
}
- (NSURL *)urlToDefaultBrowser {
NSURL *url;
LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.com/"],kLSRolesViewer,NULL,(CFURLRef *)&url);
return url;
}
- (NSMenuItem *)menuItemForBrowserAtURL:(NSURL *)url {
NSMenuItem *mi = [[NSMenuItem alloc] initWithTitle:@"" action:@selector(pickBrowser:) keyEquivalent:@""];
NSString *ident = [self identifierForBundle:url];
if (!((ident != nil) && ([[ident stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isNotEqualTo:@""])))
return nil;
NSString *browserName = [[NSFileManager defaultManager] displayNameAtPath:[url path]];
NSImage *browserIcon = [[NSWorkspace sharedWorkspace] iconForFile:[url path]];
[browserIcon setSize:NSMakeSize(16.0,16.0)];
[mi setTitle:browserName];
[mi setImage:browserIcon];
[mi setRepresentedObject:ident];
[mi setTarget:self];
[mi setEnabled:YES];
return [mi autorelease];
}
- (NSSet *)allBrowsers {
NSMutableSet *browsers = [NSMutableSet setWithCapacity:10];
NSArray *apps;
apps = [(NSArray *)LSCopyApplicationURLsForURL((CFURLRef)[NSURL URLWithString:@"http://www.google.com/"],kLSRolesViewer) autorelease];
NSEnumerator *appURLEnumerator = [apps objectEnumerator];
NSURL *appURL;
NSString *identifier;
while (appURL = [appURLEnumerator nextObject]) {
Boolean canHandleHTTPS;
if ((LSCanURLAcceptURL((CFURLRef)[NSURL URLWithString:@"https:"], (CFURLRef)appURL, kLSRolesAll, kLSAcceptDefault, &canHandleHTTPS) == noErr) && canHandleHTTPS) {
if (identifier = [self identifierForBundle:appURL]) {
if (([[identifier stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isNotEqualTo:@""]) && (identifier != nil)) {
[browsers addObject:identifier];
}
}
}
}
return browsers;
}
- (NSString*)identifierForBundle:(NSURL*)inBundleURL {
// //NSLog(@"G+G: identifier for bundle: %@", (inBundleURL ? inBundleURL : @"nil"));
if (nil == inBundleURL) return nil;
NSBundle* tmpBundle = [NSBundle bundleWithPath:[[inBundleURL path] stringByStandardizingPath]];
if (tmpBundle) {
NSString* tmpBundleID = [tmpBundle bundleIdentifier];
if ((tmpBundleID != nil) && ([[tmpBundleID stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isNotEqualTo:@""])) {
return tmpBundleID;
}
}
return nil;
}
- (void)windowDidResignKey:(NSNotification *)aNotification {
[self updateFields];
}
- (void)windowDidBecomeKey:(NSNotification *)aNotification {
[self updateFields];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
return YES;
}
@end