1 | //␊ |
2 | // PartitionInfoManager.mm␊ |
3 | // ChameleonPrefPane␊ |
4 | //␊ |
5 | // Created by Rekursor on 11-11-13.␊ |
6 | //␊ |
7 | ␊ |
8 | #import "PartitionInfoManager.h"␊ |
9 | #include "ShellProcess.h"␊ |
10 | ␊ |
11 | // Singleton defintion␊ |
12 | ␊ |
13 | static NSMutableArray* sParts = nil;␊ |
14 | ␊ |
15 | @implementation PartsInfoMgr␊ |
16 | ␊ |
17 | -(id) init␊ |
18 | {␊ |
19 | ␉self = [super init];␊ |
20 | ␊ |
21 | ␉return self;␊ |
22 | }␊ |
23 | ␊ |
24 | ␊ |
25 | static NSString *currentCompare = nil;␊ |
26 | static BOOL isPartMatchingNameSpec(PartitionInfoElement* pe, NSUInteger idx, BOOL* stop)␊ |
27 | {␊ |
28 | ␉NSString* s = [currentCompare stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" \t\""]];␊ |
29 | ␊ |
30 | ␉if ([ [pe hdString] isEqual:s] ␊ |
31 | ␉␉ || [ [pe bsdName] isEqual:s]␊ |
32 | ␉␉ || [ [pe vName] isEqual:s]␊ |
33 | ␉␉ || [ [pe vUUID] isEqual:s]␊ |
34 | ␉␉|| [ [pe vAliasName] isEqual: s]␊ |
35 | ␉␉)␊ |
36 | ␉␉*stop␉= YES;␊ |
37 | ␉␊ |
38 | ␉return *stop;␊ |
39 | }␊ |
40 | +(PartitionInfoElement*) partWithName: (NSString*) nameSpec␊ |
41 | {␊ |
42 | ␉return [PartsInfoMgr partWithName:nameSpec outIndex:nil];␊ |
43 | }␊ |
44 | /// Retrieve the part that maches the name ␊ |
45 | +(PartitionInfoElement*) partWithName: (NSString*) nameSpec outIndex:(NSUInteger*) ind␊ |
46 | {␊ |
47 | ␉if (nameSpec==nil || [nameSpec length]==0) return nil;␊ |
48 | ␉currentCompare = nameSpec;␊ |
49 | ␉NSArray * arr = [PartsInfoMgr parts];␊ |
50 | ␉NSUInteger idx = [arr indexOfObjectPassingTest: ␊ |
51 | ␉␉␉␉␉ ^(id pe, NSUInteger idx, BOOL* stop) {return isPartMatchingNameSpec(pe, idx, stop); } ];␊ |
52 | ␊ |
53 | ␉PartitionInfoElement* part = nil;␊ |
54 | ␉␊ |
55 | ␉if (ind!=nil) *ind = idx;␊ |
56 | ␉␊ |
57 | ␉if (idx!= NSNotFound)␊ |
58 | ␉{␊ |
59 | ␉␉part = [arr objectAtIndex: idx];␊ |
60 | ␉}␊ |
61 | ␉return part;␊ |
62 | }␊ |
63 | ␊ |
64 | /// Singleton definition␊ |
65 | +(NSArray*) stringsFromPartSpec: (NSString*) partSpec withCharsFromString:(NSString*) s␊ |
66 | {␊ |
67 | ␉if (partSpec == nil || [partSpec length]==0) return nil;␊ |
68 | ␉NSArray *chunks = [partSpec componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: s] ];␊ |
69 | ␉return chunks;␊ |
70 | }␊ |
71 | ␊ |
72 | /// Retrieve all the partitions␊ |
73 | +(NSArray*) parts␊ |
74 | {␊ |
75 | ␉if (sParts==nil)␊ |
76 | ␉{␊ |
77 | ␉␉//[PartsInfoMgr resetSwapping]; ␊ |
78 | ␉␉NSArray* partArr = [PartitionInfoElement createBSDPartitionList];␊ |
79 | ␉␉sParts = (NSMutableArray*) [PartitionInfoElement extractInfoWithBSDNames: partArr withArray: nil];␊ |
80 | ␉}␊ |
81 | ␉return [sParts retain];␊ |
82 | }␊ |
83 | ␊ |
84 | +(NSUInteger) count␊ |
85 | {␊ |
86 | ␉return [(NSArray*)[PartsInfoMgr parts] count];␊ |
87 | }␊ |
88 | ␊ |
89 | +(PartitionInfoElement*) objectAtIndex:(NSUInteger) index␊ |
90 | {␊ |
91 | ␉␊ |
92 | ␉PartitionInfoElement* elt = [[PartsInfoMgr parts] objectAtIndex:index];␊ |
93 | ␉␊ |
94 | ␉␊ |
95 | ␉return elt;␊ |
96 | }␊ |
97 | ␊ |
98 | static void hideIterateOn(NSString* partSpec, NSString*filter)␊ |
99 | {␊ |
100 | ␉for (currentCompare in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])␊ |
101 | ␉{␊ |
102 | ␉␉PartitionInfoElement * p = (currentCompare!=nil) && [currentCompare length]>0 ? [PartsInfoMgr partWithName: currentCompare] : nil;␊ |
103 | ␉␉if (p!=nil)␊ |
104 | ␉␉{␊ |
105 | ␉␉␉[p setHidden: true];␊ |
106 | ␉␉␉NSLog(@" Found part with name %@ : %@", currentCompare, p);␊ |
107 | ␉␉}␊ |
108 | ␉␉if ((currentCompare!=nil) && [currentCompare length]>0 )␊ |
109 | ␉␉{␊ |
110 | ␉␉␉// remove them so that they dont appear in next search␊ |
111 | ␉␉␉NSMutableString *mut = [NSMutableString stringWithString:partSpec];␊ |
112 | ␉␉␉[ mut replaceOccurrencesOfString: currentCompare withString:@"" options: NSCaseInsensitiveSearch range: NSMakeRange(0, [mut length]) ];␊ |
113 | ␉␉␉partSpec = mut;␊ |
114 | ␉␉}␊ |
115 | ␉}␊ |
116 | }␊ |
117 | ␉␉␉␉␉␉ ␊ |
118 | static NSString *sHideSpec=nil, *sRenSpec=nil;␊ |
119 | ␊ |
120 | /// Update partitions alias with partSpec␊ |
121 | +(void) hideParts: (NSString*) hideSpec␊ |
122 | {␊ |
123 | ␉if (hideSpec!=nil) {␊ |
124 | ␉␉if (sHideSpec!=nil) [sHideSpec release];␊ |
125 | ␉␉sHideSpec = [hideSpec copy];␊ |
126 | ␉}␊ |
127 | ␊ |
128 | ␉// first prioritize strings in double quotes␊ |
129 | ␉if ([sHideSpec rangeOfString:@"\""].location != NSNotFound)␊ |
130 | ␉␉hideIterateOn(sHideSpec, @"\"");␊ |
131 | ␉hideIterateOn(sHideSpec, @"\" \t\n");␊ |
132 | }␊ |
133 | ␊ |
134 | ␊ |
135 | static bool renIterateOn(NSString* partSpec, NSString*filter)␊ |
136 | {␊ |
137 | ␉NSString *first=nil, *second=nil;␊ |
138 | ␉␊ |
139 | ␉for (NSString* s in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])␊ |
140 | ␉{␊ |
141 | ␉␉if ([s length]==0) continue;␊ |
142 | ␉␉if (first==nil)␊ |
143 | ␉␉␉first = s;␊ |
144 | ␉␉else if (second==nil)␊ |
145 | ␉␉{␊ |
146 | ␉␉␉second = s;␊ |
147 | ␉␉␉break;␊ |
148 | ␉␉}␊ |
149 | ␉}␊ |
150 | ␉if (first && second) ␊ |
151 | ␉{␊ |
152 | ␉␉PartitionInfoElement * p = (first!=nil) && [first length]>0 ? [PartsInfoMgr partWithName: first] : nil;␊ |
153 | ␉␉if (p!=nil)␊ |
154 | ␉␉{␊ |
155 | ␉␉␉[p setVAliasName: second];␊ |
156 | ␉␉␉NSLog(@" Rename part %@ -> %@", [p vName], second);␊ |
157 | ␉␉␉return true;␊ |
158 | ␉␉}␊ |
159 | ␉}␊ |
160 | ␉return false;␊ |
161 | }␊ |
162 | ␊ |
163 | /// Update partitions hidden property with partSpec␊ |
164 | +(void) renameParts: (NSString*) renSpec␊ |
165 | {␊ |
166 | ␉if (renSpec!=nil) {␊ |
167 | ␉␉if (sRenSpec!=nil) [sRenSpec release];␊ |
168 | ␉␉sRenSpec = [renSpec copy];␉␊ |
169 | ␉} ␊ |
170 | ␉␊ |
171 | ␉NSArray* sPairs =␊ |
172 | ␉␉[sRenSpec componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @";"] ];␊ |
173 | ␉for (NSString* sPair in sPairs)␊ |
174 | ␉{␊ |
175 | ␉␉bool ret=false;␊ |
176 | ␉␉␊ |
177 | ␉␉if ([sPair rangeOfString:@"\""].location != NSNotFound)␊ |
178 | ␉␉␉ret = renIterateOn(sPair, @"\"");␊ |
179 | ␉␉if(!ret)␊ |
180 | ␉␉␉renIterateOn(sPair, @" \t\n");␊ |
181 | ␉}␊ |
182 | }␊ |
183 | ␊ |
184 | +(void) reload␊ |
185 | {␊ |
186 | ␉[PartsInfoMgr reloadWithHideSpec:nil andRenameSpec:nil];␊ |
187 | }␊ |
188 | ␊ |
189 | +(void) reloadWithHideSpec: (NSString*) hideSpec andRenameSpec:(NSString*) renSpec␊ |
190 | {␊ |
191 | ␉if (hideSpec!=nil) {␊ |
192 | ␉␉if (sHideSpec!=nil) [sHideSpec release];␊ |
193 | ␉␉sHideSpec = [hideSpec copy];␊ |
194 | ␉}␊ |
195 | ␉␊ |
196 | ␉if (renSpec!=nil) {␊ |
197 | ␉␉if (sRenSpec!=nil) [sRenSpec release];␊ |
198 | ␉␉sRenSpec = [renSpec copy];␉␊ |
199 | ␉} ␊ |
200 | ␉␊ |
201 | ␉if (sParts!=nil) [sParts release];␊ |
202 | ␉sParts = nil;␊ |
203 | ␉␊ |
204 | ␉// does auto-generate parts first time␊ |
205 | ␉[PartsInfoMgr hideParts:sHideSpec];␊ |
206 | ␉[PartsInfoMgr renameParts:sRenSpec];␊ |
207 | }␊ |
208 | ␊ |
209 | +(void)resetSwapping␊ |
210 | {␊ |
211 | ␉for (int i=0; i<MAX_HD; i++) PartitionInfoElement.hdRedirTable[i]=i;␊ |
212 | }␊ |
213 | ␊ |
214 | +(void)swapHD: (NSUInteger) src with:(NSUInteger) dst␊ |
215 | {␊ |
216 | ␉if(src > MAX_HD-1 || dst > MAX_HD-1) return;␊ |
217 | ␉PartitionInfoElement.hdRedirTable[src]=dst;␊ |
218 | ␉PartitionInfoElement.hdRedirTable[dst]=src;␊ |
219 | ␉␊ |
220 | }␊ |
221 | ␊ |
222 | +(NSString*) getRenameStringFrom:(NSString*) currentStr andPartition: (PartitionInfoElement*) partInfo␊ |
223 | {␊ |
224 | ␉if (partInfo != nil && currentStr!=nil)␊ |
225 | ␉{␊ |
226 | ␉␉NSMutableString* ms = [[[NSMutableString alloc ] initWithCapacity: 512] autorelease];␊ |
227 | ␉␉␊ |
228 | ␉␉NSArray* sPairs =␊ |
229 | ␉␉ [currentStr componentsSeparatedByCharactersInSet: ␊ |
230 | ␉␉␉[NSCharacterSet characterSetWithCharactersInString: @";"] ];␊ |
231 | ␉␉␊ |
232 | ␉␉if ([sPairs count] > 0 )␊ |
233 | ␉␉{␊ |
234 | ␉␉␉␊ |
235 | ␉␉␉for (NSString* s in sPairs)␊ |
236 | ␉␉␉{␊ |
237 | ␉␉␉␉if ( s==nil || [s length] == 0) continue;␊ |
238 | ␊ |
239 | ␉␉␉␉if ( [ s rangeOfString:[partInfo hdString]].location == NSNotFound )␊ |
240 | ␉␉␉␉{␊ |
241 | ␉␉␉␉␉if ([ms length] > 0) [ ms appendString:@";" ];␊ |
242 | ␉␉␉␉␉[ms appendString: [NSString stringWithFormat:@"%@",s ]];␊ |
243 | ␉␉␉␉}␊ |
244 | ␉␉␉}␊ |
245 | ␉␉␉␊ |
246 | ␉␉}␊ |
247 | ␉␉// now filter the case where name eauls alias -> no rename anymore␉␉␊ |
248 | ␉␉if ( ![[partInfo vName] isEqual: [partInfo vAliasName]])␊ |
249 | ␉␉{␊ |
250 | ␉␉␉if ([ms length] > 0) [ ms appendString:@";" ];␊ |
251 | ␉␉␉[ms appendString: [NSString stringWithFormat:@"%@ \"%@\"" ,␊ |
252 | ␉␉␉␉␉␉␉ [partInfo hdString], [partInfo vAliasName] ]];␊ |
253 | ␉␉}␊ |
254 | ␉␉return ms;␊ |
255 | ␉}␊ |
256 | ␊ |
257 | ␉return nil;␊ |
258 | }␊ |
259 | ␊ |
260 | @end␊ |
261 | |