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