Replace use of deprecated selectRow:byExtendingSelection:.
[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 objectAtIndex: [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 objectAtIndex:i] entries];
71 for(id key in entries) {
72 Target* target = (Target*) [entries objectForKey: 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 objectAtIndex:[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 objectAtIndex: 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 objectAtIndex: 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 objectAtIndex: 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 setObject:[config name] forKey:@"name"];
134 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
135 for(id key in [config entries]) {
136 [cfgEntries setObject:[[[config entries]objectForKey:key]stringify] forKey: key];
137 }
138 [cfgInfo setObject: cfgEntries forKey: @"entries"];
139 [ary addObject: cfgInfo];
140 }
141 [envelope setObject: ary forKey: @"configurationList"];
142 [envelope setObject: [NSNumber numberWithInt: [configs indexOfObject: [self currentNeutralConfig] ] ] forKey: @"selectedIndex"];
143 return envelope;
144 }
145 -(void) loadAllFrom: (NSDictionary*) envelope{
146 if(envelope == NULL)
147 return;
148 NSArray* ary = [envelope objectForKey: @"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 objectAtIndex:i] objectForKey:@"name"]];
155 [newConfigs addObject: cfg];
156 }
157 [[configs objectAtIndex:0] setProtect: YES];
158 for(int i=0; i<[ary count]; i++) {
159 NSDictionary* dict = [[ary objectAtIndex:i] objectForKey:@"entries"];
160 for(id key in dict) {
161 [[[newConfigs objectAtIndex:i] entries]
162 setObject: [Target unstringify: [dict objectForKey: key] withConfigList: newConfigs]
163 forKey: key];
164 }
165 }
166
167 configs = newConfigs;
168 [tableView reloadData];
169 currentConfig = NULL;
170 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
171
172 int index = [[envelope objectForKey: @"selectedIndex"] intValue];
173 if (index < configs.count)
174 [self activateConfig: [configs objectAtIndex: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 objectAtIndex:i];
180 if([[cfg name] isEqualToString: name]) {
181 [self activateConfig: cfg forApplication: &psn];
182 return;
183 }
184 }
185 [self restoreNeutralConfig];
186 }
187
188 -(ProcessSerialNumber*) targetApplication {
189 if(neutralConfig)
190 return &attachedApplication;
191 return NULL;
192 }
193
194 @end