fb50bc32c163c5f29114e5a6468583be1402fc91
[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 && ![mergeInto.entries[key] isEqual:cfg.entries[key]]) {
205 conflict = YES;
206 break;
207 }
208 }
209
210 if (conflict) {
211 NSAlert *conflictAlert = [[NSAlert alloc] init];
212 conflictAlert.messageText = @"Replace existing mappings?";
213 conflictAlert.informativeText =
214 [NSString stringWithFormat:
215 @"This file contains inputs you've already mapped in \"%@\". Do you "
216 @"want to merge them and replace your existing mappings, or import this "
217 @"as a separate mapping?", cfg.name];
218 [conflictAlert addButtonWithTitle:@"Merge"];
219 [conflictAlert addButtonWithTitle:@"Cancel"];
220 [conflictAlert addButtonWithTitle:@"New Mapping"];
221 NSInteger res = [conflictAlert runModal];
222 if (res == NSAlertSecondButtonReturn)
223 return;
224 else if (res == NSAlertThirdButtonReturn)
225 mergeInto = nil;
226 }
227
228 if (mergeInto) {
229 [mergeInto.entries addEntriesFromDictionary:cfg.entries];
230 cfg = mergeInto;
231 } else {
232 [_configs addObject:cfg];
233 [tableView reloadData];
234 }
235
236 [self save];
237 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
238 [self activateConfig:cfg];
239 [targetController loadCurrent];
240
241 if (conflict && !mergeInto) {
242 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_configs.count - 1] byExtendingSelection:NO];
243 [tableView editColumn:0 row:_configs.count - 1 withEvent:nil select:YES];
244 }
245 }
246
247 if (error) {
248 [window presentError:error
249 modalForWindow:window
250 delegate:nil
251 didPresentSelector:nil
252 contextInfo:nil];
253 }
254 }];
255
256 }
257
258 - (void)exportPressed:(id)sender {
259 NSSavePanel *panel = [NSSavePanel savePanel];
260 panel.allowedFileTypes = @[ @"enjoyable" ];
261 Config *cfg = _currentConfig;
262 panel.nameFieldStringValue = cfg.name;
263 NSWindow *window = NSApplication.sharedApplication.keyWindow;
264 [panel beginSheetModalForWindow:window
265 completionHandler:^(NSInteger result) {
266 if (result != NSFileHandlingPanelOKButton)
267 return;
268 [panel close];
269 NSError *error;
270 NSDictionary *serialization = [cfg serialize];
271 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
272 options:NSJSONWritingPrettyPrinted
273 error:&error];
274 if (!error)
275 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
276
277 if (error) {
278 [window presentError:error
279 modalForWindow:window
280 delegate:nil
281 didPresentSelector:nil
282 contextInfo:nil];
283 }
284 }];
285 }
286
287 @end