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