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