Forked Enjoy, mouse movement
[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 if([configs indexOfObject:neutralConfig] < 0) {// deleted, keep what we have
27 neutralConfig = NULL;
28 return;
29 }
30 [self activateConfig: neutralConfig forApplication: NULL];
31 }
32
33 -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn {
34 if(currentConfig == config)
35 return;
36
37 if(psn) {
38 if(!neutralConfig)
39 neutralConfig = currentConfig;
40 attachedApplication = *psn;
41 } else {
42 neutralConfig = NULL;
43 }
44
45 if(currentConfig != NULL) {
46 [targetController reset];
47 }
48 currentConfig = config;
49 [removeButton setEnabled: ![config protect]];
50 [targetController load];
51 [[[NSApplication sharedApplication] delegate] configChanged];
52 [tableView selectRow: [configs indexOfObject: config] byExtendingSelection: NO];
53 }
54
55 -(IBAction) addPressed: (id)sender {
56 Config* newConfig = [[Config alloc] init];
57 [newConfig setName: @"untitled"];
58 [configs addObject: newConfig];
59 [[[NSApplication sharedApplication] delegate] configsChanged];
60 [tableView reloadData];
61 [tableView selectRow: ([configs count]-1) byExtendingSelection: NO];
62 [tableView editColumn: 0 row:([configs count]-1) withEvent:nil select:YES];
63 }
64 -(IBAction) removePressed: (id)sender {
65 // save changes first
66 [tableView reloadData];
67 Config* current_config = [configs objectAtIndex: [tableView selectedRow]];
68 if([current_config protect])
69 return;
70 [configs removeObjectAtIndex: [tableView selectedRow]];
71
72 // remove all "switch to configuration" actions
73 for(int i=0; i<[configs count]; i++) {
74 NSMutableDictionary* entries = [(Config*)[configs objectAtIndex:i] entries];
75 for(id key in entries) {
76 Target* target = (Target*) [entries objectForKey: key];
77 if([target isKindOfClass: [TargetConfig class]] && [(TargetConfig*)target config] == current_config)
78 [entries removeObjectForKey: key];
79 }
80 }
81 [[[NSApplication sharedApplication] delegate] configsChanged];
82
83 [tableView reloadData];
84 }
85
86 -(void)tableViewSelectionDidChange:(NSNotification*) notify {
87 [self activateConfig: (Config*)[configs objectAtIndex:[tableView selectedRow]] forApplication: NULL];
88 }
89
90 -(id) tableView: (NSTableView*)view objectValueForTableColumn: (NSTableColumn*) column row: (int) index {
91 NSParameterAssert(index >= 0 && index < [configs count]);
92 return [[configs objectAtIndex: index] name];
93 }
94
95 -(void) tableView: (NSTableView*) view setObjectValue:obj forTableColumn:(NSTableColumn*) col row: (int)index {
96 NSParameterAssert(index >= 0 && index < [configs count]);
97 /* ugly hack so stringification doesn't fail */
98 NSString* newName = [(NSString*)obj stringByReplacingOccurrencesOfString: @"~" withString: @""];
99 [(Config*)[configs objectAtIndex: index] setName: newName];
100 [targetController refreshConfigsPreservingSelection:YES];
101 [tableView reloadData];
102 [[[NSApplication sharedApplication] delegate] configsChanged];
103 }
104
105 -(int)numberOfRowsInTableView: (NSTableView*)table {
106 return [configs count];
107 }
108
109 -(BOOL)tableView: (NSTableView*)view shouldEditTableColumn: (NSTableColumn*) column row: (int) index {
110 return ![[configs objectAtIndex: index] protect];
111 }
112
113 -(Config*) currentConfig {
114 return currentConfig;
115 }
116
117 -(Config*) currentNeutralConfig {
118 if(neutralConfig)
119 return neutralConfig;
120 return currentConfig;
121 }
122
123 -(void) save {
124 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
125 [[NSUserDefaults standardUserDefaults] synchronize];
126 }
127 -(void) load {
128 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
129 }
130
131 -(NSDictionary*) dumpAll {
132 NSMutableDictionary *envelope = [[NSMutableDictionary alloc] init];
133 NSMutableArray* ary = [[NSMutableArray alloc] init];
134 for(Config* config in configs) {
135 NSMutableDictionary* cfgInfo = [[NSMutableDictionary alloc] init];
136 [cfgInfo setObject:[config name] forKey:@"name"];
137 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
138 for(id key in [config entries]) {
139 [cfgEntries setObject:[[[config entries]objectForKey:key]stringify] forKey: key];
140 }
141 [cfgInfo setObject: cfgEntries forKey: @"entries"];
142 [ary addObject: cfgInfo];
143 }
144 [envelope setObject: ary forKey: @"configurationList"];
145 [envelope setObject: [NSNumber numberWithInt: [configs indexOfObject: [self currentNeutralConfig] ] ] forKey: @"selectedIndex"];
146 return envelope;
147 }
148 -(void) loadAllFrom: (NSDictionary*) envelope{
149 if(envelope == NULL)
150 return;
151 NSArray* ary = [envelope objectForKey: @"configurationList"];
152
153 NSMutableArray* newConfigs = [[NSMutableArray alloc] init];
154 // have to do two passes in case config1 refers to config2 via a TargetConfig
155 for(int i=0; i<[ary count]; i++) {
156 Config* cfg = [[Config alloc] init];
157 [cfg setName: [[ary objectAtIndex:i] objectForKey:@"name"]];
158 [newConfigs addObject: cfg];
159 }
160 [[configs objectAtIndex:0] setProtect: YES];
161 for(int i=0; i<[ary count]; i++) {
162 NSDictionary* dict = [[ary objectAtIndex:i] objectForKey:@"entries"];
163 for(id key in dict) {
164 [[[newConfigs objectAtIndex:i] entries]
165 setObject: [Target unstringify: [dict objectForKey: key] withConfigList: newConfigs]
166 forKey: key];
167 }
168 }
169
170 configs = newConfigs;
171 [tableView reloadData];
172 currentConfig = NULL;
173 [[[NSApplication sharedApplication] delegate] configsChanged];
174
175 int index = [[envelope objectForKey: @"selectedIndex"] intValue];
176 [self activateConfig: [configs objectAtIndex:index] forApplication: NULL];
177 }
178
179 -(void) applicationSwitchedTo: (NSString*) name withPsn: (ProcessSerialNumber) psn {
180 for(int i=0; i<[configs count]; i++) {
181 Config* cfg = [configs objectAtIndex:i];
182 if([[cfg name] isEqualToString: name]) {
183 [self activateConfig: cfg forApplication: &psn];
184 return;
185 }
186 }
187 [self restoreNeutralConfig];
188 }
189
190 -(ProcessSerialNumber*) targetApplication {
191 if(neutralConfig)
192 return &attachedApplication;
193 return NULL;
194 }
195
196 @end