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 const NSString* const kPreferencesFilePath = @"/Library/Preferences/com.chameleon.prefPane.plist";␊ |
33 | static const NSString* const keyPreferencesFileVersion = @"version"; // for future back compatibility␊ |
34 | static const int CurrentPreferencesFileVersion = 0x01; // for future back compatibility␊ |
35 | static const NSString* const keyForceBootConfigPath = @"forceBootConfigPath";␊ |
36 | static const NSString* const keySwapHD01 = @"swapHD01";␊ |
37 | static const NSString* const keySwapHD02 = @"swapHD02";␊ |
38 | static const NSString* const keyUseFrozenParts = @"useFrozenParts";␊ |
39 | static const NSString* const keyPartitionsList = @"partitionsList";␊ |
40 | static const char cPartDescSep = ';'; // partition descriptor separator␊ |
41 | static const char* sPartDescSep = ";"; // cstring version␊ |
42 | //--------------------------------------------------------------------------␊ |
43 | // Static file variables␊ |
44 | //--------------------------------------------------------------------------␊ |
45 | static std::string sCurrentDefaultPartition;␊ |
46 | ␊ |
47 | static PartitionExtractor * partExtractor=NULL;␊ |
48 | static PropertyList * prop = NULL;␊ |
49 | static int currentRowSel = -1;␊ |
50 | //--------------------------------------------------------------------------␊ |
51 | ␊ |
52 | @implementation ChameleonPrefPane␊ |
53 | ␊ |
54 | //--------------------------------------------------------------------------␊ |
55 | /**␊ |
56 | * SFAuthorization delegates␊ |
57 | */␊ |
58 | - (void)authorizationViewDidAuthorize:(SFAuthorizationView *)view {␊ |
59 | [self selectDefaultPartition];␊ |
60 | ␉[self refreshLockStates];␊ |
61 | ␊ |
62 | } ␊ |
63 | ␊ |
64 | //--------------------------------------------------------------------------␊ |
65 | - (void)authorizationViewDidDeauthorize:(SFAuthorizationView *)view {␊ |
66 | [self refreshLockStates];␊ |
67 | }␊ |
68 | ␊ |
69 | //--------------------------------------------------------------------------␊ |
70 | - (void) refreshLockStates␊ |
71 | {␊ |
72 | [mPartitionsTable setEnabled:[self isUnlocked]];␊ |
73 | [mSwapHD01 setEnabled:[self isUnlocked]];␊ |
74 | [mSwapHD02 setEnabled:[self isUnlocked]];␊ |
75 | [mStatusText setEnabled:[self isUnlocked]];␊ |
76 | [mFreezeParts setEnabled:[self isUnlocked]];␊ |
77 | [mInjectFrozenParts setEnabled:[self isUnlocked]];␊ |
78 | }␊ |
79 | ␊ |
80 | ␊ |
81 | //--------------------------------------------------------------------------␊ |
82 | - (bool)isUnlocked ␊ |
83 | {␊ |
84 | return [authView authorizationState] == SFAuthorizationViewUnlockedState;␊ |
85 | }␊ |
86 | ␊ |
87 | //--------------------------------------------------------------------------␊ |
88 | - (id) getImageResource: (NSString *) str ofType: (NSString*) sType␊ |
89 | {␊ |
90 | ␉NSImage * img=nil;␊ |
91 | ␉if(!str) return nil;␊ |
92 | ␉NSBundle * b = [NSBundle bundleForClass:[self class]];␊ |
93 | ␉NSString* sRes = [b pathForResource: str ofType:sType ];␊ |
94 | ␉img = [[NSImage alloc] initWithContentsOfFile: sRes];␊ |
95 | ␉return img;␊ |
96 | }␊ |
97 | ␊ |
98 | //--------------------------------------------------------------------------␊ |
99 | -(bool) savePreferences: (NSDictionary*) dict␊ |
100 | {␊ |
101 | ␉std::string sPath = [kPreferencesFilePath UTF8String];␊ |
102 | ␉␊ |
103 | ␉if(dict==nil || sPath.length()==0) return false;␊ |
104 | ␉␊ |
105 | ␉AuthorizationRef auth = [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
106 | ␉␊ |
107 | ␉PropertyList::chmodFile(sPath.c_str(), "0777", auth);␊ |
108 | ␉[dict writeToFile:kPreferencesFilePath atomically:YES];␊ |
109 | ␉PropertyList::chmodFile(sPath.c_str(), "0644", auth);␊ |
110 | ␉return true;␊ |
111 | }␊ |
112 | //--------------------------------------------------------------------------␊ |
113 | - (void) loadFrozenParts␊ |
114 | {␊ |
115 | ␉std::vector<PartitionInfo>& partList = partExtractor->editPartList(); //rw␊ |
116 | ␉// iterate for all entries to add␊ |
117 | ␉char keyPartN[32] = "";␊ |
118 | ␉char buffer[256]="";␊ |
119 | ␉int k=0;␊ |
120 | ␉partList.clear();␊ |
121 | ␉for (int i=0; i<[mPartitionsDict count]; i++)␊ |
122 | ␉{␊ |
123 | ␉␉// get the key␊ |
124 | ␉␉snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);␊ |
125 | ␉␉NSString* obj = [mPartitionsDict objectForKey: [[NSString alloc] initWithUTF8String: keyPartN] ];␊ |
126 | ␉␉// assign this key if valid␊ |
127 | ␉␉if (obj!=nil && [obj length]>0)␊ |
128 | ␉␉{␊ |
129 | ␉␉␉PartitionInfo p;␊ |
130 | ␉␉␉// parse string␊ |
131 | ␉␉␉strncpy(buffer, [obj UTF8String], sizeof(buffer)-1);␊ |
132 | ␉␉␉k=0;␊ |
133 | ␉␉␉for(const char* word = strtok(buffer,";"); word; word=strtok(NULL,sPartDescSep),k++)␊ |
134 | ␉␉␉{␊ |
135 | ␉␉␉␉switch (k) {␊ |
136 | ␉␉␉␉␉case 0: // parse disk number␊ |
137 | ␉␉␉␉␉␉if (isdigit(*word)) p.disk(*word-'0');␊ |
138 | ␉␉␉␉␉␉break;␊ |
139 | ␉␉␉␉␉case 1: // parse partition number␊ |
140 | ␉␉␉␉␉␉if (isdigit(*word)) p.partition(*word-'0');␊ |
141 | ␉␉␉␉␉␉break;␊ |
142 | ␉␉␉␉␉case 2: // parse volume label␊ |
143 | ␉␉␉␉␉␉p.label(word);␊ |
144 | ␉␉␉␉␉case 3:␊ |
145 | ␉␉␉␉␉␉p.fsType(word);␊ |
146 | ␉␉␉␉␉␉break;␊ |
147 | ␉␉␉␉␉default:␊ |
148 | ␉␉␉␉␉␉break;␊ |
149 | ␉␉␉␉}␊ |
150 | ␉␉␉}␊ |
151 | ␉␉␉partList.push_back(p);␊ |
152 | ␉␉}␊ |
153 | ␉}␊ |
154 | }␊ |
155 | //--------------------------------------------------------------------------␊ |
156 | - (void) loadPreferences␊ |
157 | ␊ |
158 | {␊ |
159 | ␉id oldGlobalPreferences = [ [NSDictionary dictionaryWithContentsOfFile: ␊ |
160 | ␉␉␉␉␉␉␉␉kPreferencesFilePath ] retain];␊ |
161 | ␊ |
162 | ␉mPartitionsDict = [[NSMutableDictionary alloc] init];␊ |
163 | ␉[mPartitionsDict retain];␊ |
164 | ␊ |
165 | if (oldGlobalPreferences!=nil)␊ |
166 | ␉{␊ |
167 | ␉␉mPreferenceFileVersion= [[oldGlobalPreferences objectForKey: keyPreferencesFileVersion] intValue ];␊ |
168 | ␉␉[mSwapHD01 setIntValue: [[oldGlobalPreferences objectForKey:␉keySwapHD01] intValue]];␊ |
169 | ␉␉[mSwapHD02 setIntValue: [[oldGlobalPreferences objectForKey:␉keySwapHD02] intValue]];␊ |
170 | ␉␉[mFreezeParts setIntValue: [[oldGlobalPreferences objectForKey: keyUseFrozenParts] intValue] ];␊ |
171 | ␉␉[mPartitionsDict addEntriesFromDictionary: [oldGlobalPreferences objectForKey: keyPartitionsList] ];␊ |
172 | ␉}␊ |
173 | ␉else ␊ |
174 | ␉{ // Create a preference plist file with Defaults values␊ |
175 | ␉␉[mSwapHD01 setIntValue: 0];␊ |
176 | ␉␉[mSwapHD02 setIntValue: 0];␊ |
177 | ␉␉[mFreezeParts setIntValue: 0];␊ |
178 | ␉␉// Initialize defaults␊ |
179 | ␉␉oldGlobalPreferences = [[NSMutableDictionary alloc] init];␊ |
180 | ␉␉[oldGlobalPreferences setObject: [[NSNumber alloc] initWithInt: CurrentPreferencesFileVersion] ␊ |
181 | ␉␉␉␉␉␉␉␉ forKey: keyPreferencesFileVersion];␊ |
182 | ␉␉[oldGlobalPreferences setObject: [[NSNumber alloc] initWithBool: false] forKey: keySwapHD01];␊ |
183 | ␉␉[oldGlobalPreferences setObject: [[NSNumber alloc] initWithBool: false] forKey: keySwapHD02];␊ |
184 | ␉␉[oldGlobalPreferences setObject:[[NSNumber alloc] initWithBool: false] forKey: keyUseFrozenParts];␊ |
185 | ␉␉[oldGlobalPreferences setObject: mPartitionsDict forKey: keyPartitionsList];␊ |
186 | ␉␉␊ |
187 | ␉␉// Save the preferences file␊ |
188 | ␉␉[ self savePreferences:oldGlobalPreferences ];␊ |
189 | ␉}␊ |
190 | ␊ |
191 | ␉mOptionsDict = [[NSMutableDictionary alloc] init];␊ |
192 | [mOptionsDict addEntriesFromDictionary:oldGlobalPreferences];␊ |
193 | ␉if (mPartitionsDict!=nil) [mPartitionsDict retain];␊ |
194 | [oldGlobalPreferences release];␊ |
195 | }␊ |
196 | ␊ |
197 | //--------------------------------------------------------------------------␊ |
198 | - (id) init␊ |
199 | {␊ |
200 | ␉self = [super init];␊ |
201 | ␊ |
202 | ␉// set the image to display the current file being played␊ |
203 | ␉mMacOSXImage = [self getImageResource: @"MacOSX" ofType: @"png"];␊ |
204 | ␉mWindowsImage = [self getImageResource: @"Windows" ofType: @"png"];␊ |
205 | ␉mLinuxImage = [self getImageResource: @"Linux" ofType: @"png"];␊ |
206 | ␉mCDROMImage = [self getImageResource: @"CDROM" ofType: @"png"];␊ |
207 | ␉mUnknownImage = [self getImageResource: @"Chameleon" ofType: @"tiff"];␊ |
208 | ␉␊ |
209 | ␉␊ |
210 | ␉// Retrieve the com.chameleon.prefPane.plist config␊ |
211 | ␉return self;␊ |
212 | }␊ |
213 | ␊ |
214 | //--------------------------------------------------------------------------␊ |
215 | /** When called here, all outlets references are initialized */␊ |
216 | - (void)awakeFromNib␊ |
217 | { // called more than once, we only need one resource init␊ |
218 | ␉static bool ft=true;␊ |
219 | ␉if(ft)␊ |
220 | ␉{␊ |
221 | ␉␉ft=false;␊ |
222 | ␉␉[self loadPreferences];␊ |
223 | ␉␉[self initBootConfig];␊ |
224 | ␉}␊ |
225 | }␊ |
226 | ␊ |
227 | //--------------------------------------------------------------------------␊ |
228 | - (void) initBootConfig␊ |
229 | {␊ |
230 | ␉static bool ft=true;␊ |
231 | ␉␊ |
232 | ␉// Cosmetics setup␊ |
233 | ␉// Setup security for changing boot options␊ |
234 | AuthorizationItem items = {kAuthorizationRightExecute, 0, NULL, 0};␊ |
235 | AuthorizationRights rights = {1, &items};␊ |
236 | ␊ |
237 | ␉[authView setAuthorizationRights:&rights];␊ |
238 | authView.delegate = self;␊ |
239 | [authView updateStatus:nil];␊ |
240 | ␊ |
241 | ␉// create the propertylist object that will handle com.apple.Boot.plist␊ |
242 | ␉if(!prop) prop = new PropertyList();␊ |
243 | ␊ |
244 | ␉// create the process that will extract the diskutil list infos␊ |
245 | ␉if(!partExtractor) partExtractor = new PartitionExtractor();␊ |
246 | ␉␊ |
247 | ␉if (!prop->isValid())␊ |
248 | ␉{␊ |
249 | ␉␉std::string sPath;␊ |
250 | ␉␉AuthorizationRef auth = [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
251 | ␉␉CFStringRef errorString=NULL;␊ |
252 | ␉␉bool cont =true;␊ |
253 | ␉␉␊ |
254 | ␉␉id sForcedPath = [mOptionsDict valueForKey: keyForceBootConfigPath];␊ |
255 | ␉␉const char * szForcedPath = sForcedPath!=nil ? [sForcedPath UTF8String] : NULL;␊ |
256 | ␉␉if (szForcedPath && *szForcedPath)␊ |
257 | ␉␉{␊ |
258 | ␉␉␉cont = !prop->open(szForcedPath, &errorString, auth);␊ |
259 | ␉␉}␊ |
260 | ␉␉else {␊ |
261 | ␉␉␉for(int i=0; szBootPaths[i] && cont; i++)␊ |
262 | ␉␉␉{␊ |
263 | ␉␉␉␉sPath = szBootPaths[i];␊ |
264 | ␉␉␉␉sPath += szPropFileName;␊ |
265 | ␉␉␉␉cont = !prop->open(sPath.c_str(), &errorString, auth);␊ |
266 | ␉␉␉}␊ |
267 | ␉␉}␊ |
268 | ␉␉if (cont)␊ |
269 | ␉␉{␊ |
270 | ␉␉␉if(!ft) return;␊ |
271 | ␉␉␉ft=false;␊ |
272 | ␉␉␉[mStatusText setTextColor: [NSColor redColor] ];␊ |
273 | ␉␉␉if (errorString)␊ |
274 | ␉␉␉␉[mStatusText setStringValue:[NSString stringWithFormat: @"Error while parsing com.apple.Boot.plist : %@",␊ |
275 | ␉␉␉␉␉␉␉␉␉␉␉ errorString] ];␊ |
276 | ␉␉␉else␊ |
277 | ␉␉␉␉[mStatusText setStringValue: @"Error while searching for com.apple.Boot.plist"];␊ |
278 | ␉␉␉␊ |
279 | ␉␉}␊ |
280 | ␉␉else␊ |
281 | ␉␉{ ␊ |
282 | ␉␉␉[mStatusText setTextColor: [NSColor grayColor] ];␊ |
283 | ␉␉␉NSString* ns = [ [NSString alloc] initWithUTF8String:prop->bootConfigPath() ];␊ |
284 | ␉␉␉[mStatusText setStringValue: [NSString stringWithFormat: @"bootConfig: %@", ns] ];␊ |
285 | ␉␉}␊ |
286 | ␉␉if (prop->isValid())␊ |
287 | ␉␉{␊ |
288 | ␉␉␉// read options in the plist file␊ |
289 | ␉␉␉␊ |
290 | ␉␉␉partExtractor->hidePartitions(prop->getStringForKey(kHidePartition));␊ |
291 | ␉␉␉partExtractor->renamedPartitions(prop->getStringForKey(kRenamePartition));␊ |
292 | ␉␉␉␊ |
293 | ␉␉␉// partExtractor->resetSwapping();␊ |
294 | ␉␉␉id val = [mOptionsDict valueForKey: keySwapHD01];␊ |
295 | ␉␉␉[mSwapHD01 setIntValue: [val intValue] ];␊ |
296 | ␉␉␉[self doSwapHD: [val boolValue] save: false src:0 dst:1];␊ |
297 | ␉␉␉␊ |
298 | ␉␉␉val = [mOptionsDict valueForKey: keySwapHD02];␊ |
299 | ␉␉␉[mSwapHD02 setIntValue: [val intValue] ];␊ |
300 | ␉␉␉[self doSwapHD: [val boolValue] save: false src:0 dst:2];␊ |
301 | ␉␉␉␊ |
302 | ␉␉␉[self selectDefaultPartition];␊ |
303 | ␉␉}␊ |
304 | ␉␉␊ |
305 | ␉}␉␊ |
306 | }␊ |
307 | //--------------------------------------------------------------------------␊ |
308 | - (void) selectDefaultPartition␊ |
309 | {␊ |
310 | if(!authView) return;␊ |
311 | ␉␊ |
312 | ␉[self refreshLockStates];␊ |
313 | ␉␊ |
314 | ␉// try to get the current default partition if any␊ |
315 | ␉if(partExtractor && prop && prop->isValid())␊ |
316 | ␉{␊ |
317 | ␉␉const char *sdp = prop->getStringForKey(kDefaultPartition);␊ |
318 | ␉␉sCurrentDefaultPartition = sdp ? sdp : ""; ␊ |
319 | ␉␉if (sCurrentDefaultPartition.size())␊ |
320 | ␉␉{␊ |
321 | ␉␉␉int index = partExtractor->getIndexFromHdStringSpec(sCurrentDefaultPartition.c_str());␊ |
322 | ␉␉␉if (index>=0)␊ |
323 | ␉␉␉{␊ |
324 | ␉␉␉␉[mPartitionsTable selectRowIndexes:␊ |
325 | ␉␉␉␉ [NSIndexSet indexSetWithIndex:index] byExtendingSelection:NO];␊ |
326 | ␉␉␉␉currentRowSel = index;␊ |
327 | ␉␉␉}␊ |
328 | ␉␉}␊ |
329 | ␉}␊ |
330 | ␉␊ |
331 | }␊ |
332 | ␊ |
333 | //--------------------------------------------------------------------------␊ |
334 | - (void) swapDisks: (bool) bSwap src: (int) iSrc dst: (int) iDst;␊ |
335 | {␊ |
336 | ␉if(!partExtractor || !prop || !prop->isValid()) return;␊ |
337 | ␉if (bSwap)␊ |
338 | ␉{␊ |
339 | ␉␉partExtractor->swapHD(iSrc, iDst);␉␊ |
340 | ␉}␊ |
341 | ␉␊ |
342 | ␉if ([mFreezeParts intValue]==0) ␊ |
343 | ␉␉partExtractor->extractPartitions();␊ |
344 | ␉else ␊ |
345 | ␉␉[self loadFrozenParts ];␊ |
346 | ␉␊ |
347 | ␉[ self selectDefaultPartition];␊ |
348 | ␊ |
349 | }␊ |
350 | ␊ |
351 | //--------------------------------------------------------------------------␊ |
352 | - (void) doSwapHD: (int) val save: (bool) doSave src: (int) isrc dst: (int) idst␊ |
353 | {␊ |
354 | ␉␊ |
355 | ␉if( val>0 && ![mFreezeParts intValue]) //on␊ |
356 | ␉{␊ |
357 | ␉␉[self swapDisks: true src:isrc dst:idst ];␊ |
358 | ␉}␊ |
359 | ␉else␊ |
360 | ␉{␊ |
361 | ␉␉[self swapDisks: false src:isrc dst:idst ];␊ |
362 | ␉}␊ |
363 | ␉␊ |
364 | ␉if(doSave)␊ |
365 | ␉{␊ |
366 | ␉␉if (isrc==0 && idst==1)␊ |
367 | ␉␉␉[mOptionsDict setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD01];␊ |
368 | ␉␉if (isrc==0 && idst==2)␊ |
369 | ␉␉␉[mOptionsDict setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD02];␊ |
370 | ␉␉[ self savePreferences:mOptionsDict ];␊ |
371 | ␉}␊ |
372 | ␉␊ |
373 | ␉[mPartitionsTable reloadData];␊ |
374 | ␉[mPartitionsTable scrollRowToVisible: 0];␊ |
375 | ␉//[self tableViewSelectionDidChange: nil];␊ |
376 | ␉␊ |
377 | }␊ |
378 | //--------------------------------------------------------------------------␊ |
379 | - (IBAction)onSwapHD: (id)sender␊ |
380 | {␊ |
381 | ␉partExtractor->resetSwapping();␊ |
382 | ␉[self doSwapHD: [mSwapHD01 intValue] save:true src:0 dst:1];␊ |
383 | ␉[self doSwapHD: [mSwapHD02 intValue] save:true src:0 dst:2];␊ |
384 | }␊ |
385 | //--------------------------------------------------------------------------␊ |
386 | - (IBAction)onUseFrozenParts: (id)sender␊ |
387 | {␊ |
388 | ␉bool val = !![sender intValue];␊ |
389 | ␉[mOptionsDict setObject: [NSNumber numberWithBool: val] forKey: keyUseFrozenParts];␊ |
390 | ␉[self savePreferences: mOptionsDict];␊ |
391 | ␉[self onSwapHD: nil];␊ |
392 | }␊ |
393 | ␊ |
394 | //--------------------------------------------------------------------------␊ |
395 | - (IBAction)onInjectPartsToFreeze: (id)sender␊ |
396 | {␊ |
397 | ␉int size = partExtractor ? partExtractor->partList().size() : 0;␊ |
398 | ␉if (!size) ␊ |
399 | ␉{ // nothing to inject␊ |
400 | ␉␉NSRunAlertPanel(@"Inject Partitions to Freeze Configuration", ␊ |
401 | ␉␉␉␉␉␉@"No current partitions to inject, did you check boot config file ?",@"OK", nil,nil);␊ |
402 | ␉␉return;␊ |
403 | ␉}␊ |
404 | ␉// generate the parts list in preferences proplist␊ |
405 | ␉NSInteger n = NSRunAlertPanel(@"Inject Partitions to Freeze Configuration", ␊ |
406 | ␉␉␉␉␉␉␉␉ @"Are you sure you want to overwrite your Freeze settings with current partition list ?",␊ |
407 | ␉␉␉␉␉␉␉␉ @"OK", @"Cancel",nil);␊ |
408 | ␉if (n==1)␊ |
409 | ␉{␊ |
410 | ␉␉// populate the dictionary with the current partitions␊ |
411 | ␉␉␊ |
412 | ␉␉// empty dictionary and update key in parent:␊ |
413 | ␉␉[mOptionsDict removeObjectForKey: keyPartitionsList];␊ |
414 | ␉␉[mPartitionsDict removeAllObjects ];␊ |
415 | ␉␉␊ |
416 | ␉␉// iterate for all entries to add␊ |
417 | ␉␉char partDesc[256]="";␊ |
418 | ␉␉char keyPartN[32] = "";␊ |
419 | ␉␉for (int i=0; i< size; i++)␊ |
420 | ␉␉{␊ |
421 | ␉␉␉␊ |
422 | ␉␉␉const PartitionInfo& p =partExtractor->partList()[i];␊ |
423 | ␉␉␉␊ |
424 | ␉␉␉// format the partition key and descriptor string␊ |
425 | ␉␉␉snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);␊ |
426 | ␉␉␉␊ |
427 | ␉␉␉snprintf(partDesc, sizeof(partDesc)-1, "%d%c%d%c%s%c%s",␊ |
428 | ␉␉␉␉␉ p.disk(), cPartDescSep,␊ |
429 | ␉␉␉␉␉ p.partition(), cPartDescSep,␊ |
430 | ␉␉␉␉␉ p.label().c_str(), cPartDescSep,␊ |
431 | ␉␉␉␉␉ p.fsType().c_str());␊ |
432 | ␉␉␉␊ |
433 | ␉␉␉// write it to the dictionary␊ |
434 | ␉␉␉NSString * key = [[NSString alloc] initWithUTF8String: keyPartN];␊ |
435 | ␉␉␉NSString * desc = [[NSString alloc] initWithUTF8String: partDesc]; ␊ |
436 | ␉␉␉[mPartitionsDict setObject: desc forKey: key];␊ |
437 | ␉␉}␊ |
438 | ␉␉[mOptionsDict setObject: mPartitionsDict forKey: keyPartitionsList];␊ |
439 | ␉␉[self savePreferences: mOptionsDict];␊ |
440 | ␉}␊ |
441 | }␊ |
442 | //--------------------------------------------------------------------------␊ |
443 | // following DieBuch recommendation : using applescript and system events (thanks!):␊ |
444 | - (IBAction)onRestart: (id)sender␊ |
445 | {␊ |
446 | ␉NSInteger n = NSRunAlertPanel(@"Restarting OS X", ␊ |
447 | ␉␉␉␉␉␉␉␉ @"Are you sure you want to restart your computer now ?",␊ |
448 | ␉␉␉␉␉␉␉␉ @"OK", @"Cancel", nil);␊ |
449 | ␉if (n==1)␊ |
450 | ␉{␊ |
451 | ␉␉AuthorizationRef auth = [[authView authorization] authorizationRef];␊ |
452 | ␉␉executePrivilegedCmd(auth,"/usr/bin/osascript","-e 'tell app \"System Events\" to restart'");␊ |
453 | ␉}␊ |
454 | ␉␊ |
455 | }␊ |
456 | //--------------------------------------------------------------------------␊ |
457 | - (IBAction)onShutdown: (id)sender␊ |
458 | {␊ |
459 | ␉NSInteger n = NSRunAlertPanel(@"Shutting Down OS X", ␊ |
460 | ␉␉␉␉␉␉␉␉ @"Are you sure you want to shut down your computer now ?",␊ |
461 | ␉␉␉␉␉␉␉␉ @"OK", @"Cancel", /*ThirdButtonHere:*/nil␊ |
462 | ␉␉␉␉␉␉␉␉ /*, args for a printf-style msg go here */);␊ |
463 | ␉if (n==1)␊ |
464 | ␉{␊ |
465 | ␉␉system("/usr/bin/osascript -e 'tell app \"System Events\" to shut down'");␊ |
466 | ␉}␊ |
467 | }␊ |
468 | ␊ |
469 | - (IBAction)onSleep: (id)sender␊ |
470 | {␊ |
471 | ␉system("/usr/bin/osascript -e 'tell app \"System Events\" to sleep'");␊ |
472 | }␊ |
473 | ␊ |
474 | //--------------------------------------------------------------------------␊ |
475 | - (void)tableViewSelectionDidChange:(NSNotification *)notification {␊ |
476 | ␊ |
477 | ␉NSTableView* tv= mPartitionsTable;␊ |
478 | ␉if ([tv selectedRow] != currentRowSel)␊ |
479 | ␉{␊ |
480 | ␉␉currentRowSel = [tv selectedRow];␊ |
481 | ␉␉if (currentRowSel>=0)␊ |
482 | ␉␉{␊ |
483 | ␉␉␉const std::vector<PartitionInfo>& partInfo = partExtractor->partList();␊ |
484 | ␉␉␉char hd[7+1]="hd(n,m)";␊ |
485 | ␉␉␉hd[3]= ('0'+partInfo[currentRowSel].disk());␊ |
486 | ␉␉␉hd[5]= ('0'+partInfo[currentRowSel].partition());␉␊ |
487 | ␉␉␉AuthorizationRef auth= [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
488 | ␉␉␉if(prop->setStringForKey(kDefaultPartition, hd))␊ |
489 | ␉␉␉ prop->save(auth);␊ |
490 | ␉␉}␊ |
491 | ␉}␊ |
492 | ␉␉␊ |
493 | }␊ |
494 | ␊ |
495 | //--------------------------------------------------------------------------␊ |
496 | - (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView␊ |
497 | {␊ |
498 | ␉int size = partExtractor ? partExtractor->partList().size() : 0;␊ |
499 | ␉return size;␊ |
500 | }␊ |
501 | ␊ |
502 | //--------------------------------------------------------------------------␊ |
503 | - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row␊ |
504 | {␊ |
505 | ␉id ret=nil;␊ |
506 | ␉␊ |
507 | ␉const std::vector<PartitionInfo>& partInfo = partExtractor->partList();␊ |
508 | ␊ |
509 | ␉if (tableColumn == mPartitionImgColumn) ␊ |
510 | ␉{␊ |
511 | ␉␉switch(partInfo[row].imageIndexFromFs())␊ |
512 | ␉␉{␊ |
513 | ␉␉␉case 0:␊ |
514 | ␉␉␉␉ret = mMacOSXImage;␊ |
515 | ␉␉␉␉break;␊ |
516 | ␉␉␉case 1:␊ |
517 | ␉␉␉␉ret = mWindowsImage;␊ |
518 | ␉␉␉␉break;␊ |
519 | ␉␉␉case 2:␊ |
520 | ␉␉␉␉ret = mLinuxImage;␊ |
521 | ␉␉␉␉break;␊ |
522 | ␉␉␉defualt:␊ |
523 | ␉␉␉␉ret = mUnknownImage;␊ |
524 | ␉␉␉␉break;␊ |
525 | ␉␉␉␉␊ |
526 | ␉␉}␊ |
527 | ␉}␊ |
528 | ␉else if (tableColumn == mFileSystemColumn) ␊ |
529 | ␉{␊ |
530 | ␉␉ret = [NSString stringWithFormat: @"%s", partInfo[row].fsType().c_str() ];␊ |
531 | ␉}␊ |
532 | ␉else if (tableColumn == mPartitionNameColumn) ␊ |
533 | ␉{␊ |
534 | ␉␉ret = [NSString stringWithFormat: @"%s", ␊ |
535 | ␉␉␉␉partInfo[row].label().c_str()␊ |
536 | ␉␉␉␉];␊ |
537 | ␉}␊ |
538 | ␉else if (tableColumn == mPartitionIDColumn) ␊ |
539 | ␉{␊ |
540 | ␉␉ret= [NSString stringWithFormat: @"hd(%d,%d)", ␊ |
541 | ␉␉␉␉partInfo[row].disk(),␊ |
542 | ␉␉␉␉partInfo[row].partition()␊ |
543 | ␉␉␉␉];␊ |
544 | ␉} ␊ |
545 | ␉␊ |
546 | ␉return ret;␊ |
547 | }␊ |
548 | //--------------------------------------------------------------------------␊ |
549 | ␊ |
550 | @end␊ |
551 | |