1 | //␊ |
2 | // BootSetupController.mm␊ |
3 | // ChameleonPrefPane␊ |
4 | //␊ |
5 | // Created by Rekursor on 1/22/10.␊ |
6 | //␊ |
7 | ␊ |
8 | #import "BootSetupController.h"␊ |
9 | #import "ChameleonPrefPane.h"␊ |
10 | #import "TableViewsController.h"␊ |
11 | #import "ShellProcess.h"␊ |
12 | #import "PartitionInfoManager.h"␊ |
13 | ␊ |
14 | static const char cPartDescSep = ';'; // partition descriptor separator␊ |
15 | ␊ |
16 | static BootSetupController *gInstance = NULL;␊ |
17 | ␊ |
18 | @implementation BootSetupController␊ |
19 | ␊ |
20 | //--------------------------------------------------------------------------␊ |
21 | - (id) init␊ |
22 | {␊ |
23 | ␉self = [super init];␊ |
24 | ␉return (gInstance = self);␊ |
25 | }␉␊ |
26 | ␊ |
27 | //--------------------------------------------------------------------------␊ |
28 | - (void) addOptionsDesc␊ |
29 | {␊ |
30 | BootProp::instance().addOptionDesc(mDefaultPartition, mDefaultPartitionText, OptionString, "Default Partition", "");␊ |
31 | BootProp::instance().addOptionDesc(mHidePartition , mHidePartitionText, OptionString, "Hide Partition", "");␊ |
32 | BootProp::instance().addOptionDesc(mRenamePartition , mRenamePartitionText, OptionString, "Rename Partition", "");␊ |
33 | }␊ |
34 | ␊ |
35 | //--------------------------------------------------------------------------␊ |
36 | - (void) refreshLockStates␊ |
37 | {␊ |
38 | ␉// non boot config options (not automatized)␊ |
39 | [PreferencesControllerBase refreshLockState: mSwapHD01 ];␊ |
40 | [PreferencesControllerBase refreshLockState: mSwapHD02 ];␊ |
41 | [PreferencesControllerBase refreshLockState: mFreezeParts ];␊ |
42 | [PreferencesControllerBase refreshLockState: mInjectFrozenParts ];␊ |
43 | [PreferencesControllerBase refreshLockState: mBootConfigPath ];␊ |
44 | [PreferencesControllerBase refreshLockState: mBootConfigPathText ];␊ |
45 | }␊ |
46 | ␊ |
47 | //--------------------------------------------------------------------------␊ |
48 | - (void) setDefaultsValues: (NSMutableDictionary*) dict␊ |
49 | {␊ |
50 | ␉[dict setObject: [[NSNumber alloc] initWithBool: false] forKey: keySwapHD01];␊ |
51 | ␉[dict setObject: [[NSNumber alloc] initWithBool: false] forKey: keySwapHD02];␊ |
52 | ␉[dict setObject:[[NSNumber alloc] initWithBool: false] forKey: keyUseFrozenParts];␊ |
53 | ␉[dict setObject:[[NSNumber alloc] initWithBool: false] forKey: keyUseFrozenParts];␊ |
54 | ␉[dict setObject:[[NSString alloc] initWithString: @""] forKey: keyForceBootConfigPath];␊ |
55 | }␊ |
56 | //--------------------------------------------------------------------------␊ |
57 | -(void) loadOptionsFromPreferencesFile: (NSMutableDictionary*) dict␊ |
58 | {␊ |
59 | ␉[mSwapHD01 setIntValue: [[dict objectForKey:␉keySwapHD01] intValue]];␊ |
60 | ␉[mSwapHD02 setIntValue: [[dict objectForKey:␉keySwapHD02] intValue]];␊ |
61 | ␉[mFreezeParts setIntValue: [[dict objectForKey: keyUseFrozenParts] intValue] ];␊ |
62 | ␉id obj = [dict objectForKey: keyForceBootConfigPath];␊ |
63 | ␉[mBootConfigPathText setStringValue: obj ? obj : @"" ];␊ |
64 | ␉int val = (obj && [[mBootConfigPathText stringValue] length] >0 ? 1 : 0);␊ |
65 | ␉[mBootConfigPath setIntValue: val];␊ |
66 | ␉[dict setObject:[[NSString alloc] initWithString: [mBootConfigPathText stringValue]] ␊ |
67 | ␉␉␉ forKey: keyForceBootConfigPath];␊ |
68 | ␉[mBootConfigPathText setEnabled: val ? true : false]; ␊ |
69 | ␉[mBootConfigPathText setEditable: val ? true : false];␊ |
70 | ␊ |
71 | }␊ |
72 | ␊ |
73 | //--------------------------------------------------------------------------␊ |
74 | - (void) loadFrozenParts␊ |
75 | {␊ |
76 | ␉// iterate for all entries to add␊ |
77 | ␉char keyPartN[32] = ""; ␊ |
78 | ␉int k=0;␊ |
79 | ␉␊ |
80 | ␉NSMutableArray* partList = (NSMutableArray*) [PartsInfoMgr parts];␊ |
81 | ␉[partList removeAllObjects];␊ |
82 | ␉␊ |
83 | ␉for (int i=0; i< [[self preferencesParts] count]; i++)␊ |
84 | ␉{␊ |
85 | ␉␉// get the key␊ |
86 | ␉␉snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);␊ |
87 | ␉␉NSString* obj = [[self preferencesParts] objectForKey: [[NSString alloc] initWithUTF8String: keyPartN] ];␊ |
88 | ␉␉// assign this key if valid␊ |
89 | ␉␉if (obj!=nil && [obj length]>0)␊ |
90 | ␉␉{␊ |
91 | ␉␉␉PartitionInfoElement* p = [[PartitionInfoElement alloc] init];␊ |
92 | ␉␉␉// parse string␊ |
93 | ␉␉␉NSArray* words = [obj componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @";"]];␊ |
94 | ␉␉␉k=0;␊ |
95 | ␉␉␉int diskNum=0, partNum=0;␊ |
96 | ␉␉␉for (NSString * word in words)␊ |
97 | ␉␉␉{␊ |
98 | ␉␉␉␉switch (k) {␊ |
99 | ␉␉␉␉␉case 0: // disk number␊ |
100 | ␉␉␉␉␉␉diskNum = [word intValue] ;␊ |
101 | ␉␉␉␉␉␉[ p setDiskNumber:diskNum];␊ |
102 | ␉␉␉␉␉␉break;␊ |
103 | ␉␉␉␉␉case 1: // partition number␊ |
104 | ␉␉␉␉␉␉partNum = [word intValue] ;␊ |
105 | ␉␉␉␉␉␉[ p setPartitionNumber: partNum];␉␉␉␉␉␉␉␊ |
106 | ␉␉␉␉␉␉break;␊ |
107 | ␉␉␉␉␉case 2: // volume label␊ |
108 | ␉␉␉␉␉␉[p setVName: word];␊ |
109 | ␉␉␉␉␉␉[p setVAliasName: word];␊ |
110 | ␉␉␉␉␉␉break;␊ |
111 | ␉␉␉␉␉case 3: // fstype␊ |
112 | ␉␉␉␉␉␉[p setVKind: word];␊ |
113 | ␉␉␉␉␉␉break;␊ |
114 | ␉␉␉␉␉case 4: // vUUID␊ |
115 | ␉␉␉␉␉␉[p setVUUID: word];␊ |
116 | ␉␉␉␉␉␉break;␊ |
117 | ␉␉␉␉␉default:␊ |
118 | ␉␉␉␉␉␉break;␊ |
119 | ␉␉␉␉}␊ |
120 | ␉␉␉␉k++;␊ |
121 | ␉␉␉}␊ |
122 | ␉␉␉␊ |
123 | ␉␉␉[partList addObject:p];␊ |
124 | ␉␉}␊ |
125 | ␉}␊ |
126 | ␉// for mgr to refresh hide and rename attributes␊ |
127 | ␉[PartsInfoMgr hideParts:nil];␊ |
128 | ␉[PartsInfoMgr renameParts:nil];␊ |
129 | ␉␊ |
130 | }␊ |
131 | ␊ |
132 | //--------------------------------------------------------------------------␊ |
133 | - (void) swapDisks: (bool) bSwap src: (int) iSrc dst: (int) iDst;␊ |
134 | {␊ |
135 | ␉//if(!BootProp::instance().isValid()) return;␊ |
136 | ␉if ([mFreezeParts intValue]==0) ␊ |
137 | ␉␉[PartsInfoMgr reload];␊ |
138 | ␉else ␊ |
139 | ␉␉[self loadFrozenParts ];␊ |
140 | ␉␊ |
141 | ␉if (bSwap)␊ |
142 | ␉{␊ |
143 | ␉␉[PartsInfoMgr swapHD:iSrc with: iDst ];␉␊ |
144 | ␉}␊ |
145 | ␉␊ |
146 | ␉[ [self chameleon] selectDefaultPartition];␊ |
147 | ␉␊ |
148 | }␊ |
149 | ␊ |
150 | //--------------------------------------------------------------------------␊ |
151 | - (void) doSwapHD: (int) val save: (bool) doSave src: (int) isrc dst: (int) idst␊ |
152 | {␊ |
153 | ␉␊ |
154 | ␉if( val>0 && ![[BootSetupController instance]->mFreezeParts intValue]) //on␊ |
155 | ␉{␊ |
156 | ␉␉[self swapDisks: true src:isrc dst:idst ];␊ |
157 | ␉}␊ |
158 | ␉else␊ |
159 | ␉{␊ |
160 | ␉␉[self swapDisks: false src:isrc dst:idst ];␊ |
161 | ␉}␊ |
162 | ␉␊ |
163 | ␉if(doSave)␊ |
164 | ␉{␊ |
165 | ␉␉if (isrc==0 && idst==1)␊ |
166 | ␉␉␉[[self preferencesFile] setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD01];␊ |
167 | ␉␉if (isrc==0 && idst==2)␊ |
168 | ␉␉␉[[self preferencesFile] setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD02];␊ |
169 | ␉␉[ [self chameleon] savePreferences:[self preferencesFile] ];␊ |
170 | ␉}␊ |
171 | ␉␊ |
172 | ␉[[[TableViewsController instance] partitionsTable] reloadData];␊ |
173 | ␉[[[TableViewsController instance] partitionsTable] scrollRowToVisible: 0];␊ |
174 | ␉//[self tableViewSelectionDidChange: nil];␊ |
175 | ␉␊ |
176 | }␊ |
177 | ␊ |
178 | ␊ |
179 | //--------------------------------------------------------------------------␊ |
180 | - (IBAction)onInjectPartsToFreeze: (id)sender␊ |
181 | {␊ |
182 | ␉int size = [[PartsInfoMgr parts] count ];␊ |
183 | ␉if (!size) ␊ |
184 | ␉{ // nothing to inject␊ |
185 | ␉␉NSRunAlertPanel(@"Inject Partitions to Freeze Configuration", ␊ |
186 | ␉␉␉␉␉␉@"No current partitions to inject, did you check boot config file ?",@"OK", nil,nil);␊ |
187 | ␉␉return;␊ |
188 | ␉}␊ |
189 | ␉// generate the parts list in preferences proplist␊ |
190 | ␉NSInteger n = NSRunAlertPanel(@"Inject Partitions to Freeze Configuration", ␊ |
191 | ␉␉␉␉␉␉␉␉ @"Are you sure you want to overwrite your Freeze settings with current partition list ?",␊ |
192 | ␉␉␉␉␉␉␉␉ @"OK", @"Cancel",nil);␊ |
193 | ␉if (n==1)␊ |
194 | ␉{␊ |
195 | ␉␉// populate the dictionary with the current partitions␊ |
196 | ␉␉␊ |
197 | ␉␉// empty dictionary and update key in parent:␊ |
198 | ␉␉[[self preferencesFile] removeObjectForKey: keyPartitionsList];␊ |
199 | ␉␉[[self preferencesParts] removeAllObjects ];␊ |
200 | ␉␉␊ |
201 | ␉␉// iterate for all entries to add␊ |
202 | ␉␉char partDesc[256]="";␊ |
203 | ␉␉char keyPartN[32] = "";␊ |
204 | ␉␉for (int i=0; i< size; i++)␊ |
205 | ␉␉{␊ |
206 | ␉␉␉␊ |
207 | ␉␉␉PartitionInfoElement* p = [PartsInfoMgr objectAtIndex:i];␊ |
208 | ␉␉␉␊ |
209 | ␉␉␉// format the partition key and descriptor string␊ |
210 | ␉␉␉snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);␊ |
211 | ␉␉␉␊ |
212 | ␉␉␉snprintf(partDesc, sizeof(partDesc)-1, "%d%c%d%c%s%c%s%c%s",␊ |
213 | ␉␉␉␉␉ [p diskNumber], cPartDescSep,␊ |
214 | ␉␉␉␉␉ [p partitionNumber], cPartDescSep,␊ |
215 | ␉␉␉␉␉ [[p vAliasName] UTF8String], cPartDescSep,␊ |
216 | ␉␉␉␉␉ [[p vKind] UTF8String], cPartDescSep, ␊ |
217 | ␉␉␉␉␉ [[p vUUID] UTF8String]);␊ |
218 | ␉␉␉␊ |
219 | ␉␉␉// write it to the dictionary␊ |
220 | ␉␉␉NSString * key = [[NSString alloc] initWithUTF8String: keyPartN];␊ |
221 | ␉␉␉NSString * desc = [[NSString alloc] initWithUTF8String: partDesc]; ␊ |
222 | ␉␉␉[[self preferencesParts] setObject: desc forKey: key];␊ |
223 | ␉␉␉␊ |
224 | ␉␉␉[p release];␊ |
225 | ␉␉}␊ |
226 | ␉␉[[self preferencesFile] setObject: [self preferencesParts] forKey: keyPartitionsList];␊ |
227 | ␉␉[self savePreferences ];␊ |
228 | ␉}␊ |
229 | }␊ |
230 | ␊ |
231 | //--------------------------------------------------------------------------␊ |
232 | - (IBAction) onForceBootConfigPath: (id) sender␊ |
233 | {␊ |
234 | ␉if (sender == mBootConfigPath)␊ |
235 | ␉{␊ |
236 | ␉␉int val = [mBootConfigPath intValue];␊ |
237 | ␉␉[mBootConfigPathText setEnabled: val ? true : false]; ␊ |
238 | ␉␉[mBootConfigPathText setEditable: val ? true : false];␊ |
239 | ␉␉␊ |
240 | ␉␉if (val) ␊ |
241 | ␉␉{␊ |
242 | ␉␉␉NSString * f = [self selectPlistFile: @"org.chameleon.Boot.plist"];␊ |
243 | ␉␉␉if(f)␊ |
244 | ␉␉␉{␊ |
245 | ␉␉␉␉[mBootConfigPathText setStringValue:f];␊ |
246 | ␉␉␉␉[f release];␊ |
247 | ␉␉␉}␊ |
248 | ␉␉}␊ |
249 | ␉␉␉␊ |
250 | ␊ |
251 | ␉␉[[self preferencesFile] setObject: ␊ |
252 | ␉␉ val ? [mBootConfigPathText stringValue] : [[NSString alloc] initWithUTF8String: ""] ␊ |
253 | ␉␉␉␉␉␉␉␉ forKey: keyForceBootConfigPath];␊ |
254 | ␉␉␊ |
255 | ␉}␊ |
256 | ␉else␊ |
257 | ␉␉[[self preferencesFile] setObject: [mBootConfigPathText stringValue] forKey: keyForceBootConfigPath];␊ |
258 | ␉[self savePreferences ];␊ |
259 | ␊ |
260 | ␉␉␊ |
261 | ␉[PreferencesControllerBase loadOptionsFromBootFile ];␊ |
262 | ␊ |
263 | }␊ |
264 | //--------------------------------------------------------------------------␊ |
265 | -(IBAction) onCheckButtonChange: (NSButton*) sender␊ |
266 | {␊ |
267 | ␉// IMPROVE ME: Should automatize the callback/load/save mechanism ␊ |
268 | ␉// for the preferences file like the bootConfig file␊ |
269 | ␉if (sender == mSwapHD01 || sender == mSwapHD02)␊ |
270 | ␉{␊ |
271 | ␉␉[PartsInfoMgr resetSwapping];␊ |
272 | ␉␉[self doSwapHD: [mSwapHD01 intValue] save:true src:0 dst:1];␊ |
273 | ␉␉[self doSwapHD: [mSwapHD02 intValue] save:true src:0 dst:2];␊ |
274 | ␉}␊ |
275 | ␉else if (sender == mFreezeParts)␊ |
276 | ␉{␊ |
277 | ␉␉bool val = !![sender intValue];␊ |
278 | ␉␉[[self preferencesFile] setObject: [NSNumber numberWithBool: val] forKey: keyUseFrozenParts];␊ |
279 | ␉␉[[self chameleon] savePreferences: [self preferencesFile]];␊ |
280 | ␉␉[ self onCheckButtonChange: mSwapHD01];␊ |
281 | ␉}␊ |
282 | ␉else if (sender == mInjectFrozenParts)␊ |
283 | ␉{␊ |
284 | ␉␉[self onInjectPartsToFreeze: mInjectFrozenParts];␊ |
285 | ␉}␉␊ |
286 | ␉else if (sender == mDefaultPartition || sender == mHidePartition␊ |
287 | ␉␉␉ || sender == mRenamePartition )␊ |
288 | ␉{ // sync with other panels␊ |
289 | ␉␉[self handleSender:sender];␊ |
290 | ␉}␉␊ |
291 | ␉else if (sender == mBootConfigPath || (NSTextField*)sender == mBootConfigPathText)␊ |
292 | ␉{ // sync with other panels␊ |
293 | ␉␉[self onForceBootConfigPath: sender];␊ |
294 | ␉}␉␊ |
295 | ␉else if (sender == mBootExtraPath || (NSTextField*)sender == mBootExtraPathText)␊ |
296 | ␉{ // sync with other panels␊ |
297 | ␉␉[self onForceBootConfigPath: sender];␊ |
298 | ␉}␉␊ |
299 | ␉else if (sender == mBootCdbootPath || (NSTextField*)sender == mBootCdbootPathText)␊ |
300 | ␉{ // sync with other panels␊ |
301 | ␉␉[self onForceBootConfigPath: sender];␊ |
302 | ␉}␉␊ |
303 | ␉// Handle BootOptions generically:␊ |
304 | ␉else ␊ |
305 | ␉␉[self handleSender:sender];␊ |
306 | }␊ |
307 | //--------------------------------------------------------------------------␊ |
308 | -(IBAction) onTextFiedChange: (NSTextField*) sender␊ |
309 | {␊ |
310 | ␉if ( sender == mDefaultPartitionText || sender == mHidePartitionText ||␊ |
311 | ␉␉␉ sender == mRenamePartitionText )␊ |
312 | ␉{␊ |
313 | ␉␉[self handleSender:sender];␊ |
314 | ␊ |
315 | ␉␉[PreferencesControllerBase loadOptionsFromBootFile ];␊ |
316 | ␉␉[PartsInfoMgr reloadWithHideSpec: [mHidePartitionText stringValue] andRenameSpec: [mRenamePartitionText stringValue] ];␊ |
317 | ␊ |
318 | ␉␉[self doSwapHD: [mSwapHD01 intValue] save:true src:0 dst:1];␊ |
319 | ␉␉[self doSwapHD: [mSwapHD02 intValue] save:true src:0 dst:2];␊ |
320 | ␉}␉␊ |
321 | }␊ |
322 | //--------------------------------------------------------------------------␊ |
323 | - (IBAction) onCdBootConfigPath: (id) sender␊ |
324 | {␊ |
325 | ␉if ( sender == mBootCdbootPath)␊ |
326 | ␉{␊ |
327 | ␉␉NSString * dir = [self selectDirectory:@"/"];␊ |
328 | ␉␉if (dir!=nil)␊ |
329 | ␉␉␉[mBootCdbootPathText setStringValue: dir];␊ |
330 | ␉}␊ |
331 | ␉␉␉␊ |
332 | }␊ |
333 | //--------------------------------------------------------------------------␊ |
334 | - (IBAction) onExtraConfigPath: (id) sender␊ |
335 | {␊ |
336 | ␉if ( sender == mBootExtraPath)␊ |
337 | ␉{␊ |
338 | ␉␉NSString * dir = [self selectDirectory:@"/"];␊ |
339 | ␉␉if (dir!=nil)␊ |
340 | ␉␉␉[mBootExtraPathText setStringValue: dir];␊ |
341 | ␉}␊ |
342 | }␊ |
343 | ␊ |
344 | //--------------------------------------------------------------------------␊ |
345 | - (IBAction) onCreateBootCD: (id) sender␊ |
346 | {␊ |
347 | ␉// using rek modified rohan script from Lizard boot cd creator:␊ |
348 | ␉NSString* destination = @"~/Desktop/Chameleon";␊ |
349 | ␉NSString* pathTask = [self getResourcePath:@"cdBootCreator" ofType:@"sh"];␊ |
350 | ␉NSString* pathExtra = [mBootExtraPathText stringValue];␊ |
351 | ␉NSString* pathCdboot = [mBootCdbootPathText stringValue];␊ |
352 | ␉NSMutableArray* arr = [[NSMutableArray alloc] init];␊ |
353 | ␉bool result;␊ |
354 | ␉␊ |
355 | ␉if ( ␊ |
356 | ␉␉pathTask!=nil && [pathTask length] > 0 ␊ |
357 | ␉␉&& pathExtra !=nil && [pathExtra length] > 0 ␊ |
358 | ␉␉&& pathCdboot !=nil && [pathCdboot length] > 0 ␊ |
359 | ␉ )␊ |
360 | ␉{␊ |
361 | ␉␉[arr addObject: pathExtra];␊ |
362 | ␉␉[arr addObject: pathCdboot];␉␊ |
363 | ␉␉result = [self executeTaskAndWaitForTermination: pathTask withArgs: arr];␊ |
364 | ␉␉if (result)␊ |
365 | ␉␉{␊ |
366 | ␉␉␉[self msgBoxInfo:␊ |
367 | ␉␉␉ [NSString stringWithFormat:@"Boot cd creator script successfully executed. BootCD.iso created in %@.", ␊ |
368 | ␉␉␉␉destination]];␊ |
369 | ␉␉}␊ |
370 | ␉␉else␊ |
371 | ␉␉␉[self msgBoxError:@"ERROR: Couldn't execute boot cd creator script successfully!"];␊ |
372 | ␉}␊ |
373 | ␉␊ |
374 | ␉[arr release];␊ |
375 | }␊ |
376 | //--------------------------------------------------------------------------␊ |
377 | + (BootSetupController *)instance { return(gInstance);}␊ |
378 | ␊ |
379 | @end␊ |
380 | |