5 // Created by Sam McCall on 4/05/09.
8 #import "ConfigsController.h"
10 #import "ApplicationController.h"
12 #import "ConfigsController.h"
14 #import "TargetController.h"
17 @implementation ConfigsController {
18 NSMutableArray *_configs;
23 if ((self = [super init])) {
24 _configs = [[NSMutableArray alloc] init];
25 _currentConfig = [[Config alloc] initWithName:@"(default)"];
26 manualConfig = _currentConfig;
27 [_configs addObject:_currentConfig];
32 - (Config *)objectForKeyedSubscript:(NSString *)name {
33 for (Config *config in _configs)
34 if ([name isEqualToString:config.name])
39 - (void)activateConfigForProcess:(NSString *)processName {
40 Config *oldConfig = manualConfig;
41 Config *newConfig = self[processName];
43 newConfig = oldConfig;
44 if (newConfig != _currentConfig)
45 [self activateConfig:newConfig];
46 manualConfig = oldConfig;
49 - (void)activateConfig:(Config *)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];
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];
72 - (IBAction)removePressed:(id)sender {
73 if (tableView.selectedRow == 0)
76 [_configs removeObjectAtIndex:tableView.selectedRow];
77 [tableView reloadData];
78 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
79 [self activateConfig:_configs[0]];
83 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
84 if (tableView.selectedRow >= 0)
85 [self activateConfig:_configs[tableView.selectedRow]];
88 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
89 return [_configs[index] name];
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];
98 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
99 return _configs.count;
102 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
107 NSLog(@"Saving mappings to defaults.");
108 [NSUserDefaults.standardUserDefaults setObject:[self dumpAll] forKey:@"configurations"];
112 [self loadAllFrom:[NSUserDefaults.standardUserDefaults objectForKey:@"configurations"]];
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) };
123 - (void)loadAllFrom:(NSDictionary*) envelope{
124 NSArray *storedConfigs = envelope[@"configurations"];
125 NSMutableArray* newConfigs = [[NSMutableArray alloc] initWithCapacity:storedConfigs.count];
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];
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];
140 config.entries[key] = target;
144 if (newConfigs.count) {
145 unsigned current = [envelope[@"selected"] unsignedIntValue];
146 if (current >= newConfigs.count)
148 _configs = newConfigs;
149 [tableView reloadData];
150 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
151 [self activateConfig:_configs[current]];
155 - (Config *)configWithURL:(NSURL *)url error:(NSError **)error {
156 NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
158 NSDictionary *serialization = !*error
159 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
163 if (!([serialization isKindOfClass:NSDictionary.class]
164 && [serialization[@"name"] isKindOfClass:NSString.class]
165 && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
166 *error = [NSError errorWithDomain:@"Enjoyable"
168 description:@"This isn't a valid mapping file."];
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];
180 cfg.entries[key] = target;
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)
197 Config *cfg = [self configWithURL:panel.URL error:&error];
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]]) {
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)
224 else if (res == NSAlertThirdButtonReturn)
229 [mergeInto.entries addEntriesFromDictionary:cfg.entries];
232 [_configs addObject:cfg];
233 [tableView reloadData];
237 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
238 [self activateConfig:cfg];
239 [targetController loadCurrent];
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];
248 [window presentError:error
249 modalForWindow:window
251 didPresentSelector:nil
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)
270 NSDictionary *serialization = [cfg serialize];
271 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
272 options:NSJSONWritingPrettyPrinted
275 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
278 [window presentError:error
279 modalForWindow:window
281 didPresentSelector:nil