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