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

Archive Download this file

Revision: 50