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