Actually make it impossible to remove the default configuration. Various other small...
[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 *neutralConfig;
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 setName: @"(default)"];
21 [configs addObject:currentConfig];
22 }
23 return self;
24 }
25
26 // TODO: Neutral config stuff is a mess.
27
28 -(void) restoreNeutralConfig {
29 if(!neutralConfig)
30 return;
31 [self activateConfig: neutralConfig forApplication: NULL];
32 }
33
34 -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
35 if(currentConfig == config)
36 return;
37
38 if(psn) {
39 if(!neutralConfig)
40 neutralConfig = currentConfig;
41 } else {
42 neutralConfig = NULL;
43 }
44
45 if(currentConfig != NULL) {
46 [targetController reset];
47 }
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];
53 }
54
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];
63 }
64
65 - (IBAction)removePressed:(id)sender {
66 if (tableView.selectedRow == 0)
67 return;
68
69 Config *current_config = configs[tableView.selectedRow];
70 [configs removeObjectAtIndex:tableView.selectedRow];
71
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];
80 }
81 }
82 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
83 [tableView reloadData];
84 }
85
86 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
87 if (tableView.selectedRow >= 0)
88 [self activateConfig:configs[tableView.selectedRow] forApplication: NULL];
89 }
90
91 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
92 return [configs[index] name];
93 }
94
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];
102 }
103
104 - (int)numberOfRowsInTableView:(NSTableView*)table {
105 return [configs count];
106 }
107
108 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
109 return index > 0;
110 }
111
112 - (Config *)currentConfig {
113 return currentConfig;
114 }
115
116 - (Config *)currentNeutralConfig {
117 if (neutralConfig)
118 return neutralConfig;
119 return currentConfig;
120 }
121
122 -(void) save {
123 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
124 [[NSUserDefaults standardUserDefaults] synchronize];
125 }
126 -(void) load {
127 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
128 }
129
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];
139 }
140 cfgInfo[@"entries"] = cfgEntries;
141 [ary addObject: cfgInfo];
142 }
143 envelope[@"configurationList"] = ary;
144 envelope[@"selectedIndex"] = @([configs indexOfObject: [self currentNeutralConfig] ]);
145 return envelope;
146 }
147 -(void) loadAllFrom: (NSDictionary*) envelope{
148 if(envelope == NULL)
149 return;
150 NSArray* ary = envelope[@"configurationList"];
151
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];
158 }
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];
163 }
164 }
165
166 configs = newConfigs;
167 [tableView reloadData];
168 currentConfig = NULL;
169 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
170
171 int index = [envelope[@"selectedIndex"] intValue];
172 if (index < configs.count)
173 [self activateConfig: configs[index] forApplication: NULL];
174 }
175
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];
181 return;
182 }
183 }
184 [self restoreNeutralConfig];
185 }
186
187 @end