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