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 [NSNotificationCenter.defaultCenter
47 postNotificationName:NJEventMappingListChanged
51 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
52 objects:(__unsafe_unretained id [])buffer
53 count:(NSUInteger)len {
54 return [_mappings countByEnumeratingWithState:state
60 - (void)activateMappingForProcess:(NSString *)processName {
61 NJMapping *oldMapping = manualMapping;
62 NJMapping *newMapping = self[processName];
64 newMapping = oldMapping;
65 if (newMapping != _currentMapping)
66 [self activateMapping:newMapping];
67 manualMapping = oldMapping;
70 - (void)activateMapping:(NJMapping *)mapping {
72 mapping = manualMapping;
73 if (mapping == _currentMapping)
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];
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];
96 - (IBAction)removePressed:(id)sender {
97 if (tableView.selectedRow == 0)
100 [_mappings removeObjectAtIndex:tableView.selectedRow];
101 [self mappingsChanged];
102 [self activateMapping:_mappings[0]];
105 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
106 [self activateMapping:self[tableView.selectedRow]];
109 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
110 return self[index].name;
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];
121 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
122 return _mappings.count;
125 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
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"];
138 NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"];
139 NSArray *mappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"];
140 [self loadAllFrom:mappings andActivate:selected];
143 - (void)loadAllFrom:(NSArray *)storedMappings andActivate:(NSUInteger)selected {
144 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
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];
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];
159 mapping.entries[key] = output;
163 if (newMappings.count) {
164 _mappings = newMappings;
165 if (selected >= newMappings.count)
167 [self mappingsChanged];
168 [self activateMapping:_mappings[selected]];
172 - (NJMapping *)mappingWithURL:(NSURL *)url error:(NSError **)error {
173 NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
175 NSDictionary *serialization = !*error
176 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
180 if (!([serialization isKindOfClass:NSDictionary.class]
181 && [serialization[@"name"] isKindOfClass:NSString.class]
182 && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
183 *error = [NSError errorWithDomain:@"Enjoyable"
185 description:@"This isn't a valid mapping file."];
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];
197 mapping.entries[key] = output;
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)
214 NJMapping *mapping = [self mappingWithURL:panel.URL error:&error];
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]]) {
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)
241 else if (res == NSAlertThirdButtonReturn)
246 [mergeInto.entries addEntriesFromDictionary:mapping.entries];
249 [_mappings addObject:mapping];
252 [self mappingsChanged];
253 [self activateMapping:mapping];
254 [outputController loadCurrent];
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];
263 [window presentError:error
264 modalForWindow:window
266 didPresentSelector:nil
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)
284 [NSProcessInfo.processInfo disableSuddenTermination];
286 NSDictionary *serialization = [mapping serialize];
287 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
288 options:NSJSONWritingPrettyPrinted
291 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
293 [NSProcessInfo.processInfo enableSuddenTermination];
295 [window presentError:error
296 modalForWindow:window
298 didPresentSelector:nil
304 - (IBAction)mappingPressed:(id)sender {
305 [popover showRelativeToRect:popoverActivate.bounds ofView:popoverActivate preferredEdge:NSMinXEdge];
308 - (void)popoverWillShow:(NSNotification *)notification {
309 popoverActivate.state = NSOnState;
312 - (void)popoverWillClose:(NSNotification *)notification {
313 popoverActivate.state = NSOffState;