1 | //␊ |
2 | // PreferencesControllerBase.mm␊ |
3 | // ChameleonPrefPane␊ |
4 | //␊ |
5 | // Created by Rekursor on 1/22/10.␊ |
6 | //␊ |
7 | ␊ |
8 | #import "PreferencesControllerBase.h"␊ |
9 | #import "KernOptionsParser.h"␊ |
10 | #include <string>␊ |
11 | #include <list>␊ |
12 | #include <map>␊ |
13 | #include <ctype.h>␊ |
14 | ␊ |
15 | ␊ |
16 | //--------------------------------------------------------------------------␊ |
17 | static std::list<id> groupList;␊ |
18 | ␊ |
19 | // for unix-like options types␊ |
20 | static std::map<void*, std::string> IdToUCmdList;␊ |
21 | static KernOptionsParser kernelFlags;␊ |
22 | ␊ |
23 | //--------------------------------------------------------------------------␊ |
24 | ␊ |
25 | ␊ |
26 | @implementation PreferencesControllerBase␊ |
27 | ␊ |
28 | -(ChameleonPrefPane*) chameleon { return [ChameleonPrefPane instance]; }␊ |
29 | ␊ |
30 | ␊ |
31 | //--------------------------------------------------------------------------␊ |
32 | - (id) init␊ |
33 | {␊ |
34 | ␉self = [super init];␊ |
35 | ␉␊ |
36 | ␉[PreferencesControllerBase registerPreferencesGroup: self];␊ |
37 | ␉␊ |
38 | ␉return self;␊ |
39 | }␉␊ |
40 | ␊ |
41 | //--------------------------------------------------------------------------␊ |
42 | // from the id to desc map in prop, set all default values for dict␊ |
43 | + (void) setDefaultValues: (NSMutableDictionary*) dict ␊ |
44 | {␊ |
45 | ␉for(const BootOptionDesc* bod = prop->firstOption();bod; bod=prop->nextOption())␊ |
46 | ␉{␊ |
47 | ␉␉switch (bod->Type) ␊ |
48 | ␉␉{␊ |
49 | ␉␉␉case OptionYesNo:␊ |
50 | ␉␉␉␉[(NSButton*)bod->ID setIntValue: (toupper(bod->Default[0])=='Y' ? 1 : 0) ];␊ |
51 | ␉␉␉␉break;␊ |
52 | ␉␉␉case OptionKernel:␊ |
53 | ␉␉␉case OptionString:␊ |
54 | ␉␉␉␉[(NSButton*)bod->ID setIntValue: 0 ];␊ |
55 | ␉␉␉case OptionKernel1:␊ |
56 | ␉␉␉␉[(NSTextField*)bod->contentID setStringValue: ␊ |
57 | ␉␉␉␉␉[[NSString alloc] initWithUTF8String: bod->Default] ];␊ |
58 | ␉␉␉␉break;␊ |
59 | ␉␉␉case OptionUnix:␊ |
60 | ␉␉␉␉[(NSButton*)bod->ID setIntValue: 0 ];␊ |
61 | ␉␉␉␉break;␊ |
62 | ␉␉␉␉␊ |
63 | ␉␉␉default:␊ |
64 | ␉␉␉␉break;␊ |
65 | ␉␉}␊ |
66 | ␉}␊ |
67 | }␊ |
68 | ␊ |
69 | //--------------------------------------------------------------------------␊ |
70 | + (void) registerPreferencesGroup:(id) myGroup␊ |
71 | {␊ |
72 | ␉groupList.push_back(myGroup);␊ |
73 | }␊ |
74 | ␊ |
75 | //--------------------------------------------------------------------------␊ |
76 | + (void) doForEachGroup: (GroupAction) action withOption:(id) option␊ |
77 | {␊ |
78 | ␉std::list<id>::iterator it;␊ |
79 | ␉for (it=groupList.begin(); it!=groupList.end(); it++)␊ |
80 | ␉{␊ |
81 | ␉␉switch (action) {␊ |
82 | ␉␉␉case SetDefaultValues:␊ |
83 | ␉␉␉␉[*it setDefaultValues: option];␊ |
84 | ␉␉␉␉break;␊ |
85 | ␉␉␉case RefreshLockStates:␊ |
86 | ␉␉␉␉[*it refreshLockStates ];␊ |
87 | ␉␉␉␉break;␊ |
88 | ␉␉␉case LoadPreferencesOptions:␊ |
89 | ␉␉␉␉[*it loadOptionsFromPreferencesFile: option];␊ |
90 | ␉␉␉␉break;␊ |
91 | ␉␉␉case LoadBootConfigOptions:␊ |
92 | ␉␉␉␉[*it loadOptionsFromBootFile];␊ |
93 | ␉␉␉␉break;␊ |
94 | ␉␉␉case AddOptionsDesc:␊ |
95 | ␉␉␉␉[*it addOptionsDesc];␊ |
96 | ␉␉␉␉break;␊ |
97 | ␉␉␉case SaveBootConfigOptions:␊ |
98 | ␉␉␉␉break;␊ |
99 | ␉␉␉default:␊ |
100 | ␉␉␉␉break;␊ |
101 | ␉␉}␊ |
102 | ␉}␊ |
103 | }␊ |
104 | ␊ |
105 | //--------------------------------------------------------------------------␊ |
106 | + (void) loadOptionsFromBootFile␊ |
107 | {␊ |
108 | ␉// *** TODO␊ |
109 | ␉// parse unix like command string:␊ |
110 | ␉kernelFlags.parseOptions(prop->getStringForKey(kKernelFlags));␊ |
111 | ␉␊ |
112 | ␉for (const BootOptionDesc* bod=prop->firstOption(); bod; bod=prop->nextOption())␊ |
113 | ␉{␊ |
114 | ␉␉[PreferencesControllerBase loadOptionFromBootFile:(id)bod->ID ];␊ |
115 | ␉}␊ |
116 | }␊ |
117 | ␊ |
118 | //--------------------------------------------------------------------------␊ |
119 | ␊ |
120 | + (void) loadOptionFromBootFile:(id) optionID␊ |
121 | {␊ |
122 | ␉const BootOptionDesc* bod = prop->findOption(optionID);␊ |
123 | ␉if (!bod) ␊ |
124 | ␉{␊ |
125 | ␉␉NSRunAlertPanel(@"Error Parsing Option",@"loadOptionFromBootFile failed",@"OK", nil, nil);␊ |
126 | ␉␉return;␊ |
127 | ␉} ␊ |
128 | ␉␊ |
129 | ␉const char * stringForKey = prop->getStringForKey(bod->Name);␊ |
130 | ␉std::string s = stringForKey ? trim(stringForKey) : "";␊ |
131 | ␉␊ |
132 | ␉switch (bod->Type) ␊ |
133 | ␉{␊ |
134 | ␉␉case OptionYesNo:␊ |
135 | ␉␉␉if (stringForKey!=NULL) ␊ |
136 | ␉␉␉␉[(NSButton*)optionID setIntValue: (toupper(s[0])=='Y' ? 1 : 0 ) ];␊ |
137 | ␉␉␉break;␊ |
138 | ␉␉␉␊ |
139 | ␉␉case OptionKernel1:␊ |
140 | ␉␉{␊ |
141 | ␉␉␉int val = (s.length()>0 ? 1 : 0 );␊ |
142 | ␉␉␉[(NSButton*)optionID setIntValue: val ];␊ |
143 | ␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
144 | ␉␉␉ [[NSString alloc] initWithUTF8String: s.c_str()] ];␊ |
145 | ␉␉␉[(NSTextField*) bod->contentID setEnabled: val ? true : false]; ␊ |
146 | ␉␉␉[(NSTextField*) bod->contentID setEditable: val ? true : false]; ␊ |
147 | ␉␉}␊ |
148 | ␉␉␉break;␊ |
149 | ␉␉case OptionString:␊ |
150 | ␉␉␉[(NSButton*)optionID setIntValue: (s.length()>0 ? 1 : 0 ) ];␊ |
151 | ␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
152 | ␉␉␉␉[[NSString alloc] initWithUTF8String: s.c_str()] ];␊ |
153 | ␉␉␉break;␊ |
154 | ␉␉␉␊ |
155 | ␉␉case OptionUnix:␊ |
156 | ␉␉case OptionKernel:␊ |
157 | ␉␉{␊ |
158 | ␉␉␉std::string s = kernelFlags.stringFromKey(bod->Name);␊ |
159 | ␉␉␉if (s.length()>0)␊ |
160 | ␉␉␉{␊ |
161 | ␉␉␉␉[(NSButton*)optionID setIntValue: 1 ];␊ |
162 | ␉␉␉␉if(bod->Type==OptionKernel)␊ |
163 | ␉␉␉␉{␊ |
164 | ␉␉␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
165 | ␉␉␉␉␉ [[NSString alloc] initWithUTF8String: ␊ |
166 | ␉␉␉␉␉␉kernelFlags.rightMember(s).c_str()] ];␊ |
167 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEnabled: true]; ␊ |
168 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEditable: true]; ␊ |
169 | ␉␉␉␉}␊ |
170 | ␊ |
171 | ␉␉␉}␊ |
172 | ␉␉␉else␊ |
173 | ␉␉␉{ // set the default for thiso option ␊ |
174 | ␉␉␉␉[(NSButton*)optionID setIntValue: (bod->Default[0] ? 1 :0) ];␊ |
175 | ␉␉␉␉if(bod->Type==OptionKernel)␊ |
176 | ␉␉␉␉{␊ |
177 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEnabled: false]; ␊ |
178 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEditable: false]; ␊ |
179 | ␉␉␉␉}␊ |
180 | ␊ |
181 | ␉␉␉}␊ |
182 | ␉␉}␊ |
183 | ␉␉␉break;␊ |
184 | ␉␉default:␊ |
185 | ␉␉␉break;␊ |
186 | ␉}␊ |
187 | ␉␊ |
188 | }␊ |
189 | //--------------------------------------------------------------------------␊ |
190 | - (void) refreshLockState: (id) item␊ |
191 | {␊ |
192 | [item setEnabled:[[ChameleonPrefPane instance] isUnlocked]];␊ |
193 | ␉␊ |
194 | }␊ |
195 | ␊ |
196 | //--------------------------------------------------------------------------␊ |
197 | - (void) loadPreferences␊ |
198 | {␊ |
199 | ␉[ [ChameleonPrefPane instance] loadPreferences];␊ |
200 | }␊ |
201 | ␊ |
202 | //--------------------------------------------------------------------------␊ |
203 | - (bool) savePreferences␊ |
204 | {␊ |
205 | ␉return [ [ChameleonPrefPane instance] savePreferences: [self preferencesFile] ];␊ |
206 | ␉␊ |
207 | }␊ |
208 | //--------------------------------------------------------------------------␊ |
209 | -(NSMutableDictionary*) preferencesFile␊ |
210 | {␉␊ |
211 | ␉return [[ChameleonPrefPane instance] preferencesFile];␊ |
212 | }␊ |
213 | ␊ |
214 | //--------------------------------------------------------------------------␊ |
215 | -(NSMutableDictionary*) preferencesParts ␊ |
216 | { ␊ |
217 | ␉return [[ChameleonPrefPane instance] preferencesParts];␊ |
218 | }␊ |
219 | ␊ |
220 | //--------------------------------------------------------------------------␊ |
221 | - (bool) handleSender: (id) sender␊ |
222 | {␊ |
223 | ␊ |
224 | ␉const BootOptionDesc * bod = prop->findOption(sender);␊ |
225 | ␉␊ |
226 | ␉if (!bod) {␊ |
227 | ␉␉NSTextField* textField = (NSTextField*) sender;␊ |
228 | ␉␉std::string content = [[textField stringValue] UTF8String ];␊ |
229 | ␉}␊ |
230 | ␉else␊ |
231 | ␉{␊ |
232 | ␉␉␊ |
233 | ␉␉int state = [sender intValue];␊ |
234 | ␉␊ |
235 | ␉␉switch (bod->Type) {␊ |
236 | ␉␉␉case OptionKernel:␊ |
237 | ␉␉␉case OptionKernel1:␊ |
238 | ␉␉␉case OptionString:␊ |
239 | ␉␉␉[(NSTextField*) bod->contentID setEnabled: state ? true : false];␊ |
240 | ␉␉␉[(NSTextField*) bod->contentID setEditable: state ? true : false];␊ |
241 | ␉␉␉␉break;␊ |
242 | ␉␉␉default:␊ |
243 | ␉␉␉␉break;␊ |
244 | ␉␉}␊ |
245 | ␉}␉␊ |
246 | ␉return true;␊ |
247 | }␊ |
248 | ␊ |
249 | @end␊ |
250 | |