5 // Created by Sam McCall on 4/05/09.
8 @implementation ConfigsController {
9 NSMutableArray *configs;
10 Config *neutralConfig;
13 @synthesize currentConfig;
17 if ((self = [super init])) {
18 configs = [[NSMutableArray alloc] init];
19 currentConfig = [[Config alloc] init];
20 [currentConfig setName: @"(default)"];
21 [configs addObject:currentConfig];
26 // TODO: Neutral config stuff is a mess.
28 -(void) restoreNeutralConfig {
31 [self activateConfig: neutralConfig forApplication: NULL];
34 -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
35 if(currentConfig == config)
40 neutralConfig = currentConfig;
45 if(currentConfig != NULL) {
46 [targetController reset];
48 currentConfig = config;
49 [removeButton setEnabled:configs[0] != config];
50 [targetController load];
51 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
52 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[configs indexOfObject:config]] byExtendingSelection:NO];
55 - (IBAction)addPressed:(id)sender {
56 Config *newConfig = [[Config alloc] init];
57 newConfig.name = @"untitled";
58 [configs addObject:newConfig];
59 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
60 [tableView reloadData];
61 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:configs.count - 1] byExtendingSelection:NO];
62 [tableView editColumn:0 row:[configs count] - 1 withEvent:nil select:YES];
65 - (IBAction)removePressed:(id)sender {
66 if (tableView.selectedRow == 0)
69 Config *current_config = configs[tableView.selectedRow];
70 [configs removeObjectAtIndex:tableView.selectedRow];
72 // remove all "switch to configuration" actions
73 for (Config *config in configs) {
74 NSMutableDictionary *entries = config.entries;
75 for (id key in entries) {
76 Target *target = entries[key];
77 if ([target isKindOfClass:[TargetConfig class]]
78 && [(TargetConfig *)target config] == current_config)
79 [entries removeObjectForKey: key];
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] forApplication: NULL];
91 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
92 return [configs[index] name];
95 - (void)tableView:(NSTableView *)view setObjectValue:obj forTableColumn:(NSTableColumn *)col row:(int)index {
96 /* ugly hack so stringification doesn't fail */
97 NSString* newName = [(NSString*)obj stringByReplacingOccurrencesOfString: @"~" withString: @""];
98 [(Config *)configs[index] setName:newName];
99 [targetController refreshConfigsPreservingSelection:YES];
100 [tableView reloadData];
101 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
104 - (int)numberOfRowsInTableView:(NSTableView*)table {
105 return [configs count];
108 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
112 - (Config *)currentConfig {
113 return currentConfig;
116 - (Config *)currentNeutralConfig {
118 return neutralConfig;
119 return currentConfig;
123 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
124 [[NSUserDefaults standardUserDefaults] synchronize];
127 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
130 -(NSDictionary*) dumpAll {
131 NSMutableDictionary *envelope = [[NSMutableDictionary alloc] init];
132 NSMutableArray* ary = [[NSMutableArray alloc] init];
133 for(Config* config in configs) {
134 NSMutableDictionary* cfgInfo = [[NSMutableDictionary alloc] init];
135 cfgInfo[@"name"] = [config name];
136 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
137 for(id key in [config entries]) {
138 cfgEntries[key] = [[config entries][key]stringify];
140 cfgInfo[@"entries"] = cfgEntries;
141 [ary addObject: cfgInfo];
143 envelope[@"configurationList"] = ary;
144 envelope[@"selectedIndex"] = @([configs indexOfObject: [self currentNeutralConfig] ]);
147 -(void) loadAllFrom: (NSDictionary*) envelope{
150 NSArray* ary = envelope[@"configurationList"];
152 NSMutableArray* newConfigs = [[NSMutableArray alloc] init];
153 // have to do two passes in case config1 refers to config2 via a TargetConfig
154 for(int i=0; i<[ary count]; i++) {
155 Config* cfg = [[Config alloc] init];
156 [cfg setName: ary[i][@"name"]];
157 [newConfigs addObject: cfg];
159 for(int i=0; i<[ary count]; i++) {
160 NSDictionary* dict = ary[i][@"entries"];
161 for(id key in dict) {
162 [newConfigs[i] entries][key] = [Target unstringify: dict[key] withConfigList: newConfigs];
166 configs = newConfigs;
167 [tableView reloadData];
168 currentConfig = NULL;
169 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
171 int index = [envelope[@"selectedIndex"] intValue];
172 if (index < configs.count)
173 [self activateConfig: configs[index] forApplication: NULL];
176 -(void) applicationSwitchedTo: (NSString*) name withPsn: (ProcessSerialNumber) psn {
177 for(int i=0; i<[configs count]; i++) {
178 Config* cfg = configs[i];
179 if([[cfg name] isEqualToString: name]) {
180 [self activateConfig: cfg forApplication: &psn];
184 [self restoreNeutralConfig];