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