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];
82 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
83 if (tableView.selectedRow >= 0)
84 [self activateConfig:_configs[tableView.selectedRow]];
87 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)index {
88 return [_configs[index] name];
91 - (void)tableView:(NSTableView *)view setObjectValue:(NSString *)obj forTableColumn:(NSTableColumn *)col row:(NSInteger)index {
92 [(Config *)_configs[index] setName:obj];
93 [tableView reloadData];
94 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
97 - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
98 return _configs.count;
101 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index {
106 NSLog(@"Saving defaults.");
107 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
111 [self loadAllFrom:[[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
114 - (NSDictionary *)dumpAll {
115 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_configs.count];
116 for (Config *config in _configs)
117 [ary addObject:[config serialize]];
118 NSUInteger current = _currentConfig ? [_configs indexOfObject:_currentConfig] : 0;
119 return @{ @"configurations": ary, @"selected": @(current) };
122 - (void)loadAllFrom:(NSDictionary*) envelope{
123 NSArray *storedConfigs = envelope[@"configurations"];
124 NSMutableArray* newConfigs = [[NSMutableArray alloc] initWithCapacity:storedConfigs.count];
126 // have to do two passes in case config1 refers to config2 via a TargetConfig
127 for (NSDictionary *storedConfig in storedConfigs) {
128 Config *cfg = [[Config alloc] initWithName:storedConfig[@"name"]];
129 [newConfigs addObject:cfg];
132 for (unsigned i = 0; i < storedConfigs.count; ++i) {
133 NSDictionary *entries = storedConfigs[i][@"entries"];
134 Config *config = newConfigs[i];
135 for (id key in entries)
136 config.entries[key] = [Target targetDeserialize:entries[key]
137 withConfigs:newConfigs];
140 if (newConfigs.count) {
141 unsigned current = [envelope[@"selected"] unsignedIntValue];
142 if (current >= newConfigs.count)
144 _configs = newConfigs;
145 [tableView reloadData];
146 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
147 [self activateConfig:_configs[current]];
151 - (void)exportPressed:(id)sender {
152 NSSavePanel *panel = [NSSavePanel savePanel];
153 panel.allowedFileTypes = @[ @"enjoyable" ];
154 if ([panel runModal] == NSFileHandlingPanelOKButton) {
156 NSDictionary *serialization = [_currentConfig serialize];
157 NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
158 options:NSJSONWritingPrettyPrinted
161 [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
164 [[NSAlert alertWithError:error] runModal];