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