Show an error message if opening input devices fail. Move real vs. configuration...
[enjoyable.git] / ConfigsController.m
1 //
2 // ConfigsController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "ConfigsController.h"
9
10 #import "ApplicationController.h"
11 #import "Config.h"
12 #import "ConfigsController.h"
13 #import "Target.h"
14 #import "TargetController.h"
15
16 @implementation ConfigsController {
17 NSMutableArray *configs;
18 Config *manualConfig;
19 }
20
21 @synthesize currentConfig;
22 @synthesize configs;
23
24 - (id)init {
25 if ((self = [super init])) {
26 configs = [[NSMutableArray alloc] init];
27 currentConfig = [[Config alloc] init];
28 currentConfig.name = @"(default)";
29 manualConfig = currentConfig;
30 [configs addObject:currentConfig];
31 }
32 return self;
33 }
34
35 - (Config *)objectForKeyedSubscript:(NSString *)name {
36 for (Config *config in configs)
37 if ([name isEqualToString:config.name])
38 return config;
39 return nil;
40 }
41
42 - (void)activateConfigForProcess:(NSString *)processName {
43 Config *oldConfig = manualConfig;
44 [self activateConfig:self[processName]];
45 manualConfig = oldConfig;
46 }
47
48 - (void)activateConfig:(Config *)config {
49 if (!config)
50 config = manualConfig;
51 if (currentConfig == config)
52 return;
53 manualConfig = config;
54 currentConfig = config;
55 [targetController reset];
56 [removeButton setEnabled:configs[0] != config];
57 [targetController load];
58 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
59 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[configs indexOfObject:config]] byExtendingSelection:NO];
60 }
61
62 - (IBAction)addPressed:(id)sender {
63 Config *newConfig = [[Config alloc] init];
64 newConfig.name = @"untitled";
65 [configs addObject:newConfig];
66 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
67 [tableView reloadData];
68 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:configs.count - 1] byExtendingSelection:NO];
69 [tableView editColumn:0 row:[configs count] - 1 withEvent:nil select:YES];
70 }
71
72 - (IBAction)removePressed:(id)sender {
73 if (tableView.selectedRow == 0)
74 return;
75
76 Config *toRemove = configs[tableView.selectedRow];
77 [configs removeObjectAtIndex:tableView.selectedRow];
78
79 if (toRemove == currentConfig)
80 currentConfig = configs[0];
81 if (toRemove == manualConfig)
82 manualConfig = configs[0];
83
84 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
85 [tableView reloadData];
86 }
87
88 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
89 if (tableView.selectedRow >= 0)
90 [self activateConfig:configs[tableView.selectedRow]];
91 }
92
93 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
94 return [configs[index] name];
95 }
96
97 - (void)tableView:(NSTableView *)view setObjectValue:(NSString *)obj forTableColumn:(NSTableColumn *)col row:(int)index {
98 [(Config *)configs[index] setName:obj];
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 - (void)save {
113 NSLog(@"Saving defaults.");
114 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
115 }
116
117 - (void)load {
118 [self loadAllFrom:[[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
119 }
120
121 - (NSDictionary *)dumpAll {
122 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:configs.count];
123 for (Config *config in configs) {
124 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] initWithCapacity:config.entries.count];
125 for (id key in config.entries)
126 cfgEntries[key] = [config.entries[key] serialize];
127 [ary addObject:@{ @"name": config.name,
128 @"entries": cfgEntries,
129 }];
130 }
131 NSUInteger current = currentConfig ? [configs indexOfObject:currentConfig] : 0;
132 return @{ @"configurationList": ary,
133 @"selectedConfiguration": @(current) };
134 }
135
136 - (void)loadAllFrom:(NSDictionary*) envelope{
137 NSArray *storedConfigs = envelope[@"configurationList"];
138 NSMutableArray* newConfigs = [[NSMutableArray alloc] initWithCapacity:storedConfigs.count];
139
140 // have to do two passes in case config1 refers to config2 via a TargetConfig
141 for (NSDictionary *storedConfig in storedConfigs) {
142 Config *cfg = [[Config alloc] init];
143 cfg.name = storedConfig[@"name"];
144 [newConfigs addObject:cfg];
145 }
146
147 for (int i = 0; i < storedConfigs.count; ++i) {
148 NSDictionary *entries = storedConfigs[i][@"entries"];
149 Config *config = newConfigs[i];
150 for (id key in entries)
151 config.entries[key] = [Target targetDeserialize:entries[key]
152 withConfigs:newConfigs];
153 }
154
155 if (newConfigs.count) {
156 int current = [envelope[@"selectedConfiguration"] unsignedIntValue];
157 if (current >= newConfigs.count)
158 current = 0;
159 configs = newConfigs;
160 [tableView reloadData];
161 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
162 [self activateConfig:configs[current]];
163 }
164 }
165
166 @end