26ef35f7754f4550ef6d02706515d932512bc3a2
[enjoyable.git] / 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 "NJMappingsController.h"
12 #import "NJOutput.h"
13 #import "NJOutputController.h"
14 #import "NJEvents.h"
15
16 @implementation NJMappingsController {
17 NSMutableArray *_mappings;
18 NJMapping *manualMapping;
19 }
20
21 - (id)init {
22 if ((self = [super init])) {
23 _mappings = [[NSMutableArray alloc] init];
24 _currentMapping = [[NJMapping alloc] initWithName:@"(default)"];
25 manualMapping = _currentMapping;
26 [_mappings addObject:_currentMapping];
27 }
28 return self;
29 }
30
31 - (NJMapping *)objectForKeyedSubscript:(NSString *)name {
32 for (NJMapping *mapping in _mappings)
33 if ([name isEqualToString:mapping.name])
34 return mapping;
35 return nil;
36 }
37
38 - (NJMapping *)objectAtIndexedSubscript:(NSUInteger)idx {
39 return idx < _mappings.count ? _mappings[idx] : nil;
40 }
41
42 - (void)mappingsChanged {
43 [self save];
44 [tableView reloadData];
45 popoverActivate.title = _currentMapping.name;
46 [NSNotificationCenter.defaultCenter
47 postNotificationName:NJEventMappingListChanged
48 object:_mappings];
49 }
50
51 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
52 objects:(__unsafe_unretained id [])buffer
53 count:(NSUInteger)len {
54 return [_mappings countByEnumeratingWithState:state
55 objects:buffer
56 count:len];
57 }
58
59
60 - (void)activateMappingForProcess:(NSString *)processName {
61 NJMapping *oldMapping = manualMapping;
62 NJMapping *newMapping = self[processName];
63 if (!newMapping)
64 newMapping = oldMapping;
65 if (newMapping != _currentMapping)
66 [self activateMapping:newMapping];
67 manualMapping = oldMapping;
68 }
69
70 - (void)activateMapping:(NJMapping *)mapping {
71 if (!mapping)
72 mapping = manualMapping;
73 if (mapping == _currentMapping)
74 return;
75 NSLog(@"Switching to mapping %@.", mapping.name);
76 manualMapping = mapping;
77 _currentMapping = mapping;
78 [removeButton setEnabled:_mappings[0] != mapping];
79 [outputController loadCurrent];
80 popoverActivate.title = _currentMapping.name;
81 NSUInteger selected = [_mappings indexOfObject:mapping];
82 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selected] byExtendingSelection:NO];
83 [NSUserDefaults.standardUserDefaults setInteger:selected forKey:@"selected"];
84 [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
85 object:_currentMapping];
86 }
87
88 - (IBAction)addPressed:(id)sender {
89 NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"];
90 [_mappings addObject:newMapping];
91 [self mappingsChanged];
92 [self activateMapping:newMapping];
93 [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
94 }
95
96 - (IBAction)removePressed:(id)sender {
97 if (tableView.selectedRow == 0)
98 return;
99
100 [_mappings removeObjectAtIndex:tableView.selectedRow];
101 [self mappingsChanged];
102 [self activateMapping:_mappings[0]];
103 }
104
105 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
106 [self activateMapping:self[tableView.selectedRow]];
107 }
108
109 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
110 return self[index].name;
111 }
112
113 - (void)tableView:(NSTableView *)view
114 setObjectValue:(NSString *)obj
115 forTableColumn:(NSTableColumn *)col
116 row:(NSInteger)index {
117 self[index].name = obj;
118 [self mappingsChanged];
119 }
120
121 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
122 return _mappings.count;
123 }
124
125 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
126 return YES;
127 }
128
129 - (void)save {
130 NSLog(@"Saving mappings to defaults.");
131 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_mappings.count];
132 for (NJMapping *mapping in _mappings)
133 [ary addObject:[mapping serialize]];
134 [NSUserDefaults.standardUserDefaults setObject:ary forKey:@"mappings"];
135 }
136
137 - (void)load {
138 NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"];
139 NSArray *mappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"];
140 [self loadAllFrom:mappings andActivate:selected];
141 }
142
143 - (void)loadAllFrom:(NSArray *)storedMappings andActivate:(NSUInteger)selected {
144 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
145
146 // have to do two passes in case mapping1 refers to mapping2 via a NJOutputMapping
147 for (NSDictionary *storedMapping in storedMappings) {
148 NJMapping *mapping = [[NJMapping alloc] initWithName:storedMapping[@"name"]];
149 [newMappings addObject:mapping];
150 }
151
152 for (unsigned i = 0; i < storedMappings.count; ++i) {
153 NSDictionary *entries = storedMappings[i][@"entries"];
154 NJMapping *mapping = newMappings[i];
155 for (id key in entries) {
156 NJOutput *output = [NJOutput outputDeserialize:entries[key]
157 withMappings:newMappings];
158 if (output)
159 mapping.entries[key] = output;
160 }
161 }
162
163 if (newMappings.count) {
164 _mappings = newMappings;
165 if (selected >= newMappings.count)
166 selected = 0;
167 [self mappingsChanged];
168 [self activateMapping:_mappings[selected]];
169 }
170 }
171
172 - (NJMapping *)mappingWithURL:(NSURL *)url error:(NSError **)error {
173 NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
174 [stream open];
175 NSDictionary *serialization = !*error
176 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
177 : nil;
178 [stream close];
179
180 if (!([serialization isKindOfClass:NSDictionary.class]
181 && [serialization[@"name"] isKindOfClass:NSString.class]
182 && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
183 *error = [NSError errorWithDomain:@"Enjoyable"
184 code:0
185 description:@"This isn't a valid mapping file."];
186 return nil;
187 }
188
189 NSDictionary *entries = serialization[@"entries"];
190 NJMapping *mapping = [[NJMapping alloc] initWithName:serialization[@"name"]];
191 for (id key in entries) {
192 NSDictionary *value = entries[key];
193 if ([key isKindOfClass:NSString.class]) {
194 NJOutput *output = [NJOutput outputDeserialize:value
195 withMappings:_mappings];
196 if (output)
197 mapping.entries[key] = output;
198 }
199 }
200 return mapping;
201 }
202
203 - (void)importPressed:(id)sender {
204 NSOpenPanel *panel = [NSOpenPanel openPanel];
205 panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
206 NSWindow *window = NSApplication.sharedApplication.keyWindow;
207 [panel beginSheetModalForWindow:window
208 completionHandler:^(NSInteger result) {
209 if (result != NSFileHandlingPanelOKButton)
210 return;
211
212 [panel close];
213 NSError *error;
214 NJMapping *mapping = [self mappingWithURL:panel.URL error:&error];
215
216 if (!error) {
217 BOOL conflict = NO;
218 NJMapping *mergeInto = self[mapping.name];
219 for (id key in mapping.entries) {
220 if (mergeInto.entries[key]
221 && ![mergeInto.entries[key] isEqual:mapping.entries[key]]) {
222 conflict = YES;
223 break;
224 }
225 }
226
227 if (conflict) {
228 NSAlert *conflictAlert = [[NSAlert alloc] init];
229 conflictAlert.messageText = @"Replace existing mappings?";
230 conflictAlert.informativeText =
231 [NSString stringWithFormat:
232 @"This file contains inputs you've already mapped in \"%@\". Do you "
233 @"want to merge them and replace your existing mappings, or import this "
234 @"as a separate mapping?", mapping.name];
235 [conflictAlert addButtonWithTitle:@"Merge"];
236 [conflictAlert addButtonWithTitle:@"Cancel"];
237 [conflictAlert addButtonWithTitle:@"New Mapping"];
238 NSInteger res = [conflictAlert runModal];
239 if (res == NSAlertSecondButtonReturn)
240 return;
241 else if (res == NSAlertThirdButtonReturn)
242 mergeInto = nil;
243 }
244
245 if (mergeInto) {
246 [mergeInto.entries addEntriesFromDictionary:mapping.entries];
247 mapping = mergeInto;
248 } else {
249 [_mappings addObject:mapping];
250 }
251
252 [self mappingsChanged];
253 [self activateMapping:mapping];
254 [outputController loadCurrent];
255
256 if (conflict && !mergeInto) {
257 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO];
258 [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
259 }
260 }
261
262 if (error) {
263 [window presentError:error
264 modalForWindow:window
265 delegate:nil
266 didPresentSelector:nil
267 contextInfo:nil];
268 }
269 }];
270
271 }
272
273 - (void)exportPressed:(id)sender {
274 NSSavePanel *panel = [NSSavePanel savePanel];
275 panel.allowedFileTypes = @[ @"enjoyable" ];
276 NJMapping *mapping = _currentMapping;
277 panel.nameFieldStringValue = mapping.name;
278 NSWindow *window = NSApplication.sharedApplication.keyWindow;
279 [panel beginSheetModalForWindow:window
280 completionHandler:^(NSInteger result) {
281 if (result != NSFileHandlingPanelOKButton)
282 return;
283 [panel close];
284 NSError *error;
285 NSDictionary *serialization = [mapping serialize];
286 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
287 options:NSJSONWritingPrettyPrinted
288 error:&error];
289 if (!error)
290 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
291
292 if (error) {
293 [window presentError:error
294 modalForWindow:window
295 delegate:nil
296 didPresentSelector:nil
297 contextInfo:nil];
298 }
299 }];
300 }
301
302 - (IBAction)mappingPressed:(id)sender {
303 [popover showRelativeToRect:popoverActivate.bounds ofView:popoverActivate preferredEdge:NSMinXEdge];
304 }
305
306 - (void)popoverWillShow:(NSNotification *)notification {
307 popoverActivate.state = NSOnState;
308 }
309
310 - (void)popoverWillClose:(NSNotification *)notification {
311 popoverActivate.state = NSOffState;
312 }
313
314 @end