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