-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPGResourceAdapter.m
565 lines (508 loc) · 18.1 KB
/
PGResourceAdapter.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* Copyright © 2007-2008 The Sequential Project. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal with the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.
3. Neither the name of The Sequential Project nor the names of its
contributors may be used to endorse or promote products derived from
this Software without specific prior written permission.
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 CONTRIBUTORS 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 WITH THE SOFTWARE. */
#import "PGResourceAdapter.h"
// Models
#import "PGDocument.h"
#import "PGNode.h"
#import "PGContainerAdapter.h"
#import "PGResourceIdentifier.h"
// Other
#import "PGCFMutableArray.h"
// Categories
#import "NSImageRepAdditions.h"
#import "NSMenuItemAdditions.h"
#import "NSStringAdditions.h"
NSString *const PGSubstitutedClassKey = @"PGSubstitutedClass";
NSString *const PGBundleTypeFourCCsKey = @"PGBundleTypeFourCCs";
NSString *const PGCFBundleTypeMIMETypesKey = @"CFBundleTypeMIMETypes";
NSString *const PGCFBundleTypeOSTypesKey = @"CFBundleTypeOSTypes";
NSString *const PGCFBundleTypeExtensionsKey = @"CFBundleTypeExtensions";
#define PGThumbnailSize 96.0
static NSConditionLock *PGThumbnailsNeededLock = nil;
static NSMutableArray *PGAdaptersThatRequestedThumbnails = nil;
static NSMutableArray *PGAdaptersWaitingForThumbnails = nil;
static NSMutableArray *PGInfoDictionaries = nil;
@interface PGResourceAdapter (Private)
+ (void)_threaded_generateThumbnails;
+ (void)_setThumbnailWithDictionary:(NSDictionary *)aDict;
- (id)_initWithPriority:(PGMatchPriority)priority info:(NSDictionary *)info;
- (NSComparisonResult)_matchPriorityCompare:(PGResourceAdapter *)adapter;
- (void)_threaded_requestThumbnailGenerationWithInfo:(NSDictionary *)info; // Request on a separate thread so the main one doesn't have to block.
@end
@implementation PGResourceAdapter
#pragma mark Class Methods
+ (NSDictionary *)typesDictionary
{
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"PGResourceAdapterClasses"];
}
+ (NSDictionary *)typeDictionary
{
return [[self typesDictionary] objectForKey:NSStringFromClass(self)];
}
+ (NSArray *)supportedExtensionsWhichMustAlwaysLoad:(BOOL)flag
{
NSMutableArray *const exts = [NSMutableArray array];
NSDictionary *const types = [self typesDictionary];
NSString *classString;
NSEnumerator *const classStringEnum = [types keyEnumerator];
while((classString = [classStringEnum nextObject])) {
id const adapterClass = NSClassFromString(classString);
if(!adapterClass || (flag && ![adapterClass alwaysLoads])) continue;
NSDictionary *const typeDict = [types objectForKey:classString];
[exts addObjectsFromArray:[typeDict objectForKey:PGCFBundleTypeExtensionsKey]];
NSArray *const OSTypes = [typeDict objectForKey:PGCFBundleTypeOSTypesKey];
if(!OSTypes || ![OSTypes count]) continue;
NSString *type;
NSEnumerator *const typeEnum = [OSTypes objectEnumerator];
while((type = [typeEnum nextObject])) [exts addObject:NSFileTypeForHFSTypeCode(PGHFSTypeCodeForPseudoFileType(type))];
}
return exts;
}
+ (NSArray *)adapterClassesInstantiated:(BOOL)flag
forNode:(PGNode *)node
withInfoDicts:(NSArray *)dicts
{
NSParameterAssert(node);
NSParameterAssert(dicts);
NSMutableArray *const adapters = [NSMutableArray array];
NSDictionary *const types = [self typesDictionary];
NSDictionary *info;
NSEnumerator *const infoEnum = [dicts objectEnumerator];
while((info = [infoEnum nextObject])) {
Class const agreedClass = [info objectForKey:PGAdapterClassKey];
if(agreedClass) {
[adapters addObject:(flag ? [[[agreedClass alloc] _initWithPriority:PGMatchByPriorAgreement info:info] autorelease] : agreedClass)];
continue;
}
NSString *classString;
NSEnumerator *const classStringEnum = [types keyEnumerator];
while((classString = [classStringEnum nextObject])) {
Class const class = NSClassFromString(classString);
if(![node shouldLoadAdapterClass:class]) continue;
NSMutableDictionary *const mutableInfo = [[info mutableCopy] autorelease];
PGMatchPriority const p = [class matchPriorityForNode:node withInfo:mutableInfo];
if(!p) continue;
Class altClass = [mutableInfo objectForKey:PGSubstitutedClassKey];
if(!altClass) altClass = class;
[adapters addObject:(flag ? [[[altClass alloc] _initWithPriority:p info:mutableInfo] autorelease] : altClass)];
}
}
if(flag) [adapters sortUsingSelector:@selector(_matchPriorityCompare:)];
return adapters;
}
+ (PGMatchPriority)matchPriorityForNode:(PGNode *)node
withInfo:(NSMutableDictionary *)info
{
if([[info objectForKey:PGDataExistenceKey] intValue] == PGDoesNotExist) return PGNotAMatch;
NSDictionary *const type = [self typeDictionary];
if([[type objectForKey:PGBundleTypeFourCCsKey] containsObject:[info objectForKey:PGFourCCDataKey]]) return PGMatchByFourCC;
if([[type objectForKey:PGCFBundleTypeMIMETypesKey] containsObject:[info objectForKey:PGMIMETypeKey]]) return PGMatchByMIMEType;
if([[type objectForKey:PGCFBundleTypeOSTypesKey] containsObject:[info objectForKey:PGOSTypeKey]]) return PGMatchByOSType;
if([[type objectForKey:PGCFBundleTypeExtensionsKey] containsObject:[[info objectForKey:PGExtensionKey] lowercaseString]]) return PGMatchByExtension;
return PGNotAMatch;
}
+ (BOOL)alwaysLoads
{
return [PGResourceAdapter class] != self;
}
#pragma mark Private Protocol
+ (void)_threaded_generateThumbnails
{
NSParameterAssert(PGThumbnailsNeededLock);
for(;;) {
NSAutoreleasePool *const pool = [[NSAutoreleasePool alloc] init];
[PGThumbnailsNeededLock lockWhenCondition:YES];
PGResourceAdapter *const adapter = [PGAdaptersThatRequestedThumbnails objectAtIndex:0];
[PGAdaptersThatRequestedThumbnails removeObjectAtIndex:0];
NSDictionary *const info = [[[PGInfoDictionaries objectAtIndex:0] retain] autorelease];
[PGInfoDictionaries removeObjectAtIndex:0];
NSImage *const thumbnail = [adapter threaded_generateThumbnailWithInfo:info];
[PGThumbnailsNeededLock unlockWithCondition:!![PGAdaptersThatRequestedThumbnails count]];
[self performSelectorOnMainThread:@selector(_setThumbnailWithDictionary:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:thumbnail, @"Thumbnail", [NSValue valueWithNonretainedObject:adapter], @"AdapterValue", nil] waitUntilDone:NO];
[pool release];
}
}
+ (void)_setThumbnailWithDictionary:(NSDictionary *)aDict
{
PGResourceAdapter *const adapter = [[aDict objectForKey:@"AdapterValue"] nonretainedObjectValue];
if(![PGAdaptersWaitingForThumbnails containsObject:adapter]) return;
[PGAdaptersWaitingForThumbnails removeObject:adapter];
[adapter setThumbnail:[aDict objectForKey:@"Thumbnail"]];
}
#pragma mark Instance Methods
- (PGNode *)node
{
return _node;
}
- (void)setNode:(PGNode *)aNode
{
@synchronized(self) {
if(aNode == _node) return;
_node = aNode;
[self noteIsViewableDidChange];
}
}
#pragma mark -
- (BOOL)adapterIsViewable
{
return NO;
}
- (BOOL)shouldLoad
{
return [[self node] shouldLoadAdapterClass:[self class]];
}
- (PGLoadPolicy)descendentLoadPolicy
{
return PGLoadToMaxDepth;
}
- (void)loadIfNecessary
{
if(![self shouldLoad]) return [[self node] loadFinished];
NSAutoreleasePool *const pool = [[NSAutoreleasePool alloc] init]; // We load recursively, so memory use can be a problem.
[self load];
[pool release];
}
- (void)load
{
[[self node] loadFinished];
}
- (void)fallbackLoad
{
[self load];
}
- (void)read
{
[[self node] readFinishedWithImageRep:nil error:nil];
}
#pragma mark -
- (NSImage *)thumbnail
{
if(_thumbnail) return [[_thumbnail retain] autorelease];
if([self canGenerateThumbnail] && ![PGAdaptersWaitingForThumbnails containsObject:self]) {
if(!PGThumbnailsNeededLock) {
PGThumbnailsNeededLock = [[NSConditionLock alloc] initWithCondition:NO];
PGAdaptersThatRequestedThumbnails = [[NSMutableArray alloc] initWithCallbacks:NULL];
PGAdaptersWaitingForThumbnails = [[NSMutableArray alloc] initWithCallbacks:NULL];
PGInfoDictionaries = [[NSMutableArray alloc] init];
[NSApplication detachDrawingThread:@selector(_threaded_generateThumbnails) toTarget:[PGResourceAdapter class] withObject:nil];
}
[PGAdaptersWaitingForThumbnails addObject:self];
[NSThread detachNewThreadSelector:@selector(_threaded_requestThumbnailGenerationWithInfo:) toTarget:self withObject:[[[self info] copy] autorelease]];
}
return [[self identifier] icon];
}
- (void)setThumbnail:(NSImage *)anImage
{
if(anImage == _thumbnail) return;
[_thumbnail release];
_thumbnail = [anImage retain];
[[self document] noteNodeThumbnailDidChange:[self node]];
}
- (BOOL)canGenerateThumbnail
{
return NO;
}
- (NSImage *)threaded_generateThumbnailWithInfo:(NSDictionary *)info
{
NSData *data;
@synchronized(self) {
data = [[self node] dataWithInfo:info fast:NO];
}
return data ? [self threaded_generateThumbnailWithData:data] : nil;
}
- (NSImage *)threaded_generateThumbnailWithData:(NSData *)data
{
NSImageRep *const rep = [NSImageRep AE_bestImageRepWithData:data];
if(!rep) return nil;
NSSize const originalSize = NSMakeSize([rep pixelsWide], [rep pixelsHigh]);
NSSize const s = PGScaleSizeByFloat(originalSize, MIN(PGThumbnailSize / originalSize.width, PGThumbnailSize / originalSize.height));
NSBitmapImageRep *const thumbRep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:s.width pixelsHigh:s.height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0] autorelease];
if(!thumbRep) return nil;
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithAttributes:[NSDictionary dictionaryWithObject:thumbRep forKey:NSGraphicsContextDestinationAttributeName]]];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[rep drawInRect:(NSRect){NSZeroPoint, s}];
[NSGraphicsContext setCurrentContext:nil];
NSImage *const image = [[[NSImage alloc] initWithSize:s] autorelease];
[image addRepresentation:thumbRep];
return image;
}
- (void)cancelThumbnailGeneration
{
[PGAdaptersWaitingForThumbnails removeObject:self];
[PGThumbnailsNeededLock lock];
unsigned const i = [PGAdaptersThatRequestedThumbnails indexOfObject:self];
if(NSNotFound != i) {
[PGAdaptersThatRequestedThumbnails removeObjectAtIndex:i];
[PGInfoDictionaries removeObjectAtIndex:i];
}
[PGThumbnailsNeededLock unlockWithCondition:!![PGAdaptersThatRequestedThumbnails count]];
}
#pragma mark -
- (void)noteResourceDidChange {}
#pragma mark Private Protocol
- (id)_initWithPriority:(PGMatchPriority)priority
info:(NSDictionary *)info
{
if((self = [self init])) {
_priority = priority;
[[self info] addEntriesFromDictionary:info];
}
return self;
}
- (NSComparisonResult)_matchPriorityCompare:(PGResourceAdapter *)adapter
{
NSParameterAssert([adapter isKindOfClass:[PGResourceAdapter class]]);
if(_priority < adapter->_priority) return NSOrderedAscending;
if(_priority > adapter->_priority) return NSOrderedDescending;
return NSOrderedSame;
}
#pragma mark -
- (void)_threaded_requestThumbnailGenerationWithInfo:(NSDictionary *)info
{
NSAutoreleasePool *const pool = [[NSAutoreleasePool alloc] init];
[PGThumbnailsNeededLock lock];
[PGAdaptersThatRequestedThumbnails addObject:self];
[PGInfoDictionaries addObject:info];
[PGThumbnailsNeededLock unlockWithCondition:YES];
[pool release];
}
#pragma mark PGResourceAdapting Protocol
- (PGNode *)parentNode
{
return [[self parentAdapter] node];
}
- (PGContainerAdapter *)parentAdapter
{
return [_node parentAdapter];
}
- (PGContainerAdapter *)containerAdapter
{
return [self parentAdapter];
}
- (PGNode *)rootNode
{
return [[self node] rootNode];
}
- (PGContainerAdapter *)rootContainerAdapter
{
return [[self parentAdapter] rootContainerAdapter];
}
- (PGDocument *)document
{
return [_node document];
}
#pragma mark -
- (PGResourceIdentifier *)identifier
{
return [[self node] identifier];
}
- (NSMutableDictionary *)info
{
return [[_info retain] autorelease];
}
- (NSData *)data
{
return [[self node] dataWithInfo:_info fast:NO];
}
- (BOOL)canGetData
{
return [[self node] canGetDataWithInfo:_info];
}
- (BOOL)canExtractData
{
return NO;
}
#pragma mark -
- (BOOL)isContainer
{
return NO;
}
- (NSArray *)exifEntries
{
return nil;
}
- (PGOrientation)orientation
{
return [[self document] baseOrientation];
}
- (BOOL)isResolutionIndependent
{
return NO;
}
- (void)clearCache {}
#pragma mark -
- (BOOL)hasViewableNodes
{
return [[self node] isViewable];
}
- (BOOL)hasDataNodes
{
return [[self node] canGetData];
}
- (unsigned)viewableNodeIndex
{
return [[self parentAdapter] viewableIndexOfChild:[self node]];
}
- (unsigned)viewableNodeCount
{
return [[self node] isViewable] ? 1 : 0;
}
#pragma mark -
- (PGNode *)sortedViewableNodeFirst:(BOOL)flag
{
return [self sortedViewableNodeFirst:flag stopAtNode:nil includeSelf:YES];
}
- (PGNode *)sortedViewableNodeFirst:(BOOL)flag
stopAtNode:(PGNode *)descendent
includeSelf:(BOOL)includeSelf
{
return includeSelf && [[self node] isViewable] && [self node] != descendent ? [self node] : nil;
}
- (PGNode *)sortedViewableNodeNext:(BOOL)flag
{
return [self sortedViewableNodeNext:flag includeChildren:YES];
}
- (PGNode *)sortedViewableNodeNext:(BOOL)flag
includeChildren:(BOOL)children
{
return [[self parentAdapter] outwardSearchForward:flag fromChild:[self node] withSelector:@selector(sortedViewableNodeFirst:) context:nil];
}
- (PGNode *)sortedViewableNodeNext:(BOOL)flag
afterRemovalOfChildren:(NSArray *)removedChildren
fromNode:(PGNode *)changedNode
{
if(!removedChildren) return [self node];
PGNode *const potentiallyRemovedAncestor = [[self node] ancestorThatIsChildOfNode:changedNode];
if(!potentiallyRemovedAncestor || NSNotFound == [removedChildren indexOfObjectIdenticalTo:potentiallyRemovedAncestor]) return [self node];
return [[self sortedViewableNodeNext:flag] sortedViewableNodeNext:flag afterRemovalOfChildren:removedChildren fromNode:changedNode];
}
- (PGNode *)sotedFirstViewableNodeInFolderNext:(BOOL)flag
{
PGNode *const node = [[self parentAdapter] outwardSearchForward:flag fromChild:[self node] withSelector:@selector(sortedFirstViewableNodeInFolderFirst:) context:nil];
return node || flag ? node : [[self rootContainerAdapter] sortedViewableNodeFirst:YES stopAtNode:[self node] includeSelf:YES];
}
- (PGNode *)sortedFirstViewableNodeInFolderFirst:(BOOL)flag
{
return nil;
}
- (PGNode *)sortedViewableNodeNext:(BOOL)flag
matchSearchTerms:(NSArray *)terms
{
PGNode *const node = [[self parentAdapter] outwardSearchForward:flag fromChild:[self node] withSelector:@selector(sortedViewableNodeFirst:matchSearchTerms:stopAtNode:) context:terms];
return node ? node : [[self rootContainerAdapter] sortedViewableNodeFirst:flag matchSearchTerms:terms stopAtNode:[self node]];
}
- (PGNode *)sortedViewableNodeFirst:(BOOL)flag
matchSearchTerms:(NSArray *)terms
stopAtNode:(PGNode *)descendent
{
return [[self node] isViewable] && [self node] != descendent && [[[self identifier] displayName] AE_matchesSearchTerms:terms] ? [self node] : nil;
}
#pragma mark -
- (PGNode *)nodeForIdentifier:(PGResourceIdentifier *)ident
{
return ident && [[self identifier] isEqual:ident] ? [self node] : nil;
}
- (PGNode *)ancestorThatIsChildOfNode:(PGNode *)aNode
{
PGNode *const parent = [self parentNode];
return aNode == parent ? [self node] : [parent ancestorThatIsChildOfNode:aNode];
}
- (BOOL)isDescendantOfNode:(PGNode *)aNode
{
return [self ancestorThatIsChildOfNode:aNode] != nil;
}
#pragma mark -
- (void)addMenuItemsToMenu:(NSMenu *)aMenu
{
[[[self node] menuItem] AE_removeFromMenu];
[aMenu addItem:[[self node] menuItem]];
}
#pragma mark -
- (void)noteFileEventDidOccurDirect:(BOOL)flag
{
[[self node] startLoadWithInfo:nil];
}
- (void)noteSortOrderDidChange {}
- (void)noteIsViewableDidChange
{
[[self node] noteIsViewableDidChange];
}
#pragma mark PGLoading Protocol
- (NSString *)loadDescription
{
return [[self identifier] displayName];
}
- (float)loadProgress
{
return 0;
}
- (id<PGLoading>)parentLoad
{
return [self parentAdapter] ? [self parentAdapter] : [PGLoadManager sharedLoadManager];
}
- (NSArray *)subloads
{
return [[_subloads retain] autorelease];
}
- (void)setSubload:(id<PGLoading>)obj
isLoading:(BOOL)flag
{
if(!flag) [_subloads removeObjectIdenticalTo:obj];
else if([_subloads indexOfObjectIdenticalTo:obj] == NSNotFound) [_subloads addObject:obj];
[[self parentLoad] setSubload:[self node] isLoading:[_subloads count] != 0];
}
- (void)prioritizeSubload:(id<PGLoading>)obj
{
unsigned const i = [_subloads indexOfObjectIdenticalTo:[[obj retain] autorelease]];
if(NSNotFound == i) return;
[_subloads removeObjectAtIndex:i];
[_subloads insertObject:obj atIndex:0];
[[self parentLoad] prioritizeSubload:[self node]];
}
- (void)cancelLoad
{
[_subloads makeObjectsPerformSelector:@selector(cancelLoad)];
}
#pragma mark NSObject Protocol
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@ %p: %@>", [self class], self, [self identifier]];
}
#pragma mark NSObject
- (id)init
{
if((self = [super init])) {
_info = [[NSMutableDictionary alloc] init];
_subloads = [[NSMutableArray alloc] initWithCallbacks:NULL];
}
return self;
}
- (void)dealloc
{
[self cancelThumbnailGeneration];
[_info release];
[_thumbnail release];
[_subloads release];
[super dealloc];
}
@end