Rename methods uniquely between mapping/device controllers.
[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 *)mappingForKey:(NSString *)name {
31 for (NJMapping *mapping in _mappings)
32 if ([name isEqualToString:mapping.name])
33 return mapping;
34 return nil;
35 }
36
37 - (void)mappingsSet {
38 [self postLoadProcess];
39 [NSNotificationCenter.defaultCenter
40 postNotificationName:NJEventMappingListChanged
41 object:self
42 userInfo:@{ NJMappingListKey: _mappings,
43 NJMappingKey: _currentMapping }];
44 }
45
46 - (void)mappingsChanged {
47 [self save];
48 [self mappingsSet];
49 }
50
51 - (void)activateMappingForProcess:(NSRunningApplication *)app {
52 NJMapping *oldMapping = _manualMapping;
53 NSArray *names = app.possibleMappingNames;
54 BOOL found = NO;
55 for (NSString *name in names) {
56 NJMapping *mapping = [self mappingForKey:name];
57 if (mapping) {
58 [self activateMapping:mapping];
59 found = YES;
60 break;
61 }
62 }
63
64 if (!found) {
65 [self activateMapping:oldMapping];
66 if ([oldMapping.name.lowercaseString isEqualToString:@"@application"]
67 || [oldMapping.name.lowercaseString isEqualToString:
68 NSLocalizedString(@"@Application", nil).lowercaseString]) {
69 oldMapping.name = app.bestMappingName;
70 [self mappingsChanged];
71 }
72 }
73 _manualMapping = oldMapping;
74 }
75
76 - (void)activateMappingForcibly:(NJMapping *)mapping {
77 NSLog(@"Switching to mapping %@.", mapping.name);
78 _currentMapping = mapping;
79 NSUInteger idx = [self indexOfMapping:_currentMapping];
80 [NSNotificationCenter.defaultCenter
81 postNotificationName:NJEventMappingChanged
82 object:self
83 userInfo:@{ NJMappingKey : _currentMapping,
84 NJMappingIndexKey: @(idx) }];
85 }
86
87 - (void)activateMapping:(NJMapping *)mapping {
88 if (!mapping)
89 mapping = _manualMapping;
90 if (mapping == _currentMapping)
91 return;
92 _manualMapping = mapping;
93 [self activateMappingForcibly:mapping];
94 }
95
96 - (void)save {
97 NSLog(@"Saving mappings to defaults.");
98 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_mappings.count];
99 for (NJMapping *mapping in _mappings)
100 [ary addObject:[mapping serialize]];
101 [NSUserDefaults.standardUserDefaults setObject:ary forKey:@"mappings"];
102 }
103
104 - (void)postLoadProcess {
105 for (NJMapping *mapping in self.mappings)
106 [mapping postLoadProcess:self.mappings];
107 }
108
109 - (void)load {
110 NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"];
111 NSArray *storedMappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"];
112 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
113
114 for (NSDictionary *serialization in storedMappings)
115 [newMappings addObject:
116 [[NJMapping alloc] initWithSerialization:serialization]];
117
118 if (newMappings.count) {
119 _mappings = newMappings;
120 if (selected >= newMappings.count)
121 selected = 0;
122 [self activateMapping:_mappings[selected]];
123 [self mappingsSet];
124 }
125 }
126
127 - (NSInteger)indexOfMapping:(NJMapping *)mapping {
128 return [_mappings indexOfObjectIdenticalTo:mapping];
129 }
130
131 - (void)mergeMapping:(NJMapping *)mapping intoMapping:(NJMapping *)existing {
132 [existing mergeEntriesFrom:mapping];
133 [self mappingsChanged];
134 if (existing == _currentMapping)
135 [self activateMappingForcibly:mapping];
136 }
137
138 - (void)renameMapping:(NJMapping *)mapping to:(NSString *)name {
139 mapping.name = name;
140 [self mappingsChanged];
141 if (mapping == _currentMapping)
142 [self activateMappingForcibly:mapping];
143 }
144
145 - (void)addMapping:(NJMapping *)mapping {
146 [self insertMapping:mapping atIndex:_mappings.count];
147 }
148
149 - (void)insertMapping:(NJMapping *)mapping atIndex:(NSInteger)idx {
150 [_mappings insertObject:mapping atIndex:idx];
151 [self mappingsChanged];
152 }
153
154 - (void)removeMappingAtIndex:(NSInteger)idx {
155 NSInteger currentIdx = [self indexOfMapping:_currentMapping];
156 [_mappings removeObjectAtIndex:idx];
157 [self activateMapping:self.mappings[MIN(currentIdx, _mappings.count - 1)]];
158 [self mappingsChanged];
159 }
160
161 - (void)moveMoveMappingFromIndex:(NSInteger)fromIdx toIndex:(NSInteger)toIdx {
162 [_mappings moveObjectAtIndex:fromIdx toIndex:toIdx];
163 [self mappingsChanged];
164 }
165
166 @end