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 | ␉[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 | ␉␉[partArr release];␊ |
81 | ␉}␊ |
82 | ␉return [sParts retain];␊ |
83 | }␊ |
84 | ␊ |
85 | +(NSUInteger) count␊ |
86 | {␊ |
87 | ␉return [(NSArray*)[PartsInfoMgr parts] count];␊ |
88 | }␊ |
89 | ␊ |
90 | +(PartitionInfoElement*) objectAtIndex:(NSUInteger) index␊ |
91 | {␊ |
92 | ␉␊ |
93 | ␉PartitionInfoElement* elt = [ [[PartsInfoMgr parts] objectAtIndex:index] retain];␊ |
94 | ␉␊ |
95 | ␉␊ |
96 | ␉return elt;␊ |
97 | }␊ |
98 | ␊ |
99 | static void hideIterateOn(NSString* partSpec, NSString*filter)␊ |
100 | {␊ |
101 | ␉for (currentCompare in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])␊ |
102 | ␉{␊ |
103 | ␉␉PartitionInfoElement * p = (currentCompare!=nil) && [currentCompare length]>0 ? [PartsInfoMgr partWithName: currentCompare] : nil;␊ |
104 | ␉␉if (p!=nil)␊ |
105 | ␉␉{␊ |
106 | ␉␉␉[p setHidden: true];␊ |
107 | ␉␉␉NSLog(@" Found part with name %@ : %@", currentCompare, p);␊ |
108 | ␉␉}␊ |
109 | ␉␉if ((currentCompare!=nil) && [currentCompare length]>0 )␊ |
110 | ␉␉{␊ |
111 | ␉␉␉// remove them so that they dont appear in next search␊ |
112 | ␉␉␉NSMutableString *mut = [NSMutableString stringWithString:partSpec];␊ |
113 | ␉␉␉[ mut replaceOccurrencesOfString: currentCompare withString:@"" options: NSCaseInsensitiveSearch range: NSMakeRange(0, [mut length]) ];␊ |
114 | ␉␉␉partSpec = mut;␊ |
115 | ␉␉}␊ |
116 | ␉}␊ |
117 | }␊ |
118 | ␉␉␉␉␉␉ ␊ |
119 | static NSString *sHideSpec=nil, *sRenSpec=nil;␊ |
120 | ␊ |
121 | /// Update partitions alias with partSpec␊ |
122 | +(void) hideParts: (NSString*) hideSpec␊ |
123 | {␊ |
124 | ␉if (hideSpec!=nil) {␊ |
125 | ␉␉if (sHideSpec!=nil) [sHideSpec release];␊ |
126 | ␉␉sHideSpec = [hideSpec copy];␊ |
127 | ␉}␊ |
128 | ␊ |
129 | ␉// first prioritize strings in double quotes␊ |
130 | ␉if ([sHideSpec rangeOfString:@"\""].location != NSNotFound)␊ |
131 | ␉␉hideIterateOn(sHideSpec, @"\"");␊ |
132 | ␉hideIterateOn(sHideSpec, @"\" \t\n");␊ |
133 | }␊ |
134 | ␊ |
135 | ␊ |
136 | static bool renIterateOn(NSString* partSpec, NSString*filter)␊ |
137 | {␊ |
138 | ␉NSString *first=nil, *second=nil;␊ |
139 | ␉␊ |
140 | ␉for (NSString* s in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])␊ |
141 | ␉{␊ |
142 | ␉␉if ([s length]==0) continue;␊ |
143 | ␉␉if (first==nil)␊ |
144 | ␉␉␉first = s;␊ |
145 | ␉␉else if (second==nil)␊ |
146 | ␉␉{␊ |
147 | ␉␉␉second = s;␊ |
148 | ␉␉␉break;␊ |
149 | ␉␉}␊ |
150 | ␉}␊ |
151 | ␉if (first && second) ␊ |
152 | ␉{␊ |
153 | ␉␉PartitionInfoElement * p = (first!=nil) && [first length]>0 ? [PartsInfoMgr partWithName: first] : nil;␊ |
154 | ␉␉if (p!=nil)␊ |
155 | ␉␉{␊ |
156 | ␉␉␉[p setVAliasName: second];␊ |
157 | ␉␉␉NSLog(@" Rename part %@ -> %@", [p vName], second);␊ |
158 | ␉␉␉return true;␊ |
159 | ␉␉}␊ |
160 | ␉}␊ |
161 | ␉return false;␊ |
162 | }␊ |
163 | ␊ |
164 | /// Update partitions hidden property with partSpec␊ |
165 | +(void) renameParts: (NSString*) renSpec␊ |
166 | {␊ |
167 | ␉if (renSpec!=nil) {␊ |
168 | ␉␉if (sRenSpec!=nil) [sRenSpec release];␊ |
169 | ␉␉sRenSpec = [renSpec copy];␉␊ |
170 | ␉} ␊ |
171 | ␉␊ |
172 | ␉NSArray* sPairs =␊ |
173 | ␉␉[sRenSpec componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @";"] ];␊ |
174 | ␉for (NSString* sPair in sPairs)␊ |
175 | ␉{␊ |
176 | ␉␉bool ret=false;␊ |
177 | ␉␉␊ |
178 | ␉␉if ([sPair rangeOfString:@"\""].location != NSNotFound)␊ |
179 | ␉␉␉ret = renIterateOn(sPair, @"\"");␊ |
180 | ␉␉if(!ret)␊ |
181 | ␉␉␉renIterateOn(sPair, @" \t\n");␊ |
182 | ␉}␊ |
183 | }␊ |
184 | ␊ |
185 | +(void) reload␊ |
186 | {␊ |
187 | ␉[PartsInfoMgr reloadWithHideSpec:nil andRenameSpec:nil];␊ |
188 | }␊ |
189 | ␊ |
190 | +(void) reloadWithHideSpec: (NSString*) hideSpec andRenameSpec:(NSString*) renSpec␊ |
191 | {␊ |
192 | ␉if (hideSpec!=nil) {␊ |
193 | ␉␉if (sHideSpec!=nil) [sHideSpec release];␊ |
194 | ␉␉sHideSpec = [hideSpec copy];␊ |
195 | ␉}␊ |
196 | ␉␊ |
197 | ␉if (renSpec!=nil) {␊ |
198 | ␉␉if (sRenSpec!=nil) [sRenSpec release];␊ |
199 | ␉␉sRenSpec = [renSpec copy];␉␊ |
200 | ␉} ␊ |
201 | ␉␊ |
202 | ␉if (sParts!=nil) [sParts release];␊ |
203 | ␉sParts = nil;␊ |
204 | ␉␊ |
205 | ␉// does auto-generate parts first time␊ |
206 | ␉[PartsInfoMgr hideParts:sHideSpec];␊ |
207 | ␉[PartsInfoMgr renameParts:sRenSpec];␊ |
208 | }␊ |
209 | ␊ |
210 | +(void)resetSwapping␊ |
211 | {␊ |
212 | ␉for (int i=0; i<MAX_HD; i++) PartitionInfoElement.hdRedirTable[i]=i;␊ |
213 | }␊ |
214 | ␊ |
215 | +(void)swapHD: (NSUInteger) src with:(NSUInteger) dst␊ |
216 | {␊ |
217 | ␉if(src > MAX_HD-1 || dst > MAX_HD-1) return;␊ |
218 | ␉PartitionInfoElement.hdRedirTable[src]=dst;␊ |
219 | ␉PartitionInfoElement.hdRedirTable[dst]=src;␊ |
220 | ␉␊ |
221 | }␊ |
222 | ␊ |
223 | +(NSString*) getRenameStringFrom:(NSString*) currentStr andPartition: (PartitionInfoElement*) partInfo␊ |
224 | {␊ |
225 | ␉if (partInfo != nil && currentStr!=nil)␊ |
226 | ␉{␊ |
227 | ␉␉NSMutableString* ms = [[NSMutableString alloc ] initWithCapacity: 512];␊ |
228 | ␉␉␊ |
229 | ␉␉NSArray* sPairs =␊ |
230 | ␉␉ [currentStr componentsSeparatedByCharactersInSet: ␊ |
231 | ␉␉␉[NSCharacterSet characterSetWithCharactersInString: @";"] ];␊ |
232 | ␉␉␊ |
233 | ␉␉if ([sPairs count] > 0 )␊ |
234 | ␉␉{␊ |
235 | ␉␉␉␊ |
236 | ␉␉␉for (NSString* s in sPairs)␊ |
237 | ␉␉␉{␊ |
238 | ␉␉␉␉if ( s==nil || [s length] == 0) continue;␊ |
239 | ␊ |
240 | ␉␉␉␉if ( [ s rangeOfString:[partInfo hdString]].location == NSNotFound )␊ |
241 | ␉␉␉␉{␊ |
242 | ␉␉␉␉␉if ([ms length] > 0) [ ms appendString:@";" ];␊ |
243 | ␉␉␉␉␉[ms appendString: [NSString stringWithFormat:@"%@",s ]];␊ |
244 | ␉␉␉␉}␊ |
245 | ␉␉␉}␊ |
246 | ␉␉␉␊ |
247 | ␉␉}␊ |
248 | ␉␉// now filter the case where name eauls alias -> no rename anymore␉␉␊ |
249 | ␉␉if ( ![[partInfo vName] isEqual: [partInfo vAliasName]])␊ |
250 | ␉␉{␊ |
251 | ␉␉␉if ([ms length] > 0) [ ms appendString:@";" ];␊ |
252 | ␉␉␉[ms appendString: [NSString stringWithFormat:@"%@ \"%@\"" ,␊ |
253 | ␉␉␉␉␉␉␉ [partInfo hdString], [partInfo vAliasName] ]];␊ |
254 | ␉␉}␊ |
255 | ␉␉return ms;␊ |
256 | ␉}␊ |
257 | ␊ |
258 | ␉return nil;␊ |
259 | }␊ |
260 | ␊ |
261 | @end␊ |
262 | |