Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/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
14static const char cPartDescSep = ';'; // partition descriptor separator
15
16static BootSetupController *gInstance = NULL;
17
18@implementation BootSetupController
19
20//--------------------------------------------------------------------------
21- (id) init
22{
23self = [super init];
24return (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] ];
62id obj = [dict objectForKey: keyForceBootConfigPath];
63[mBootConfigPathText setStringValue: obj ? obj : @"" ];
64int 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
77char keyPartN[32] = "";
78int k=0;
79
80NSMutableArray* partList = (NSMutableArray*) [PartsInfoMgr parts];
81[partList removeAllObjects];
82
83for (int i=0; i< [[self preferencesParts] count]; i++)
84{
85// get the key
86snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);
87NSString* obj = [[self preferencesParts] objectForKey: [[NSString alloc] initWithUTF8String: keyPartN] ];
88// assign this key if valid
89if (obj!=nil && [obj length]>0)
90{
91PartitionInfoElement* p = [[PartitionInfoElement alloc] init];
92// parse string
93NSArray* words = [obj componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @";"]];
94k=0;
95int diskNum=0, partNum=0;
96for (NSString * word in words)
97{
98switch (k) {
99case 0: // disk number
100diskNum = [word intValue] ;
101[ p setDiskNumber:diskNum];
102break;
103case 1: // partition number
104partNum = [word intValue] ;
105[ p setPartitionNumber: partNum];
106break;
107case 2: // volume label
108[p setVName: word];
109case 3: // fstype
110[p setVKind: word];
111break;
112case 4: // vUUID
113[p setVUUID: word];
114break;
115default:
116break;
117}
118}
119
120[partList addObject:p];
121}
122}
123// PartitionExtractor::instance().sortPartList();
124
125}
126
127//--------------------------------------------------------------------------
128- (void) swapDisks: (bool) bSwap src: (int) iSrc dst: (int) iDst;
129{
130if(!BootProp::instance().isValid()) return;
131if (bSwap)
132{
133[PartsInfoMgr swapHD:iSrc with: iDst ];
134}
135
136if ([mFreezeParts intValue]==0)
137[PartsInfoMgr reload];
138else
139[self loadFrozenParts ];
140
141[ [self chameleon] selectDefaultPartition];
142
143}
144
145//--------------------------------------------------------------------------
146- (void) doSwapHD: (int) val save: (bool) doSave src: (int) isrc dst: (int) idst
147{
148
149if( val>0 && ![[BootSetupController instance]->mFreezeParts intValue]) //on
150{
151[self swapDisks: true src:isrc dst:idst ];
152}
153else
154{
155[self swapDisks: false src:isrc dst:idst ];
156}
157
158if(doSave)
159{
160if (isrc==0 && idst==1)
161[[self preferencesFile] setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD01];
162if (isrc==0 && idst==2)
163[[self preferencesFile] setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD02];
164[ [self chameleon] savePreferences:[self preferencesFile] ];
165}
166
167[[[TableViewsController instance] partitionsTable] reloadData];
168[[[TableViewsController instance] partitionsTable] scrollRowToVisible: 0];
169//[self tableViewSelectionDidChange: nil];
170
171}
172
173
174//--------------------------------------------------------------------------
175- (IBAction)onInjectPartsToFreeze: (id)sender
176{
177int size = [[PartsInfoMgr parts] count ];
178if (!size)
179{ // nothing to inject
180NSRunAlertPanel(@"Inject Partitions to Freeze Configuration",
181@"No current partitions to inject, did you check boot config file ?",@"OK", nil,nil);
182return;
183}
184// generate the parts list in preferences proplist
185NSInteger n = NSRunAlertPanel(@"Inject Partitions to Freeze Configuration",
186 @"Are you sure you want to overwrite your Freeze settings with current partition list ?",
187 @"OK", @"Cancel",nil);
188if (n==1)
189{
190// populate the dictionary with the current partitions
191
192// empty dictionary and update key in parent:
193[[self preferencesFile] removeObjectForKey: keyPartitionsList];
194[[self preferencesParts] removeAllObjects ];
195
196// iterate for all entries to add
197char partDesc[256]="";
198char keyPartN[32] = "";
199for (int i=0; i< size; i++)
200{
201
202PartitionInfoElement* p = [PartsInfoMgr objectAtIndex:i];
203
204// format the partition key and descriptor string
205snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);
206
207snprintf(partDesc, sizeof(partDesc)-1, "%d%c%d%c%s%c%s",
208 [p diskNumber], cPartDescSep,
209 [p partitionNumber], cPartDescSep,
210 [[p vName] UTF8String], cPartDescSep,
211 [[p vKind] UTF8String], cPartDescSep,
212 [[p vUUID] UTF8String]);
213
214// write it to the dictionary
215NSString * key = [[NSString alloc] initWithUTF8String: keyPartN];
216NSString * desc = [[NSString alloc] initWithUTF8String: partDesc];
217[[self preferencesParts] setObject: desc forKey: key];
218
219[p release];
220}
221[[self preferencesFile] setObject: [self preferencesParts] forKey: keyPartitionsList];
222[self savePreferences ];
223}
224}
225
226//--------------------------------------------------------------------------
227- (IBAction) onForceBootConfigPath: (id) sender
228{
229if (sender == mBootConfigPath)
230{
231int val = [mBootConfigPath intValue];
232[mBootConfigPathText setEnabled: val ? true : false];
233[mBootConfigPathText setEditable: val ? true : false];
234
235if (val)
236{
237NSString * f = [self selectPlistFile: @"org.chameleon.Boot.plist"];
238if(f)
239{
240[mBootConfigPathText setStringValue:f];
241[f release];
242}
243}
244
245
246[[self preferencesFile] setObject:
247 val ? [mBootConfigPathText stringValue] : [[NSString alloc] initWithUTF8String: ""]
248 forKey: keyForceBootConfigPath];
249
250}
251else
252[[self preferencesFile] setObject: [mBootConfigPathText stringValue] forKey: keyForceBootConfigPath];
253[self savePreferences ];
254
255
256[PreferencesControllerBase loadOptionsFromBootFile ];
257
258}
259//--------------------------------------------------------------------------
260-(IBAction) onCheckButtonChange: (NSButton*) sender
261{
262// IMPROVE ME: Should automatize the callback/load/save mechanism
263// for the preferences file like the bootConfig file
264if (sender == mSwapHD01 || sender == mSwapHD02)
265{
266[PartsInfoMgr resetSwapping];
267[self doSwapHD: [mSwapHD01 intValue] save:true src:0 dst:1];
268[self doSwapHD: [mSwapHD02 intValue] save:true src:0 dst:2];
269}
270else if (sender == mFreezeParts)
271{
272bool val = !![sender intValue];
273[[self preferencesFile] setObject: [NSNumber numberWithBool: val] forKey: keyUseFrozenParts];
274[[self chameleon] savePreferences: [self preferencesFile]];
275[ self onCheckButtonChange: mSwapHD01];
276}
277else if (sender == mInjectFrozenParts)
278{
279[self onInjectPartsToFreeze: mInjectFrozenParts];
280}
281else if (sender == mDefaultPartition || sender == mHidePartition
282 || sender == mRenamePartition )
283{ // sync with other panels
284[self handleSender:sender];
285}
286else if (sender == mBootConfigPath || (NSTextField*)sender == mBootConfigPathText)
287{ // sync with other panels
288[self onForceBootConfigPath: sender];
289}
290// Handle BootOptions generically:
291else
292[self handleSender:sender];
293}
294//--------------------------------------------------------------------------
295-(IBAction) onTextFiedChange: (NSTextField*) sender
296{
297if ( sender == mDefaultPartitionText || sender == mHidePartitionText ||
298 sender == mRenamePartitionText )
299{
300[self handleSender:sender];
301
302[PreferencesControllerBase loadOptionsFromBootFile ];
303[PartsInfoMgr reloadWithHideSpec: [mHidePartitionText stringValue] andRenameSpec: [mRenamePartitionText stringValue] ];
304
305[self doSwapHD: [mSwapHD01 intValue] save:true src:0 dst:1];
306[self doSwapHD: [mSwapHD02 intValue] save:true src:0 dst:2];
307}
308}
309
310//--------------------------------------------------------------------------
311+ (BootSetupController *)instance { return(gInstance);}
312
313@end
314

Archive Download this file

Revision: 346