Fix a variety of issues with incorrect / unexpected / unfriendly first responder...
[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 - (id)init {
22 if ((self = [super init])) {
23 _configs = [[NSMutableArray alloc] init];
24 _currentConfig = [[Config alloc] initWithName:@"(default)"];
25 manualConfig = _currentConfig;
26 [_configs addObject:_currentConfig];
27 }
28 return self;
29 }
30
31 - (Config *)objectForKeyedSubscript:(NSString *)name {
32 for (Config *config in _configs)
33 if ([name isEqualToString:config.name])
34 return config;
35 return nil;
36 }
37
38 - (void)activateConfigForProcess:(NSString *)processName {
39 Config *oldConfig = manualConfig;
40 [self activateConfig:self[processName]];
41 manualConfig = oldConfig;
42 }
43
44 - (void)activateConfig:(Config *)config {
45 if (!config)
46 config = manualConfig;
47 if (_currentConfig == config)
48 return;
49 manualConfig = config;
50 _currentConfig = config;
51 [removeButton setEnabled:_configs[0] != config];
52 [targetController loadCurrent];
53 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
54 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[_configs indexOfObject:config]] byExtendingSelection:NO];
55 }
56
57 - (IBAction)addPressed:(id)sender {
58 Config *newConfig = [[Config alloc] initWithName:@"Untitled"];
59 [_configs addObject:newConfig];
60 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
61 [tableView reloadData];
62 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_configs.count - 1] byExtendingSelection:NO];
63 [tableView editColumn:0 row:[_configs count] - 1 withEvent:nil select:YES];
64 }
65
66 - (IBAction)removePressed:(id)sender {
67 if (tableView.selectedRow == 0)
68 return;
69
70 Config *toRemove = _configs[tableView.selectedRow];
71 [_configs removeObjectAtIndex:tableView.selectedRow];
72
73 if (toRemove == _currentConfig)
74 _currentConfig = _configs[0];
75 if (toRemove == manualConfig)
76 manualConfig = _configs[0];
77
78 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
79 [tableView reloadData];
80 }
81
82 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
83 if (tableView.selectedRow >= 0)
84 [self activateConfig:_configs[tableView.selectedRow]];
85 }
86
87 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
88 return [_configs[index] name];
89 }
90
91 - (void)tableView:(NSTableView *)view setObjectValue:(NSString *)obj forTableColumn:(NSTableColumn *)col row:(int)index {
92 [(Config *)_configs[index] setName:obj];
93 [tableView reloadData];
94 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
95 }
96
97 - (int)numberOfRowsInTableView:(NSTableView*)table {
98 return [_configs count];
99 }
100
101 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
102 return index > 0;
103 }
104
105 - (void)save {
106 NSLog(@"Saving defaults.");
107 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
108 }
109
110 - (void)load {
111 [self loadAllFrom:[[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
112 }
113
114 - (NSDictionary *)dumpAll {
115 NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_configs.count];
116 for (Config *config in _configs) {
117 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] initWithCapacity:config.entries.count];
118 for (id key in config.entries)
119 cfgEntries[key] = [config.entries[key] serialize];
120 [ary addObject:@{ @"name": config.name,
121 @"entries": cfgEntries,
122 }];
123 }
124 NSUInteger current = _currentConfig ? [_configs indexOfObject:_currentConfig] : 0;
125 return @{ @"configurationList": ary,
126 @"selectedConfiguration": @(current) };
127 }
128
129 - (void)loadAllFrom:(NSDictionary*) envelope{
130 NSArray *storedConfigs = envelope[@"configurationList"];
131 NSMutableArray* newConfigs = [[NSMutableArray alloc] initWithCapacity:storedConfigs.count];
132
133 // have to do two passes in case config1 refers to config2 via a TargetConfig
134 for (NSDictionary *storedConfig in storedConfigs) {
135 Config *cfg = [[Config alloc] initWithName:storedConfig[@"name"]];
136 [newConfigs addObject:cfg];
137 }
138
139 for (int i = 0; i < storedConfigs.count; ++i) {
140 NSDictionary *entries = storedConfigs[i][@"entries"];
141 Config *config = newConfigs[i];
142 for (id key in entries)
143 config.entries[key] = [Target targetDeserialize:entries[key]
144 withConfigs:newConfigs];
145 }
146
147 if (newConfigs.count) {
148 int current = [envelope[@"selectedConfiguration"] unsignedIntValue];
149 if (current >= newConfigs.count)
150 current = 0;
151 _configs = newConfigs;
152 [tableView reloadData];
153 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
154 [self activateConfig:_configs[current]];
155 }
156 }
157
158 @end