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