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