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 -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
30 if(currentConfig == config)
35 neutralConfig = currentConfig;
36 attachedApplication = *psn;
41 if(currentConfig != NULL) {
42 [targetController reset];
44 currentConfig = config;
45 [removeButton setEnabled: ![config protect]];
46 [targetController load];
47 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
48 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[configs indexOfObject:config]] byExtendingSelection:NO];
51 -(IBAction) addPressed: (id)sender {
52 Config* newConfig = [[Config alloc] init];
53 [newConfig setName: @"untitled"];
54 [configs addObject: newConfig];
55 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
56 [tableView reloadData];
57 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:configs.count - 1] byExtendingSelection:NO];
58 [tableView editColumn: 0 row:([configs count]-1) withEvent:nil select:YES];
60 -(IBAction) removePressed: (id)sender {
62 [tableView reloadData];
63 Config* current_config = [configs objectAtIndex: [tableView selectedRow]];
64 if([current_config protect])
66 [configs removeObjectAtIndex: [tableView selectedRow]];
68 // remove all "switch to configuration" actions
69 for(int i=0; i<[configs count]; i++) {
70 NSMutableDictionary* entries = [(Config*)[configs objectAtIndex:i] entries];
71 for(id key in entries) {
72 Target* target = (Target*) [entries objectForKey: key];
73 if([target isKindOfClass: [TargetConfig class]] && [(TargetConfig*)target config] == current_config)
74 [entries removeObjectForKey: key];
77 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
79 [tableView reloadData];
82 -(void)tableViewSelectionDidChange:(NSNotification*) notify {
83 if (tableView.selectedRow < configs.count)
84 [self activateConfig: (Config*)[configs objectAtIndex:[tableView selectedRow]] forApplication: NULL];
87 -(id) tableView: (NSTableView*)view objectValueForTableColumn: (NSTableColumn*) column row: (int) index {
88 NSParameterAssert(index >= 0 && index < [configs count]);
89 return [[configs objectAtIndex: index] name];
92 -(void) tableView: (NSTableView*) view setObjectValue:obj forTableColumn:(NSTableColumn*) col row: (int)index {
93 NSParameterAssert(index >= 0 && index < [configs count]);
94 /* ugly hack so stringification doesn't fail */
95 NSString* newName = [(NSString*)obj stringByReplacingOccurrencesOfString: @"~" withString: @""];
96 [(Config*)[configs objectAtIndex: index] setName: newName];
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 {
107 return ![[configs objectAtIndex: index] protect];
110 -(Config*) currentConfig {
111 return currentConfig;
114 -(Config*) currentNeutralConfig {
116 return neutralConfig;
117 return currentConfig;
121 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
122 [[NSUserDefaults standardUserDefaults] synchronize];
125 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
128 -(NSDictionary*) dumpAll {
129 NSMutableDictionary *envelope = [[NSMutableDictionary alloc] init];
130 NSMutableArray* ary = [[NSMutableArray alloc] init];
131 for(Config* config in configs) {
132 NSMutableDictionary* cfgInfo = [[NSMutableDictionary alloc] init];
133 [cfgInfo setObject:[config name] forKey:@"name"];
134 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
135 for(id key in [config entries]) {
136 [cfgEntries setObject:[[[config entries]objectForKey:key]stringify] forKey: key];
138 [cfgInfo setObject: cfgEntries forKey: @"entries"];
139 [ary addObject: cfgInfo];
141 [envelope setObject: ary forKey: @"configurationList"];
142 [envelope setObject: [NSNumber numberWithInt: [configs indexOfObject: [self currentNeutralConfig] ] ] forKey: @"selectedIndex"];
145 -(void) loadAllFrom: (NSDictionary*) envelope{
148 NSArray* ary = [envelope objectForKey: @"configurationList"];
150 NSMutableArray* newConfigs = [[NSMutableArray alloc] init];
151 // have to do two passes in case config1 refers to config2 via a TargetConfig
152 for(int i=0; i<[ary count]; i++) {
153 Config* cfg = [[Config alloc] init];
154 [cfg setName: [[ary objectAtIndex:i] objectForKey:@"name"]];
155 [newConfigs addObject: cfg];
157 [[configs objectAtIndex:0] setProtect: YES];
158 for(int i=0; i<[ary count]; i++) {
159 NSDictionary* dict = [[ary objectAtIndex:i] objectForKey:@"entries"];
160 for(id key in dict) {
161 [[[newConfigs objectAtIndex:i] entries]
162 setObject: [Target unstringify: [dict objectForKey: key] withConfigList: newConfigs]
167 configs = newConfigs;
168 [tableView reloadData];
169 currentConfig = NULL;
170 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
172 int index = [[envelope objectForKey: @"selectedIndex"] intValue];
173 if (index < configs.count)
174 [self activateConfig: [configs objectAtIndex:index] forApplication: NULL];
177 -(void) applicationSwitchedTo: (NSString*) name withPsn: (ProcessSerialNumber) psn {
178 for(int i=0; i<[configs count]; i++) {
179 Config* cfg = [configs objectAtIndex:i];
180 if([[cfg name] isEqualToString: name]) {
181 [self activateConfig: cfg forApplication: &psn];
185 [self restoreNeutralConfig];
188 -(ProcessSerialNumber*) targetApplication {
190 return &attachedApplication;