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 if([configs indexOfObject:neutralConfig] < 0) {// deleted, keep what we have
30 [self activateConfig: neutralConfig forApplication: NULL];
33 -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
34 if(currentConfig == config)
39 neutralConfig = currentConfig;
40 attachedApplication = *psn;
45 if(currentConfig != NULL) {
46 [targetController reset];
48 currentConfig = config;
49 [removeButton setEnabled: ![config protect]];
50 [targetController load];
51 [[[NSApplication sharedApplication] delegate] configChanged];
52 [tableView selectRow: [configs indexOfObject: config] byExtendingSelection: NO];
55 -(IBAction) addPressed: (id)sender {
56 Config* newConfig = [[Config alloc] init];
57 [newConfig setName: @"untitled"];
58 [configs addObject: newConfig];
59 [[[NSApplication sharedApplication] delegate] configsChanged];
60 [tableView reloadData];
61 [tableView selectRow: ([configs count]-1) byExtendingSelection: NO];
62 [tableView editColumn: 0 row:([configs count]-1) withEvent:nil select:YES];
64 -(IBAction) removePressed: (id)sender {
66 [tableView reloadData];
67 Config* current_config = [configs objectAtIndex: [tableView selectedRow]];
68 if([current_config protect])
70 [configs removeObjectAtIndex: [tableView selectedRow]];
72 // remove all "switch to configuration" actions
73 for(int i=0; i<[configs count]; i++) {
74 NSMutableDictionary* entries = [(Config*)[configs objectAtIndex:i] entries];
75 for(id key in entries) {
76 Target* target = (Target*) [entries objectForKey: key];
77 if([target isKindOfClass: [TargetConfig class]] && [(TargetConfig*)target config] == current_config)
78 [entries removeObjectForKey: key];
81 [[[NSApplication sharedApplication] delegate] configsChanged];
83 [tableView reloadData];
86 -(void)tableViewSelectionDidChange:(NSNotification*) notify {
87 [self activateConfig: (Config*)[configs objectAtIndex:[tableView selectedRow]] forApplication: NULL];
90 -(id) tableView: (NSTableView*)view objectValueForTableColumn: (NSTableColumn*) column row: (int) index {
91 NSParameterAssert(index >= 0 && index < [configs count]);
92 return [[configs objectAtIndex: index] name];
95 -(void) tableView: (NSTableView*) view setObjectValue:obj forTableColumn:(NSTableColumn*) col row: (int)index {
96 NSParameterAssert(index >= 0 && index < [configs count]);
97 /* ugly hack so stringification doesn't fail */
98 NSString* newName = [(NSString*)obj stringByReplacingOccurrencesOfString: @"~" withString: @""];
99 [(Config*)[configs objectAtIndex: index] setName: newName];
100 [targetController refreshConfigsPreservingSelection:YES];
101 [tableView reloadData];
102 [[[NSApplication sharedApplication] delegate] configsChanged];
105 -(int)numberOfRowsInTableView: (NSTableView*)table {
106 return [configs count];
109 -(BOOL)tableView: (NSTableView*)view shouldEditTableColumn: (NSTableColumn*) column row: (int) index {
110 return ![[configs objectAtIndex: index] protect];
113 -(Config*) currentConfig {
114 return currentConfig;
117 -(Config*) currentNeutralConfig {
119 return neutralConfig;
120 return currentConfig;
124 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
125 [[NSUserDefaults standardUserDefaults] synchronize];
128 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
131 -(NSDictionary*) dumpAll {
132 NSMutableDictionary *envelope = [[NSMutableDictionary alloc] init];
133 NSMutableArray* ary = [[NSMutableArray alloc] init];
134 for(Config* config in configs) {
135 NSMutableDictionary* cfgInfo = [[NSMutableDictionary alloc] init];
136 [cfgInfo setObject:[config name] forKey:@"name"];
137 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
138 for(id key in [config entries]) {
139 [cfgEntries setObject:[[[config entries]objectForKey:key]stringify] forKey: key];
141 [cfgInfo setObject: cfgEntries forKey: @"entries"];
142 [ary addObject: cfgInfo];
144 [envelope setObject: ary forKey: @"configurationList"];
145 [envelope setObject: [NSNumber numberWithInt: [configs indexOfObject: [self currentNeutralConfig] ] ] forKey: @"selectedIndex"];
148 -(void) loadAllFrom: (NSDictionary*) envelope{
151 NSArray* ary = [envelope objectForKey: @"configurationList"];
153 NSMutableArray* newConfigs = [[NSMutableArray alloc] init];
154 // have to do two passes in case config1 refers to config2 via a TargetConfig
155 for(int i=0; i<[ary count]; i++) {
156 Config* cfg = [[Config alloc] init];
157 [cfg setName: [[ary objectAtIndex:i] objectForKey:@"name"]];
158 [newConfigs addObject: cfg];
160 [[configs objectAtIndex:0] setProtect: YES];
161 for(int i=0; i<[ary count]; i++) {
162 NSDictionary* dict = [[ary objectAtIndex:i] objectForKey:@"entries"];
163 for(id key in dict) {
164 [[[newConfigs objectAtIndex:i] entries]
165 setObject: [Target unstringify: [dict objectForKey: key] withConfigList: newConfigs]
170 configs = newConfigs;
171 [tableView reloadData];
172 currentConfig = NULL;
173 [[[NSApplication sharedApplication] delegate] configsChanged];
175 int index = [[envelope objectForKey: @"selectedIndex"] intValue];
176 [self activateConfig: [configs objectAtIndex:index] forApplication: NULL];
179 -(void) applicationSwitchedTo: (NSString*) name withPsn: (ProcessSerialNumber) psn {
180 for(int i=0; i<[configs count]; i++) {
181 Config* cfg = [configs objectAtIndex:i];
182 if([[cfg name] isEqualToString: name]) {
183 [self activateConfig: cfg forApplication: &psn];
187 [self restoreNeutralConfig];
190 -(ProcessSerialNumber*) targetApplication {
192 return &attachedApplication;