5 // Created by Sam McCall on 4/05/09.
8 @implementation ConfigsController
13 if(self = [super init]) {
14 configs = [[NSMutableArray alloc] init];
15 currentConfig = [[Config alloc] init];
16 [currentConfig setName: @"(default)"];
17 [currentConfig setProtect: YES];
18 [configs addObject: currentConfig];
23 -(void) restoreNeutralConfig {
26 [self activateConfig: neutralConfig forApplication: NULL];
29 // TODO: Not an appropriate way to track 'neutral configs', it should just
30 // always be the first config and be unremovable.
32 -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
33 if(currentConfig == config)
38 neutralConfig = currentConfig;
43 if(currentConfig != NULL) {
44 [targetController reset];
46 currentConfig = config;
47 [removeButton setEnabled: ![config protect]];
48 [targetController load];
49 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
50 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[configs indexOfObject:config]] byExtendingSelection:NO];
53 -(IBAction) addPressed: (id)sender {
54 Config* newConfig = [[Config alloc] init];
55 [newConfig setName: @"untitled"];
56 [configs addObject: newConfig];
57 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
58 [tableView reloadData];
59 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:configs.count - 1] byExtendingSelection:NO];
60 [tableView editColumn: 0 row:([configs count]-1) withEvent:nil select:YES];
62 -(IBAction) removePressed: (id)sender {
64 [tableView reloadData];
65 Config* current_config = configs[[tableView selectedRow]];
66 if([current_config protect])
68 [configs removeObjectAtIndex: [tableView selectedRow]];
70 // remove all "switch to configuration" actions
71 for(int i=0; i<[configs count]; i++) {
72 NSMutableDictionary* entries = [(Config*)configs[i] entries];
73 for(id key in entries) {
74 Target* target = (Target*) entries[key];
75 if([target isKindOfClass: [TargetConfig class]] && [(TargetConfig*)target config] == current_config)
76 [entries removeObjectForKey: key];
79 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
81 [tableView reloadData];
84 -(void)tableViewSelectionDidChange:(NSNotification*) notify {
85 if (tableView.selectedRow < configs.count)
86 [self activateConfig: (Config*)configs[[tableView selectedRow]] forApplication: NULL];
89 -(id) tableView: (NSTableView*)view objectValueForTableColumn: (NSTableColumn*) column row: (int) index {
90 NSParameterAssert(index >= 0 && index < [configs count]);
91 return [configs[index] name];
94 -(void) tableView: (NSTableView*) view setObjectValue:obj forTableColumn:(NSTableColumn*) col row: (int)index {
95 NSParameterAssert(index >= 0 && index < [configs count]);
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 {
109 return ![configs[index] protect];
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 [configs[0] setProtect: YES];
160 for(int i=0; i<[ary count]; i++) {
161 NSDictionary* dict = ary[i][@"entries"];
162 for(id key in dict) {
163 [newConfigs[i] entries][key] = [Target unstringify: dict[key] withConfigList: newConfigs];
167 configs = newConfigs;
168 [tableView reloadData];
169 currentConfig = NULL;
170 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
172 int index = [envelope[@"selectedIndex"] intValue];
173 if (index < configs.count)
174 [self activateConfig: configs[index] forApplication: NULL];
177 -(void) applicationSwitchedTo: (NSString*) name withPsn: (ProcessSerialNumber) psn {
178 for(int i=0; i<[configs count]; i++) {
179 Config* cfg = configs[i];
180 if([[cfg name] isEqualToString: name]) {
181 [self activateConfig: cfg forApplication: &psn];
185 [self restoreNeutralConfig];