Post active mapping changes through notification center rather than to the applicatio...
[enjoyable.git] / ConfigsController.m
1 //
2 // ConfigsController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "ConfigsController.h"
9
10 #import "ApplicationController.h"
11 #import "Config.h"
12 #import "ConfigsController.h"
13 #import "Target.h"
14 #import "TargetController.h"
15 #import "NJEvents.h"
16
17 @implementation ConfigsController {
18 NSMutableArray *_configs;
19 Config *manualConfig;
20 }
21
22 - (id)init {
23 if ((self = [super init])) {
24 _configs = [[NSMutableArray alloc] init];
25 _currentConfig = [[Config alloc] initWithName:@"(default)"];
26 manualConfig = _currentConfig;
27 [_configs addObject:_currentConfig];
28 }
29 return self;
30 }
31
32 - (Config *)objectForKeyedSubscript:(NSString *)name {
33 for (Config *config in _configs)
34 if ([name isEqualToString:config.name])
35 return config;
36 return nil;
37 }
38
39 - (void)activateConfigForProcess:(NSString *)processName {
40 Config *oldConfig = manualConfig;
41 Config *newConfig = self[processName];
42 if (!newConfig)
43 newConfig = oldConfig;
44 if (newConfig != _currentConfig)
45 [self activateConfig:newConfig];
46 manualConfig = oldConfig;
47 }
48
49 - (void)activateConfig:(Config *)config {
50 if (!config)
51 config = manualConfig;
52 NSLog(@"Switching to mapping %@.", config.name);
53 manualConfig = config;
54 _currentConfig = config;
55 [removeButton setEnabled:_configs[0] != config];
56 [targetController loadCurrent];
57 [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
58 object:_currentConfig];
59 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[_configs indexOfObject:config]] byExtendingSelection:NO];
60 }
61
62 - (IBAction)addPressed:(id)sender {
63 Config *newConfig = [[Config alloc] initWithName:@"Untitled"];
64 [_configs addObject:newConfig];
65 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
66 [tableView reloadData];
67 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_configs.count - 1] byExtendingSelection:NO];
68 [tableView editColumn:0 row:_configs.count - 1 withEvent:nil select:YES];
69 [self activateConfig:newConfig];
70 }
71
72 - (IBAction)removePressed:(id)sender {
73 if (tableView.selectedRow == 0)
74 return;
75
76 [_configs removeObjectAtIndex:tableView.selectedRow];
77 [tableView reloadData];
78 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
79 [self activateConfig:_configs[0]];
80 [self save];
81 }
82
83 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
84 if (tableView.selectedRow >= 0)
85 [self activateConfig:_configs[tableView.selectedRow]];
86 }
87
88 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
89 return [_configs[index] name];
90 }
91
92 - (void)tableView:(NSTableView *)view setObjectValue:(NSString *)obj forTableColumn:(NSTableColumn *)col row:(NSInteger)index {
93 [(Config *)_configs[index] setName:obj];
94 [tableView reloadData];
95 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
96 }
97
98 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
99 return _configs.count;
100 }
101
102 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
103 return index > 0;
104 }
105
106 - (void)save {
107 NSLog(@"Saving mappings to defaults.");
108 [NSUserDefaults.standardUserDefaults setObject:[self dumpAll] forKey:@"configurations"];
109 }
110
111 - (void)load {
112 [self loadAllFrom:[NSUserDefaults.standardUserDefaults objectForKey:@"configurations"]];
113 }
114
115 - (NSDictionary *)dumpAll {
116 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_configs.count];
117 for (Config *config in _configs)
118 [ary addObject:[config serialize]];
119 NSUInteger current = _currentConfig ? [_configs indexOfObject:_currentConfig] : 0;
120 return @{ @"configurations": ary, @"selected": @(current) };
121 }
122
123 - (void)loadAllFrom:(NSDictionary*) envelope{
124 NSArray *storedConfigs = envelope[@"configurations"];
125 NSMutableArray* newConfigs = [[NSMutableArray alloc] initWithCapacity:storedConfigs.count];
126
127 // have to do two passes in case config1 refers to config2 via a TargetConfig
128 for (NSDictionary *storedConfig in storedConfigs) {
129 Config *cfg = [[Config alloc] initWithName:storedConfig[@"name"]];
130 [newConfigs addObject:cfg];
131 }
132
133 for (unsigned i = 0; i < storedConfigs.count; ++i) {
134 NSDictionary *entries = storedConfigs[i][@"entries"];
135 Config *config = newConfigs[i];
136 for (id key in entries) {
137 Target *target = [Target targetDeserialize:entries[key]
138 withConfigs:newConfigs];
139 if (target)
140 config.entries[key] = target;
141 }
142 }
143
144 if (newConfigs.count) {
145 unsigned current = [envelope[@"selected"] unsignedIntValue];
146 if (current >= newConfigs.count)
147 current = 0;
148 _configs = newConfigs;
149 [tableView reloadData];
150 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
151 [self activateConfig:_configs[current]];
152 }
153 }
154
155 - (Config *)configWithURL:(NSURL *)url error:(NSError **)error {
156 NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
157 [stream open];
158 NSDictionary *serialization = !*error
159 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
160 : nil;
161 [stream close];
162
163 if (!([serialization isKindOfClass:NSDictionary.class]
164 && [serialization[@"name"] isKindOfClass:NSString.class]
165 && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
166 *error = [NSError errorWithDomain:@"Enjoyable"
167 code:0
168 description:@"This isn't a valid mapping file."];
169 return nil;
170 }
171
172 NSDictionary *entries = serialization[@"entries"];
173 Config *cfg = [[Config alloc] initWithName:serialization[@"name"]];
174 for (id key in entries) {
175 NSDictionary *value = entries[key];
176 if ([key isKindOfClass:NSString.class]) {
177 Target *target = [Target targetDeserialize:value
178 withConfigs:_configs];
179 if (target)
180 cfg.entries[key] = target;
181 }
182 }
183 return cfg;
184 }
185
186 - (void)importPressed:(id)sender {
187 NSOpenPanel *panel = [NSOpenPanel openPanel];
188 panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
189 NSWindow *window = NSApplication.sharedApplication.keyWindow;
190 [panel beginSheetModalForWindow:window
191 completionHandler:^(NSInteger result) {
192 if (result != NSFileHandlingPanelOKButton)
193 return;
194
195 [panel close];
196 NSError *error;
197 Config *cfg = [self configWithURL:panel.URL error:&error];
198
199 if (!error) {
200 BOOL conflict;
201 Config *mergeInto = self[cfg.name];
202 for (id key in cfg.entries) {
203 if (mergeInto.entries[key]) {
204 conflict = YES;
205 break;
206 }
207 }
208
209 if (conflict) {
210 NSAlert *conflictAlert = [[NSAlert alloc] init];
211 conflictAlert.messageText = @"Replace existing mappings?";
212 conflictAlert.informativeText =
213 [NSString stringWithFormat:
214 @"This file contains inputs you've already mapped in \"%@\". Do you "
215 @"want to merge them and replace your existing mappings, or import this "
216 @"as a separate mapping?", cfg.name];
217 [conflictAlert addButtonWithTitle:@"Merge"];
218 [conflictAlert addButtonWithTitle:@"Cancel"];
219 [conflictAlert addButtonWithTitle:@"New Mapping"];
220 NSInteger res = [conflictAlert runModal];
221 if (res == NSAlertSecondButtonReturn)
222 return;
223 else if (res == NSAlertThirdButtonReturn)
224 mergeInto = nil;
225 }
226
227 if (mergeInto) {
228 [mergeInto.entries addEntriesFromDictionary:cfg.entries];
229 cfg = mergeInto;
230 } else {
231 [_configs addObject:cfg];
232 [tableView reloadData];
233 }
234
235 [self save];
236 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
237 [self activateConfig:cfg];
238 [targetController loadCurrent];
239
240 if (conflict && !mergeInto) {
241 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_configs.count - 1] byExtendingSelection:NO];
242 [tableView editColumn:0 row:_configs.count - 1 withEvent:nil select:YES];
243 }
244 }
245
246 if (error) {
247 [window presentError:error
248 modalForWindow:window
249 delegate:nil
250 didPresentSelector:nil
251 contextInfo:nil];
252 }
253 }];
254
255 }
256
257 - (void)exportPressed:(id)sender {
258 NSSavePanel *panel = [NSSavePanel savePanel];
259 panel.allowedFileTypes = @[ @"enjoyable" ];
260 Config *cfg = _currentConfig;
261 panel.nameFieldStringValue = cfg.name;
262 NSWindow *window = NSApplication.sharedApplication.keyWindow;
263 [panel beginSheetModalForWindow:window
264 completionHandler:^(NSInteger result) {
265 if (result != NSFileHandlingPanelOKButton)
266 return;
267 [panel close];
268 NSError *error;
269 NSDictionary *serialization = [cfg serialize];
270 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
271 options:NSJSONWritingPrettyPrinted
272 error:&error];
273 if (!error)
274 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
275
276 if (error) {
277 [window presentError:error
278 modalForWindow:window
279 delegate:nil
280 didPresentSelector:nil
281 contextInfo:nil];
282 }
283 }];
284 }
285
286 @end