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 | ␊ |
11 | static const char* sPartDescSep = ";"; // cstring version␊ |
12 | static const char cPartDescSep = ';'; // partition descriptor separator␊ |
13 | ␊ |
14 | static BootSetupController *gInstance = NULL;␊ |
15 | ␊ |
16 | @implementation BootSetupController␊ |
17 | ␊ |
18 | //--------------------------------------------------------------------------␊ |
19 | - (id) init␊ |
20 | {␊ |
21 | ␉self = [super init];␊ |
22 | ␉return (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 | -(void) loadOptionsFromPreferencesFile: (NSMutableDictionary*) dict␊ |
55 | {␊ |
56 | ␉[mSwapHD01 setIntValue: [[dict objectForKey:␉keySwapHD01] intValue]];␊ |
57 | ␉[mSwapHD02 setIntValue: [[dict objectForKey:␉keySwapHD02] intValue]];␊ |
58 | ␉[mFreezeParts setIntValue: [[dict objectForKey: keyUseFrozenParts] intValue] ];␊ |
59 | ␉id obj = [dict objectForKey: keyForceBootConfigPath];␊ |
60 | ␉if(obj)␉[mBootConfigPathText setStringValue: obj ];␊ |
61 | ␉else␉[dict setObject:[[NSString alloc] initWithString: @""] forKey: keyForceBootConfigPath];␊ |
62 | ␉int val = ([[mBootConfigPathText stringValue] length] >0 ? 1 : 0);␊ |
63 | ␉[mBootConfigPath setIntValue: val];␊ |
64 | ␊ |
65 | }␊ |
66 | ␊ |
67 | //--------------------------------------------------------------------------␊ |
68 | - (void) loadFrozenParts␊ |
69 | {␊ |
70 | ␉std::vector<PartitionInfo>& partList = partExtractor->editPartList(); //rw␊ |
71 | ␉// iterate for all entries to add␊ |
72 | ␉char keyPartN[32] = ""; ␊ |
73 | ␉char buffer[256]="";␊ |
74 | ␉int k=0;␊ |
75 | ␉␊ |
76 | ␉partList.clear();␊ |
77 | ␉for (int i=0; i< [[self preferencesParts] count]; i++)␊ |
78 | ␉{␊ |
79 | ␉␉// get the key␊ |
80 | ␉␉snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);␊ |
81 | ␉␉NSString* obj = [[self preferencesParts] objectForKey: [[NSString alloc] initWithUTF8String: keyPartN] ];␊ |
82 | ␉␉// assign this key if valid␊ |
83 | ␉␉if (obj!=nil && [obj length]>0)␊ |
84 | ␉␉{␊ |
85 | ␉␉␉PartitionInfo p;␊ |
86 | ␉␉␉// parse string␊ |
87 | ␉␉␉strncpy(buffer, [obj UTF8String], sizeof(buffer)-1);␊ |
88 | ␉␉␉k=0;␊ |
89 | ␉␉␉for(const char* word = strtok(buffer,";"); word; word=strtok(NULL,sPartDescSep),k++)␊ |
90 | ␉␉␉{␊ |
91 | ␉␉␉␉switch (k) {␊ |
92 | ␉␉␉␉␉case 0: // parse disk number␊ |
93 | ␉␉␉␉␉␉if (isdigit(*word)) p.disk(*word-'0');␊ |
94 | ␉␉␉␉␉␉break;␊ |
95 | ␉␉␉␉␉case 1: // parse partition number␊ |
96 | ␉␉␉␉␉␉if (isdigit(*word)) p.partition(*word-'0');␊ |
97 | ␉␉␉␉␉␉break;␊ |
98 | ␉␉␉␉␉case 2: // parse volume label␊ |
99 | ␉␉␉␉␉␉p.label(word);␊ |
100 | ␉␉␉␉␉case 3:␊ |
101 | ␉␉␉␉␉␉p.fsType(word);␊ |
102 | ␉␉␉␉␉␉break;␊ |
103 | ␉␉␉␉␉default:␊ |
104 | ␉␉␉␉␉␉break;␊ |
105 | ␉␉␉␉}␊ |
106 | ␉␉␉}␊ |
107 | ␉␉␉partList.push_back(p);␊ |
108 | ␉␉}␊ |
109 | ␉}␊ |
110 | ␉partExtractor->sortPartList();␊ |
111 | ␉␊ |
112 | }␊ |
113 | ␊ |
114 | //--------------------------------------------------------------------------␊ |
115 | - (void) swapDisks: (bool) bSwap src: (int) iSrc dst: (int) iDst;␊ |
116 | {␊ |
117 | ␉if(!partExtractor || !BootProp::instance().isValid()) return;␊ |
118 | ␉if (bSwap)␊ |
119 | ␉{␊ |
120 | ␉␉partExtractor->swapHD(iSrc, iDst);␉␊ |
121 | ␉}␊ |
122 | ␉␊ |
123 | ␉if ([mFreezeParts intValue]==0) ␊ |
124 | ␉␉partExtractor->extractPartitions();␊ |
125 | ␉else ␊ |
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 | ␉␊ |
136 | ␉if( val>0 && ![[BootSetupController instance]->mFreezeParts intValue]) //on␊ |
137 | ␉{␊ |
138 | ␉␉[self swapDisks: true src:isrc dst:idst ];␊ |
139 | ␉}␊ |
140 | ␉else␊ |
141 | ␉{␊ |
142 | ␉␉[self swapDisks: false src:isrc dst:idst ];␊ |
143 | ␉}␊ |
144 | ␉␊ |
145 | ␉if(doSave)␊ |
146 | ␉{␊ |
147 | ␉␉if (isrc==0 && idst==1)␊ |
148 | ␉␉␉[[self preferencesFile] setObject: [NSNumber numberWithBool: val ? true : false] forKey: keySwapHD01];␊ |
149 | ␉␉if (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 | {␊ |
164 | ␉int size = partExtractor ? partExtractor->partList().size() : 0;␊ |
165 | ␉if (!size) ␊ |
166 | ␉{ // nothing to inject␊ |
167 | ␉␉NSRunAlertPanel(@"Inject Partitions to Freeze Configuration", ␊ |
168 | ␉␉␉␉␉␉@"No current partitions to inject, did you check boot config file ?",@"OK", nil,nil);␊ |
169 | ␉␉return;␊ |
170 | ␉}␊ |
171 | ␉// generate the parts list in preferences proplist␊ |
172 | ␉NSInteger 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);␊ |
175 | ␉if (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␊ |
184 | ␉␉char partDesc[256]="";␊ |
185 | ␉␉char keyPartN[32] = "";␊ |
186 | ␉␉for (int i=0; i< size; i++)␊ |
187 | ␉␉{␊ |
188 | ␉␉␉␊ |
189 | ␉␉␉const PartitionInfo& p =partExtractor->partList()[i];␊ |
190 | ␉␉␉␊ |
191 | ␉␉␉// format the partition key and descriptor string␊ |
192 | ␉␉␉snprintf(keyPartN, sizeof(keyPartN)-1, "partition%02d",i);␊ |
193 | ␉␉␉␊ |
194 | ␉␉␉snprintf(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␊ |
201 | ␉␉␉NSString * key = [[NSString alloc] initWithUTF8String: keyPartN];␊ |
202 | ␉␉␉NSString * 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 | {␊ |
213 | ␉if (sender == mBootConfigPath)␊ |
214 | ␉{␊ |
215 | ␉␉int 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 | ␉}␊ |
223 | ␉else␊ |
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␊ |
236 | ␉if (sender == mSwapHD01 || sender == mSwapHD01)␊ |
237 | ␉{␊ |
238 | ␉␉partExtractor->resetSwapping();␊ |
239 | ␉␉[self doSwapHD: [mSwapHD01 intValue] save:true src:0 dst:1];␊ |
240 | ␉␉[self doSwapHD: [mSwapHD02 intValue] save:true src:0 dst:2];␊ |
241 | ␉}␊ |
242 | ␉else if (sender == mFreezeParts)␊ |
243 | ␉{␊ |
244 | ␉␉bool val = !![sender intValue];␊ |
245 | ␉␉[[self preferencesFile] setObject: [NSNumber numberWithBool: val] forKey: keyUseFrozenParts];␊ |
246 | ␉␉[[self chameleon] savePreferences: [self preferencesFile]];␊ |
247 | ␉␉[ self onCheckButtonChange: mSwapHD01];␊ |
248 | ␉}␊ |
249 | ␉else if (sender == mInjectFrozenParts)␊ |
250 | ␉{␊ |
251 | ␉␉[self onInjectPartsToFreeze: mInjectFrozenParts];␊ |
252 | ␉}␉␊ |
253 | ␉else if (sender == mDefaultPartition || sender == mHidePartition␊ |
254 | ␉␉␉ || sender == mRenamePartition )␊ |
255 | ␉{ // sync with other panels␊ |
256 | ␉␉[self handleSender:sender];␊ |
257 | ␉}␉␊ |
258 | ␉else if (sender == mBootConfigPath || (NSTextField*)sender == mBootConfigPathText)␊ |
259 | ␉{ // sync with other panels␊ |
260 | ␉␉[self onForceBootConfigPath: sender];␊ |
261 | ␉}␉␊ |
262 | ␉// Handle BootOptions generically:␊ |
263 | ␉else ␊ |
264 | ␉␉[self handleSender:sender];␊ |
265 | }␊ |
266 | //--------------------------------------------------------------------------␊ |
267 | -(IBAction) onTextFiedChange: (NSTextField*) sender␊ |
268 | {␊ |
269 | ␉if ( sender == mDefaultPartitionText || sender == mHidePartitionText ||␊ |
270 | ␉␉␉ sender == mRenamePartitionText )␊ |
271 | ␉{␊ |
272 | ␉␉[self handleSender:sender];␊ |
273 | ␊ |
274 | ␉␉[PreferencesControllerBase loadOptionsFromBootFile ];␊ |
275 | ␉␉partExtractor->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 | |