2 // NJMappingsController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "NJMappingsController.h"
14 @implementation NJMappingsController {
15 NSMutableArray *_mappings;
16 NJMapping *_manualMapping;
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];
30 - (NJMapping *)objectForKeyedSubscript:(NSString *)name {
31 for (NJMapping *mapping in _mappings)
32 if ([name isEqualToString:mapping.name])
37 - (NJMapping *)objectAtIndexedSubscript:(NSUInteger)idx {
38 return idx < _mappings.count ? _mappings[idx] : nil;
42 [self postLoadProcess];
43 [NSNotificationCenter.defaultCenter
44 postNotificationName:NJEventMappingListChanged
46 userInfo:@{ NJMappingListKey: _mappings,
47 NJMappingKey: _currentMapping }];
50 - (void)mappingsChanged {
55 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
56 objects:(__unsafe_unretained id [])buffer
57 count:(NSUInteger)len {
58 return [_mappings countByEnumeratingWithState:state
63 - (void)activateMappingForProcess:(NSRunningApplication *)app {
64 NJMapping *oldMapping = _manualMapping;
65 NSArray *names = app.possibleMappingNames;
67 for (NSString *name in names) {
68 NJMapping *mapping = self[name];
70 [self activateMapping:mapping];
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];
85 _manualMapping = oldMapping;
88 - (void)activateMapping:(NJMapping *)mapping {
90 mapping = _manualMapping;
91 if (mapping == _currentMapping)
93 NSLog(@"Switching to mapping %@.", mapping.name);
94 _manualMapping = mapping;
95 _currentMapping = mapping;
96 NSUInteger idx = [self indexOfMapping:_currentMapping];
97 [NSNotificationCenter.defaultCenter
98 postNotificationName:NJEventMappingChanged
100 userInfo:@{ NJMappingKey : _currentMapping,
101 NJMappingIndexKey: @(idx) }];
105 NSLog(@"Saving mappings to defaults.");
106 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_mappings.count];
107 for (NJMapping *mapping in _mappings)
108 [ary addObject:[mapping serialize]];
109 [NSUserDefaults.standardUserDefaults setObject:ary forKey:@"mappings"];
112 - (void)postLoadProcess {
113 for (NJMapping *mapping in self)
114 [mapping postLoadProcess:self];
118 NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"];
119 NSArray *storedMappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"];
120 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
122 for (NSDictionary *serialization in storedMappings)
123 [newMappings addObject:
124 [[NJMapping alloc] initWithSerialization:serialization]];
126 if (newMappings.count) {
127 _mappings = newMappings;
128 if (selected >= newMappings.count)
130 [self activateMapping:_mappings[selected]];
135 - (NSInteger)indexOfMapping:(NJMapping *)mapping {
136 return [_mappings indexOfObjectIdenticalTo:mapping];
139 - (void)mergeMapping:(NJMapping *)mapping intoMapping:(NJMapping *)existing {
140 [existing mergeEntriesFrom:mapping];
141 [self mappingsChanged];
142 if (existing == _currentMapping) {
143 // FIXME: Hack to trigger updates in the rest of the UI.
144 _currentMapping = nil;
145 NJMapping *manual = _manualMapping;
146 [self activateMapping:existing];
147 _manualMapping = manual;
151 - (void)renameMapping:(NJMapping *)mapping to:(NSString *)name {
153 [self mappingsChanged];
154 if (mapping == _currentMapping) {
155 // FIXME: Hack to trigger updates in the rest of the UI.
156 _currentMapping = nil;
157 NJMapping *manual = _manualMapping;
158 [self activateMapping:mapping];
159 _manualMapping = manual;
163 - (void)addMapping:(NJMapping *)mapping {
164 [self insertMapping:mapping atIndex:_mappings.count];
167 - (void)insertMapping:(NJMapping *)mapping atIndex:(NSInteger)idx {
168 [_mappings insertObject:mapping atIndex:idx];
169 [self mappingsChanged];
172 - (void)removeMappingAtIndex:(NSInteger)idx {
173 NSInteger currentIdx = [self indexOfMapping:_currentMapping];
174 [_mappings removeObjectAtIndex:idx];
175 [self activateMapping:self[MIN(currentIdx, _mappings.count - 1)]];
176 [self mappingsChanged];
179 - (void)moveMoveMappingFromIndex:(NSInteger)fromIdx toIndex:(NSInteger)toIdx {
180 [_mappings moveObjectAtIndex:fromIdx toIndex:toIdx];
181 [self mappingsChanged];
184 - (NSUInteger)count {
185 return _mappings.count;