-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathClickBoxPref.m
More file actions
1646 lines (1414 loc) · 60.1 KB
/
ClickBoxPref.m
File metadata and controls
1646 lines (1414 loc) · 60.1 KB
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
/*
Copyright 2003-2010 Greg Schueler
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//
// ClickBoxPref.m
// ClickBox
//
// Created by Greg Schueler on Wed Jul 16 2003.
// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//
#import "ClickBoxPref.h"
extern double CornerClickVersionNumber;
@interface ClickBoxPref (InternalMethods)
- (NSArray *) quickScriptsList;
- (NSArray *) quickFilesList;
- (void) loadQuickItems;
@end
@implementation ClickBoxPref
- (void) mainViewDidLoad
{
appSettings = [self appSettings];
[readmeTextView readRTFDFromFile:[[self bundle] pathForResource:@"Information" ofType:@"rtf"]];
[readmeTextView setContinuousSpellCheckingEnabled: NO];
[versionStringField setStringValue: MARKETING_VERSION_STRING ];
[self loadQuickItems];
}
- (void) addActionMenuItems: (NSArray *) list toMenu: (NSMenu *) menu
{
NSInteger i;
BOOL x=YES;
for(i=0;i<[list count];i++){
id obj = [list objectAtIndex:i];
if([obj isKindOfClass:[NSArray class]]){
NSArray *arr = (NSArray *)obj;
NSMenu *newMenu = [[[NSMenu alloc] initWithTitle:(NSString *)[arr objectAtIndex:0]] autorelease];
[self addActionMenuItems: [arr subarrayWithRange:NSMakeRange(1,[arr count]-1)]
toMenu: newMenu];
if([newMenu numberOfItems] > 0){
NSMenuItem *subItem = [[[NSMenuItem alloc] initWithTitle:(NSString *)[arr objectAtIndex:0]
action:nil
keyEquivalent:@""] autorelease];
[menu addItem: subItem];
[menu setSubmenu:newMenu forItem:subItem];
x=NO;
}
continue;
}else if([obj isKindOfClass:[NSString class]]){
NSString *path = (NSString *)obj;
NSString *exp = [path stringByExpandingTildeInPath];
BOOL isDir=NO;
if([path isEqualToString:@"-"]){
if(!x)
[ menu addItem:[NSMenuItem separatorItem]];
x=YES;
}else if([[NSFileManager defaultManager] fileExistsAtPath:exp isDirectory: &isDir] || isDir){
[ menu addItem:[[[FileActionMenuItem alloc] initWithFilePath:exp andClickPref:self] autorelease]];
x=NO;
}
}
}
}
- (void) loadQuickItems
{
if(!loadedQuickFiles){
//quick apps
[quickFilePopup removeAllItems];
NSMenuItem *scriptItem = [[[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""] autorelease];
NSImage *scptIcon = [[[[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(kGenericApplicationIcon)] retain] autorelease];
[scptIcon setSize:NSMakeSize(16,16)];
[scriptItem setImage:scptIcon];
[scriptItem setTitle:LOCALIZE([self bundle],@"Files")];
[[quickFilePopup menu] addItem:scriptItem];
//[quickFilePopup addItemWithTitle:LOCALIZE([self bundle],@"Quick...")];
DEBUG(@"Bundle path: %@",[[self bundle] bundlePath]);
[[quickFilePopup menu] addItem:[[[FileActionMenuItem alloc] initWithFilePath: [[self bundle] bundlePath]
andClickPref: self
andTitle: LOCALIZE([self bundle], @"CornerClick Preferences")] autorelease]];
NSArray *list = [self quickFilesList];
//[self _storeList:list atPath:@"~/Desktop/quickFiles.plist"];
[self addActionMenuItems:list toMenu:[quickFilePopup menu]];
loadedQuickFiles=YES;
}
}
/*
- (void) _storeList: (id) plist atPath:(NSString *)aPath
{
NSString *path = [aPath stringByExpandingTildeInPath];
NSData *xmlData;
NSString *error;
xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if(xmlData)
{
NSLog(@"No error creating XML data.");
[xmlData writeToFile:path atomically:YES];
}
else
{
NSLog(error);
[error release];
}
}*/
+ (NSArray *) loadPlistArrayFromFile: (NSString *) path
{
NSData *plistData;
NSString *error;
id plist;
plistData = [NSData dataWithContentsOfFile:path];
plist = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:nil
errorDescription:&error];
if(!plist)
{
NSLog(@"Error: %@",error);
[error release];
return [[[NSArray alloc] init] autorelease];
}else if(! [plist isKindOfClass: [NSArray class]]){
NSLog(@"quickFiles.plist is not an Array");
return [[[NSArray alloc] init] autorelease];
}else{
return (NSArray *)plist;
}
}
- (void) loadQuickScripts
{
if(!loadedQuickScripts){
[quickScriptPopup removeAllItems];
NSMenuItem *scriptItem = [[[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""] autorelease];
NSImage *scptIcon = [[NSWorkspace sharedWorkspace] iconForFileType:@"scpt"];
[scptIcon setSize:NSMakeSize(16,16)];
[scriptItem setImage:scptIcon];
[scriptItem setTitle:LOCALIZE([self bundle],@"Scripts")];
[[quickScriptPopup menu] addItem:scriptItem];
//[quickScriptPopup addItemWithTitle:LOCALIZE([self bundle],@"Quick...")];
NSArray *list = [self quickScriptsList];
[self addActionMenuItems:list toMenu:[quickScriptPopup menu]];
/*
for(i=0;i<[list count];i++){
NSString *path = (NSString *)[list objectAtIndex:i];
[[quickScriptPopup menu] addItem:[[[FileActionMenuItem alloc] initWithFilePath:path andClickPref:self] autorelease]];
}*/
loadedQuickScripts=YES;
}
}
- (void) showQuickScriptsPopup
{
[self loadQuickScripts];
[quickScriptPopup setHidden:NO];
}
- (void) hideQuickScriptsPopup
{
[quickScriptPopup setHidden:YES];
}
- (NSArray *) quickFilesList
{
return [ClickBoxPref loadPlistArrayFromFile:
[[NSBundle bundleForClass:[self class]] pathForResource:@"quickFiles"
ofType:@"plist"]];
}
- (NSArray *) quickScriptsList
{
NSString *file;
BOOL isDir;
NSMutableArray *marr = [[[NSMutableArray alloc] init] autorelease];
NSArray *arrs = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSInteger i;
for(i=0;i<[arrs count];i++){
NSString *tempDir = [[[[arrs objectAtIndex:i] stringByAppendingString:LOCALIZE([self bundle],@"/Scripts")] retain] autorelease];
//NSLog(@"looking in dir for scripts: %@",tempDir);
isDir=NO;
if([[NSFileManager defaultManager] fileExistsAtPath:tempDir isDirectory:&isDir] && isDir){
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]
enumeratorAtPath:tempDir];
while (file = [enumerator nextObject]) {
OSType hfsType = [[enumerator fileAttributes] fileHFSTypeCode];
//NSLog(@"file has osType: %@",[[NSNumber numberWithUnsignedInt:hfsType] description]);
if ([[file pathExtension] isEqualToString:@"scpt"] || hfsType == 'osas'){
[marr addObject:[NSString stringWithFormat:@"%@/%@",tempDir,file]];
//NSLog(@"Added script with path: %@",[NSString stringWithFormat:@"%@/%@",tempDir,file]);
}
}
}
}
return marr;
}
/*- (IBAction)loadInformationRTF:(id)sender
{
[[NSWorkspace sharedWorkspace] openFile:[[self bundle] pathForResource:@"Information" ofType:@"rtf"]];
}*/
- (IBAction)loadReadmeRTF:(id)sender
{
[[NSWorkspace sharedWorkspace] openFile:[[self bundle] pathForResource:@"Readme" ofType:@"rtf"]];
}
- (IBAction)openExposePrefPane: (id) sender
{
[[NSWorkspace sharedWorkspace] openFile:@"/System/Library/PreferencePanes/Expose.prefPane"];
}
- (IBAction)checkVersionUpdate: (id) sender
{
}
- (CornerClickSettings *) appSettings
{
return [CornerClickSettings sharedSettings];
}
+ (void) initialize
{
[CornerClickSettings sharedSettingsFromUserPreferences];
NSLog(@"initialized ClickBoxPref");
}
- (void)setAppSettings: (CornerClickSettings *) settings
{
[settings retain];
[appSettings release];
appSettings=settings;
}
- (NSMutableDictionary *) mySettings
{
return mySettings;
}
- (void)setMySettings: (NSMutableDictionary *) settings
{
[settings retain];
[mySettings release];
mySettings=settings;
}
static NSString *UI_VIEW_CHANGE_CTX=@"UI_VIEW_CHANGE_CTX";
- (void) observeValueForKeyPath: (NSString *)path ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(object==[self appSettings] && context == UI_VIEW_CHANGE_CTX){
[self saveChanges];
}else if(object==currentAction){
[self saveChanges];
[actionTable reloadData];
}
}
- (void) didSelect
{
[[self appSettings] addObserver:self
forKeyPath:@"toolTipEnabled"
options:NSKeyValueObservingOptionNew
context:UI_VIEW_CHANGE_CTX];
[[self appSettings] addObserver:self
forKeyPath:@"toolTipDelayed"
options:NSKeyValueObservingOptionNew
context:UI_VIEW_CHANGE_CTX];
[[self appSettings] addObserver:self
forKeyPath:@"hoverDelayTime"
options:NSKeyValueObservingOptionNew
context:UI_VIEW_CHANGE_CTX];
[[self appSettings] addObserver:self
forKeyPath:@"tooltipDelayTime"
options:NSKeyValueObservingOptionNew
context:UI_VIEW_CHANGE_CTX];
[[self appSettings] addObserver:self
forKeyPath:@"textSize"
options:NSKeyValueObservingOptionNew
context:UI_VIEW_CHANGE_CTX];
[[self appSettings] addObserver:self
forKeyPath:@"iconSize"
options:NSKeyValueObservingOptionNew
context:UI_VIEW_CHANGE_CTX];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(saveChangesFromNotification:)
name:NSApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector:@selector(tableSelectionChanged:)
name:NSTableViewSelectionDidChangeNotification
object:actionTable];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(screenChangedNotification:)
name: @"NSApplicationDidChangeScreenParametersNotification"
object: nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver: self
selector: @selector(helperAppIsRunning:)
name: @"CornerClickPingBackNotification"
object: nil
suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
chosenCorner=0;
chosenScreen=0;
active=NO;
[appLaunchIndicator setStyle: NSProgressIndicatorSpinningStyle];
// [showTooltipCheckBox setState:[appSettings toolTipEnabled]?1:0];
// [delayTooltipCheckBox setState:[appSettings toolTipDelayed]?1:0];
// [delayTooltipCheckBox setEnabled: [appSettings toolTipEnabled]];
[appEnabledCheckBox setState: [appSettings appEnabled]];
[highlightColorWell setEnabled:YES];
if([appSettings highlightColor]!=nil){
[highlightColorWell setColor:[appSettings highlightColor]];
}else{
[highlightColorWell setColor:[NSColor blackColor]];
}
[highlightPopup selectItemAtIndex:[appSettings colorOption]];
[highlightColorWell setHidden:[appSettings colorOption]!=2];
//[bubbleColorWellA setColor:[appSettings bubbleColorA]];
//[bubbleColorWellA setEnabled: [appSettings toolTipEnabled]];
//[bubbleColorWellB setColor:[appSettings bubbleColorB]];
//[bubbleColorWellB setEnabled: [appSettings toolTipEnabled]];
//[[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
[[NSColorPanel sharedColorPanel] setTarget:nil];
[self checkScreens];
[self refreshWithCornerSettings];
[self refreshWithSettings:nil];
/*
[readmeTextView setEditable:YES];
[readmeTextView setSelectedRange: NSMakeRange([[readmeTextView string] length],0)];
[readmeTextView insertText:@"\n"];
[readmeTextView insertText: [self makeAttributedLink:@"mailto:greg-cc@vario.us" forString:@"greg-cc@vario.us"]];
[readmeTextView insertText:@"\n"];
[readmeTextView insertText: [self makeAttributedLink:@"http://greg.vario.us" forString:@"http://greg.vario.us"]];
[readmeTextView setEditable:NO];
[readmeTextView scrollRangeToVisible: NSMakeRange(0,0)];
*/
awaitingDisabledHelperNotification=NO;
reloadHelperOnHelperDeactivation=NO;
[self checkIfHelperAppRunning];
}
- (void) didUnselect
{
[highlightColorWell setEnabled:NO];
[[NSColorPanel sharedColorPanel] setTarget:nil];
[self saveChanges];
}
- (NSAttributedString *)makeAttributedLink:(NSString *) link forString:(NSString *) string
{
return
[[[NSAttributedString alloc] initWithString:string
attributes:
[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:[NSURL URLWithString:link],[NSColor blueColor],[NSNumber numberWithInteger:1],nil]
forKeys:
[NSArray arrayWithObjects:NSLinkAttributeName,NSForegroundColorAttributeName,NSUnderlineStyleAttributeName,nil]
]] autorelease];
}
- (void) checkScreens
{
NSInteger i;
NSArray *s;
if(allScreens !=nil)
[allScreens release];
s = [NSScreen screens];
allScreens = [[NSMutableArray arrayWithCapacity:[s count]] retain];
if(screenNums !=nil)
[screenNums release];
screenNums = [[NSMutableDictionary dictionaryWithCapacity:[s count]] retain];
for(i=0;i<[s count];i++){
[screenNums setObject:[s objectAtIndex:i] forKey:[[[s objectAtIndex:i] deviceDescription] objectForKey:@"NSScreenNumber"]];
[allScreens addObject:[[[s objectAtIndex:i] deviceDescription] objectForKey:@"NSScreenNumber"]];
}
[screenIDButton removeAllItems];
[screenIDButton addItemWithTitle:LOCALIZE([self bundle],@"Main Screen")];
if([allScreens count]>1){
[screenCountTextField setStringValue:[NSString stringWithFormat:LOCALIZE([self bundle],@"%ld screens"), (long)[allScreens count]]];
[screenCountTextField setHidden:NO];
[screenIDButton setEnabled:YES];
[cycleScreensButton setEnabled:YES];
for(i=1;i<[allScreens count]; i++){
//NSLog(@"add title for screen %d",(i+1));
[screenIDButton addItemWithTitle:[NSString stringWithFormat:LOCALIZE([self bundle],@"Screen #%ld"),(long)(i+1)]];
}
if(chosenScreen >= [allScreens count] || chosenScreen < 0){
chosenScreen=0;
[screenIDButton selectItemAtIndex: 0];
}
}else{
[screenCountTextField setHidden:YES];
[screenIDButton setEnabled:NO];
[cycleScreensButton setEnabled:NO];
chosenScreen=0;
}
[screenIDButton selectItemAtIndex: chosenScreen];
i=chosenScreen;
chosenScreen=-1;
[self doChooseScreen:i withPopupWindow:NO];
}
- (void) checkIfHelperAppRunning
{
//NSLog(@"send ping to app");
[[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"CornerClickPingAppNotification"
object: nil
userInfo: [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: [NSNumber numberWithDouble: CornerClickVersionNumber],[NSNumber numberWithInteger:CC_APP_VERSION],[NSNumber numberWithInteger:CC_APP_MIN_VERSION], [NSNumber numberWithInteger:CC_PATCH_VERSION], nil]
forKeys:
[NSArray arrayWithObjects: @"CornerClickAppVersionDouble", @"CornerClickAppVersion",@"CornerClickAppMinVersion", @"CornerClickPatchVersion",nil]]
deliverImmediately: YES];
}
- (void) setAutoLaunch: (BOOL) launch forApp: (NSString *)path
{
NSMutableDictionary *prefs;
NSDictionary *item;
NSMutableArray *mprefs;
NSInteger i;
prefs = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"loginwindow"] mutableCopy];
if(prefs!=nil){
mprefs = [[prefs objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];
for(i=0;i<[mprefs count];i++){
item = (NSDictionary *)[mprefs objectAtIndex: i];
if([path isEqualToString: [item objectForKey:@"Path"]]){
if(launch)
return;
else{
[mprefs removeObjectAtIndex: i];
[prefs setObject: mprefs forKey:@"AutoLaunchedApplicationDictionary"];
[[NSUserDefaults standardUserDefaults] setPersistentDomain: prefs forName:@"loginwindow"];
[[NSUserDefaults standardUserDefaults] synchronize];
return;
}
}
}
if(launch){
[mprefs addObject: [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: path, [NSNumber numberWithBool:NO],nil]
forKeys: [NSArray arrayWithObjects: @"Path", @"Hide",nil]
]
];
[prefs setObject: mprefs forKey:@"AutoLaunchedApplicationDictionary"];
[[NSUserDefaults standardUserDefaults] setPersistentDomain: prefs forName:@"loginwindow"];
[[NSUserDefaults standardUserDefaults] synchronize];
return;
}
}
}
- (void) alertSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
switch(returnCode){
case NSAlertDefaultReturn:
//DEBUG(@"disabling and enabling");
//reloadHelperOnHelperDeactivation=YES;
//[self deactivateHelper];
break;
case NSAlertAlternateReturn:
DEBUG(@"Open readme file");
[self loadReadmeRTF:self];
break;
case NSAlertErrorReturn:
DEBUG(@"Error running alert sheet");
break;
default:
DEBUG(@"Unknown returnCode in alertSheetDidEnd");
}
}
- (void) helperAppIsRunning: (NSNotification *) notice
{
NSNumber *majnum = [[notice userInfo] objectForKey:@"CornerClickAppVersion"];
NSNumber *minnum = [[notice userInfo] objectForKey:@"CornerClickAppMinVersion"];
NSNumber *patnum = [[notice userInfo] objectForKey:@"CornerClickPatchVersion"];
NSNumber *numf = [[notice userInfo] objectForKey:@"CornerClickAppVersionDouble"];
NSInteger maj = [majnum integerValue];
NSInteger min = (nil==minnum?0:[minnum integerValue]);
NSInteger pat = (nil==patnum?0:[patnum integerValue]);
NSInteger vers = maj*1000 + min*100 + pat;
NSInteger cpat= (NSInteger) ( CC_PATCH_VERSION );
NSInteger cmin= (NSInteger) ( CC_APP_MIN_VERSION );
NSInteger cmaj =(NSInteger)(CC_APP_VERSION);
NSInteger cvers = cmaj *1000 + cmin*100 + cpat;
active=YES;
if(nil==numf || [numf doubleValue] != CornerClickVersionNumber || vers!=cvers ){
reloadHelperOnHelperDeactivation=YES;
[self deactivateHelper];
if(nil!=numf && vers >0 && cvers > 0){
NSBeginAlertSheet(LOCALIZE([self bundle],@"New CornerClick Version"),
LOCALIZE([self bundle],@"OK"),
LOCALIZE([self bundle],@"Open Readme File"),
nil, [NSApp mainWindow], self, @selector(alertSheetDidEnd:returnCode:contextInfo:), NULL, NULL,
LOCALIZE([self bundle],@"An old version of CornerClick was active (version %ld.%ld.%ld). It will be deactivated and version %ld.%ld.%ld will be activated."),(long)maj,(long)min,(long)pat,(long)cmaj,(long)cmin,(long)cpat);
}
}
[appLaunchIndicator stopAnimation:self];
[appEnabledCheckBox setState: 1 ];
}
- (void) helperAppDidTerminate: (NSNotification *)notice
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self
name:@"CornerClickDisableAppReplyNotification"
object:nil];
if(disableTimer!=nil){
[disableTimer invalidate];
[disableTimer release];
disableTimer=nil;
}
[appLaunchErrorLabel setStringValue: @""];
[appLaunchIndicator stopAnimation:self];
[appEnabledCheckBox setState:0];
active=NO;
[appSettings setAppEnabled:active];
//[appPrefs setObject:[NSNumber numberWithInt: active?1:0] forKey:@"appEnabled"];
awaitingDisabledHelperNotification=NO;
[appEnabledCheckBox setEnabled:YES];
if(reloadHelperOnHelperDeactivation){
reloadHelperOnHelperDeactivation=NO;
[self activateHelper];
}
}
- (void) helperAppTerminateTimeout
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self
name:@"CornerClickDisableAppReplyNotification"
object:nil];
if(disableTimer!=nil){
[disableTimer invalidate];
[disableTimer release];
disableTimer=nil;
}
//NSLog(@"Failed to quit bg app");
[appLaunchIndicator stopAnimation:self];
[appLaunchErrorLabel setStringValue: @""];
NSBeginAlertSheet(LOCALIZE([self bundle],@"Deactivate Failed"),
LOCALIZE([self bundle],@"OK"),
nil,nil, [NSApp mainWindow], self, NULL, NULL, NULL,
LOCALIZE([self bundle],@"The CornerClick helper application could not be deactivated."));
//[appLaunchErrorLabel setStringValue: @"Couldn't quit helper application"];
[appEnabledCheckBox setState:1];
[appEnabledCheckBox setNeedsDisplay: YES];
active=YES;
[appSettings setAppEnabled:active];
//[appPrefs setObject:[NSNumber numberWithInt: active?1:0] forKey:@"appEnabled"];
awaitingDisabledHelperNotification=NO;
[appEnabledCheckBox setEnabled:YES];
}
- (void) deactivateHelper
{
NSString *apppath;
apppath = [[self bundle] pathForResource:@"CornerClickBG" ofType:@"app"];
if(disableTimer==nil){
[appLaunchIndicator startAnimation:self];
//register for app terminated messages
[[NSDistributedNotificationCenter defaultCenter] addObserver: self
selector: @selector(helperAppDidTerminate:)
name: @"CornerClickDisableAppReplyNotification"
object: nil
suspensionBehavior:NSNotificationSuspensionBehaviorCoalesce];
NSInvocation *nsinv = [NSInvocation invocationWithMethodSignature: [self methodSignatureForSelector:@selector(helperAppTerminateTimeout)]];
[nsinv setSelector:@selector(helperAppTerminateTimeout)];
[nsinv setTarget:self];
disableTimer = [[NSTimer scheduledTimerWithTimeInterval:20 invocation:nsinv repeats:NO] retain];
[self setAutoLaunch:NO forApp:apppath];
}
[[NSDistributedNotificationCenter defaultCenter] postNotificationName: @"CornerClickDisableAppNotification"
object: nil
userInfo:nil
deliverImmediately:YES];
[appLaunchErrorLabel setStringValue: LOCALIZE([self bundle],@"Trying to deactivate")];
awaitingDisabledHelperNotification=YES;
[appEnabledCheckBox setState:0];
[appEnabledCheckBox setEnabled:NO];
}
- (void) activateHelper
{
BOOL success;
NSString *apppath;
apppath = [[self bundle] pathForResource:@"CornerClickBG" ofType:@"app"];
//[appLaunchErrorLabel setStringValue: @""];
[appLaunchIndicator startAnimation:self];
[appLaunchErrorLabel setStringValue: LOCALIZE([self bundle],@"Trying to activate")];
[appEnabledCheckBox setEnabled:NO];
[self saveChanges];
if(apppath ==nil)
success=NO;
else
success = [[NSWorkspace sharedWorkspace] launchApplication: apppath];
if(!success){
[appLaunchIndicator stopAnimation:self];
[appEnabledCheckBox setState:0];
[appLaunchErrorLabel setStringValue: @""];
NSBeginAlertSheet(LOCALIZE([self bundle],@"Activate Failed"),
LOCALIZE([self bundle],@"OK"),
nil,nil, [NSApp mainWindow], self, NULL, NULL, NULL,
LOCALIZE([self bundle],@"The CornerClick helper application could not be launched."));
active=NO;
}else{
[self setAutoLaunch:YES forApp:apppath];
[appLaunchErrorLabel setStringValue: @""];
[appLaunchIndicator stopAnimation:self];
[appEnabledCheckBox setState:1];
active=YES;
}
[appEnabledCheckBox setEnabled:YES];
}
- (IBAction)tableViewAction:(id)sender
{
//NSLog(@"table view action: %@",sender);
}
- (IBAction)appEnable:(id)sender
{
//appLaunchIndicator
if(active && [sender state]==0){
[self deactivateHelper];
}else if(active && awaitingDisabledHelperNotification){
[sender setState:0];
return;
}else if(!active && [sender state]){
[self activateHelper];
}
//[appPrefs setObject:[NSNumber numberWithInt: active?1:0] forKey:@"appEnabled"];
}
- (void) refreshWithCornerSettings
{
[enabledCheckBox setState:[appSettings cornerEnabled:chosenCorner forScreen:[allScreens objectAtIndex:chosenScreen]]];
//[appEnabledCheckBox setState:[[appPrefs objectForKey:@"appEnabled"] intValue]];
}
- (void) refreshWithSettings:(ClickAction *)theAction
{
NSInteger flags=0; //NSLog(@"retain count of settings: %d",[settings retainCount]);
if(theAction!=nil){
flags = [theAction modifiers];
[removeActionButton setEnabled:YES];
[optionKeyCheckBox setEnabled:YES];
[shiftKeyCheckBox setEnabled:YES];
[controlKeyCheckBox setEnabled:YES];
[commandKeyCheckBox setEnabled:YES];
[functionKeyCheckBox setEnabled:YES];
//NSLog(@"flags (%d) & OPTION_MASK (%d) : %d",flags,OPTION_MASK,(flags & OPTION_MASK));
[optionKeyCheckBox setState: ((flags & OPTION_MASK) > 0 ? NSOnState: NSOffState)];
[shiftKeyCheckBox setState: ((flags & SHIFT_MASK) > 0 ? NSOnState: NSOffState)];
[controlKeyCheckBox setState: ((flags & CONTROL_MASK) > 0 ? NSOnState: NSOffState)];
[commandKeyCheckBox setState: ((flags & COMMAND_MASK) > 0 ? NSOnState: NSOffState)];
[functionKeyCheckBox setState: ((flags & FN_MASK) > 0 ? NSOnState: NSOffState)];
[actionChoicePopupButton setEnabled:YES];
[triggerChoicePopupButton setEnabled:YES];
[actionChoicePopupButton selectItemAtIndex: [theAction type]];
[triggerChoicePopupButton selectItemAtIndex: [theAction trigger]]; //TODO handle other types of triggers
[hoverTriggerDelayedCheckbox setEnabled:[theAction trigger]==TRIGGER_HOVER];
[hoverTriggerDelayedCheckbox setHidden:[theAction trigger]!=TRIGGER_HOVER];
[hoverTriggerDelayedCheckbox setState: ([theAction trigger]==TRIGGER_HOVER && [theAction hoverTriggerDelayed]) ? NSOnState:NSOffState];
NSString *str = [theAction string];
NSString *label = [theAction labelSetting];
switch([theAction type]){
case ACT_FILE:
if(str!=nil){
if([str isEqualToString:[[self bundle] bundlePath]]){
[chosenFileTitle setStringValue: LOCALIZE([self bundle],@"CornerClick Preferences")];
}else if([str hasSuffix:@".app"]){
[chosenFileTitle setStringValue: [[str lastPathComponent] stringByDeletingPathExtension]];
}else{
[chosenFileTitle setStringValue: [str lastPathComponent]];
}
[chosenFileLabel setStringValue: str];
[fileIconImageView setImage: [[NSWorkspace sharedWorkspace] iconForFile: str]];
}else{
[chosenFileTitle setStringValue: LOCALIZE([self bundle],@"no file chosen")];
[chosenFileLabel setStringValue: @""];
[fileIconImageView setImage: nil];
}
DEBUG(@"set choose button hidden: no");
[fileChooseButton setHidden:NO];
[quickFilePopup setHidden:NO];
[actionChoicePopupButton setNextKeyView: quickFilePopup];
[fileChooseButton setNextKeyView: screenIDButton];
[self hideQuickScriptsPopup];
break;
case ACT_URL:
if(str!=nil){
[urlTextField setStringValue:str];
}else{
[urlTextField setStringValue:@"http://"];
}
if(label!=nil){
[urlLabelField setStringValue:label];
}else{
[urlLabelField setStringValue:@""];
}
[fileChooseButton setHidden:YES];
[quickFilePopup setHidden:YES];
[actionChoicePopupButton setNextKeyView: urlTextField];
[self hideQuickScriptsPopup];
break;
case ACT_SCPT:
if(str!=nil){
[chosenScriptLabel setStringValue: str];
[scriptIconImageView setImage: [[NSWorkspace sharedWorkspace] iconForFile: str]];
}else{
[chosenScriptLabel setStringValue: LOCALIZE([self bundle],@"no script chosen")];
[scriptIconImageView setImage: nil];
}
if(label!=nil){
[scriptLabelField setStringValue: label];
}else{
[scriptLabelField setStringValue: @""];
}
[fileChooseButton setHidden:NO];
[quickFilePopup setHidden:YES];
[actionChoicePopupButton setNextKeyView: quickScriptPopup];
[fileChooseButton setNextKeyView: urlLabelField];
[self showQuickScriptsPopup];
break;
default:
[fileChooseButton setHidden:YES];
[quickFilePopup setHidden:YES];
[actionChoicePopupButton setNextKeyView: screenIDButton];
[self hideQuickScriptsPopup];
break;
}
[self setSubFrameForActionType: [theAction type]];
}else{
[fileChooseButton setHidden:YES];
[quickFilePopup setHidden:YES];
[self hideQuickScriptsPopup];
[removeActionButton setEnabled:NO];
[optionKeyCheckBox setEnabled:NO];
[shiftKeyCheckBox setEnabled:NO];
[controlKeyCheckBox setEnabled:NO];
[commandKeyCheckBox setEnabled:NO];
[functionKeyCheckBox setEnabled:NO];
[optionKeyCheckBox setState:NSOffState];
[shiftKeyCheckBox setState:NSOffState];
[controlKeyCheckBox setState:NSOffState];
[commandKeyCheckBox setState:NSOffState];
[functionKeyCheckBox setState:NSOffState];
[actionChoicePopupButton setEnabled:NO];
[triggerChoicePopupButton setEnabled:NO];
[hoverTriggerDelayedCheckbox setEnabled:NO];
[hoverTriggerDelayedCheckbox setHidden:YES];
[hoverTriggerDelayedCheckbox setState:NSOffState];
[self setSubFrameForActionType: -1];
[chosenFileLabel setStringValue:@""];
[chosenFileTitle setStringValue:@""];
[fileIconImageView setImage:nil];
[urlTextField setStringValue:@""];
[urlLabelField setStringValue:@""];
[chosenScriptLabel setStringValue:@""];
[scriptLabelField setStringValue:@""];
[scriptIconImageView setImage:nil];
}
}
- (void)openSheetDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
NSString *thefile;
//NSLog(@"Sheet finished");
[sheet orderOut: self];
if(returnCode==NSOKButton){
thefile = [[[sheet filenames] objectAtIndex:0] retain];
[self setSelectedActionPath: thefile];
}
}
- (void) setSelectedActionPath: (NSString *) thefile
{
[self setSelectedActionPath:thefile resettingScriptLabel:NO];
}
- (void) setSelectedActionPath: (NSString *) thefile resettingScriptLabel:(BOOL) doReset
{
switch([currentAction type]){
case ACT_FILE:
[currentAction setString:thefile];
[currentAction setLabelSetting:nil];
if([thefile isEqualToString:[[self bundle] bundlePath]]){
[chosenFileTitle setStringValue: LOCALIZE([self bundle],@"CornerClick Preferences")];
[currentAction setLabelSetting: LOCALIZE([self bundle],@"CornerClick Preferences")];
}else if([thefile hasSuffix:@".app"]){
[chosenFileTitle setStringValue: [[thefile lastPathComponent] stringByDeletingPathExtension]];
}else{
[chosenFileTitle setStringValue: [thefile lastPathComponent]];
}
[chosenFileLabel setStringValue: thefile];
[fileIconImageView setImage: [[NSWorkspace sharedWorkspace] iconForFile: thefile]];
break;
case ACT_SCPT:
[currentAction setString:thefile];
[currentAction setLabelSetting:nil];
[chosenScriptLabel setStringValue: thefile];
[scriptIconImageView setImage: [[NSWorkspace sharedWorkspace] iconForFile: thefile]];
if(doReset || [[scriptLabelField stringValue] length] == 0){
[scriptLabelField setStringValue: [[thefile lastPathComponent] stringByDeletingPathExtension]];
}
[scriptLabelField selectText:self];
break;
}
[self saveChanges];
[actionTable reloadData];
[thefile release];
}
- (void) urlEntered: (id) sender
{
switch([currentAction type]){
case ACT_URL:
[currentAction setString:[urlTextField stringValue]];
if([[urlLabelField stringValue] length] > 0){
[currentAction setLabelSetting:[urlLabelField stringValue]];
}else{
[currentAction setLabelSetting:nil];
}
break;
case ACT_SCPT:
if([[scriptLabelField stringValue] length] > 0){
[currentAction setLabelSetting:[scriptLabelField stringValue]];
}else{
[currentAction setLabelSetting:nil];
}
break;
}
//[self notifyAppOfPreferences: [appSettings asDictionary]];
[self saveChanges];
[actionTable reloadData];
}
- (void) saveChangesFromNotification:(NSNotification*)aNotification
{
[self saveChanges];
}
- (void) screenChangedNotification: (NSNotification *)notice
{
[self checkScreens];
}
- (void) saveChanges
{
[CornerClickSupport savePreferences:[CornerClickSettings sharedSettings]];
[self notifyAppOfPreferences: [appSettings asDictionary]];
}
- (void) notifyAppOfPreferences: (NSDictionary *) prefs
{
//notify the app if it's running
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName: @"CornerClickLoadPrefsNotification" object: nil
userInfo:prefs
deliverImmediately:YES];
}
+ (NSString *)ordinalForNumber: (NSInteger) which
{
switch(which){
case 1: return @"A";
case 2: return @"B";
case 3: return @"C";
case 4: return @"D";
case 5: return @"E";
case 6: return @"F";
default:
return @"ZZZZ";
}
}
- (IBAction)actionChosen:(id)sender
{
//NSLog(@"Choose action: %d",[sender indexOfSelectedItem]);
NSInteger oldval = [currentAction type];
if(oldval==[sender indexOfSelectedItem]){
return;
}
[currentAction setType: [sender indexOfSelectedItem] ];
[currentAction setString:nil];
[currentAction setLabelSetting:nil];
//DEBUG(@"before refresh settings");
[self refreshWithSettings:currentAction];
//DEBUG(@"after refresh settings");
//[self setSubFrameForActionType: [sender indexOfSelectedItem]];
[self saveChanges];
//DEBUG(@"reload data");