Use a weak reference for TargetConfig configurations to avoid a circular reference...
[enjoyable.git] / ConfigsController.m
1 //
2 // ConfigsController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 @implementation ConfigsController {
9 NSMutableArray *configs;
10 Config *manualConfig;
11 }
12
13 @synthesize currentConfig;
14 @synthesize configs;
15
16 - (id)init {
17 if ((self = [super init])) {
18 configs = [[NSMutableArray alloc] init];
19 currentConfig = [[Config alloc] init];
20 currentConfig.name = @"(default)";
21 manualConfig = currentConfig;
22 [configs addObject:currentConfig];
23 }
24 return self;
25 }
26
27 - (Config *)objectForKeyedSubscript:(NSString *)name {
28 for (Config *config in configs)
29 if ([name isEqualToString:config.name])
30 return config;
31 return nil;
32 }
33
34 - (void)activateConfigForProcess:(NSString *)processName {
35 Config *oldConfig = manualConfig;
36 [self activateConfig:self[processName]];
37 manualConfig = oldConfig;
38 }
39
40 - (void)activateConfig:(Config *)config {
41 if (!config)
42 config = manualConfig;
43 if (currentConfig == config)
44 return;
45 manualConfig = config;
46 currentConfig = config;
47 [targetController reset];
48 [removeButton setEnabled:configs[0] != config];
49 [targetController load];
50 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged];
51 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[configs indexOfObject:config]] byExtendingSelection:NO];
52 }
53
54 - (IBAction)addPressed:(id)sender {
55 Config *newConfig = [[Config alloc] init];
56 newConfig.name = @"untitled";
57 [configs addObject:newConfig];
58 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
59 [tableView reloadData];
60 [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:configs.count - 1] byExtendingSelection:NO];
61 [tableView editColumn:0 row:[configs count] - 1 withEvent:nil select:YES];
62 }
63
64 - (IBAction)removePressed:(id)sender {
65 if (tableView.selectedRow == 0)
66 return;
67
68 Config *toRemove = configs[tableView.selectedRow];
69 [configs removeObjectAtIndex:tableView.selectedRow];
70
71 if (toRemove == currentConfig)
72 currentConfig = configs[0];
73 if (toRemove == manualConfig)
74 manualConfig = configs[0];
75
76 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
77 [tableView reloadData];
78 }
79
80 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
81 if (tableView.selectedRow >= 0)
82 [self activateConfig:configs[tableView.selectedRow]];
83 }
84
85 - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index {
86 return [configs[index] name];
87 }
88
89 - (void)tableView:(NSTableView *)view setObjectValue:obj forTableColumn:(NSTableColumn *)col row:(int)index {
90 /* ugly hack so stringification doesn't fail */
91 NSString* newName = [(NSString*)obj stringByReplacingOccurrencesOfString: @"~" withString: @""];
92 [(Config *)configs[index] setName:newName];
93 [targetController refreshConfigsPreservingSelection:YES];
94 [tableView reloadData];
95 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
96 }
97
98 - (int)numberOfRowsInTableView:(NSTableView*)table {
99 return [configs count];
100 }
101
102 - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index {
103 return index > 0;
104 }
105
106 -(void) save {
107 [[NSUserDefaults standardUserDefaults] setObject:[self dumpAll] forKey:@"configurations"];
108 [[NSUserDefaults standardUserDefaults] synchronize];
109 }
110 -(void) load {
111 [self loadAllFrom: [[NSUserDefaults standardUserDefaults] objectForKey:@"configurations"]];
112 }
113
114 -(NSDictionary*) dumpAll {
115 NSMutableDictionary *envelope = [[NSMutableDictionary alloc] init];
116 NSMutableArray* ary = [[NSMutableArray alloc] init];
117 for(Config* config in configs) {
118 NSMutableDictionary* cfgInfo = [[NSMutableDictionary alloc] init];
119 cfgInfo[@"name"] = [config name];
120 NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] init];
121 for(id key in [config entries]) {
122 cfgEntries[key] = [[config entries][key]stringify];
123 }
124 cfgInfo[@"entries"] = cfgEntries;
125 [ary addObject: cfgInfo];
126 }
127 envelope[@"configurationList"] = ary;
128 return envelope;
129 }
130
131 -(void) loadAllFrom: (NSDictionary*) envelope{
132 if(envelope == NULL)
133 return;
134 NSArray* ary = envelope[@"configurationList"];
135
136 NSMutableArray* newConfigs = [[NSMutableArray alloc] init];
137 // have to do two passes in case config1 refers to config2 via a TargetConfig
138 for(int i=0; i<[ary count]; i++) {
139 Config* cfg = [[Config alloc] init];
140 [cfg setName: ary[i][@"name"]];
141 [newConfigs addObject: cfg];
142 }
143 for(int i=0; i<[ary count]; i++) {
144 NSDictionary* dict = ary[i][@"entries"];
145 for(id key in dict) {
146 [newConfigs[i] entries][key] = [Target unstringify: dict[key] withConfigList: newConfigs];
147 }
148 }
149
150 if (newConfigs.count) {
151 configs = newConfigs;
152 [tableView reloadData];
153 currentConfig = configs[0];
154 manualConfig = configs[0];
155 [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged];
156 }
157 }
158
159 @end