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