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