Chameleon Applications

Chameleon Applications Svn Source Tree

Root/branches/ErmaC/ChameleonPrefPane/Sources/BootSetupController.mm

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

Archive Download this file

Revision: 396