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
11static const char* sPartDescSep = ";"; // cstring version
12static const char cPartDescSep = ';'; // partition descriptor separator
13
14static BootSetupController *gInstance = NULL;
15
16@implementation BootSetupController
17
18//--------------------------------------------------------------------------
19- (id) init
20{
21self = [super init];
22return (gInstance = self);
23}
24
25//--------------------------------------------------------------------------
26- (void) addOptionsDesc
27{
28 BootProp::instance().addOptionDesc(mDefaultPartition, mDefaultPartitionText, OptionString, "Default Partition", "");
29 BootProp::instance().addOptionDesc(mHidePartition , mHidePartitionText, OptionString, "Hide Partition", "");
30 BootProp::instance().addOptionDesc(mRenamePartition , mRenamePartitionText, OptionString, "Rename Partition", "");
31}
32
33//--------------------------------------------------------------------------
34- (void) refreshLockStates
35{
36// non boot config options (not automatized)
37 [PreferencesControllerBase refreshLockState: mSwapHD01 ];
38 [PreferencesControllerBase refreshLockState: mSwapHD02 ];
39 [PreferencesControllerBase refreshLockState: mFreezeParts ];
40 [PreferencesControllerBase refreshLockState: mInjectFrozenParts ];
41 [PreferencesControllerBase refreshLockState: mBootConfigPath ];
42}
43
44//--------------------------------------------------------------------------
45- (void) setDefaultsValues: (NSMutableDictionary*) dict
46{
47[dict setObject: [[NSNumber alloc] initWithBool: false] forKey: keySwapHD01];
48[dict setObject: [[NSNumber alloc] initWithBool: false] forKey: keySwapHD02];
49[dict setObject:[[NSNumber alloc] initWithBool: false] forKey: keyUseFrozenParts];
50}
51
52//--------------------------------------------------------------------------
53-(void) loadOptionsFromPreferencesFile: (NSMutableDictionary*) dict
54{
55[mSwapHD01 setIntValue: [[dict objectForKey:keySwapHD01] intValue]];
56[mSwapHD02 setIntValue: [[dict objectForKey:keySwapHD02] intValue]];
57[mFreezeParts setIntValue: [[dict objectForKey: keyUseFrozenParts] intValue] ];
58id obj = [dict objectForKey: keyForceBootConfigPath];
59[mBootConfigPathText setStringValue: obj ];
60int val = ([[mBootConfigPathText stringValue] length] >0 ? 1 : 0);
61[mBootConfigPath setIntValue: val];
62
63}
64
65//--------------------------------------------------------------------------
66- (void) loadFrozenParts
67{
68std::vector<PartitionInfo>& partList = partExtractor->editPartList(); //rw
69// iterate for all entries to add
70char keyPartN[32] = "";
71char buffer[256]="";
72int k=0;
73
74partList.clear();
75for (int i=0; i< [[self preferencesParts] count]; i++)
76{
77// get the key
78snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);
79NSString* obj = [[self preferencesParts] objectForKey: [[NSString alloc] initWithUTF8String: keyPartN] ];
80// assign this key if valid
81if (obj!=nil && [obj length]>0)
82{
83PartitionInfo p;
84// parse string
85strncpy(buffer, [obj UTF8String], sizeof(buffer)-1);
86k=0;
87for(const char* word = strtok(buffer,";"); word; word=strtok(NULL,sPartDescSep),k++)
88{
89switch (k) {
90case 0: // parse disk number
91if (isdigit(*word)) p.disk(*word-'0');
92break;
93case 1: // parse partition number
94if (isdigit(*word)) p.partition(*word-'0');
95break;
96case 2: // parse volume label
97p.label(word);
98case 3:
99p.fsType(word);
100break;
101default:
102break;
103}
104}
105partList.push_back(p);
106}
107}
108partExtractor->sortPartList();
109
110}
111
112//--------------------------------------------------------------------------
113- (void) swapDisks: (bool) bSwap src: (int) iSrc dst: (int) iDst;
114{
115if(!partExtractor || !BootProp::instance().isValid()) return;
116if (bSwap)
117{
118partExtractor->swapHD(iSrc, iDst);
119}
120
121if ([mFreezeParts intValue]==0)
122partExtractor->extractPartitions();
123else
124[self loadFrozenParts ];
125
126[ [self chameleon] selectDefaultPartition];
127
128}
129
130//--------------------------------------------------------------------------
131- (void) doSwapHD: (int) val save: (bool) doSave src: (int) isrc dst: (int) idst
132{
133
134if( val>0 && ![[BootSetupController instance]->mFreezeParts intValue]) //on
135{
136[self swapDisks: true src:isrc dst:idst ];
137}
138else
139{
140[self swapDisks: false src:isrc dst:idst ];
141}
142
143if(doSave)
144{
145if (isrc==0 && idst==1)
146[[self preferencesFile] setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD01];
147if (isrc==0 && idst==2)
148[[self preferencesFile] setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD02];
149[ [self chameleon] savePreferences:[self preferencesFile] ];
150}
151
152[[[ChameleonPrefPane instance] partitionsTable] reloadData];
153[[[ChameleonPrefPane instance] partitionsTable] scrollRowToVisible: 0];
154//[self tableViewSelectionDidChange: nil];
155
156}
157
158
159//--------------------------------------------------------------------------
160- (IBAction)onInjectPartsToFreeze: (id)sender
161{
162int size = partExtractor ? partExtractor->partList().size() : 0;
163if (!size)
164{ // nothing to inject
165NSRunAlertPanel(@"Inject Partitions to Freeze Configuration",
166@"No current partitions to inject, did you check boot config file ?",@"OK", nil,nil);
167return;
168}
169// generate the parts list in preferences proplist
170NSInteger n = NSRunAlertPanel(@"Inject Partitions to Freeze Configuration",
171 @"Are you sure you want to overwrite your Freeze settings with current partition list ?",
172 @"OK", @"Cancel",nil);
173if (n==1)
174{
175// populate the dictionary with the current partitions
176
177// empty dictionary and update key in parent:
178[[self preferencesFile] removeObjectForKey: keyPartitionsList];
179[[self preferencesParts] removeAllObjects ];
180
181// iterate for all entries to add
182char partDesc[256]="";
183char keyPartN[32] = "";
184for (int i=0; i< size; i++)
185{
186
187const PartitionInfo& p =partExtractor->partList()[i];
188
189// format the partition key and descriptor string
190snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);
191
192snprintf(partDesc, sizeof(partDesc)-1, "%d%c%d%c%s%c%s",
193 p.disk(), cPartDescSep,
194 p.partition(), cPartDescSep,
195 p.clabel(), cPartDescSep,
196 p.cfsType());
197
198// write it to the dictionary
199NSString * key = [[NSString alloc] initWithUTF8String: keyPartN];
200NSString * desc = [[NSString alloc] initWithUTF8String: partDesc];
201[[self preferencesParts] setObject: desc forKey: key];
202}
203[[self preferencesFile] setObject: [self preferencesParts] forKey: keyPartitionsList];
204[self savePreferences ];
205}
206}
207
208//--------------------------------------------------------------------------
209- (IBAction) onForceBootConfigPath: (id) sender
210{
211if (sender == mBootConfigPath)
212{
213int val = [mBootConfigPath intValue];
214[mBootConfigPathText setEnabled: val ? true : false];
215[mBootConfigPathText setEditable: val ? true : false];
216[[self preferencesFile] setObject:
217 val ? [mBootConfigPathText stringValue] : [[NSString alloc] initWithUTF8String: ""]
218 forKey: keyForceBootConfigPath];
219
220}
221else
222[[self preferencesFile] setObject: [mBootConfigPathText stringValue] forKey: keyForceBootConfigPath];
223[self savePreferences ];
224
225
226[PreferencesControllerBase loadOptionsFromBootFile ];
227
228}
229//--------------------------------------------------------------------------
230-(IBAction) onCheckButtonChange: (NSButton*) sender
231{
232// IMPROVE ME: Should automatize the callback/load/save mechanism
233// for the preferences file like the bootConfig file
234if (sender == mSwapHD01 || sender == mSwapHD01)
235{
236partExtractor->resetSwapping();
237[self doSwapHD: [mSwapHD01 intValue] save:true src:0 dst:1];
238[self doSwapHD: [mSwapHD02 intValue] save:true src:0 dst:2];
239}
240else if (sender == mFreezeParts)
241{
242bool val = !![sender intValue];
243[[self preferencesFile] setObject: [NSNumber numberWithBool: val] forKey: keyUseFrozenParts];
244[[self chameleon] savePreferences: [self preferencesFile]];
245[ self onCheckButtonChange: mSwapHD01];
246}
247else if (sender == mInjectFrozenParts)
248{
249[self onInjectPartsToFreeze: mInjectFrozenParts];
250}
251else if (sender == mDefaultPartition || sender == mHidePartition
252 || sender == mRenamePartition )
253{ // sync with other panels
254[self handleSender:sender];
255}
256else if (sender == mBootConfigPath || (NSTextField*)sender == mBootConfigPathText)
257{ // sync with other panels
258[self onForceBootConfigPath: sender];
259}
260// Handle BootOptions generically:
261else
262[self handleSender:sender];
263}
264//--------------------------------------------------------------------------
265-(IBAction) onTextFiedChange: (NSTextField*) sender
266{
267if ( sender == mDefaultPartitionText || sender == mHidePartitionText ||
268 sender == mRenamePartitionText )
269{
270[self handleSender:sender];
271
272[PreferencesControllerBase loadOptionsFromBootFile ];
273partExtractor->extractPartitions(
274 [[mHidePartitionText stringValue] UTF8String],
275 [[mRenamePartitionText stringValue] UTF8String]
276 );
277[self doSwapHD: [mSwapHD01 intValue] save:true src:0 dst:1];
278[self doSwapHD: [mSwapHD02 intValue] save:true src:0 dst:2];
279}
280}
281
282//--------------------------------------------------------------------------
283+ (BootSetupController *)instance { return(gInstance);}
284
285@end
286

Archive Download this file

Revision: 51