Save after deleting a config. Reset target to no-op when deleting its config, rather...
[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 Config *toRemove = _configs[tableView.selectedRow];
71 [_configs removeObjectAtIndex:tableView.selectedRow];
72
73 if (toRemove == _currentConfig)
74 _currentConfig = _configs[0];
75 if (toRemove == manualConfig)
76 manualConfig = _configs[0];
77
78 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
79 [tableView reloadData];
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 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 - (void)importPressed:(id)sender {
156 NSOpenPanel *panel = [NSOpenPanel openPanel];
157 panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
158 if ([panel runModal] == NSFileHandlingPanelOKButton) {
159 NSError *error;
160 NSInputStream *stream = [NSInputStream inputStreamWithURL:panel.URL];
161 [stream open];
162 NSDictionary *serialization = !error
163 ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:&error]
164 : nil;
165 [stream close];
166
167 if (!([serialization isKindOfClass:[NSDictionary class]]
168 && serialization[@"entries"])) {
169 error = [NSError errorWithDomain:@"Enjoyable"
170 code:0
171 description:@"This isn't a valid mapping file."];
172 }
173
174
175 if (!error) {
176 NSDictionary *entries = serialization[@"entries"];
177 Config *cfg = [[Config alloc] initWithName:serialization[@"name"]];
178 Config *mergeInto = self[cfg.name];
179 BOOL conflict = NO;
180 for (id key in entries) {
181 cfg.entries[key] = [Target targetDeserialize:entries[key]
182 withConfigs:_configs];
183 if (mergeInto.entries[key])
184 conflict = YES;
185 }
186
187 if (conflict) {
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)
200 return;
201 else if (res == NSAlertThirdButtonReturn)
202 mergeInto = nil;
203 }
204
205 if (mergeInto) {
206 [mergeInto.entries addEntriesFromDictionary:cfg.entries];
207 cfg = mergeInto;
208 } else {
209 [_configs addObject:cfg];
210 [tableView reloadData];
211 }
212
213 [self save];
214 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
215 [self activateConfig:cfg];
216 [targetController loadCurrent];
217
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];
221 }
222 }
223
224 if (error)
225 [[NSAlert alertWithError:error] runModal];
226 }
227 }
228
229 - (void)exportPressed:(id)sender {
230 NSSavePanel *panel = [NSSavePanel savePanel];
231 panel.allowedFileTypes = @[ @"enjoyable" ];
232 if ([panel runModal] == NSFileHandlingPanelOKButton) {
233 NSError *error;
234 NSDictionary *serialization = [_currentConfig serialize];
235 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
236 options:NSJSONWritingPrettyPrinted
237 error:&error];
238 if (!error)
239 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
240
241 if (error)
242 [[NSAlert alertWithError:error] runModal];
243 }
244 }
245
246 @end