1 | //␊ |
2 | // StartupPrefPanePref.mm␊ |
3 | //␊ |
4 | // Created by Rekursor on 1/16/10.␊ |
5 | //␊ |
6 | ␊ |
7 | #import "ChameleonPrefPane.h"␊ |
8 | #import "BootSetupController.h"␊ |
9 | #import "BootFlagsController.h"␊ |
10 | #import "PeripheralsController.h"␊ |
11 | #import "AdvancedSetupController.h"␊ |
12 | ␊ |
13 | #include "ShellProcess.h"␊ |
14 | #import "ChameleonPropertyList.h"␊ |
15 | #include <string>␊ |
16 | ␊ |
17 | //--------------------------------------------------------------------------␊ |
18 | // Constants␊ |
19 | //--------------------------------------------------------------------------␊ |
20 | static const char * const szBootPaths[]= {␊ |
21 | ␉"/",␊ |
22 | ␉"/Extra/",␊ |
23 | ␉"/Volumes/EFI/Extra/",␊ |
24 | ␉"/Volumes/Cham/Extra/",␊ |
25 | ␉"/Volumes/BootLoaders/Extra/",␊ |
26 | ␉"/Volumes/RX0/Extra/",␊ |
27 | ␉"/Library/Preferences/SystemConfiguration/",␊ |
28 | ␉NULL␊ |
29 | };␊ |
30 | ␊ |
31 | static const char* const szPropFileName = "com.apple.Boot.plist";␊ |
32 | ␊ |
33 | static const int CurrentPreferencesFileVersion = 0x02; // for future back compatibility␊ |
34 | //--------------------------------------------------------------------------␊ |
35 | // Static file variables␊ |
36 | //--------------------------------------------------------------------------␊ |
37 | static std::string sCurrentDefaultPartition;␊ |
38 | ␊ |
39 | PartitionExtractor * partExtractor=NULL;␊ |
40 | ␊ |
41 | static int currentRowSel = -1;␊ |
42 | static ChameleonPrefPane *gInstance = NULL;␊ |
43 | ␊ |
44 | //--------------------------------------------------------------------------␊ |
45 | ␊ |
46 | @implementation ChameleonPrefPane␊ |
47 | ␊ |
48 | + (ChameleonPrefPane *)instance { return(gInstance);}␊ |
49 | ␊ |
50 | //--------------------------------------------------------------------------␊ |
51 | - (id) init␊ |
52 | {␊ |
53 | ␉self = [super init];␊ |
54 | ␉gInstance = self;␊ |
55 | ␉␊ |
56 | ␉// set the image to display the current file being played␊ |
57 | ␉mMacOSXImage = [self getImageResource: @"MacOSX" ofType: @"png"];␊ |
58 | ␉mWindowsImage = [self getImageResource: @"Windows" ofType: @"png"];␊ |
59 | ␉mLinuxImage = [self getImageResource: @"Linux" ofType: @"png"];␊ |
60 | ␉mCDROMImage = [self getImageResource: @"CDROM" ofType: @"png"];␊ |
61 | ␉mUnknownImage = [self getImageResource: @"Chameleon" ofType: @"tiff"];␊ |
62 | ␉␊ |
63 | ␉␊ |
64 | // create the process that will extract the diskutil list infos␊ |
65 | ␉if(!partExtractor) partExtractor = new PartitionExtractor();␊ |
66 | ␉␊ |
67 | ␉// Retrieve the com.chameleon.prefPane.plist config␊ |
68 | ␉return self;␊ |
69 | }␊ |
70 | - (void)dealloc␊ |
71 | {␊ |
72 | ␉// release the colors␊ |
73 | ␉[mOptionsDict release];␊ |
74 | ␉[mPartitionsDict release];␊ |
75 | ␉␊ |
76 | ␉[super dealloc];␊ |
77 | }␊ |
78 | ␊ |
79 | - (NSMutableDictionary*) preferencesFile {␉return mOptionsDict; }␊ |
80 | - (NSMutableDictionary*) preferencesParts {␉return mPartitionsDict; }␊ |
81 | - (NSTableView*) partitionsTable { return mPartitionsTable;}␊ |
82 | ␊ |
83 | //--------------------------------------------------------------------------␊ |
84 | -(bool) savePreferences: (NSDictionary*) dict␊ |
85 | {␊ |
86 | ␉std::string sPath = [kPreferencesFilePath UTF8String];␊ |
87 | ␉␊ |
88 | ␉if(dict==nil || sPath.length()==0) return false;␊ |
89 | ␉␊ |
90 | ␉AuthorizationRef auth = [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
91 | ␉␊ |
92 | ␉PropertyList::chmodFile(sPath.c_str(), "0777", auth);␊ |
93 | ␉[dict writeToFile:kPreferencesFilePath atomically:YES];␊ |
94 | ␉PropertyList::chmodFile(sPath.c_str(), "0644", auth);␊ |
95 | ␉return true;␊ |
96 | }␊ |
97 | ␊ |
98 | //--------------------------------------------------------------------------␊ |
99 | // SFAuthorization implementation␊ |
100 | //--------------------------------------------------------------------------␊ |
101 | ␊ |
102 | // SFAuthorization delegates␊ |
103 | - (void)authorizationViewDidAuthorize:(SFAuthorizationView *)view {␊ |
104 | [self selectDefaultPartition];␊ |
105 | ␉[self refreshLockStates];␊ |
106 | ␊ |
107 | } ␊ |
108 | ␊ |
109 | //--------------------------------------------------------------------------␊ |
110 | - (void)authorizationViewDidDeauthorize:(SFAuthorizationView *)view {␊ |
111 | [self refreshLockStates];␊ |
112 | }␊ |
113 | ␊ |
114 | //--------------------------------------------------------------------------␊ |
115 | // Setup security for changing boot options␊ |
116 | -(void) initAuthorization␊ |
117 | {␊ |
118 | AuthorizationItem items = {kAuthorizationRightExecute, 0, NULL, 0};␊ |
119 | AuthorizationRights rights = {1, &items};␊ |
120 | ␊ |
121 | ␉[authView setAuthorizationRights:&rights];␊ |
122 | authView.delegate = self;␊ |
123 | [authView updateStatus:nil];␊ |
124 | }␊ |
125 | //--------------------------------------------------------------------------␊ |
126 | - (␉AuthorizationRef) auth␊ |
127 | {␊ |
128 | ␉return [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
129 | ␉␊ |
130 | }␊ |
131 | ␊ |
132 | //--------------------------------------------------------------------------␊ |
133 | - (void) refreshLockStates␊ |
134 | {␊ |
135 | [mPartitionsTable setEnabled:[self isUnlocked]];␊ |
136 | [mStatusText setEnabled:[self isUnlocked]];␊ |
137 | ␉␊ |
138 | ␉// Refresh other panels␊ |
139 | ␉[PreferencesControllerBase doForEachGroup: RefreshLockStates withOption: nil];␊ |
140 | }␊ |
141 | ␊ |
142 | ␊ |
143 | //--------------------------------------------------------------------------␊ |
144 | - (bool)isUnlocked ␊ |
145 | {␊ |
146 | return [authView authorizationState] == SFAuthorizationViewUnlockedState;␊ |
147 | }␊ |
148 | ␊ |
149 | //--------------------------------------------------------------------------␊ |
150 | - (id) getImageResource: (NSString *) str ofType: (NSString*) sType␊ |
151 | {␊ |
152 | ␉NSImage * img=nil;␊ |
153 | ␉if(!str) return nil;␊ |
154 | ␉NSBundle * b = [NSBundle bundleForClass:[self class]];␊ |
155 | ␉NSString* sRes = [b pathForResource: str ofType:sType ];␊ |
156 | ␉img = [[NSImage alloc] initWithContentsOfFile: sRes];␊ |
157 | ␉return img;␊ |
158 | }␊ |
159 | ␊ |
160 | ␊ |
161 | //--------------------------------------------------------------------------␊ |
162 | /** When called here, all outlets references are initialized */␊ |
163 | - (void)awakeFromNib␊ |
164 | { // called more than once, we only need one resource init␊ |
165 | ␉static bool ft=true;␊ |
166 | ␉if(ft)␊ |
167 | ␉{␊ |
168 | ␉␉ft=false;␊ |
169 | ␉␉[self␉loadPreferences];␊ |
170 | ␉␉[self initBootConfig];␊ |
171 | ␉}␊ |
172 | }␊ |
173 | ␊ |
174 | //--------------------------------------------------------------------------␊ |
175 | - (void) loadPreferences␊ |
176 | {␊ |
177 | //␉PrefsProp::instance().open([kPreferencesFilePath UTF8String], CreateIfNotExist,␊ |
178 | //␉␉␉␉␉␉␉␉␉␉␉ [self auth]);␊ |
179 | ␉␊ |
180 | ␉// test with preferences file already created and when no prefs exists␊ |
181 | ␉␊ |
182 | ␉id oldGlobalPreferences = [ [NSDictionary dictionaryWithContentsOfFile: ␊ |
183 | ␉␉␉␉␉␉␉␉ kPreferencesFilePath ] retain];␊ |
184 | ␉␊ |
185 | ␉mPartitionsDict = [[NSMutableDictionary alloc] init];␊ |
186 | ␉[mPartitionsDict retain];␊ |
187 | ␉␊ |
188 | ␉// Initialize bootConfig desc dict␊ |
189 | ␉[PreferencesControllerBase doForEachGroup: AddOptionsDesc withOption: nil];␊ |
190 | ␉␊ |
191 | ␉if (oldGlobalPreferences!=nil)␊ |
192 | ␉{␊ |
193 | ␉␉mPreferenceFileVersion= [[oldGlobalPreferences objectForKey: keyPreferencesFileVersion] intValue ];␊ |
194 | ␉␉[PreferencesControllerBase doForEachGroup: LoadPreferencesOptions withOption: oldGlobalPreferences];␊ |
195 | ␉␉[mPartitionsDict addEntriesFromDictionary: [oldGlobalPreferences objectForKey: keyPartitionsList] ];␊ |
196 | ␉}␊ |
197 | ␉else ␊ |
198 | ␉{ // Create a preference plist file with Defaults values␊ |
199 | ␉␉oldGlobalPreferences = [[NSMutableDictionary alloc] init];␊ |
200 | ␉␉[PreferencesControllerBase doForEachGroup: SetDefaultValues withOption: oldGlobalPreferences];␊ |
201 | ␉␉␊ |
202 | ␉␉// Initialize defaults␊ |
203 | ␉␉[oldGlobalPreferences setObject: [[NSNumber alloc] initWithInt: CurrentPreferencesFileVersion] ␊ |
204 | ␉␉␉␉␉␉␉␉ forKey: keyPreferencesFileVersion];␊ |
205 | ␉␉[oldGlobalPreferences setObject: mPartitionsDict forKey: keyPartitionsList];␊ |
206 | ␉␉␊ |
207 | ␉␉// Save the preferences file␊ |
208 | ␉␉[ self savePreferences:oldGlobalPreferences ];␊ |
209 | ␉}␊ |
210 | ␉␊ |
211 | ␉mOptionsDict = [[NSMutableDictionary alloc] init];␊ |
212 | ␉[mOptionsDict addEntriesFromDictionary:oldGlobalPreferences];␊ |
213 | ␉if (mPartitionsDict!=nil) [mPartitionsDict retain];␊ |
214 | ␉[oldGlobalPreferences release];␊ |
215 | }␊ |
216 | ␊ |
217 | //--------------------------------------------------------------------------␊ |
218 | - (void) initBootConfig␊ |
219 | {␊ |
220 | ␉static bool ft=true;␊ |
221 | ␊ |
222 | ␉[self initAuthorization];␊ |
223 | ␉␊ |
224 | ␉if (!BootProp::instance().isValid())␊ |
225 | ␉{␊ |
226 | ␉␉std::string sPath;␊ |
227 | ␉␉CFStringRef errorString=NULL;␊ |
228 | ␉␉bool cont =true;␊ |
229 | ␉␉␊ |
230 | ␉␉id sForcedPath = [mOptionsDict valueForKey: keyForceBootConfigPath];␊ |
231 | ␉␉const char * szForcedPath = sForcedPath!=nil ? [sForcedPath UTF8String] : NULL;␊ |
232 | ␉␉if (szForcedPath && *szForcedPath)␊ |
233 | ␉␉{␊ |
234 | ␉␉␉cont = !BootProp::instance().open(szForcedPath, [self auth]);␊ |
235 | ␉␉}␊ |
236 | ␉␉else {␊ |
237 | ␉␉␉for(int i=0; szBootPaths[i] && cont; i++)␊ |
238 | ␉␉␉{␊ |
239 | ␉␉␉␉sPath = szBootPaths[i];␊ |
240 | ␉␉␉␉sPath += szPropFileName;␊ |
241 | ␉␉␉␉cont = !BootProp::instance().open(sPath.c_str(), [self auth]);␊ |
242 | ␉␉␉}␊ |
243 | ␉␉}␊ |
244 | ␉␉if (cont)␊ |
245 | ␉␉{␊ |
246 | ␉␉␉if(!ft) return;␊ |
247 | ␉␉␉ft=false;␊ |
248 | ␉␉␉[mStatusText setTextColor: [NSColor redColor] ];␊ |
249 | ␉␉␉if (errorString)␊ |
250 | ␉␉␉␉[mStatusText setStringValue:[NSString stringWithFormat: @"Error while parsing com.apple.Boot.plist : %@",␊ |
251 | ␉␉␉␉␉␉␉␉␉␉␉ errorString] ];␊ |
252 | ␉␉␉else␊ |
253 | ␉␉␉␉[mStatusText setStringValue: @"Error while searching for com.apple.Boot.plist"];␊ |
254 | ␉␉␉␊ |
255 | ␉␉}␊ |
256 | ␉␉else␊ |
257 | ␉␉{ ␊ |
258 | ␉␉␉[mStatusText setTextColor: [NSColor grayColor] ];␊ |
259 | ␉␉␉NSString* ns = [ [NSString alloc] initWithUTF8String:BootProp::instance().propFilePath() ];␊ |
260 | ␉␉␉[mStatusText setStringValue: [NSString stringWithFormat: @"bootConfig: %@", ns] ];␊ |
261 | ␉␉␉[[BootSetupController instance]->mBootConfigPathText setStringValue: ␊ |
262 | ␉␉␉␉[[NSString alloc] initWithUTF8String: BootProp::instance().propFilePath()] ];␊ |
263 | ␊ |
264 | ␉␉}␊ |
265 | ␉␉␊ |
266 | ␉␉if (BootProp::instance().isValid())␊ |
267 | ␉␉{␊ |
268 | ␉␉␉// read options in the plist file␊ |
269 | ␉␉␉␊ |
270 | ␉␉␉partExtractor->hidePartitions(BootProp::instance().getStringForKey(kHidePartition));␊ |
271 | ␉␉␉partExtractor->renamedPartitions(BootProp::instance().getStringForKey(kRenamePartition));␊ |
272 | ␉␉␉␊ |
273 | ␉␉␉// partExtractor->resetSwapping();␊ |
274 | ␉␉␉id val = [mOptionsDict valueForKey: keySwapHD01];␊ |
275 | ␉␉␉[[BootSetupController instance]->mSwapHD01 setIntValue: [val intValue] ];␊ |
276 | ␉␉␉[[BootSetupController instance] doSwapHD: [val boolValue] save: false src:0 dst:1];␊ |
277 | ␉␉␉␊ |
278 | ␉␉␉val = [mOptionsDict valueForKey: keySwapHD02];␊ |
279 | ␉␉␉[[BootSetupController instance]->mSwapHD02 setIntValue: [val intValue] ];␊ |
280 | ␉␉␉[[BootSetupController instance] doSwapHD: [val boolValue] save: false src:0 dst:2];␊ |
281 | ␊ |
282 | ␉␉␉// Load all boot Options into the interface components␊ |
283 | ␉␉␉[PreferencesControllerBase loadOptionsFromBootFile];␊ |
284 | ␉␉␉[self selectDefaultPartition];␊ |
285 | ␉␉␉␉ ␊ |
286 | ␉␉}␊ |
287 | ␉␉␊ |
288 | ␉}␉␊ |
289 | }␊ |
290 | //--------------------------------------------------------------------------␊ |
291 | - (void) selectDefaultPartition␊ |
292 | {␊ |
293 | if(!authView) return;␊ |
294 | ␉␊ |
295 | ␉[self refreshLockStates];␊ |
296 | ␉␊ |
297 | ␉// try to get the current default partition if any␊ |
298 | ␉if(partExtractor && BootProp::instance().isValid())␊ |
299 | ␉{␊ |
300 | ␉␉const char *sdp = BootProp::instance().getStringForKey(kDefaultPartition);␊ |
301 | ␉␉sCurrentDefaultPartition = sdp ? sdp : ""; ␊ |
302 | ␉␉if (sCurrentDefaultPartition.size())␊ |
303 | ␉␉{␊ |
304 | ␉␉␉int index = partExtractor->getIndexFromHdStringSpec(sCurrentDefaultPartition.c_str());␊ |
305 | ␉␉␉if (index>=0)␊ |
306 | ␉␉␉{␊ |
307 | ␉␉␉␉[mPartitionsTable selectRowIndexes:␊ |
308 | ␉␉␉␉ [NSIndexSet indexSetWithIndex:index] byExtendingSelection:NO];␊ |
309 | ␉␉␉␉currentRowSel = index;␊ |
310 | ␉␉␉}␊ |
311 | ␉␉}␊ |
312 | ␉}␊ |
313 | ␉␊ |
314 | }␊ |
315 | ␊ |
316 | //--------------------------------------------------------------------------␊ |
317 | // following DieBuch recommendation : using applescript and system events (thanks!):␊ |
318 | - (IBAction)onRestart: (id)sender␊ |
319 | {␊ |
320 | ␉NSInteger n = NSRunAlertPanel(@"Restarting OS X", ␊ |
321 | ␉␉␉␉␉␉␉␉ @"Are you sure you want to restart your computer now ?",␊ |
322 | ␉␉␉␉␉␉␉␉ @"OK", @"Cancel", nil);␊ |
323 | ␉if (n==1)␊ |
324 | ␉{␊ |
325 | ␉␉AuthorizationRef auth = [[authView authorization] authorizationRef];␊ |
326 | ␉␉executePrivilegedCmd(auth,"/usr/bin/osascript","-e 'tell app \"System Events\" to restart'");␊ |
327 | ␉}␊ |
328 | ␉␊ |
329 | }␊ |
330 | //--------------------------------------------------------------------------␊ |
331 | - (IBAction)onShutdown: (id)sender␊ |
332 | {␊ |
333 | ␉NSInteger n = NSRunAlertPanel(@"Shutting Down OS X", ␊ |
334 | ␉␉␉␉␉␉␉␉ @"Are you sure you want to shut down your computer now ?",␊ |
335 | ␉␉␉␉␉␉␉␉ @"OK", @"Cancel", /*ThirdButtonHere:*/nil␊ |
336 | ␉␉␉␉␉␉␉␉ /*, args for a printf-style msg go here */);␊ |
337 | ␉if (n==1)␊ |
338 | ␉{␊ |
339 | ␉␉system("/usr/bin/osascript -e 'tell app \"System Events\" to shut down'");␊ |
340 | ␉}␊ |
341 | }␊ |
342 | ␊ |
343 | - (IBAction)onSleep: (id)sender␊ |
344 | {␊ |
345 | ␉system("/usr/bin/osascript -e 'tell app \"System Events\" to sleep'");␊ |
346 | }␊ |
347 | ␊ |
348 | //--------------------------------------------------------------------------␊ |
349 | - (void)tableViewSelectionDidChange:(NSNotification *)notification {␊ |
350 | ␊ |
351 | ␉NSTableView* tv= mPartitionsTable;␊ |
352 | ␉if ([tv selectedRow] != currentRowSel)␊ |
353 | ␉{␊ |
354 | ␉␉currentRowSel = [tv selectedRow];␊ |
355 | ␉␉if (currentRowSel>=0)␊ |
356 | ␉␉{␊ |
357 | ␉␉␉const std::vector<PartitionInfo>& partInfo = partExtractor->partList();␊ |
358 | ␉␉␉char hd[7+1]="hd(n,m)";␊ |
359 | ␉␉␉hd[3]= ('0'+partInfo[currentRowSel].disk());␊ |
360 | ␉␉␉hd[5]= ('0'+partInfo[currentRowSel].partition());␉␊ |
361 | ␉␉␉AuthorizationRef auth= [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
362 | ␉␉␉if(BootProp::instance().setStringForKey(kDefaultPartition, hd))␊ |
363 | ␉␉␉ BootProp::instance().save(auth);␊ |
364 | ␉␉␉[ PreferencesControllerBase loadOptionsFromBootFile];␊ |
365 | ␉␉}␊ |
366 | ␉␉else␊ |
367 | ␉␉{ // no line selected␊ |
368 | ␉␉␉BootProp::instance().removeKeyAndValue(kDefaultPartition);␊ |
369 | ␉␉␉[ PreferencesControllerBase loadOptionsFromBootFile];␊ |
370 | ␉␉␉␊ |
371 | ␉␉}␊ |
372 | ␉}␊ |
373 | ␉␉␊ |
374 | }␊ |
375 | ␊ |
376 | //--------------------------------------------------------------------------␊ |
377 | - (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView␊ |
378 | {␊ |
379 | ␉int size = partExtractor ? partExtractor->partList().size() : 0;␊ |
380 | ␉return size;␊ |
381 | }␊ |
382 | ␊ |
383 | //--------------------------------------------------------------------------␊ |
384 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row␊ |
385 | {␊ |
386 | ␉id ret=nil;␊ |
387 | ␉␊ |
388 | ␉const std::vector<PartitionInfo>& partInfo = partExtractor->partList();␊ |
389 | ␊ |
390 | ␉if (tableColumn == mPartitionImgColumn) ␊ |
391 | ␉{␊ |
392 | ␉␉switch(partInfo[row].imageIndexFromFs())␊ |
393 | ␉␉{␊ |
394 | ␉␉␉case 0:␊ |
395 | ␉␉␉␉ret = mMacOSXImage;␊ |
396 | ␉␉␉␉break;␊ |
397 | ␉␉␉case 1:␊ |
398 | ␉␉␉␉ret = mWindowsImage;␊ |
399 | ␉␉␉␉break;␊ |
400 | ␉␉␉case 2:␊ |
401 | ␉␉␉␉ret = mLinuxImage;␊ |
402 | ␉␉␉␉break;␊ |
403 | ␉␉␉default:␊ |
404 | ␉␉␉␉ret = mUnknownImage;␊ |
405 | ␉␉␉␉break;␊ |
406 | ␉␉␉␉␊ |
407 | ␉␉}␊ |
408 | ␉}␊ |
409 | ␉else if (tableColumn == mFileSystemColumn) ␊ |
410 | ␉{␊ |
411 | ␉␉ret = [NSString stringWithFormat: @"%s", partInfo[row].cfsType() ];␊ |
412 | ␉}␊ |
413 | ␉else if (tableColumn == mPartitionNameColumn) ␊ |
414 | ␉{␊ |
415 | ␉␉ret = [NSString stringWithFormat: @"%s", partInfo[row].clabel()␉];␊ |
416 | ␉}␊ |
417 | ␉else if (tableColumn == mPartitionIDColumn) ␊ |
418 | ␉{␊ |
419 | ␉␉ret= [NSString stringWithFormat: @"hd(%d,%d)", ␊ |
420 | ␉␉␉␉partInfo[row].disk(),␊ |
421 | ␉␉␉␉partInfo[row].partition()␊ |
422 | ␉␉␉␉];␊ |
423 | ␉} ␊ |
424 | ␉␊ |
425 | ␉return ret;␊ |
426 | }␊ |
427 | //--------------------------------------------------------------------------␊ |
428 | ␊ |
429 | @end␊ |
430 | |