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"
16 @implementation ConfigsController {
17 NSMutableArray *_configs;
22 if ((self = [super init])) {
23 _configs = [[NSMutableArray alloc] init];
24 _currentConfig = [[Config alloc] initWithName:@"(default)"];
25 manualConfig = _currentConfig;
26 [_configs addObject:_currentConfig];
31 - (Config *)objectForKeyedSubscript:(NSString *)name {
32 for (Config *config in _configs)
33 if ([name isEqualToString:config.name])
38 - (void)activateConfigForProcess:(NSString *)processName {
39 Config *oldConfig = manualConfig;
40 [self activateConfig:self[processName]];
41 manualConfig = oldConfig;
44 - (void)activateConfig:(Config *)config {
46 config = manualConfig;
47 if (_currentConfig == config)
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];
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];
66 - (IBAction)removePressed:(id)sender {
67 if (tableView.selectedRow == 0)
70 [_configs removeObjectAtIndex:tableView.selectedRow];
71 [tableView reloadData];
72 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
73 [self activateConfig:_configs[0]];
77 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
78 if (tableView.selectedRow >= 0)
79 [self activateConfig:_configs[tableView.selectedRow]];
82 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
83 return [_configs[index] name];
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];
92 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
93 return _configs.count;
96 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
101 NSLog(@"Saving defaults.");
102 [NSUserDefaults.standardUserDefaults setObject:[self dumpAll] forKey:@"configurations"];
106 [self loadAllFrom:[NSUserDefaults.standardUserDefaults objectForKey:@"configurations"]];
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) };
117 - (void)loadAllFrom:(NSDictionary*) envelope{
118 NSArray *storedConfigs = envelope[@"configurations"];
119 NSMutableArray* newConfigs = [[NSMutableArray alloc] initWithCapacity:storedConfigs.count];
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];
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];
134 config.entries[key] = target;
138 if (newConfigs.count) {
139 unsigned current = [envelope[@"selected"] unsignedIntValue];
140 if (current >= newConfigs.count)
142 _configs = newConfigs;
143 [tableView reloadData];
144 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
145 [self activateConfig:_configs[current]];
149 - (Config *)configWithURL:(NSURL *)url error:(NSError **)error {
150 NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
152 NSDictionary *serialization = !*error
153 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
157 if (!([serialization isKindOfClass:NSDictionary.class]
158 && serialization[@"entries"])) {
159 *error = [NSError errorWithDomain:@"Enjoyable"
161 description:@"This isn't a valid mapping file."];
165 NSDictionary *entries = serialization[@"entries"];
166 Config *cfg = [[Config alloc] initWithName:serialization[@"name"]];
167 for (id key in entries)
168 cfg.entries[key] = [Target targetDeserialize:entries[key]
169 withConfigs:_configs];
173 - (void)importPressed:(id)sender {
174 NSOpenPanel *panel = [NSOpenPanel openPanel];
175 panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
176 NSWindow *window = NSApplication.sharedApplication.keyWindow;
177 [panel beginSheetModalForWindow:window
178 completionHandler:^(NSInteger result) {
179 if (result != NSFileHandlingPanelOKButton)
184 Config *cfg = [self configWithURL:panel.URL error:&error];
188 Config *mergeInto = self[cfg.name];
189 for (id key in cfg.entries) {
190 if (mergeInto.entries[key]) {
197 NSAlert *conflictAlert = [[NSAlert alloc] init];
198 conflictAlert.messageText = @"Replace existing mappings?";
199 conflictAlert.informativeText =
200 [NSString stringWithFormat:
201 @"This file contains inputs you've already mapped in \"%@\". Do you "
202 @"want to merge them and replace your existing mappings, or import this "
203 @"as a separate mapping?", cfg.name];
204 [conflictAlert addButtonWithTitle:@"Merge"];
205 [conflictAlert addButtonWithTitle:@"Cancel"];
206 [conflictAlert addButtonWithTitle:@"New Mapping"];
207 NSInteger res = [conflictAlert runModal];
208 if (res == NSAlertSecondButtonReturn)
210 else if (res == NSAlertThirdButtonReturn)
215 [mergeInto.entries addEntriesFromDictionary:cfg.entries];
218 [_configs addObject:cfg];
219 [tableView reloadData];
223 [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
224 [self activateConfig:cfg];
225 [targetController loadCurrent];
227 if (conflict && !mergeInto) {
228 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_configs.count - 1] byExtendingSelection:NO];
229 [tableView editColumn:0 row:_configs.count - 1 withEvent:nil select:YES];
234 [[NSAlert alertWithError:error] beginSheetModalForWindow:window
243 - (void)exportPressed:(id)sender {
244 NSSavePanel *panel = [NSSavePanel savePanel];
245 panel.allowedFileTypes = @[ @"enjoyable" ];
246 Config *cfg = _currentConfig;
247 panel.nameFieldStringValue = cfg.name;
248 NSWindow *window = NSApplication.sharedApplication.keyWindow;
249 [panel beginSheetModalForWindow:window
250 completionHandler:^(NSInteger result) {
251 if (result == NSFileHandlingPanelOKButton) {
253 NSDictionary *serialization = [cfg serialize];
254 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
255 options:NSJSONWritingPrettyPrinted
258 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
261 // FIXME: Ideally, this sheet is attached to the
262 // panel, and the panel doesn't close, so you
263 // can maybe fix what is wrong and try saving
264 // again. But it seems to be impossible to force
265 // the panel to stay open.
267 [[NSAlert alertWithError:error] beginSheetModalForWindow:window