-
Notifications
You must be signed in to change notification settings - Fork 1
/
ECQTComponent.m
441 lines (356 loc) · 10.9 KB
/
ECQTComponent.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* ECQTComponent.m
* Copyright (c) 2006 Ken Aspeslagh
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
//
// ECQTComponent.m
// QTManager
//
// Created by Ken on 12/15/07.
//
#import "ECQTComponent.h"
#define DISABLED_SUFFIX @" (Disabled)"
@implementation ECQTComponent
NSMutableDictionary* mCache = 0;
#define SET_KEY(key, value) [mParameters setObject:(value) forKey:(key)]
#define SET_KEY_BOOLVAL(key, value) [mParameters setObject:[NSNumber numberWithBool:(value)] forKey:(key)]
#define GET_KEY(key) [mParameters objectForKey:(key)]
#define GET_KEY_BOOLVAL(key) (([mParameters objectForKey:(key)]) ? [[mParameters objectForKey:(key)] boolValue] : NO)
+(id)componentWithPath:(NSString*)path
{
return [[[ECQTComponent alloc] initWithPath:path] autorelease];
}
-(id)initWithPath:(NSString*)path
{
// NSLog(@"init with %@", path);
//#warning TODO(durin42) There's a lot of caching here - I don't think we really need it to be efficient.
if (!mCache)
{
mCache = [[NSMutableDictionary dictionary] retain];
}
NSDictionary* cachedParams = [mCache objectForKey:path];
if (cachedParams)
{
mParameters = [cachedParams retain];
}
else
{
mParameters = [[NSMutableDictionary dictionary] retain];
SET_KEY(@"Location", path);
[self setComponentInfo];
}
SET_KEY_BOOLVAL(@"Enabled", !([path rangeOfString:DISABLED_SUFFIX].length > 0));
[self setComponentScope];
return self;
}
-(void)setComponentInfo
{
short resFile = 0;
NSString* filePath = GET_KEY(@"Location");
BOOL isDirectory = NO;
NSFileManager* fm = [NSFileManager defaultManager];
NSURL* url = [NSURL fileURLWithPath:filePath];
if (![fm fileExistsAtPath:filePath isDirectory:&isDirectory])
{
return;
}
CFBundleRef bundleRef = 0;
if (isDirectory)
{
bundleRef = CFBundleCreate(0, (CFURLRef)url);
if (!bundleRef)
return;
resFile = CFBundleOpenBundleResourceMap(bundleRef);
NSString* cfvers = (NSString*)CFBundleGetValueForInfoDictionaryKey(bundleRef, kCFBundleVersionKey);
if (cfvers)
SET_KEY(@"Version", cfvers);
}
else
{
FSRef ref;
if (CFURLGetFSRef((CFURLRef)url,&ref))
{
resFile = FSOpenResFile(&ref, fsRdPerm);
}
}
NSString* name = @"Unknown";
short saveRes = CurResFile();
if (resFile > 0)
{
UseResFile(resFile);
int x = 0;
int count = CountResources('thng');
NSString* firstType = 0;
BOOL mixed = false;
NSString* singletype = 0;
NSMutableArray* allTypes = [NSMutableArray array];
NSMutableArray* allNames = [NSMutableArray array];
for (x = 1; x <= count; x++)
{
Handle data = Get1IndResource('thng', x);
if (data)
{
ComponentResource* thng = (ComponentResource*)*data;
ComponentDescription desc;
memcpy(&desc, thng, sizeof(ComponentDescription));
Handle nameData = Get1Resource(thng->componentName.resType, thng->componentName.resID);
if (firstType == 0 && singletype)
firstType = singletype;
if (desc.componentType == 'vdig')
singletype = @"Camera Driver";
else if (desc.componentType == 'sgpn')
singletype = @"Camera Driver";
else if (desc.componentType == 'spit')
singletype = @"Exporter";
else if (desc.componentType == 'eat ')
singletype = @"Media Importer";
else if (desc.componentType == 'imdc')
singletype = @"Image Codec";
else if (desc.componentType == 'imco')
singletype = @"Image Codec";
else if (desc.componentType == 'sdec')
singletype = @"Sound Decoder";
else if (desc.componentType == 'scom')
singletype = @"Sound Compressor";
else if (desc.componentType == 'aenc')
singletype = @"Audio Encoder";
else if (desc.componentType == 'adec')
singletype = @"Audio Decoder";
else if (desc.componentType == 'aunt')
singletype = @"Audio Unit";
else if (desc.componentType == 'grip')
singletype = @"Graphics Importer";
else if (desc.componentType == 'shlr')
singletype = @"Sound Handler";
else if (desc.componentType == 'dhlr')
singletype = @"Data Handler";
else if (desc.componentType == 'mhlr')
singletype = @"Media Handler";
else if (desc.componentType == 'tsvc')
singletype = @"Text Service";
else if (desc.componentType == 'osa ')
singletype = @"Scripting Component";
else if (desc.componentType == 'crnc')
singletype = @"Cruncher";
else if (desc.componentType == 'ihlr')
singletype = @"Isochronous Handler";
else if (desc.componentType == 'acca')
singletype = @"Audio Context";
else if (desc.componentType == 'sift')
singletype = @"Sound Converter";
else
singletype = @"Unknown";
if (firstType && singletype && ![firstType isEqualToString:singletype])
{
mixed = true;
}
if (![allTypes containsObject:singletype])
[allTypes addObject:singletype];
// NSLog(@"%@", singletype);
if (nameData && *nameData)
{
name = [[[NSString alloc] initWithBytes:(*nameData) + 1 length:**nameData encoding:NSUTF8StringEncoding] autorelease];
if (!name)
name = [[[NSString alloc] initWithBytes:(*nameData) + 1 length:**nameData encoding:NSUnicodeStringEncoding] autorelease];
}
if (![allNames containsObject:name])
[allNames addObject:name];
if (GetHandleSize(data) >= (Size)sizeof(ExtComponentResource))
{
SET_KEY_BOOLVAL(@"Extended", YES);
NSMutableArray* arches = [NSMutableArray array];
ExtComponentResource* extthng = (ExtComponentResource*)*data;
if (extthng)
{
SET_KEY(@"ComponentVersion", [NSNumber numberWithInt:extthng->componentVersion]);
SET_KEY(@"RegisterFlags", [NSNumber numberWithInt:extthng->componentRegisterFlags]);
int i= 0;
for (i = 0; i < extthng->count; i++)
{
ComponentPlatformInfo* info = &(extthng->platformArray[i]);
NSMutableDictionary* pinfo = [NSMutableDictionary dictionary];
[pinfo setObject:[NSNumber numberWithInt:info->platformType] forKey:@"Platform"];
[pinfo setObject:[NSNumber numberWithInt:info->componentFlags] forKey:@"Flags"];
[arches addObject:pinfo];
}
SET_KEY(@"Arches", arches);
}
}
}
}
name = [GET_KEY(@"Location") lastPathComponent];
if ([[name pathExtension] isEqualToString:@"component"])
name = [name stringByDeletingPathExtension];
SET_KEY(@"Name", name);
SET_KEY(@"Type", singletype);
SET_KEY(@"Types", allTypes);
SET_KEY(@"Names", allNames);
[mCache setObject:mParameters forKey:filePath];
}
UseResFile(saveRes);
if (isDirectory && bundleRef)
{
if (resFile >= 0)
CFBundleCloseBundleResourceMap(bundleRef, resFile);
CFRelease(bundleRef);
}
else if (!isDirectory && resFile >= 0)
{
CloseResFile(resFile);
}
}
-(void)setComponentScope
{
NSString* mLocation = GET_KEY(@"Location");
if ([mLocation hasPrefix:@"/System/"])
mWhere = componentIsSystem;
else if ([mLocation hasPrefix:@"/Library/"])
mWhere = componentIsGlobal;
else if ([mLocation hasPrefix:@"/Users/"])
mWhere = componentIsLocal;
}
-(void)dealloc
{
if (mParameters)
[mParameters release];
[super dealloc];
}
-(NSString*)name
{
return GET_KEY(@"Name");
}
-(NSString*)location
{
return GET_KEY(@"Location");
}
-(NSString*)version
{
if (GET_KEY(@"Version"))
return GET_KEY(@"Version");
else
return GET_KEY(@"ComponentVersion");
}
-(NSString*)type
{
return GET_KEY(@"Type");
}
-(NSString*)types
{
return [GET_KEY(@"Types") componentsJoinedByString:@", "];
}
-(NSString*)names
{
return [GET_KEY(@"Names") componentsJoinedByString:@", "];
}
-(NSString*)whereSort
{
if (mWhere == componentIsLocal)
return @"Home";
else if (mWhere == componentIsGlobal)
return @"Library";
else if (mWhere == componentIsSystem)
return @"System";
return @"";
}
-(NSString*)enabledSort
{
return [NSString stringWithFormat:@"%d", GET_KEY_BOOLVAL(@"Enabled")];
}
-(componentScope)isWhere
{
return mWhere;
}
-(BOOL)extended
{
return GET_KEY_BOOLVAL(@"Extended");
}
-(NSNumber*)compVersion
{
return GET_KEY(@"ComponentVersion");
}
-(NSNumber*)compFlags
{
return GET_KEY(@"RegisterFlags");
}
-(NSString*)arches
{
NSArray* arches = GET_KEY(@"Arches");
NSMutableString* archesString = [NSMutableString string];
int x= 0;
int count = [arches count];
for (x = 0; x < count; x++)
{
NSDictionary* pinfo = [arches objectAtIndex:x];
int platform = [[pinfo objectForKey:@"Platform"] intValue];
if (platform ==platformPowerPCNativeEntryPoint)
[archesString appendFormat:@" PowerPC"];
else if (platform ==platformIA32NativeEntryPoint)
[archesString appendFormat:@" Intel"];
else
[archesString appendFormat:@" %d", x];
}
return archesString;
}
-(NSImage*)where
{
NSImage* image = 0;
if (mWhere == componentIsLocal)
image = [NSImage imageNamed:@"user"];
else if (mWhere == componentIsGlobal)
image = [NSImage imageNamed:@"global"];
else if (mWhere == componentIsSystem)
image = [NSImage imageNamed:@"system"];
return image;
}
-(NSNumber*)componentEnabled
{
return GET_KEY(@"Enabled");
}
-(void)setEnabled:(BOOL)enabled
{
NSString* mLocation = GET_KEY(@"Location");
BOOL mEnabled = GET_KEY_BOOLVAL(@"Enabled");
NSString* componentFile = [mLocation lastPathComponent];
NSString* containingFolderPath = [mLocation stringByDeletingLastPathComponent];
NSString* containingFolder = [containingFolderPath lastPathComponent];
NSString* containingFolderFolderPath = [containingFolderPath stringByDeletingLastPathComponent];
NSString* otherFolder = 0;
if (mEnabled)
otherFolder = [containingFolderFolderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", containingFolder, DISABLED_SUFFIX]];
else
{
NSRange range = [containingFolder rangeOfString:DISABLED_SUFFIX];
if (range.length > 0)
otherFolder = [containingFolderFolderPath stringByAppendingPathComponent:[containingFolder substringToIndex:range.location]];
}
if (!otherFolder)
return;
NSFileManager* fm = [NSFileManager defaultManager];
BOOL successful = YES;
if (![fm fileExistsAtPath:otherFolder isDirectory:nil])
{
successful = [fm createDirectoryAtPath:otherFolder attributes:0];
}
NSString* destFile = [otherFolder stringByAppendingPathComponent:componentFile];
if (successful)
successful = [fm movePath:mLocation toPath:destFile handler:self];
SET_KEY(@"Location", destFile);
SET_KEY_BOOLVAL(@"Enabled", enabled);
}
- (BOOL)fileManager:(NSFileManager *)manager shouldProceedAfterError:(NSDictionary *)errorInfo
{
NSLog(@"QTComponent Manager: Error: %@", errorInfo);
return YES;
}
- (void)fileManager:(NSFileManager *)manager willProcessPath:(NSString *)path
{
}
@end