Root/
Source at commit 8 created 14 years 2 months ago. By rekursor, Excluded restart button from Auth. scheme, like in original startup panel. | |
---|---|
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 | //--------------------------------------------------------------------------␊ |
84 | - (id) init␊ |
85 | {␊ |
86 | ␉self = [super init];␊ |
87 | ␊ |
88 | ␉// set the image to display the current file being played␊ |
89 | ␉mMacOSXImage = [self getImageResource: @"MacOSX" ofType: @"tiff"];␊ |
90 | ␉mWindowsImage = [self getImageResource: @"Windows" ofType: @"tiff"];␊ |
91 | ␉mLinuxImage = [self getImageResource: @"Linux" ofType: @"tiff"];␊ |
92 | ␉mUnknownImage = [self getImageResource: @"Unknown" ofType: @"tiff"];␊ |
93 | ␉␊ |
94 | ␉␊ |
95 | ␉// Retrieve the Info.plist file info␊ |
96 | ␉NSBundle * b = [NSBundle bundleForClass:[self class]];␊ |
97 | ␉// Careful here : we must create copies of the path and dict for later use␊ |
98 | ␉// other wise it would be released...␊ |
99 | ␉mOptionsPlistPath = [[NSString alloc] initWithString: [b pathForResource: @"Data" ofType:@"plist"]];␊ |
100 | ␉NSMutableDictionary * d = [NSMutableDictionary dictionaryWithContentsOfFile: mOptionsPlistPath];␊ |
101 | ␉mOptionsDict = [[NSMutableDictionary alloc] initWithCapacity:64 ];␊ |
102 | ␉[mOptionsDict addEntriesFromDictionary: d];␊ |
103 | ␉␊ |
104 | ␉if (!mOptionsDict) NSLog(@"Error reading plist: %@", mOptionsPlistPath);␊ |
105 | ␉return self;␊ |
106 | }␊ |
107 | ␊ |
108 | //--------------------------------------------------------------------------␊ |
109 | /** When called here, all outlets references are initialized */␊ |
110 | - (void)awakeFromNib␊ |
111 | { // called more than once, we only need one resource init␊ |
112 | ␉static bool ft=true;␊ |
113 | ␉if(ft)␊ |
114 | ␉{␊ |
115 | ␉␉ft=false;␊ |
116 | ␉␉[self initBootConfig];␊ |
117 | ␉}␊ |
118 | }␊ |
119 | ␊ |
120 | //--------------------------------------------------------------------------␊ |
121 | - (void) initBootConfig␊ |
122 | {␊ |
123 | ␉static bool ft=true;␊ |
124 | ␉␊ |
125 | ␉// Cosmetics setup␊ |
126 | ␉// Setup security for changing boot options␊ |
127 | AuthorizationItem items = {kAuthorizationRightExecute, 0, NULL, 0};␊ |
128 | AuthorizationRights rights = {1, &items};␊ |
129 | ␊ |
130 | ␉[authView setAuthorizationRights:&rights];␊ |
131 | authView.delegate = self;␊ |
132 | [authView updateStatus:nil];␊ |
133 | ␊ |
134 | ␉// create the propertylist object that will handle com.apple.Boot.plist␊ |
135 | ␉if(!prop) prop = new PropertyList();␊ |
136 | ␊ |
137 | ␉// create the process that will extract the diskutil list infos␊ |
138 | ␉if(!partExtractor) partExtractor = new PartitionExtractor();␊ |
139 | ␉␊ |
140 | ␉if (!prop->isValid())␊ |
141 | ␉{␊ |
142 | ␉␉std::string sPath;␊ |
143 | ␉␉AuthorizationRef auth = [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
144 | ␉␉CFStringRef errorString=NULL;␊ |
145 | ␉␉bool cont =true;␊ |
146 | ␉␉␊ |
147 | ␉␉id sForcedPath = [mOptionsDict valueForKey: @"forceBootConfigPath"];␊ |
148 | ␉␉const char * szForcedPath = sForcedPath!=nil ? [sForcedPath UTF8String] : NULL;␊ |
149 | ␉␉if (szForcedPath && *szForcedPath)␊ |
150 | ␉␉{␊ |
151 | ␉␉␉cont = !prop->open(szForcedPath, &errorString, auth);␊ |
152 | ␉␉}␊ |
153 | ␉␉else {␊ |
154 | ␉␉␉for(int i=0; szBootPaths[i] && cont; i++)␊ |
155 | ␉␉␉{␊ |
156 | ␉␉␉␉sPath = szBootPaths[i];␊ |
157 | ␉␉␉␉sPath += szPropFileName;␊ |
158 | ␉␉␉␉cont = !prop->open(sPath.c_str(), &errorString, auth);␊ |
159 | ␉␉␉}␊ |
160 | ␉␉}␊ |
161 | ␉␉if (cont)␊ |
162 | ␉␉{␊ |
163 | ␉␉␉if(!ft) return;␊ |
164 | ␉␉␉ft=false;␊ |
165 | ␉␉␉[mStatusText setTextColor: [NSColor redColor] ];␊ |
166 | ␉␉␉if (errorString)␊ |
167 | ␉␉␉␉[mStatusText setStringValue:[NSString stringWithFormat: @"Error while parsing com.apple.Boot.plist : %@",␊ |
168 | ␉␉␉␉␉␉␉␉␉␉␉ errorString] ];␊ |
169 | ␉␉␉else␊ |
170 | ␉␉␉␉[mStatusText setStringValue: @"Error while searching for com.apple.Boot.plist"];␊ |
171 | ␉␉␉␊ |
172 | ␉␉}␊ |
173 | ␉␉else␊ |
174 | ␉␉{ ␊ |
175 | ␉␉␉[mStatusText setTextColor: [NSColor grayColor] ];␊ |
176 | ␉␉␉NSString* ns = [ [NSString alloc] initWithUTF8String:prop->bootConfigPath() ];␊ |
177 | ␉␉␉[mStatusText setStringValue: [NSString stringWithFormat: @"bootConfig: %@", ns] ];␊ |
178 | ␉␉}␊ |
179 | ␉␉if (prop->isValid())␊ |
180 | ␉␉{␊ |
181 | ␉␉␉// read options in the plist file␊ |
182 | ␉␉␉␊ |
183 | ␉␉␉partExtractor->hidePartitions(prop->getStringForKey(kHidePartition));␊ |
184 | ␉␉␉partExtractor->renamedPartitions(prop->getStringForKey(kRenamePartition));␊ |
185 | ␉␉␉␊ |
186 | ␉␉␉// partExtractor->resetSwapping();␊ |
187 | ␉␉␉id val = [mOptionsDict valueForKey:@"swapHD01"];␊ |
188 | ␉␉␉[mSwapHD01 setIntValue: [val intValue] ];␊ |
189 | ␉␉␉[self doSwapHD: [val boolValue] save: false src:0 dst:1];␊ |
190 | ␉␉␉␊ |
191 | ␉␉␉val = [mOptionsDict valueForKey:@"swapHD02"];␊ |
192 | ␉␉␉[mSwapHD02 setIntValue: [val intValue] ];␊ |
193 | ␉␉␉[self doSwapHD: [val boolValue] save: false src:0 dst:2];␊ |
194 | ␉␉␉␊ |
195 | ␉␉␉[self selectDefaultPartition];␊ |
196 | ␉␉}␊ |
197 | ␉␉␊ |
198 | ␉}␉␊ |
199 | }␊ |
200 | //--------------------------------------------------------------------------␊ |
201 | - (void) selectDefaultPartition␊ |
202 | {␊ |
203 | if(!authView) return;␊ |
204 | ␉␊ |
205 | ␉[self refreshLockStates];␊ |
206 | ␉␊ |
207 | ␉// try to get the current default partition if any␊ |
208 | ␉if(partExtractor && prop && prop->isValid())␊ |
209 | ␉{␊ |
210 | ␉␉const char *sdp = prop->getStringForKey(kDefaultPartition);␊ |
211 | ␉␉sCurrentDefaultPartition = sdp ? sdp : ""; ␊ |
212 | ␉␉if (sCurrentDefaultPartition.size())␊ |
213 | ␉␉{␊ |
214 | ␉␉␉int index = partExtractor->getIndexFromHdStringSpec(sCurrentDefaultPartition.c_str());␊ |
215 | ␉␉␉if (index>=0)␊ |
216 | ␉␉␉{␊ |
217 | ␉␉␉␉[mPartitionsTable selectRowIndexes:␊ |
218 | ␉␉␉␉ [NSIndexSet indexSetWithIndex:index] byExtendingSelection:NO];␊ |
219 | ␉␉␉␉currentRowSel = index;␊ |
220 | ␉␉␉}␊ |
221 | ␉␉}␊ |
222 | ␉}␊ |
223 | ␉␊ |
224 | }␊ |
225 | ␊ |
226 | //--------------------------------------------------------------------------␊ |
227 | - (void) swapDisks: (bool) bSwap src: (int) iSrc dst: (int) iDst;␊ |
228 | {␊ |
229 | ␉if(!partExtractor || !prop || !prop->isValid()) return;␊ |
230 | ␉if (bSwap)␊ |
231 | ␉{␊ |
232 | ␉␉partExtractor->swapHD(iSrc, iDst);␉␊ |
233 | ␉}␊ |
234 | ␉partExtractor->extractPartitions();␊ |
235 | ␉[ self selectDefaultPartition];␊ |
236 | ␊ |
237 | }␊ |
238 | ␊ |
239 | //--------------------------------------------------------------------------␊ |
240 | - (void) doSwapHD: (int) val save: (bool) doSave src: (int) isrc dst: (int) idst␊ |
241 | {␊ |
242 | ␉␊ |
243 | ␉if( val>0) //on␊ |
244 | ␉{␊ |
245 | ␉␉[self swapDisks: true src:isrc dst:idst ];␊ |
246 | ␉}␊ |
247 | ␉else␊ |
248 | ␉{␊ |
249 | ␉␉[self swapDisks: false src:isrc dst:idst ];␊ |
250 | ␉}␊ |
251 | ␉␊ |
252 | ␉if(doSave)␊ |
253 | ␉{␊ |
254 | ␉␉if (! [ mOptionsDict fileIsImmutable] )␊ |
255 | ␉␉{␊ |
256 | ␉␉␉if (isrc==0 && idst==1)␊ |
257 | ␉␉␉␉[mOptionsDict setObject: [NSNumber numberWithBool: val ? true : false] forKey:@"swapHD01"];␊ |
258 | ␉␉␉if (isrc==0 && idst==2)␊ |
259 | ␉␉␉␉[mOptionsDict setObject: [NSNumber numberWithBool: val ? true : false] forKey:@"swapHD02"];␊ |
260 | ␉␉␉[mOptionsDict writeToFile:mOptionsPlistPath atomically:YES];␊ |
261 | ␉␉}␊ |
262 | ␉}␊ |
263 | ␉␊ |
264 | ␉[mPartitionsTable reloadData];␊ |
265 | ␉[mPartitionsTable scrollRowToVisible: 0];␊ |
266 | ␉//[self tableViewSelectionDidChange: nil];␊ |
267 | ␉␊ |
268 | }␊ |
269 | //--------------------------------------------------------------------------␊ |
270 | - (IBAction)onSwapHD: (id)sender␊ |
271 | {␊ |
272 | ␉partExtractor->resetSwapping();␊ |
273 | ␉[self doSwapHD: !![mSwapHD01 intValue] save:true src:0 dst:1];␊ |
274 | ␉[self doSwapHD: !![mSwapHD02 intValue] save:true src:0 dst:2];␊ |
275 | }␊ |
276 | ␊ |
277 | //--------------------------------------------------------------------------␊ |
278 | - (IBAction)onRestart: (id)sender␊ |
279 | {␊ |
280 | ␉NSInteger n = NSRunAlertPanel(@"Restarting OS X", ␊ |
281 | ␉␉␉␉␉@"Are you sure you want to restart now ?",␊ |
282 | ␉␉␉␉␉@"OK", @"Cancel", /*ThirdButtonHere:*/nil␊ |
283 | ␉␉␉␉␉/*, args for a printf-style msg go here */);␊ |
284 | ␉if (n==1)␊ |
285 | ␉{␊ |
286 | ␉␉AuthorizationRef auth = [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
287 | ␉␉// executePrivilegedCmd(auth,"/sbin/reboot");␊ |
288 | ␉␉// following DieBuch recommendation (thanks!):␊ |
289 | ␉␉executePrivilegedCmd(auth,"/usr/bin/osascript","-e 'tell application \"System Events\" to restart'");␊ |
290 | ␉}␊ |
291 | ␉␉␊ |
292 | }␊ |
293 | ␊ |
294 | //--------------------------------------------------------------------------␊ |
295 | - (void)tableViewSelectionDidChange:(NSNotification *)notification {␊ |
296 | ␊ |
297 | ␉NSTableView* tv= mPartitionsTable;␊ |
298 | ␉if ([tv selectedRow] != currentRowSel)␊ |
299 | ␉{␊ |
300 | ␉␉currentRowSel = [tv selectedRow];␊ |
301 | ␉␉if (currentRowSel>=0)␊ |
302 | ␉␉{␊ |
303 | ␉␉␉const std::vector<PartitionInfo>& partInfo = partExtractor->partList();␊ |
304 | ␉␉␉char hd[7+1]="hd(n,m)";␊ |
305 | ␉␉␉hd[3]= ('0'+partInfo[currentRowSel].disk());␊ |
306 | ␉␉␉hd[5]= ('0'+partInfo[currentRowSel].partition());␉␊ |
307 | ␉␉␉AuthorizationRef auth= [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
308 | ␉␉␉if(prop->setStringForKey(kDefaultPartition, hd))␊ |
309 | ␉␉␉ prop->save(auth);␊ |
310 | ␉␉}␊ |
311 | ␉}␊ |
312 | ␉␉␊ |
313 | }␊ |
314 | ␊ |
315 | //--------------------------------------------------------------------------␊ |
316 | - (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView␊ |
317 | {␊ |
318 | ␉return partExtractor ? partExtractor->partList().size() : 0;␊ |
319 | }␊ |
320 | ␊ |
321 | //--------------------------------------------------------------------------␊ |
322 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row␊ |
323 | {␊ |
324 | ␉id ret=nil;␊ |
325 | ␉␊ |
326 | ␉const std::vector<PartitionInfo>& partInfo = partExtractor->partList();␊ |
327 | ␊ |
328 | ␉if (tableColumn == mPartitionImgColumn) ␊ |
329 | ␉{␊ |
330 | ␉␉switch(partInfo[row].indexFromFs())␊ |
331 | ␉␉{␊ |
332 | ␉␉␉case 0:␊ |
333 | ␉␉␉␉ret = mMacOSXImage;␊ |
334 | ␉␉␉␉break;␊ |
335 | ␉␉␉case 1:␊ |
336 | ␉␉␉␉ret = mWindowsImage;␊ |
337 | ␉␉␉␉break;␊ |
338 | ␉␉␉case 2:␊ |
339 | ␉␉␉␉ret = mLinuxImage;␊ |
340 | ␉␉␉␉break;␊ |
341 | ␉␉␉defualt:␊ |
342 | ␉␉␉␉ret = mUnknownImage;␊ |
343 | ␉␉␉␉break;␊ |
344 | ␉␉␉␉␊ |
345 | ␉␉}␊ |
346 | ␉}␊ |
347 | ␉if (tableColumn == mFileSystemColumn) ␊ |
348 | ␉{␊ |
349 | ␉␉ret = [NSString stringWithFormat: @"%s", partInfo[row].fsType().c_str() ];␊ |
350 | ␉}␊ |
351 | ␉else if (tableColumn == mPartitionNameColumn) ␊ |
352 | ␉{␊ |
353 | ␉␉ret = [NSString stringWithFormat: @"%s", ␊ |
354 | ␉␉␉␉partInfo[row].label().c_str()␊ |
355 | ␉␉␉␉];␊ |
356 | ␉}␊ |
357 | ␉else if (tableColumn == mPartitionIDColumn) ␊ |
358 | ␉{␊ |
359 | ␉␉ret= [NSString stringWithFormat: @"hd(%d,%d)", ␊ |
360 | ␉␉␉␉partInfo[row].disk(),␊ |
361 | ␉␉␉␉partInfo[row].partition()␊ |
362 | ␉␉␉␉];␊ |
363 | ␉} ␊ |
364 | ␉␊ |
365 | ␉return ret;␊ |
366 | }␊ |
367 | //--------------------------------------------------------------------------␊ |
368 | ␊ |
369 | @end␊ |
370 |