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