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