This repository was archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRingFactory.m
More file actions
1781 lines (1442 loc) · 57.3 KB
/
Copy pathRingFactory.m
File metadata and controls
1781 lines (1442 loc) · 57.3 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
//
// RingFactory.m
//
// Copyright 2009 SwiftRing. All rights reserved.
//
#import <Carbon/Carbon.h>
#import "RingFactory.h"
#import "RingView.h"
#import "Ring.h"
#import "Debug.h"
#import "RecordingPanel.h"
#import "OverlayWindow.h"
#define TAG_OUTLINE_SAVED_RINGS 0
#define TAG_OUTLINE_APPLICATIONS 1
#define SEGMENT_NORMAL 0
#define SEGMENT_SCROLL_UP 1
#define SEGMENT_SCROLL_DOWN 2
#define SEGMENT_INVALID 3
#define COLUMN_SEGMENT 0
#define COLUMN_LABEL 1
#define COLUMN_SUBRING 2
#define COLUMN_KEY_SEQUENCE 3
static NSDictionary *pPrettyNameLookup = nil;
// Utility functions
NSDecimalNumber* bigMod(NSDecimalNumber *dividend, NSDecimalNumber *divisor)
{
NSDecimalNumber *quotient = [dividend decimalNumberByDividingBy: divisor withBehavior:
[NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundDown
scale:0
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:NO]];
NSDecimalNumber *subtractAmount = [quotient decimalNumberByMultiplyingBy:divisor];
NSDecimalNumber *remainder = [dividend decimalNumberBySubtracting:subtractAmount];
return remainder;
}
BOOL validateKey(NSNumber *pMessage)
{
unsigned long long exponent = 17;
NSDecimalNumber *message = [NSDecimalNumber decimalNumberWithDecimal: [pMessage decimalValue]];
NSDecimalNumber *modulus = [NSDecimalNumber decimalNumberWithString: @"191601096808489"];
NSDecimalNumber *result = [NSDecimalNumber decimalNumberWithString: @"1"];
while (exponent > 0)
{
if (exponent & 1)
{
result = bigMod([result decimalNumberByMultiplyingBy: message], modulus);
}
exponent = exponent >> 1;
message = bigMod([message decimalNumberByMultiplyingBy: message], modulus);
}
NSString *appVerString = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSNumber *appVer = [NSNumber numberWithFloat:[appVerString floatValue]];
unsigned int ver = [appVer unsignedIntValue];
unsigned long long plainKey = [[result stringValue] longLongValue];
DebugLog(@"App Major Version: %i, PlainKey %llu", ver, plainKey);
if (((plainKey >> 28) & 0xFF) == 0x5F)
{
// Passed the 0x5F prefix check
if (((plainKey >> 20) & 0xFF) >= ver)
{
// Passed the version check, we have ourselves a valid key!
return YES;
}
else
{
DebugLog(@"Key version too old | key = v%i, version = v%i", ((plainKey >> 20) & 0xFF), ver);
}
}
else
{
DebugLog(@"Invalid key");
}
return NO;
}
@implementation RingFactory
- (void)awakeFromNib
{
BOOL isDir;
pSupportDirectory = [[NSString alloc] initWithString:[[[NSHomeDirectory()
stringByAppendingPathComponent:@"Library"]
stringByAppendingPathComponent:@"Application Support"]
stringByAppendingPathComponent:@"SwiftRing"]];
pPreferencesDirectory = [[NSString alloc] initWithString:[[[[NSHomeDirectory()
stringByAppendingPathComponent:@"Library"]
stringByAppendingPathComponent:@"Application Support"]
stringByAppendingPathComponent:@"SwiftRing"]
stringByAppendingPathComponent:@"Preferences"]];
pBundleDirectory = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"Config Files"]];
if (![[NSFileManager defaultManager] fileExistsAtPath: pSupportDirectory isDirectory: &isDir] || !isDir)
{
// The Application Support directory does not exist, create it and
// copy everything from the bundle into the Application Support directory first
[[NSFileManager defaultManager] copyItemAtPath: pBundleDirectory
toPath: pSupportDirectory
error: NULL];
DebugLog(@"Copied stuff into the support directory - Bundle: %@ Support: %@", pBundleDirectory, pSupportDirectory);
}
//
// Initialize the config/application lookup tables, start w/ 100 entries
// (mutable dictionary adds more if needed)
//
pConfigLookup = [[NSMutableDictionary alloc] initWithCapacity: 100];
pAppLookup = [[NSMutableDictionary alloc] initWithCapacity: 100];
pPrefs = [[NSMutableDictionary alloc] initWithCapacity: 10];
pFileDeleteList = [[NSMutableArray alloc] initWithCapacity: 10];
pApplicationsOutlineData = [[NSMutableArray alloc] initWithCapacity: 20];
pApplicationsOutlineDataPreSelected = [[NSMutableIndexSet alloc] init];
// Load the config data from the XML files
[self reloadConfigs];
}
- (void) reloadConfigs
{
NSArray *pConfigFiles;
NSUInteger numConfigFiles;
NSUInteger i;
NSData *pXml;
NSMutableDictionary *pDict;
NSString *pErrorDesc;
NSPropertyListFormat format;
NSArray *pApps;
NSEnumerator *pAppsEnum;
NSString *pAppName;
BOOL isDir;
BOOL validRegKey = NO;
[pConfigLookup removeAllObjects];
[pAppLookup removeAllObjects];
[pFileDeleteList removeAllObjects];
pPreviewRingParent = nil;
pPreviewRing = nil;
previewRingIsSubring = NO;
isSavedRingSelected = NO;
// Grab the program wide preferences
pDict = NULL;
pXml = NULL;
pErrorDesc = NULL;
// Grab the XML file into a data object
pXml = [[NSFileManager defaultManager] contentsAtPath:[pPreferencesDirectory stringByAppendingPathComponent:
@"Preferences.plist"]];
// Convert it into a dictionary
pDict = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:pXml
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&pErrorDesc];
[pPrefs addEntriesFromDictionary: pDict];
// Make sure everything went ok with the conversion
if (NULL == pPrefs)
{
DebugLog(@"%@", pErrorDesc);
[pErrorDesc release];
}
else
{
NSNumber *launchKey = [pPrefs objectForKey: @"LaunchKey"];
NSNumber *menuDelay = [pPrefs objectForKey: @"Delay"];
NSNumber *allowArrows = [pPrefs objectForKey: @"Arrows"];
NSNumber *enableMenuBar = [pPrefs objectForKey: @"MenuBar"];
NSNumber *regKey = [pPrefs objectForKey: @"RegKey"];
if (allowArrows == nil)
{
allowArrows = [NSNumber numberWithInt:0];
}
if (enableMenuBar == nil)
{
enableMenuBar = [NSNumber numberWithInt:1];
}
if (regKey != nil)
{
// Validate the registration key
if (validateKey(regKey))
{
DebugLog(@"Valid registration key at boot!");
[regKeyStatus setStringValue: @"Full Version"];
[regButton setEnabled: NO];
validRegKey = YES;
}
}
DebugLog(@"LaunchKey = %i, Delay = %f, Arrows = %i, Menu Bar = %i",
[launchKey intValue],
[menuDelay floatValue],
[allowArrows intValue],
[enableMenuBar intValue]);
//DebugLog(@"%@",NSStringFromClass([[pPrefs objectForKey: @"Delay"] class]));
[pLaunchKey selectItemWithTag:[launchKey integerValue]];
[pAllowArrows setState: [allowArrows intValue] > 0 ? NSOnState:NSOffState];
[pMenuDelay setFloatValue:[menuDelay floatValue]];
[pMenuDelayText setFloatValue:[menuDelay floatValue]];
[pEnableMenuBar setState:[enableMenuBar intValue] > 0 ? NSOnState:NSOffState];
[overlayWindow setLaunchKey:[launchKey intValue]];
[overlayWindow setAllowArrows:[allowArrows intValue] > 0 ? YES : NO];
[overlayWindow setMenuDelay:[menuDelay floatValue]];
[overlayWindow setEnableMenuBar:[enableMenuBar intValue] > 0 ? YES : NO];
[overlayWindow setValidKey: validRegKey];
}
// Enumerate all the items in the support directory
pConfigFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: pSupportDirectory
error: NULL];
numConfigFiles = [pConfigFiles count];
for (i = 0; i < numConfigFiles; i++)
{
DebugLog(@"%@", [pConfigFiles objectAtIndex: i]);
[[NSFileManager defaultManager] fileExistsAtPath:[pSupportDirectory stringByAppendingPathComponent:
[pConfigFiles objectAtIndex: i]]
isDirectory:&isDir];
if (isDir)
{
// The file is a directory, skip it
continue;
}
pDict = NULL;
pXml = NULL;
pErrorDesc = NULL;
// Grab the XML file into a data object
pXml = [[NSFileManager defaultManager] contentsAtPath:[pSupportDirectory stringByAppendingPathComponent:
[pConfigFiles objectAtIndex: i]]];
// Convert it into a dictionary
pDict = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:pXml
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&pErrorDesc];
// Make sure everything went ok with the conversion
if (NULL == pDict)
{
DebugLog(@"%@", pErrorDesc);
[pErrorDesc release];
continue;
}
// Add an entry to the config lookup table, indexed by the 'Name' field
[pConfigLookup setObject: pDict forKey: [pDict objectForKey: @"Name"]];
// Add entries to the app lookup table, indexed by the 'Apps' field
pApps = [[pDict objectForKey: @"Apps"] componentsSeparatedByString:@","];
if (NULL == pApps)
{
DebugLog(@"awakeFromNib: Apps key not found!");
continue;
}
pAppsEnum = [pApps objectEnumerator];
// Loop through all the app names, adding them to the app lookup table
while (pAppName = [pAppsEnum nextObject])
{
if ([pAppName localizedCaseInsensitiveCompare: @""] != NSOrderedSame)
{
[pAppLookup setObject: pDict forKey: pAppName];
}
}
}
// Print out the config lookup
for (id key in pConfigLookup)
{
DebugLog(@"reload key: '%@' value: '%@'", key, [pConfigLookup objectForKey:key]);
}
// Print out the app lookup
for (id key in pAppLookup)
{
DebugLog(@"reload key: '%@' value: '%@'", key, [pAppLookup objectForKey:key]);
}
}
+ (void) initialize
{
//
// Create the keycode to pretty name lookup dictionary
//
pPrettyNameLookup = [[NSDictionary alloc]
initWithObjects: [NSArray arrayWithObjects:
@"return",
@"tab",
@"space",
@"delete",
@"esc",
@"command", // right
@"command",
@"shift",
@"capsLock",
@"option",
@"control",
@"shift", // right
@"option", // right
@"control", // right
@"fn",
@"F17",
@"volumeUp",
@"volumeDown",
@"mute",
@"F18",
@"F19",
@"F20",
@"F5",
@"F6",
@"F7",
@"F3",
@"F8",
@"F9",
@"F11",
@"F13",
@"F16",
@"F14",
@"F10",
@"F12",
@"F15",
@"help",
@"home",
@"pageUp",
@"forwardDelete",
@"F4",
@"end",
@"F2",
@"pageDown",
@"F1",
@"left",
@"right",
@"down",
@"up",
@"dim",
@"bright",
@"expose",
@"dashboard",
nil]
forKeys: [NSArray arrayWithObjects:
@"36",
@"48",
@"49",
@"51",
@"53",
@"54",
@"55",
@"56",
@"57",
@"58",
@"59",
@"60",
@"61",
@"62",
@"63",
@"64",
@"72",
@"73",
@"74",
@"79",
@"80",
@"90",
@"96",
@"97",
@"98",
@"99",
@"100",
@"101",
@"103",
@"105",
@"106",
@"107",
@"109",
@"111",
@"113",
@"114",
@"115",
@"116",
@"117",
@"118",
@"119",
@"120",
@"121",
@"122",
@"123",
@"124",
@"125",
@"126",
@"145",
@"144",
@"160",
@"130",
nil]
];
}
+ (NSString *) prettyNameForKeycode: (NSString *) pKeyCode
{
return (nil == pPrettyNameLookup) ? nil : [pPrettyNameLookup objectForKey: pKeyCode];
}
+ (NSString *) prettyPrintKeySequence: (NSString *) pKeySequence
{
NSString *pKeyCode;
NSMutableString *pPrettyString = [[NSMutableString alloc] initWithCapacity: 30];
NSEnumerator *pKeyCodeEnum = [[pKeySequence componentsSeparatedByString: @" "] objectEnumerator];
// Loop through all the keycodes
while ((pKeyCode = [pKeyCodeEnum nextObject]) && (NSOrderedSame != [pKeyCode compare: @""]))
{
// Keycodes and up/down indication are seperated by underscores
NSArray *pKeyUpDownArray = [pKeyCode componentsSeparatedByString:@"_"];
if ([[pKeyUpDownArray objectAtIndex: 1] boolValue])
{
// Only add the key downs to the pretty string
[pPrettyString appendFormat: @"%@ ", [RingFactory stringForKeyCode: [[pKeyUpDownArray objectAtIndex: 0] integerValue]
withModifierFlags: 0]];
}
}
return pPrettyString;
}
+ (NSString *) stringForKeyCode: (unsigned short) keyCode withModifierFlags: (NSUInteger) modifierFlags
{
// Try to get a pretty name for special characters
NSString *pPrettyName = nil;
pPrettyName = [RingFactory prettyNameForKeycode:[NSString stringWithFormat:@"%i", keyCode]];
if (pPrettyName != nil)
{
return NSLocalizedString(([NSString stringWithFormat:@"%@", pPrettyName, nil]), @"Friendly Key Name");
}
// Get the name the hard way now
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
if (!currentKeyboard)
{
return NSLocalizedString(([NSString stringWithFormat:@"?", nil]), @"Friendly Key Name");
}
CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
CFRelease(currentKeyboard);
// For non-unicode layouts such as Chinese, Japanese, and Korean, get the ASCII capable layout
if (!uchr)
{
currentKeyboard = TISCopyCurrentASCIICapableKeyboardLayoutInputSource();
uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
CFRelease(currentKeyboard);
}
if (!uchr)
{
return NSLocalizedString(([NSString stringWithFormat:@"?", nil]), @"Friendly Key Name");
}
const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr);
if (keyboardLayout)
{
UInt32 deadKeyState = 0;
UniCharCount maxStringLength = 255;
UniCharCount actualStringLength = 0;
UniChar unicodeString[maxStringLength];
OSStatus status = UCKeyTranslate(keyboardLayout,
keyCode, kUCKeyActionDown, modifierFlags,
LMGetKbdType(), 0,
&deadKeyState,
maxStringLength,
&actualStringLength, unicodeString);
if (actualStringLength == 0 && deadKeyState)
{
status = UCKeyTranslate(keyboardLayout,
kVK_Space, kUCKeyActionDown, 0,
LMGetKbdType(), 0,
&deadKeyState,
maxStringLength,
&actualStringLength, unicodeString);
}
if (actualStringLength > 0 && status == noErr)
{
return [NSString stringWithCharacters:unicodeString length:(NSUInteger)actualStringLength];
}
}
/*
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*) CFDataGetBytePtr(uchr);
else if (keyboardLayout)
{
UInt32 deadKeyState;
UniCharCount maxStringLength = 255;
UniCharCount actualStringLength;
UniChar unicodeString[maxStringLength];
OSStatus status = UCKeyTranslate(keyboardLayout,
keyCode, kUCKeyActionDown, modifierFlags,
CGEventSourceGetKeyboardType((CGEventSourceRef)currentKeyboard), 0,
&deadKeyState,
maxStringLength,
&actualStringLength, unicodeString);
if(status != noErr)
{
DebugLog(@"There was an %s error translating from the '%d' key code to a human readable string: %s",
GetMacOSStatusErrorString(status), status, GetMacOSStatusCommentString(status));
}
else if(actualStringLength > 0)
{
return [NSString stringWithCharacters: unicodeString length: (NSInteger) actualStringLength];
}
else
{
DebugLog(@"Couldn't find a translation for the '%d' key code", keyCode);
}
}
else
{
DebugLog(@"Couldn't find a suitable keyboard layout from which to translate");
}
*/
// Default name is just a question mark
return NSLocalizedString(([NSString stringWithFormat:@"?", nil]), @"Friendly Key Name");
}
- (Ring *) createRing: (float) centerX: (float) centerY: (bool) isPreview
{
NSDictionary *pActiveApp = nil;
NSDictionary *pRingInfo = nil;
if (isPreview)
{
pRingInfo = pPreviewRing;
}
else
{
// Figure out what the currently focused app is, and create a ring for that app
pActiveApp = [[NSWorkspace sharedWorkspace] activeApplication];
for (id key in pActiveApp)
{
DebugLog(@"key: '%@' value: '%@'", key, [pActiveApp objectForKey:key]);
}
pRingInfo = [pAppLookup objectForKey: [pActiveApp objectForKey:@"NSApplicationName"]];
// Use the default if no app was found
if (nil == pRingInfo)
{
pRingInfo = [pAppLookup objectForKey:@"Default"];
}
}
if (pRingInfo == nil)
{
return nil;
}
return [[Ring alloc] init: pRingInfo: centerX: centerY];
}
// Outline data source methods
- (int) outlineView: (NSOutlineView *) outlineView numberOfChildrenOfItem: (id) item
{
DebugLog(@"numberOfChildrenOfItem");
switch ([outlineView tag])
{
// Saved Rings outline data
case TAG_OUTLINE_SAVED_RINGS:
if (nil == item)
{
return [pConfigLookup count];
}
return [self numberOfSubrings: item];
break;
// Applications outline data
case TAG_OUTLINE_APPLICATIONS:
if (nil == item && isSavedRingSelected)
{
return [pApplicationsOutlineData count];
}
return 0;
break;
default:
return 0;
break;
}
}
- (bool) outlineView: (NSOutlineView *) outlineView isItemExpandable: (id) item
{
DebugLog(@"isItemExpandable %@ %i", outlineView, [outlineView tag]);
switch ([outlineView tag])
{
// Saved Rings outline data
case TAG_OUTLINE_SAVED_RINGS:
if (nil == item)
{
return NO;
}
return [self numberOfSubrings: item] > 0 ? YES: NO;
break;
// Applications outline data
case TAG_OUTLINE_APPLICATIONS:
return NO;
break;
default:
return NO;
break;
}
}
- (id) outlineView: (NSOutlineView *) outlineView child: (int) index ofItem: (id) item
{
int i = 0;
switch ([outlineView tag])
{
// Saved Rings outline data
case TAG_OUTLINE_SAVED_RINGS:
if (nil == item)
{
return [pConfigLookup objectForKey: [[pConfigLookup allKeys] objectAtIndex: index]];
}
for (id key in item)
{
// Step through all the objects in this ring configuration and see if there are subrings
if ([[item objectForKey: key] isKindOfClass: [NSDictionary class]] &&
[[item objectForKey: key] objectForKey: @"Submenu"])
{
if (i == index)
{
return [item objectForKey: key];
}
i++;
}
}
return nil;
break;
// Applications outline data
case TAG_OUTLINE_APPLICATIONS:
return [pApplicationsOutlineData objectAtIndex: index];
break;
default:
return nil;
break;
}
}
- (id) outlineView: (NSOutlineView *) outlineView objectValueForTableColumn: (NSTableColumn *) tableColumn byItem: (id) item
{
switch ([outlineView tag])
{
// Saved Rings outline data
case TAG_OUTLINE_SAVED_RINGS:
if ([item objectForKey: @"Name"] != nil)
{
// If the item has a name tag, then it is a top level item, use the name
return [item objectForKey: @"Name"];
}
return [item objectForKey: @"Label"];
break;
// Applications outline data
case TAG_OUTLINE_APPLICATIONS:
return item;
break;
default:
return nil;
break;
}
}
- (void) outlineView: (NSOutlineView *) outlineView setObjectValue: (id) object
forTableColumn: (NSTableColumn *) tableColumn
byItem: (id) item
{
switch ([outlineView tag])
{
// Saved Rings outline data
case TAG_OUTLINE_SAVED_RINGS:
if ([item objectForKey: @"Submenu"] != nil)
{
// This is a sub-menu, update the item's label
[item setObject: object forKey: @"Label"];
}
else
{
// This is a main menu
if ([object caseInsensitiveCompare: [item objectForKey: @"Name"]] == NSOrderedSame ||
[[item objectForKey: @"Name"] caseInsensitiveCompare: @"Default"] == NSOrderedSame)
{
// Don't do anything, the name didnt change, or it was default
}
else
{
// Put the old name in the file delete list
if ([[NSFileManager defaultManager] fileExistsAtPath:
[NSString stringWithFormat:@"%@/%@.plist", pSupportDirectory, [item objectForKey: @"Name"]]])
{
[pFileDeleteList addObject:
[NSString stringWithFormat:@"%@/%@.plist", pSupportDirectory, [item objectForKey: @"Name"]]];
}
// Add new name to the config lookup dictionary
[pConfigLookup setObject: item forKey: object];
// Remove old name from the config lookup dictionary
[pConfigLookup removeObjectForKey: [item objectForKey:@"Name"]];
// Update the item's name
[item setObject: object forKey: @"Name"];
// Select the newly renamed ring
//[pSavedRingsOutline selectRowIndexes: [NSIndexSet indexSetWithIndex: [self indexForKey: object
// inDictionary: pConfigLookup]]
// byExtendingSelection: NO];
}
}
// Tell the saved rings outline to refresh from the root
[pSavedRingsOutline reloadItem: nil reloadChildren: YES];
// Select the newly renamed ring
[pSavedRingsOutline selectRowIndexes: [NSIndexSet indexSetWithIndex: [self indexForKey: object
inDictionary: pConfigLookup]]
byExtendingSelection: NO];
break;
// Applications outline data
case TAG_OUTLINE_APPLICATIONS:
default:
break;
}
}
// Outline delegate methods
- (void) outlineViewSelectionDidChange: (NSNotification *) aNotification
{
NSEnumerator *pAppsEnum;
NSUInteger i;
NSIndexSet *pSelectedRows;
NSString *pAppName;
NSString *pNewAppName;
NSMutableString *pNewAppList = [NSMutableString stringWithCapacity: 40];
DebugLog(@"outlineViewSelectionDidChange");
switch ([[aNotification object] tag])
{
// Saved Rings outline data
case TAG_OUTLINE_SAVED_RINGS:
if ([[aNotification object] selectedRow] >= 0)
{
isSavedRingSelected = YES;
pPreviewRing = [[aNotification object] itemAtRow: [[aNotification object] selectedRow]];
// See if this is a subring
if ([pPreviewRing objectForKey: @"Submenu"] != nil)
{
pPreviewRingParent = pPreviewRing;
pPreviewRing = [pPreviewRing objectForKey: @"Submenu"];
previewRingIsSubring = YES;
[pSavedRingsRemove setEnabled: YES];
// Hide the subring selection column
[[pSegmentSetupTable tableColumnWithIdentifier: [NSString stringWithFormat: @"%i", COLUMN_SUBRING]] setHidden: YES];
}
else
{
pPreviewRingParent = nil;
previewRingIsSubring = NO;
// See if this is the default ring, and if so don't allow its removal
if ([self isDefault])
{
[pSavedRingsRemove setEnabled: NO];
}
else
{
[pSavedRingsRemove setEnabled: YES];
}
// Display the subring selection column
[[pSegmentSetupTable tableColumnWithIdentifier: [NSString stringWithFormat: @"%i", COLUMN_SUBRING]] setHidden: NO];
}
// Create a new ring preview
[previewRingView destroyRing];
[previewRingView createRing: YES];
// Enable/Refresh all the Ring Settings controls
[self updateRingSettingsControls];
}
else
{
isSavedRingSelected = NO;
pPreviewRingParent = nil;
pPreviewRing = nil;
previewRingIsSubring = NO;
[pSavedRingsRemove setEnabled: NO];
// Destroy the ring preview
[previewRingView destroyRing];
// Disable all the Ring Settings controls
[self disableRingSettingsControls];
}
break;
// Applications outline data
case TAG_OUTLINE_APPLICATIONS:
// Loop through all the old app names, removing them from the app lookup table
pAppsEnum = [[[pPreviewRing objectForKey: @"Apps"] componentsSeparatedByString:@","] objectEnumerator];
// Loop through all the app names, removing them from the app lookup table
while (pAppName = [pAppsEnum nextObject])
{
[pAppLookup removeObjectForKey: pAppName];
}
// Loop through all the selected rows and build the new application list, adding it to the lookup table as well
pSelectedRows = [[aNotification object] selectedRowIndexes];
i = [pSelectedRows firstIndex];
while (i != NSNotFound)
{
pNewAppName = [[aNotification object] itemAtRow: i];
[pAppLookup setObject: pPreviewRing forKey: pNewAppName];
[pNewAppList appendFormat: @"%@,", pNewAppName];
i = [pSelectedRows indexGreaterThanIndex: i];
}
// Now change the rings app list
[pPreviewRing setObject: pNewAppList forKey: @"Apps"];
break;
}
}
// Table data source methods
- (NSInteger) numberOfRowsInTableView: (NSTableView *) aTableView
{
NSInteger value = 0;
if (isSavedRingSelected)
{
return [[pPreviewRing objectForKey: @"TotalSegments"] integerValue];
}
return value;
}
- (id) tableView: (NSTableView *) aTableView objectValueForTableColumn: (NSTableColumn *) aTableColumn row: (NSInteger) rowIndex
{
int tableColumn;
NSInteger segmentType;
NSDictionary *pSegmentInfo;
// Convert the tag string to a number for ease of use
tableColumn = [[aTableColumn identifier] intValue];
// Figure out segment info for this row
pSegmentInfo = [self ringSettingsRowInfo: rowIndex: &segmentType];
if (pSegmentInfo == nil)
{
return nil;
}
// Return the appropriate information for this column
switch (tableColumn)
{
case COLUMN_SEGMENT:
switch (segmentType)
{
case SEGMENT_NORMAL:
return [NSNumber numberWithInteger: rowIndex + 1];
break;
case SEGMENT_SCROLL_UP:
return [NSString stringWithString: @"Scroll Up"];
break;
case SEGMENT_SCROLL_DOWN:
return [NSString stringWithString: @"Scroll Down"];
break;
}
break;
case COLUMN_LABEL:
return [pSegmentInfo objectForKey: @"Label"];
break;
case COLUMN_SUBRING: