Tweak toolbar icons.
[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 [NSNotificationCenter.defaultCenter
46 postNotificationName:NJEventMappingListChanged
47 object:_mappings];
48 }
49
50 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
51 objects:(__unsafe_unretained id [])buffer
52 count:(NSUInteger)len {
53 return [_mappings countByEnumeratingWithState:state
54 objects:buffer
55 count:len];
56 }
57
58
59 - (void)activateMappingForProcess:(NSString *)processName {
60 NJMapping *oldMapping = manualMapping;
61 NJMapping *newMapping = self[processName];
62 if (!newMapping)
63 newMapping = oldMapping;
64 if (newMapping != _currentMapping)
65 [self activateMapping:newMapping];
66 manualMapping = oldMapping;
67 }
68
69 - (void)activateMapping:(NJMapping *)mapping {
70 if (!mapping)
71 mapping = manualMapping;
72 NSLog(@"Switching to mapping %@.", mapping.name);
73 manualMapping = mapping;
74 _currentMapping = mapping;
75 [removeButton setEnabled:_mappings[0] != mapping];
76 [outputController loadCurrent];
77 popoverActivate.title = _currentMapping.name;
78 [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
79 object:_currentMapping];
80 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[_mappings indexOfObject:mapping]] byExtendingSelection:NO];
81 }
82
83 - (IBAction)addPressed:(id)sender {
84 NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"];
85 [_mappings addObject:newMapping];
86 [self mappingsChanged];
87 [self activateMapping:newMapping];
88 [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
89 }
90
91 - (IBAction)removePressed:(id)sender {
92 if (tableView.selectedRow == 0)
93 return;
94
95 [_mappings removeObjectAtIndex:tableView.selectedRow];
96 [self mappingsChanged];
97 [self activateMapping:_mappings[0]];
98 }
99
100 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
101 [self activateMapping:self[tableView.selectedRow]];
102 }
103
104 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
105 return self[index].name;
106 }
107
108 - (void)tableView:(NSTableView *)view
109 setObjectValue:(NSString *)obj
110 forTableColumn:(NSTableColumn *)col
111 row:(NSInteger)index {
112 self[index].name = obj;
113 [self mappingsChanged];
114 }
115
116 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
117 return _mappings.count;
118 }
119
120 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
121 return index > 0;
122 }
123
124 - (void)save {
125 NSLog(@"Saving mappings to defaults.");
126 [NSUserDefaults.standardUserDefaults setValuesForKeysWithDictionary:[self dumpAll]];
127 }
128
129 - (void)load {
130 [self loadAllFrom:NSUserDefaults.standardUserDefaults.dictionaryRepresentation];
131 }
132
133 - (NSDictionary *)dumpAll {
134 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_mappings.count];
135 for (NJMapping *mapping in _mappings)
136 [ary addObject:[mapping serialize]];
137 NSUInteger current = _currentMapping ? [_mappings indexOfObject:_currentMapping] : 0;
138 return @{ @"mappings": ary, @"selected": @(current) };
139 }
140
141 - (void)loadAllFrom:(NSDictionary*)envelope {
142 NSArray *storedMappings = envelope[@"mappings"];
143 NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count];
144
145 // have to do two passes in case mapping1 refers to mapping2 via a NJOutputMapping
146 for (NSDictionary *storedMapping in storedMappings) {
147 NJMapping *mapping = [[NJMapping alloc] initWithName:storedMapping[@"name"]];
148 [newMappings addObject:mapping];
149 }
150
151 for (unsigned i = 0; i < storedMappings.count; ++i) {
152 NSDictionary *entries = storedMappings[i][@"entries"];
153 NJMapping *mapping = newMappings[i];
154 for (id key in entries) {
155 NJOutput *output = [NJOutput outputDeserialize:entries[key]
156 withMappings:newMappings];
157 if (output)
158 mapping.entries[key] = output;
159 }
160 }
161
162 if (newMappings.count) {
163 unsigned current = [envelope[@"selected"] unsignedIntValue];
164 if (current >= newMappings.count)
165 current = 0;
166 _mappings = newMappings;
167 [self mappingsChanged];
168 [self activateMapping:_mappings[current]];
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