App delegate now controls communication between device / mapping controllers and...
[enjoyable.git] / Classes / NJMappingsController.m
1 //
2 // NJMappingsController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "NJMappingsController.h"
9
10 #import "NJMapping.h"
11 #import "NJOutput.h"
12 #import "NJEvents.h"
13
14 @implementation NJMappingsController {
15 NSMutableArray *_mappings;
16 NJMapping *_manualMapping;
17 }
18
19 - (id)init {
20 if ((self = [super init])) {
21 _mappings = [[NSMutableArray alloc] init];
22 _currentMapping = [[NJMapping alloc] initWithName:
23 NSLocalizedString(@"(default)", @"default name for first the mapping")];
24 _manualMapping = _currentMapping;
25 [_mappings addObject:_currentMapping];
26 }
27 return self;
28 }
29
30 - (NJMapping *)objectForKeyedSubscript:(NSString *)name {
31 for (NJMapping *mapping in _mappings)
32 if ([name isEqualToString:mapping.name])
33 return mapping;
34 return nil;
35 }
36
37 - (NJMapping *)objectAtIndexedSubscript:(NSUInteger)idx {
38 return idx < _mappings.count ? _mappings[idx] : nil;
39 }
40
41 - (void)mappingsSet {
42 [self postLoadProcess];
43 [NSNotificationCenter.defaultCenter
44 postNotificationName:NJEventMappingListChanged
45 object:self
46 userInfo:@{ NJMappingListKey: _mappings,
47 NJMappingKey: _currentMapping }];
48 }
49
50 - (void)mappingsChanged {
51 [self save];
52 [self mappingsSet];
53 }
54
55 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
56 objects:(__unsafe_unretained id [])buffer
57 count:(NSUInteger)len {
58 return [_mappings countByEnumeratingWithState:state
59 objects:buffer
60 count:len];
61 }
62
63 - (void)activateMappingForProcess:(NSRunningApplication *)app {
64 NJMapping *oldMapping = _manualMapping;
65 NSArray *names = app.possibleMappingNames;
66 BOOL found = NO;
67 for (NSString *name in names) {
68 NJMapping *mapping = self[name];
69 if (mapping) {
70 [self activateMapping:mapping];
71 found = YES;
72 break;
73 }
74 }
75
76 if (!found) {
77 [self activateMapping:oldMapping];
78 if ([oldMapping.name.lowercaseString isEqualToString:@"@application"]
79 || [oldMapping.name.lowercaseString isEqualToString:
80 NSLocalizedString(@"@Application", nil).lowercaseString]) {
81 oldMapping.name = app.bestMappingName;
82 [self mappingsChanged];
83 }
84 }
85 _manualMapping = oldMapping;
86 }
87
88 - (void)activateMappingForcibly:(NJMapping *)mapping {
89 NSLog(@"Switching to mapping %@.", mapping.name);
90 _currentMapping = mapping;
91 NSUInteger idx = [self indexOfMapping:_currentMapping];
92 [NSNotificationCenter.defaultCenter
93 postNotificationName:NJEventMappingChanged
94 object:self
95 userInfo:@{ NJMappingKey : _currentMapping,
96 NJMappingIndexKey: @(idx) }];
97 }
98
99 - (void)activateMapping:(NJMapping *)mapping {
100 if (!mapping)
101 mapping = _manualMapping;
102 if (mapping == _currentMapping)
103 return;
104 _manualMapping = mapping;
105 [self activateMappingForcibly:mapping];
106 }
107
108 - (void)save {
109 NSLog(@"Saving mappings to defaults.");
110 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_mappings.count];
111 for (NJMapping *mapping in _mappings)
112 [ary addObject:[mapping serialize]];
113 [NSUserDefaults.standardUserDefaults setObject:ary forKey:@"mappings"];
114 }
115
116 - (void)postLoadProcess {
117 for (NJMapping *mapping in self)
118 [mapping postLoadProcess:self];
119 }
120
121 - (void)load {
122 NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"];
123 NSArray *storedMappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"];
124 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
125
126 for (NSDictionary *serialization in storedMappings)
127 [newMappings addObject:
128 [[NJMapping alloc] initWithSerialization:serialization]];
129
130 if (newMappings.count) {
131 _mappings = newMappings;
132 if (selected >= newMappings.count)
133 selected = 0;
134 [self activateMapping:_mappings[selected]];
135 [self mappingsSet];
136 }
137 }
138
139 - (NSInteger)indexOfMapping:(NJMapping *)mapping {
140 return [_mappings indexOfObjectIdenticalTo:mapping];
141 }
142
143 - (void)mergeMapping:(NJMapping *)mapping intoMapping:(NJMapping *)existing {
144 [existing mergeEntriesFrom:mapping];
145 [self mappingsChanged];
146 if (existing == _currentMapping)
147 [self activateMappingForcibly:mapping];
148 }
149
150 - (void)renameMapping:(NJMapping *)mapping to:(NSString *)name {
151 mapping.name = name;
152 [self mappingsChanged];
153 if (mapping == _currentMapping)
154 [self activateMappingForcibly:mapping];
155 }
156
157 - (void)addMapping:(NJMapping *)mapping {
158 [self insertMapping:mapping atIndex:_mappings.count];
159 }
160
161 - (void)insertMapping:(NJMapping *)mapping atIndex:(NSInteger)idx {
162 [_mappings insertObject:mapping atIndex:idx];
163 [self mappingsChanged];
164 }
165
166 - (void)removeMappingAtIndex:(NSInteger)idx {
167 NSInteger currentIdx = [self indexOfMapping:_currentMapping];
168 [_mappings removeObjectAtIndex:idx];
169 [self activateMapping:self[MIN(currentIdx, _mappings.count - 1)]];
170 [self mappingsChanged];
171 }
172
173 - (void)moveMoveMappingFromIndex:(NSInteger)fromIdx toIndex:(NSInteger)toIdx {
174 [_mappings moveObjectAtIndex:fromIdx toIndex:toIdx];
175 [self mappingsChanged];
176 }
177
178 - (NSUInteger)count {
179 return _mappings.count;
180 }
181
182 @end