Rename 'stringify' to 'uid' where it pertains to unique action IDs.
[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
10 @synthesize configs;
11
12 -(id) init {
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];
19 }
20 return self;
21 }
22
23 -(void) restoreNeutralConfig {
24 if(!neutralConfig)
25 return;
26 [self activateConfig: neutralConfig forApplication: NULL];
27 }
28
29 // TODO: Not an appropriate way to track 'neutral configs', it should just
30 // always be the first config and be unremovable.
31
32 -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
33 if(currentConfig == config)
34 return;
35
36 if(psn) {
37 if(!neutralConfig)
38 neutralConfig = currentConfig;
39 } else {
40 neutralConfig = NULL;
41 }
42
43 if(currentConfig != NULL) {
44 [targetController reset];
45 }
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];
51 }
52
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];
61 }
62 -(IBAction) removePressed: (id)sender {
63 // save changes first
64 [tableView reloadData];
65 Config* current_config = configs[[tableView selectedRow]];
66 if([current_config protect])
67 return;
68 [configs removeObjectAtIndex: [tableView selectedRow]];
69
70 // remove all "switch to configuration" actions
71 for (Config *config in configs) {
72 NSMutableDictionary *entries = config.entries;
73 for (id key in entries) {
74 Target *target = entries[key];
75 if([target isKindOfClass:[TargetConfig class]] && [(TargetConfig *)target config] == current_config)
76 [entries removeObjectForKey: key];
77 }
78 }
79 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
80
81 [tableView reloadData];
82 }
83
84 -(void)tableViewSelectionDidChange:(NSNotification*) notify {
85 if (tableView.selectedRow < configs.count)
86 [self activateConfig: (Config*)configs[[tableView selectedRow]] forApplication: NULL];
87 }
88
89 -(id) tableView: (NSTableView*)view objectValueForTableColumn: (NSTableColumn*) column row: (int) index {
90 NSParameterAssert(index >= 0 && index < [configs count]);
91 return [configs[index] name];
92 }
93
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];
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 ![configs[index] protect];
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 [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];
164 }
165 }
166
167 configs = newConfigs;
168 [tableView reloadData];
169 currentConfig = NULL;
170 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
171
172 int index = [envelope[@"selectedIndex"] intValue];
173 if (index < configs.count)
174 [self activateConfig: configs[index] forApplication: NULL];
175 }
176
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];
182 return;
183 }
184 }
185 [self restoreNeutralConfig];
186 }
187
188 @end