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