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] init];
28 currentConfig.name = @"(default)";
29 manualConfig = currentConfig;
30 [configs addObject:currentConfig];
35 - (Config *)objectForKeyedSubscript:(NSString *)name {
36 for (Config *config in configs)
37 if ([name isEqualToString:config.name])
42 - (void)activateConfigForProcess:(NSString *)processName {
43 Config *oldConfig = manualConfig;
44 [self activateConfig:self[processName]];
45 manualConfig = oldConfig;
48 - (void)activateConfig:(Config *)config {
50 config = manualConfig;
51 if (currentConfig == config)
53 manualConfig = config;
54 currentConfig = config;
55 [targetController reset];
56 [removeButton setEnabled:configs[0] != config];
57 [targetController load];
58 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
59 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[configs indexOfObject:config]] byExtendingSelection:NO];
62 - (IBAction)addPressed:(id)sender {
63 Config *newConfig = [[Config alloc] init];
64 newConfig.name = @"untitled";
65 [configs addObject:newConfig];
66 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
67 [tableView reloadData];
68 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:configs.count - 1] byExtendingSelection:NO];
69 [tableView editColumn:0 row:[configs count] - 1 withEvent:nil select:YES];
72 - (IBAction)removePressed:(id)sender {
73 if (tableView.selectedRow == 0)
76 Config *toRemove = configs[tableView.selectedRow];
77 [configs removeObjectAtIndex:tableView.selectedRow];
79 if (toRemove == currentConfig)
80 currentConfig = configs[0];
81 if (toRemove == manualConfig)
82 manualConfig = configs[0];
84 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
85 [tableView reloadData];
88 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
89 if (tableView.selectedRow >= 0)
90 [self activateConfig:configs[tableView.selectedRow]];
93 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
94 return [configs[index] name];
97 - (void)tableView:(NSTableView *)view setObjectValue:obj forTableColumn:(NSTableColumn *)col row:(int)index {
98 /* ugly hack so stringification doesn't fail */
99 NSString* newName = [(NSString*)obj stringByReplacingOccurrencesOfString: @"~" withString: @""];
100 [(Config *)configs[index] setName:newName];
101 [targetController refreshConfigsPreservingSelection:YES];
102 [tableView reloadData];
103 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
106 - (int)numberOfRowsInTableView:(NSTableView*)table {
107 return [configs count];
110 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
115 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
116 [[NSUserDefaults standardUserDefaults] synchronize];
119 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
122 -(NSDictionary*) dumpAll {
123 NSMutableDictionary *envelope = [[NSMutableDictionary alloc] init];
124 NSMutableArray* ary = [[NSMutableArray alloc] init];
125 for(Config* config in configs) {
126 NSMutableDictionary* cfgInfo = [[NSMutableDictionary alloc] init];
127 cfgInfo[@"name"] = [config name];
128 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
129 for(id key in [config entries]) {
130 cfgEntries[key] = [[config entries][key]stringify];
132 cfgInfo[@"entries"] = cfgEntries;
133 [ary addObject: cfgInfo];
135 envelope[@"configurationList"] = ary;
139 -(void) loadAllFrom: (NSDictionary*) envelope{
142 NSArray* ary = envelope[@"configurationList"];
144 NSMutableArray* newConfigs = [[NSMutableArray alloc] init];
145 // have to do two passes in case config1 refers to config2 via a TargetConfig
146 for(int i=0; i<[ary count]; i++) {
147 Config* cfg = [[Config alloc] init];
148 [cfg setName: ary[i][@"name"]];
149 [newConfigs addObject: cfg];
151 for(int i=0; i<[ary count]; i++) {
152 NSDictionary* dict = ary[i][@"entries"];
153 for(id key in dict) {
154 [newConfigs[i] entries][key] = [Target unstringify: dict[key] withConfigList: newConfigs];
158 if (newConfigs.count) {
159 configs = newConfigs;
160 [tableView reloadData];
161 currentConfig = configs[0];
162 manualConfig = configs[0];
163 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];