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;
21 @synthesize currentConfig;
25 if ((self = [super init])) {
26 configs = [[NSMutableArray alloc] init];
27 currentConfig = [[Config alloc] initWithName:@"(default)"];
28 manualConfig = currentConfig;
29 [configs addObject:currentConfig];
34 - (Config *)objectForKeyedSubscript:(NSString *)name {
35 for (Config *config in configs)
36 if ([name isEqualToString:config.name])
41 - (void)activateConfigForProcess:(NSString *)processName {
42 Config *oldConfig = manualConfig;
43 [self activateConfig:self[processName]];
44 manualConfig = oldConfig;
47 - (void)activateConfig:(Config *)config {
49 config = manualConfig;
50 if (currentConfig == config)
52 manualConfig = config;
53 currentConfig = config;
54 [targetController reset];
55 [removeButton setEnabled:configs[0] != config];
56 [targetController load];
57 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
58 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[configs indexOfObject:config]] byExtendingSelection:NO];
61 - (IBAction)addPressed:(id)sender {
62 Config *newConfig = [[Config alloc] initWithName:@"Untitled"];
63 [configs addObject:newConfig];
64 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
65 [tableView reloadData];
66 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:configs.count - 1] byExtendingSelection:NO];
67 [tableView editColumn:0 row:[configs count] - 1 withEvent:nil select:YES];
70 - (IBAction)removePressed:(id)sender {
71 if (tableView.selectedRow == 0)
74 Config *toRemove = configs[tableView.selectedRow];
75 [configs removeObjectAtIndex:tableView.selectedRow];
77 if (toRemove == currentConfig)
78 currentConfig = configs[0];
79 if (toRemove == manualConfig)
80 manualConfig = configs[0];
82 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
83 [tableView reloadData];
86 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
87 if (tableView.selectedRow >= 0)
88 [self activateConfig:configs[tableView.selectedRow]];
91 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
92 return [configs[index] name];
95 - (void)tableView:(NSTableView *)view setObjectValue:(NSString *)obj forTableColumn:(NSTableColumn *)col row:(int)index {
96 [(Config *)configs[index] setName:obj];
97 [targetController refreshConfigsPreservingSelection:YES];
98 [tableView reloadData];
99 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
102 - (int)numberOfRowsInTableView:(NSTableView*)table {
103 return [configs count];
106 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
111 NSLog(@"Saving defaults.");
112 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
116 [self loadAllFrom:[[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
119 - (NSDictionary *)dumpAll {
120 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:configs.count];
121 for (Config *config in configs) {
122 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] initWithCapacity:config.entries.count];
123 for (id key in config.entries)
124 cfgEntries[key] = [config.entries[key] serialize];
125 [ary addObject:@{ @"name": config.name,
126 @"entries": cfgEntries,
129 NSUInteger current = currentConfig ? [configs indexOfObject:currentConfig] : 0;
130 return @{ @"configurationList": ary,
131 @"selectedConfiguration": @(current) };
134 - (void)loadAllFrom:(NSDictionary*) envelope{
135 NSArray *storedConfigs = envelope[@"configurationList"];
136 NSMutableArray* newConfigs = [[NSMutableArray alloc] initWithCapacity:storedConfigs.count];
138 // have to do two passes in case config1 refers to config2 via a TargetConfig
139 for (NSDictionary *storedConfig in storedConfigs) {
140 Config *cfg = [[Config alloc] initWithName:storedConfig[@"name"]];
141 [newConfigs addObject:cfg];
144 for (int i = 0; i < storedConfigs.count; ++i) {
145 NSDictionary *entries = storedConfigs[i][@"entries"];
146 Config *config = newConfigs[i];
147 for (id key in entries)
148 config.entries[key] = [Target targetDeserialize:entries[key]
149 withConfigs:newConfigs];
152 if (newConfigs.count) {
153 int current = [envelope[@"selectedConfiguration"] unsignedIntValue];
154 if (current >= newConfigs.count)
156 configs = newConfigs;
157 [tableView reloadData];
158 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
159 [self activateConfig:configs[current]];