Chameleon Applications

Chameleon Applications Svn Source Tree

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

Archive Download this file

Revision: 78