Clean up manual (previously 'neutral') vs. automatic (i.e. process-driven) configurat...
[enjoyable.git] / ConfigsController.m
1 //
2 // ConfigsController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 @implementation ConfigsController {
9 NSMutableArray *configs;
10 Config *manualConfig;
11 }
12
13 @synthesize currentConfig;
14 @synthesize configs;
15
16 - (id)init {
17 if ((self = [super init])) {
18 configs = [[NSMutableArray alloc] init];
19 currentConfig = [[Config alloc] init];
20 currentConfig.name = @"(default)";
21 manualConfig = currentConfig;
22 [configs addObject:currentConfig];
23 }
24 return self;
25 }
26
27 - (Config *)objectForKeyedSubscript:(NSString *)name {
28 for (Config *config in configs)
29 if ([name isEqualToString:config.name])
30 return config;
31 return nil;
32 }
33
34 - (void)activateConfigForProcess:(NSString *)processName {
35 Config *oldConfig = manualConfig;
36 [self activateConfig:self[processName]];
37 manualConfig = oldConfig;
38 }
39
40 - (void)activateConfig:(Config *)config {
41 if (!config)
42 config = manualConfig;
43 if (currentConfig == config)
44 return;
45 manualConfig = config;
46 currentConfig = config;
47 [targetController reset];
48 [removeButton setEnabled:configs[0] != config];
49 [targetController load];
50 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
51 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[configs indexOfObject:config]] byExtendingSelection:NO];
52 }
53
54 - (IBAction)addPressed:(id)sender {
55 Config *newConfig = [[Config alloc] init];
56 newConfig.name = @"untitled";
57 [configs addObject:newConfig];
58 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
59 [tableView reloadData];
60 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:configs.count - 1] byExtendingSelection:NO];
61 [tableView editColumn:0 row:[configs count] - 1 withEvent:nil select:YES];
62 }
63
64 - (IBAction)removePressed:(id)sender {
65 if (tableView.selectedRow == 0)
66 return;
67
68 Config *toRemove = configs[tableView.selectedRow];
69 [configs removeObjectAtIndex:tableView.selectedRow];
70
71 if (toRemove == currentConfig)
72 currentConfig = configs[0];
73 if (toRemove == manualConfig)
74 manualConfig = configs[0];
75
76 // remove all "switch to configuration" actions
77 for (Config *config in configs) {
78 NSMutableDictionary *entries = config.entries;
79 for (id key in entries) {
80 Target *target = entries[key];
81 if ([target isKindOfClass:[TargetConfig class]]
82 && [(TargetConfig *)target config] == toRemove)
83 [entries removeObjectForKey: key];
84 }
85 }
86 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
87 [tableView reloadData];
88 }
89
90 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
91 if (tableView.selectedRow >= 0)
92 [self activateConfig:configs[tableView.selectedRow]];
93 }
94
95 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
96 return [configs[index] name];
97 }
98
99 - (void)tableView:(NSTableView *)view setObjectValue:obj forTableColumn:(NSTableColumn *)col row:(int)index {
100 /* ugly hack so stringification doesn't fail */
101 NSString* newName = [(NSString*)obj stringByReplacingOccurrencesOfString: @"~" withString: @""];
102 [(Config *)configs[index] setName:newName];
103 [targetController refreshConfigsPreservingSelection:YES];
104 [tableView reloadData];
105 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
106 }
107
108 - (int)numberOfRowsInTableView:(NSTableView*)table {
109 return [configs count];
110 }
111
112 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
113 return index > 0;
114 }
115
116 -(void) save {
117 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
118 [[NSUserDefaults standardUserDefaults] synchronize];
119 }
120 -(void) load {
121 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
122 }
123
124 -(NSDictionary*) dumpAll {
125 NSMutableDictionary *envelope = [[NSMutableDictionary alloc] init];
126 NSMutableArray* ary = [[NSMutableArray alloc] init];
127 for(Config* config in configs) {
128 NSMutableDictionary* cfgInfo = [[NSMutableDictionary alloc] init];
129 cfgInfo[@"name"] = [config name];
130 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
131 for(id key in [config entries]) {
132 cfgEntries[key] = [[config entries][key]stringify];
133 }
134 cfgInfo[@"entries"] = cfgEntries;
135 [ary addObject: cfgInfo];
136 }
137 envelope[@"configurationList"] = ary;
138 return envelope;
139 }
140
141 -(void) loadAllFrom: (NSDictionary*) envelope{
142 if(envelope == NULL)
143 return;
144 NSArray* ary = envelope[@"configurationList"];
145
146 NSMutableArray* newConfigs = [[NSMutableArray alloc] init];
147 // have to do two passes in case config1 refers to config2 via a TargetConfig
148 for(int i=0; i<[ary count]; i++) {
149 Config* cfg = [[Config alloc] init];
150 [cfg setName: ary[i][@"name"]];
151 [newConfigs addObject: cfg];
152 }
153 for(int i=0; i<[ary count]; i++) {
154 NSDictionary* dict = ary[i][@"entries"];
155 for(id key in dict) {
156 [newConfigs[i] entries][key] = [Target unstringify: dict[key] withConfigList: newConfigs];
157 }
158 }
159
160 if (newConfigs.count) {
161 configs = newConfigs;
162 [tableView reloadData];
163 currentConfig = configs[0];
164 manualConfig = configs[0];
165 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
166 }
167 }
168
169 @end