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<GroupControllerProtocol> > 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 BootProp()., set all default values for dict␊ |
43 | + (void) loadAllValues: (NSMutableDictionary*) dict ␊ |
44 | {␊ |
45 | ␉[self doForEachGroup:LoadPreferencesOptions withOption: dict];␊ |
46 | ␉for(const BootOptionDesc* bod = BootProp::instance().firstOption();bod; bod=BootProp::instance().nextOption())␊ |
47 | ␉{␊ |
48 | ␉␉switch (bod->Type) ␊ |
49 | ␉␉{␊ |
50 | ␉␉␉case OptionYesNo:␊ |
51 | ␉␉␉␉[(NSButton*)bod->ID setIntValue: (toupper(bod->Default[0])=='Y' ? 1 : 0) ];␊ |
52 | ␉␉␉␉break;␊ |
53 | ␉␉␉case OptionKernel:␊ |
54 | ␉␉␉case OptionString:␊ |
55 | ␉␉␉␉[(NSButton*)bod->ID setIntValue: 0 ];␊ |
56 | ␉␉␉case OptionKernel1:␊ |
57 | ␉␉␉␉[(NSTextField*)bod->contentID setStringValue: ␊ |
58 | ␉␉␉␉␉[[NSString alloc] initWithUTF8String: bod->Default] ];␊ |
59 | ␉␉␉␉break;␊ |
60 | ␉␉␉case OptionUnix:␊ |
61 | ␉␉␉␉[(NSButton*)bod->ID setIntValue: 0 ];␊ |
62 | ␉␉␉␉break;␊ |
63 | ␉␉␉␉␊ |
64 | ␉␉␉default:␊ |
65 | ␉␉␉␉break;␊ |
66 | ␉␉}␊ |
67 | ␉}␊ |
68 | }␊ |
69 | ␊ |
70 | //--------------------------------------------------------------------------␊ |
71 | // get authorisation from main panel lock␊ |
72 | - (AuthorizationRef) getAuthorization␊ |
73 | {␊ |
74 | ␉AuthorizationRef auth= [[self chameleon] isUnlocked] ? ␊ |
75 | ␉[[ [self chameleon]->authView authorization] authorizationRef] : NULL;␊ |
76 | ␉return auth;␊ |
77 | }␊ |
78 | //--------------------------------------------------------------------------␊ |
79 | + (void) registerPreferencesGroup:(id) myGroup␊ |
80 | {␊ |
81 | ␉groupList.push_back(myGroup);␊ |
82 | }␊ |
83 | ␊ |
84 | //--------------------------------------------------------------------------␊ |
85 | + (void) refreshLockState: (id) item␊ |
86 | {␊ |
87 | [item setEnabled:[[ChameleonPrefPane instance] isUnlocked]];␊ |
88 | ␉␊ |
89 | }␊ |
90 | ␊ |
91 | //--------------------------------------------------------------------------␊ |
92 | + (void) refreshLockStates␊ |
93 | {␊ |
94 | ␉for (const BootOptionDesc* bod=BootProp::instance().firstOption(); ␊ |
95 | ␉␉ bod; ␊ |
96 | ␉␉ bod=BootProp::instance().nextOption())␊ |
97 | ␉{␊ |
98 | ␉␉[self refreshLockState: (id) bod->ID ];␊ |
99 | ␉␉if (bod->contentID) [self refreshLockState: (id) bod->contentID ];␊ |
100 | ␉}␊ |
101 | ␉␊ |
102 | }␊ |
103 | //--------------------------------------------------------------------------␊ |
104 | + (void) doForEachGroup: (GroupAction) action withOption:(id) option␊ |
105 | {␊ |
106 | ␉std::list<id>::iterator it;␊ |
107 | ␉for (it=groupList.begin(); it!=groupList.end(); it++)␊ |
108 | ␉{␊ |
109 | ␉␉switch (action) {␊ |
110 | ␉␉␉case SetDefaultValues:␊ |
111 | ␉␉␉␉[*it setDefaultsValues: option];␊ |
112 | ␉␉␉␉break;␊ |
113 | ␉␉␉case RefreshLockStates:␊ |
114 | ␉␉␉␉[*it refreshLockStates ];␊ |
115 | ␉␉␉␉[PreferencesControllerBase refreshLockStates];␊ |
116 | ␉␉␉␉break;␊ |
117 | ␉␉␉case LoadPreferencesOptions:␊ |
118 | ␉␉␉␉[*it loadOptionsFromPreferencesFile: option];␊ |
119 | ␉␉␉␉break;␊ |
120 | ␉␉␉case LoadBootConfigOptions:␊ |
121 | ␉␉␉␉[*it loadOptionsFromBootFile];␊ |
122 | ␉␉␉␉break;␊ |
123 | ␉␉␉case AddOptionsDesc:␊ |
124 | ␉␉␉␉[*it addOptionsDesc];␊ |
125 | ␉␉␉␉break;␊ |
126 | ␉␉␉case SaveBootConfigOptions:␊ |
127 | ␉␉␉␉break;␊ |
128 | ␉␉␉default:␊ |
129 | ␉␉␉␉break;␊ |
130 | ␉␉}␊ |
131 | ␉}␊ |
132 | }␊ |
133 | ␉␉ ␊ |
134 | //--------------------------------------------------------------------------␊ |
135 | + (void) loadOptionsFromBootFile␊ |
136 | {␊ |
137 | ␉// parse unix like command string:␊ |
138 | ␉kernelFlags.parseOptions(BootProp::instance().getStringForKey(kKernelFlags));␊ |
139 | ␉␊ |
140 | ␉for (const BootOptionDesc* bod=BootProp::instance().firstOption(); ␊ |
141 | ␉␉ bod; ␊ |
142 | ␉␉ bod=BootProp::instance().nextOption())␊ |
143 | ␉{␊ |
144 | ␉␉[PreferencesControllerBase loadOptionFromBootFile:(id)bod->ID ];␊ |
145 | ␉}␊ |
146 | }␊ |
147 | ␊ |
148 | //--------------------------------------------------------------------------␊ |
149 | ␊ |
150 | + (void) loadOptionFromBootFile:(id) optionID␊ |
151 | {␊ |
152 | ␉const BootOptionDesc* bod = BootProp::instance().findOption(optionID);␊ |
153 | ␉if (!bod) ␊ |
154 | ␉{␊ |
155 | ␉␉NSRunAlertPanel(@"Error Parsing Option",@"loadOptionFromBootFile failed",@"OK", nil, nil);␊ |
156 | ␉␉return;␊ |
157 | ␉} ␊ |
158 | ␉␊ |
159 | ␉const char * stringForKey = BootProp::instance().getStringForKey(bod->Name);␊ |
160 | ␉std::string s = stringForKey ? trim(stringForKey) : "";␊ |
161 | ␉␊ |
162 | ␉switch (bod->Type) ␊ |
163 | ␉{␊ |
164 | ␉␉case OptionYesNo:␊ |
165 | ␉␉␉if (stringForKey!=NULL) ␊ |
166 | ␉␉␉␉[(NSButton*)optionID setIntValue: (toupper(s[0])=='Y' ? 1 : 0 ) ];␊ |
167 | ␉␉␉break;␊ |
168 | ␉␉␉␊ |
169 | ␉␉case OptionKernel1:␊ |
170 | ␉␉{␊ |
171 | ␉␉␉int val = (s.length()>0 ? 1 : 0 );␊ |
172 | ␉␉␉[(NSButton*)optionID setIntValue: val ];␊ |
173 | ␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
174 | ␉␉␉ [[NSString alloc] initWithUTF8String: s.c_str()] ];␊ |
175 | ␉␉␉[(NSTextField*) bod->contentID setEnabled: val ? true : false]; ␊ |
176 | ␉␉␉[(NSTextField*) bod->contentID setEditable: val ? true : false]; ␊ |
177 | ␉␉}␊ |
178 | ␉␉␉break;␊ |
179 | ␉␉case OptionString:␊ |
180 | ␉␉{␊ |
181 | ␉␉␉int val = (s.length()>0 ? 1 : 0 );␊ |
182 | ␉␉␉[(NSButton*)optionID setIntValue: val ];␊ |
183 | ␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
184 | ␉␉␉␉[[NSString alloc] initWithUTF8String: s.c_str()] ];␊ |
185 | ␉␉␉[(NSTextField*) bod->contentID setEnabled: val ? true: false]; ␊ |
186 | ␉␉␉[(NSTextField*) bod->contentID setEditable: val ? true : false]; ␊ |
187 | ␉␉}␊ |
188 | ␉␉␉break;␊ |
189 | ␉␉␉␊ |
190 | ␉␉case OptionUnix:␊ |
191 | ␉␉case OptionKernel:␊ |
192 | ␉␉{␊ |
193 | ␉␉␉std::string s = kernelFlags.stringFromKey(bod->Name);␊ |
194 | ␉␉␉if (s.length()>0)␊ |
195 | ␉␉␉{␊ |
196 | ␉␉␉␉[(NSButton*)optionID setIntValue: 1 ];␊ |
197 | ␉␉␉␉if(bod->Type==OptionKernel)␊ |
198 | ␉␉␉␉{␊ |
199 | ␉␉␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
200 | ␉␉␉␉␉ [[NSString alloc] initWithUTF8String: ␊ |
201 | ␉␉␉␉␉␉kernelFlags.rightMember(s).c_str()] ];␊ |
202 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEnabled: true]; ␊ |
203 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEditable: true]; ␊ |
204 | ␉␉␉␉}␊ |
205 | ␊ |
206 | ␉␉␉}␊ |
207 | ␉␉␉else␊ |
208 | ␉␉␉{ // set the default for thiso option ␊ |
209 | ␉␉␉␉[(NSButton*)optionID setIntValue: (bod->Default[0] ? 1 :0) ];␊ |
210 | ␉␉␉␉if(bod->Type==OptionKernel)␊ |
211 | ␉␉␉␉{␊ |
212 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEnabled: false]; ␊ |
213 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEditable: false]; ␊ |
214 | ␉␉␉␉}␊ |
215 | ␊ |
216 | ␉␉␉}␊ |
217 | ␉␉}␊ |
218 | ␉␉␉break;␊ |
219 | ␉␉default:␊ |
220 | ␉␉␉break;␊ |
221 | ␉}␊ |
222 | ␉␊ |
223 | }␊ |
224 | ␊ |
225 | //--------------------------------------------------------------------------␊ |
226 | - (void) loadPreferences␊ |
227 | {␊ |
228 | ␉[ [ChameleonPrefPane instance] loadPreferences];␊ |
229 | }␊ |
230 | ␊ |
231 | //--------------------------------------------------------------------------␊ |
232 | - (bool) savePreferences␊ |
233 | {␊ |
234 | ␉return [ [ChameleonPrefPane instance] savePreferences: [self preferencesFile] ];␊ |
235 | ␉␊ |
236 | }␊ |
237 | //--------------------------------------------------------------------------␊ |
238 | // update the boot Config with one option change and its associated desc ␊ |
239 | - (bool) saveBootConfig: (id) sender withBootOptionDesc: (BootOptionDesc*) bod␊ |
240 | {␊ |
241 | ␉if(!bod) ␊ |
242 | ␉{␊ |
243 | ␉␉return false;␊ |
244 | ␉}␊ |
245 | ␉// load boot config file so that we don't risk to loose␊ |
246 | ␉// externally modified parameters␊ |
247 | ␉␊ |
248 | ␉int val = [(NSButton*) sender intValue ];␊ |
249 | ␉std::string sDefaultValue = trim(bod->Default ? bod->Default : "");␊ |
250 | ␉bool status = false;␊ |
251 | ␉std::string name = trim(bod->Name);␊ |
252 | ␉␊ |
253 | ␉switch (bod->Type) {␊ |
254 | ␉␉case OptionYesNo:␊ |
255 | ␉␉{␊ |
256 | ␉␉␉std::string sVal = val ? "Yes" : "No"; ␊ |
257 | ␉␉␉if (sDefaultValue.length()==0) sDefaultValue␉= "No";␊ |
258 | ␉␉␉// Avoid populating bootConfig with unnecessary options:␊ |
259 | ␉␉␉if (sVal == sDefaultValue) ␊ |
260 | ␉␉␉␉status = BootProp::instance().removeKeyAndValue(name.c_str());␊ |
261 | ␉␉␉else␊ |
262 | ␉␉␉␉status = BootProp::instance().setStringForKey(name, sVal.c_str());␊ |
263 | ␉␉}␊ |
264 | ␉␉␉break;␊ |
265 | ␉␉case OptionUnix:␊ |
266 | ␉␉␉if (!val)␉kernelFlags.removeFlag(name);␊ |
267 | ␉␉␉else␉␉kernelFlags.addFlag(name);␊ |
268 | ␉␉␉BootProp::instance().setStringForKey(kKernelFlags,kernelFlags.options());␊ |
269 | ␉␉␉status = true;␊ |
270 | ␉␉␉break;␊ |
271 | ␉␉case OptionKernel:␊ |
272 | ␉␉{␊ |
273 | ␉␉␉std::string contentValue = trim(␊ |
274 | ␉␉␉[ [(NSTextField*) bod->contentID stringValue] UTF8String ]);␊ |
275 | ␉␉␉kernelFlags.removeFlag(kernelFlags.stringFromKey(bod->Name));␊ |
276 | ␉␉␉if(val && contentValue.length()>0)␊ |
277 | ␉␉␉{␊ |
278 | ␉␉␉␉std::string concat = trim(name);␊ |
279 | ␉␉␉␉concat+= "=";␊ |
280 | ␉␉␉␉concat+= trim(contentValue);␊ |
281 | ␉␉␉␉␊ |
282 | ␉␉␉␉kernelFlags.addFlag(concat);␊ |
283 | ␉␉␉}␊ |
284 | ␉␉␉BootProp::instance().setStringForKey(kKernelFlags,kernelFlags.options());␊ |
285 | ␉␉␉status = true;␊ |
286 | ␉␉}␊ |
287 | ␉␉␉break;␊ |
288 | ␉␉case OptionKernel1:␊ |
289 | ␉␉case OptionString:␊ |
290 | ␉␉␉// Avoid populating bootConfig with unnecessary options:␊ |
291 | ␉␉␉if (val == 0 && bod->Type!=OptionKernel1) ␊ |
292 | ␉␉␉␉status = BootProp::instance().removeKeyAndValue(bod->Name);␊ |
293 | ␉␉␉else␊ |
294 | ␉␉␉{␊ |
295 | ␉␉␉␉std::string contentValue =␊ |
296 | ␉␉␉␉␉[ [(NSTextField*) bod->contentID stringValue] UTF8String ];␊ |
297 | ␉␉␉␉if (contentValue.length()>0)␊ |
298 | ␉␉␉␉␉status = BootProp::instance().setStringForKey(bod->Name, contentValue.c_str());␊ |
299 | ␉␉␉␉else {␊ |
300 | ␉␉␉␉␉return false; // no content to save so don't save it␊ |
301 | ␉␉␉␉}␊ |
302 | ␊ |
303 | ␉␉␉}␊ |
304 | ␉␉␉break;␊ |
305 | ␉␉default:␊ |
306 | ␉␉␉break;␊ |
307 | ␉}␊ |
308 | ␉␊ |
309 | ␉// Now save the bootConfig␊ |
310 | ␉AuthorizationRef auth = [self getAuthorization ];␊ |
311 | ␉if (status)␉status = BootProp::instance().save(auth);␊ |
312 | ␊ |
313 | ␉return status;␊ |
314 | }␊ |
315 | //--------------------------------------------------------------------------␊ |
316 | -(NSMutableDictionary*) preferencesFile␊ |
317 | {␉␊ |
318 | ␉return [[ChameleonPrefPane instance] preferencesFile];␊ |
319 | }␊ |
320 | ␊ |
321 | //--------------------------------------------------------------------------␊ |
322 | -(NSMutableDictionary*) preferencesParts ␊ |
323 | { ␊ |
324 | ␉return [[ChameleonPrefPane instance] preferencesParts];␊ |
325 | }␊ |
326 | ␊ |
327 | //--------------------------------------------------------------------------␊ |
328 | - (bool) handleSender: (id) sender␊ |
329 | {␊ |
330 | ␊ |
331 | ␉const BootOptionDesc * bod = BootProp::instance().findOption(sender);␊ |
332 | ␉␊ |
333 | ␉if (!bod) {␊ |
334 | ␉␉bod = BootProp::instance().findOptionContent(sender);␊ |
335 | ␉␉NSTextField* textField = (NSTextField*) sender;␊ |
336 | ␉␉std::string content = [[textField stringValue] UTF8String ];␊ |
337 | ␉␉if(bod->ID!=nil) sender = (id) bod->ID;␊ |
338 | ␉}␊ |
339 | ␉else␊ |
340 | ␉{␊ |
341 | ␉␉␊ |
342 | ␉␉int state = [sender intValue];␊ |
343 | ␉␊ |
344 | ␉␉switch (bod->Type) {␊ |
345 | ␉␉␉case OptionKernel:␊ |
346 | ␉␉␉case OptionKernel1:␊ |
347 | ␉␉␉case OptionString:␊ |
348 | ␉␉␉[(NSTextField*) bod->contentID setEnabled: state ? true : false];␊ |
349 | ␉␉␉[(NSTextField*) bod->contentID setEditable: state ? true : false];␊ |
350 | ␉␉␉␉break;␊ |
351 | ␉␉␉default:␊ |
352 | ␉␉␉␉break;␊ |
353 | ␉␉}␊ |
354 | ␉}␉␊ |
355 | ␉if(![self saveBootConfig: sender withBootOptionDesc: (BootOptionDesc*) bod] && !bod->contentID )␊ |
356 | ␉{ // Couldn't save, so warn user ...␊ |
357 | ␉␉NSRunAlertPanel(@"Error saving bootConfig", @"Could not save com.apple.Boot.plist",␊ |
358 | ␉␉␉␉␉␉␉@"OK", nil, nil);␊ |
359 | ␉}␊ |
360 | ␉return true;␊ |
361 | }␊ |
362 | ␊ |
363 | @end␊ |
364 | |