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 | /// Update paritions alias with partSpec␊ |
121 | +(void) hideParts: (NSString*) partSpec␊ |
122 | {␊ |
123 | ␉// first prioritize strings in double quotes␊ |
124 | ␉if ([partSpec rangeOfString:@"\""].location != NSNotFound)␊ |
125 | ␉␉hideIterateOn(partSpec, @"\"");␊ |
126 | ␉hideIterateOn(partSpec, @"\" \t\n");␊ |
127 | }␊ |
128 | ␊ |
129 | ␊ |
130 | static bool renIterateOn(NSString* partSpec, NSString*filter)␊ |
131 | {␊ |
132 | ␉NSString *first=nil, *second=nil;␊ |
133 | ␉␊ |
134 | ␉for (NSString* s in [PartsInfoMgr stringsFromPartSpec:partSpec withCharsFromString: filter])␊ |
135 | ␉{␊ |
136 | ␉␉if ([s length]==0) continue;␊ |
137 | ␉␉if (first==nil)␊ |
138 | ␉␉␉first = s;␊ |
139 | ␉␉else if (second==nil)␊ |
140 | ␉␉{␊ |
141 | ␉␉␉second = s;␊ |
142 | ␉␉␉break;␊ |
143 | ␉␉}␊ |
144 | ␉}␊ |
145 | ␉if (first && second) ␊ |
146 | ␉{␊ |
147 | ␉␉PartitionInfoElement * p = (first!=nil) && [first length]>0 ? [PartsInfoMgr partWithName: first] : nil;␊ |
148 | ␉␉if (p!=nil)␊ |
149 | ␉␉{␊ |
150 | ␉␉␉[p setVAliasName: second];␊ |
151 | ␉␉␉NSLog(@" Rename part %@ -> %@", [p vName], second);␊ |
152 | ␉␉␉return true;␊ |
153 | ␉␉}␊ |
154 | ␉}␊ |
155 | ␉return false;␊ |
156 | }␊ |
157 | ␊ |
158 | /// Update parttiions hidden property with partSpec␊ |
159 | +(void) renameParts: (NSString*) partSpec␊ |
160 | {␊ |
161 | ␉NSArray* sPairs =␊ |
162 | ␉␉[partSpec componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @";"] ];␊ |
163 | ␉for (NSString* sPair in sPairs)␊ |
164 | ␉{␊ |
165 | ␉␉bool ret=false;␊ |
166 | ␉␉␊ |
167 | ␉␉if ([sPair rangeOfString:@"\""].location != NSNotFound)␊ |
168 | ␉␉␉ret = renIterateOn(sPair, @"\"");␊ |
169 | ␉␉if(!ret)␊ |
170 | ␉␉␉renIterateOn(sPair, @" \t\n");␊ |
171 | ␉}␊ |
172 | }␊ |
173 | ␊ |
174 | +(void) reload␊ |
175 | {␊ |
176 | ␉[PartsInfoMgr reloadWithHideSpec:nil andRenameSpec:nil];␊ |
177 | }␊ |
178 | ␊ |
179 | static NSString *sHideSpec=nil, *sRenSpec=nil;␊ |
180 | +(void) reloadWithHideSpec: (NSString*) hideSpec andRenameSpec:(NSString*) renSpec␊ |
181 | {␊ |
182 | ␉if (hideSpec!=nil) {␊ |
183 | ␉␉if (sHideSpec!=nil) [sHideSpec release];␊ |
184 | ␉␉sHideSpec = [hideSpec copy];␊ |
185 | ␉}␊ |
186 | ␉␊ |
187 | ␉if (renSpec!=nil) {␊ |
188 | ␉␉if (sRenSpec!=nil) [sRenSpec release];␊ |
189 | ␉␉sRenSpec = [renSpec copy];␉␊ |
190 | ␉} ␊ |
191 | ␉␊ |
192 | ␉if (sParts!=nil) [sParts release];␊ |
193 | ␉sParts = nil;␊ |
194 | ␉␊ |
195 | ␉// does auto-generate parts first time␊ |
196 | ␉[PartsInfoMgr hideParts:sHideSpec];␊ |
197 | ␉[PartsInfoMgr renameParts:sRenSpec];␊ |
198 | }␊ |
199 | ␊ |
200 | +(void)resetSwapping␊ |
201 | {␊ |
202 | ␉for (int i=0; i<MAX_HD; i++) PartitionInfoElement.hdRedirTable[i]=i;␊ |
203 | }␊ |
204 | ␊ |
205 | +(void)swapHD: (NSUInteger) src with:(NSUInteger) dst␊ |
206 | {␊ |
207 | ␉if(src < 0 || src > MAX_HD-1 || dst < 0 || dst > MAX_HD-1) return;␊ |
208 | ␉PartitionInfoElement.hdRedirTable[src]=dst;␊ |
209 | ␉PartitionInfoElement.hdRedirTable[dst]=src;␊ |
210 | ␉␊ |
211 | }␊ |
212 | ␊ |
213 | ␊ |
214 | @end␊ |
215 | |