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 Config *toRemove = _configs[tableView.selectedRow];
71 [_configs removeObjectAtIndex:tableView.selectedRow];
73 if (toRemove == _currentConfig)
74 _currentConfig = _configs[0];
75 if (toRemove == manualConfig)
76 manualConfig = _configs[0];
78 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
79 [tableView reloadData];
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 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 - (void)importPressed:(id)sender {
156 NSOpenPanel *panel = [NSOpenPanel openPanel];
157 panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
158 if ([panel runModal] == NSFileHandlingPanelOKButton) {
160 NSInputStream *stream = [NSInputStream inputStreamWithURL:panel.URL];
162 NSDictionary *serialization = !error
163 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:&error]
167 if (!([serialization isKindOfClass:[NSDictionary class]]
168 && serialization[@"entries"])) {
169 error = [NSError errorWithDomain:@"Enjoyable"
171 description:@"This isn't a valid mapping file."];
176 NSDictionary *entries = serialization[@"entries"];
177 Config *cfg = [[Config alloc] initWithName:serialization[@"name"]];
178 Config *mergeInto = self[cfg.name];
180 for (id key in entries) {
181 cfg.entries[key] = [Target targetDeserialize:entries[key]
182 withConfigs:_configs];
183 if (mergeInto.entries[key])
188 NSAlert *conflictAlert = [[NSAlert alloc] init];
189 conflictAlert.messageText = @"Replace existing mappings?";
190 conflictAlert.informativeText =
191 [NSString stringWithFormat:
192 @"This file contains inputs you've already mapped in \"%@\". Do you "
193 @"want to merge them and replace your existing mappings, or import this "
194 @"as a separate mapping?", cfg.name];
195 [conflictAlert addButtonWithTitle:@"Merge"];
196 [conflictAlert addButtonWithTitle:@"Cancel"];
197 [conflictAlert addButtonWithTitle:@"New Mapping"];
198 NSInteger res = [conflictAlert runModal];
199 if (res == NSAlertSecondButtonReturn)
201 else if (res == NSAlertThirdButtonReturn)
206 [mergeInto.entries addEntriesFromDictionary:cfg.entries];
209 [_configs addObject:cfg];
210 [tableView reloadData];
214 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
215 [self activateConfig:cfg];
216 [targetController loadCurrent];
218 if (conflict && !mergeInto) {
219 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_configs.count - 1] byExtendingSelection:NO];
220 [tableView editColumn:0 row:_configs.count - 1 withEvent:nil select:YES];
225 [[NSAlert alertWithError:error] runModal];
229 - (void)exportPressed:(id)sender {
230 NSSavePanel *panel = [NSSavePanel savePanel];
231 panel.allowedFileTypes = @[ @"enjoyable" ];
232 if ([panel runModal] == NSFileHandlingPanelOKButton) {
234 NSDictionary *serialization = [_currentConfig serialize];
235 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
236 options:NSJSONWritingPrettyPrinted
239 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
242 [[NSAlert alertWithError:error] runModal];