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