7db30bf9318d3a6c8bb7d6ddb48bdb75c034fa65
[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:obj forTableColumn:(NSTableColumn *)col row:(int)index {
98 /* ugly hack so stringification doesn't fail */
99 NSString* newName = [(NSString*)obj stringByReplacingOccurrencesOfString: @"~" withString: @""];
100 [(Config *)configs[index] setName:newName];
101 [targetController refreshConfigsPreservingSelection:YES];
102 [tableView reloadData];
103 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
104 }
105
106 - (int)numberOfRowsInTableView:(NSTableView*)table {
107 return [configs count];
108 }
109
110 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
111 return index > 0;
112 }
113
114 -(void) save {
115 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
116 [[NSUserDefaults standardUserDefaults] synchronize];
117 }
118 -(void) load {
119 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
120 }
121
122 -(NSDictionary*) dumpAll {
123 NSMutableDictionary *envelope = [[NSMutableDictionary alloc] init];
124 NSMutableArray* ary = [[NSMutableArray alloc] init];
125 for(Config* config in configs) {
126 NSMutableDictionary* cfgInfo = [[NSMutableDictionary alloc] init];
127 cfgInfo[@"name"] = [config name];
128 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
129 for(id key in [config entries]) {
130 cfgEntries[key] = [[config entries][key]stringify];
131 }
132 cfgInfo[@"entries"] = cfgEntries;
133 [ary addObject: cfgInfo];
134 }
135 envelope[@"configurationList"] = ary;
136 return envelope;
137 }
138
139 -(void) loadAllFrom: (NSDictionary*) envelope{
140 if(envelope == NULL)
141 return;
142 NSArray* ary = envelope[@"configurationList"];
143
144 NSMutableArray* newConfigs = [[NSMutableArray alloc] init];
145 // have to do two passes in case config1 refers to config2 via a TargetConfig
146 for(int i=0; i<[ary count]; i++) {
147 Config* cfg = [[Config alloc] init];
148 [cfg setName: ary[i][@"name"]];
149 [newConfigs addObject: cfg];
150 }
151 for(int i=0; i<[ary count]; i++) {
152 NSDictionary* dict = ary[i][@"entries"];
153 for(id key in dict) {
154 [newConfigs[i] entries][key] = [Target unstringify: dict[key] withConfigList: newConfigs];
155 }
156 }
157
158 if (newConfigs.count) {
159 configs = newConfigs;
160 [tableView reloadData];
161 currentConfig = configs[0];
162 manualConfig = configs[0];
163 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
164 }
165 }
166
167 @end