Shrink and refit window contents.
[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 -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
30 if(currentConfig == config)
31 return;
32
33 if(psn) {
34 if(!neutralConfig)
35 neutralConfig = currentConfig;
36 attachedApplication = *psn;
37 } else {
38 neutralConfig = NULL;
39 }
40
41 if(currentConfig != NULL) {
42 [targetController reset];
43 }
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];
49 }
50
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];
59 }
60 -(IBAction) removePressed: (id)sender {
61 // save changes first
62 [tableView reloadData];
63 Config* current_config = configs[[tableView selectedRow]];
64 if([current_config protect])
65 return;
66 [configs removeObjectAtIndex: [tableView selectedRow]];
67
68 // remove all "switch to configuration" actions
69 for(int i=0; i<[configs count]; i++) {
70 NSMutableDictionary* entries = [(Config*)configs[i] entries];
71 for(id key in entries) {
72 Target* target = (Target*) entries[key];
73 if([target isKindOfClass: [TargetConfig class]] && [(TargetConfig*)target config] == current_config)
74 [entries removeObjectForKey: key];
75 }
76 }
77 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
78
79 [tableView reloadData];
80 }
81
82 -(void)tableViewSelectionDidChange:(NSNotification*) notify {
83 if (tableView.selectedRow < configs.count)
84 [self activateConfig: (Config*)configs[[tableView selectedRow]] forApplication: NULL];
85 }
86
87 -(id) tableView: (NSTableView*)view objectValueForTableColumn: (NSTableColumn*) column row: (int) index {
88 NSParameterAssert(index >= 0 && index < [configs count]);
89 return [configs[index] name];
90 }
91
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[index] setName: newName];
97 [targetController refreshConfigsPreservingSelection:YES];
98 [tableView reloadData];
99 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
100 }
101
102 -(int)numberOfRowsInTableView: (NSTableView*)table {
103 return [configs count];
104 }
105
106 -(BOOL)tableView: (NSTableView*)view shouldEditTableColumn: (NSTableColumn*) column row: (int) index {
107 return ![configs[index] protect];
108 }
109
110 -(Config*) currentConfig {
111 return currentConfig;
112 }
113
114 -(Config*) currentNeutralConfig {
115 if(neutralConfig)
116 return neutralConfig;
117 return currentConfig;
118 }
119
120 -(void) save {
121 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
122 [[NSUserDefaults standardUserDefaults] synchronize];
123 }
124 -(void) load {
125 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
126 }
127
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[@"name"] = [config name];
134 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
135 for(id key in [config entries]) {
136 cfgEntries[key] = [[config entries][key]stringify];
137 }
138 cfgInfo[@"entries"] = cfgEntries;
139 [ary addObject: cfgInfo];
140 }
141 envelope[@"configurationList"] = ary;
142 envelope[@"selectedIndex"] = @([configs indexOfObject: [self currentNeutralConfig] ]);
143 return envelope;
144 }
145 -(void) loadAllFrom: (NSDictionary*) envelope{
146 if(envelope == NULL)
147 return;
148 NSArray* ary = envelope[@"configurationList"];
149
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[i][@"name"]];
155 [newConfigs addObject: cfg];
156 }
157 [configs[0] setProtect: YES];
158 for(int i=0; i<[ary count]; i++) {
159 NSDictionary* dict = ary[i][@"entries"];
160 for(id key in dict) {
161 [newConfigs[i] entries][key] = [Target unstringify: dict[key] withConfigList: newConfigs];
162 }
163 }
164
165 configs = newConfigs;
166 [tableView reloadData];
167 currentConfig = NULL;
168 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
169
170 int index = [envelope[@"selectedIndex"] intValue];
171 if (index < configs.count)
172 [self activateConfig: configs[index] forApplication: NULL];
173 }
174
175 -(void) applicationSwitchedTo: (NSString*) name withPsn: (ProcessSerialNumber) psn {
176 for(int i=0; i<[configs count]; i++) {
177 Config* cfg = configs[i];
178 if([[cfg name] isEqualToString: name]) {
179 [self activateConfig: cfg forApplication: &psn];
180 return;
181 }
182 }
183 [self restoreNeutralConfig];
184 }
185
186 -(ProcessSerialNumber*) targetApplication {
187 if(neutralConfig)
188 return &attachedApplication;
189 return NULL;
190 }
191
192 @end