2 // NJMappingsController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "NJMappingsController.h"
11 #import "NJMappingsController.h"
13 #import "NJOutputController.h"
16 @implementation NJMappingsController {
17 NSMutableArray *_mappings;
18 NJMapping *manualMapping;
22 if ((self = [super init])) {
23 _mappings = [[NSMutableArray alloc] init];
24 _currentMapping = [[NJMapping alloc] initWithName:@"(default)"];
25 manualMapping = _currentMapping;
26 [_mappings addObject:_currentMapping];
31 - (NJMapping *)objectForKeyedSubscript:(NSString *)name {
32 for (NJMapping *mapping in _mappings)
33 if ([name isEqualToString:mapping.name])
38 - (NJMapping *)objectAtIndexedSubscript:(NSUInteger)idx {
39 return idx < _mappings.count ? _mappings[idx] : nil;
42 - (void)mappingsChanged {
44 [tableView reloadData];
45 popoverActivate.title = _currentMapping.name;
46 [self updateInterfaceForCurrentMapping];
47 [NSNotificationCenter.defaultCenter
48 postNotificationName:NJEventMappingListChanged
52 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
53 objects:(__unsafe_unretained id [])buffer
54 count:(NSUInteger)len {
55 return [_mappings countByEnumeratingWithState:state
60 - (void)activateMappingForProcess:(NSString *)processName {
61 if ([manualMapping.name.lowercaseString isEqualToString:@"@application"]) {
62 manualMapping.name = processName;
63 [self mappingsChanged];
65 NJMapping *oldMapping = manualMapping;
66 NJMapping *newMapping = self[processName];
68 newMapping = oldMapping;
69 if (newMapping != _currentMapping)
70 [self activateMapping:newMapping];
71 manualMapping = oldMapping;
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"];
85 - (void)activateMapping:(NJMapping *)mapping {
87 mapping = manualMapping;
88 if (mapping == _currentMapping)
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];
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];
107 - (IBAction)removePressed:(id)sender {
108 if (tableView.selectedRow == 0)
111 [_mappings removeObjectAtIndex:tableView.selectedRow];
112 [self mappingsChanged];
113 [self activateMapping:_mappings[0]];
116 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
117 [self activateMapping:self[tableView.selectedRow]];
120 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
121 return self[index].name;
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];
132 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
133 return _mappings.count;
136 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
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"];
149 NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"];
150 NSArray *mappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"];
151 [self loadAllFrom:mappings andActivate:selected];
154 - (void)loadAllFrom:(NSArray *)storedMappings andActivate:(NSUInteger)selected {
155 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
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];
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];
170 mapping.entries[key] = output;
174 if (newMappings.count) {
175 _mappings = newMappings;
176 if (selected >= newMappings.count)
178 [self activateMapping:_mappings[selected]];
179 [self mappingsChanged];
183 - (NJMapping *)mappingWithURL:(NSURL *)url error:(NSError **)error {
184 NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
186 NSDictionary *serialization = !*error
187 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
191 if (!([serialization isKindOfClass:NSDictionary.class]
192 && [serialization[@"name"] isKindOfClass:NSString.class]
193 && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
194 *error = [NSError errorWithDomain:@"Enjoyable"
196 description:@"This isn't a valid mapping file."];
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];
208 mapping.entries[key] = output;
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)
225 NJMapping *mapping = [self mappingWithURL:panel.URL error:&error];
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]]) {
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)
252 else if (res == NSAlertThirdButtonReturn)
257 [mergeInto.entries addEntriesFromDictionary:mapping.entries];
260 [_mappings addObject:mapping];
263 [self mappingsChanged];
264 [self activateMapping:mapping];
265 [outputController loadCurrent];
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];
274 [window presentError:error
275 modalForWindow:window
277 didPresentSelector:nil
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)
295 [NSProcessInfo.processInfo disableSuddenTermination];
297 NSDictionary *serialization = [mapping serialize];
298 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
299 options:NSJSONWritingPrettyPrinted
302 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
304 [NSProcessInfo.processInfo enableSuddenTermination];
306 [window presentError:error
307 modalForWindow:window
309 didPresentSelector:nil
315 - (IBAction)mappingPressed:(id)sender {
316 [popover showRelativeToRect:popoverActivate.bounds ofView:popoverActivate preferredEdge:NSMinXEdge];
319 - (void)popoverWillShow:(NSNotification *)notification {
320 popoverActivate.state = NSOnState;
323 - (void)popoverWillClose:(NSNotification *)notification {
324 popoverActivate.state = NSOffState;
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];
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];