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

Archive Download this file

Revision: 54