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