-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPGResourceAdapter.m
482 lines (421 loc) · 14.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
/* Copyright © 2007-2009, The Sequential Project
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the 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.
THIS SOFTWARE IS PROVIDED BY THE SEQUENTIAL PROJECT ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE SEQUENTIAL PROJECT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#import "PGResourceAdapter.h"
// Models
#import "PGDocument.h"
#import "PGNode.h"
#import "PGContainerAdapter.h"
#import "PGResourceIdentifier.h"
#import "PGDataProvider.h"
// Other Sources
#import "PGAppKitAdditions.h"
#import "PGDebug.h"
#import "PGFoundationAdditions.h"
#import "PGGeometry.h"
NSString *const PGPasswordKey = @"PGPassword";
static NSString *const PGBundleTypeFourCCsKey = @"PGBundleTypeFourCCs";
static NSString *const PGLSItemContentTypes = @"LSItemContentTypes";
static NSString *const PGCFBundleTypeMIMETypesKey = @"CFBundleTypeMIMETypes";
static NSString *const PGCFBundleTypeOSTypesKey = @"CFBundleTypeOSTypes";
static NSString *const PGCFBundleTypeExtensionsKey = @"CFBundleTypeExtensions";
static NSString *const PGOrientationKey = @"PGOrientation";
#define PGThumbnailSize 128.0f
@interface PGThumbnailGenerationOperation : NSOperation
{
@private
PGResourceAdapter *_adapter;
}
- (id)initWithResourceAdapter:(PGResourceAdapter *)adapter;
@end
@implementation PGResourceAdapter
#pragma mark +PGResourceAdapter
+ (NSDictionary *)typesDictionary
{
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"PGResourceAdapterClasses"];
}
+ (NSArray *)supportedFileTypes
{
NSMutableArray *const fileTypes = [NSMutableArray array];
NSDictionary *const types = [self typesDictionary];
for(NSString *const classString in types) {
id const adapterClass = NSClassFromString(classString);
if(!adapterClass) continue;
NSDictionary *const typeDict = [types objectForKey:classString];
[fileTypes addObjectsFromArray:[typeDict objectForKey:PGCFBundleTypeExtensionsKey]];
for(NSString *const type in [typeDict objectForKey:PGCFBundleTypeOSTypesKey]) [fileTypes addObject:PGOSTypeToStringQuoted(PGOSTypeFromString(type), YES)];
}
return fileTypes;
}
+ (NSArray *)supportedMIMETypes
{
NSMutableArray *const MIMETypes = [NSMutableArray array];
NSDictionary *const types = [self typesDictionary];
for(NSString *const classString in types) {
id const adapterClass = NSClassFromString(classString);
if(!adapterClass) continue;
NSDictionary *const typeDict = [types objectForKey:classString];
[MIMETypes addObjectsFromArray:[typeDict objectForKey:PGCFBundleTypeMIMETypesKey]];
}
return MIMETypes;
}
#pragma mark -PGResourceAdapter
- (id)initWithNode:(PGNode *)node dataProvider:(PGDataProvider *)provider
{
if((self = [super init])) {
_node = node;
_dataProvider = [provider retain];
_activity = [[PGActivity alloc] initWithOwner:self];
}
return self;
}
@synthesize node = _node;
@synthesize dataProvider = _dataProvider;
#pragma mark -
- (PGContainerAdapter *)containerAdapter
{
return [self parentAdapter];
}
- (PGContainerAdapter *)rootContainerAdapter
{
return [[self parentAdapter] rootContainerAdapter];
}
- (NSUInteger)depth
{
return [[self parentAdapter] depth] + 1;
}
- (PGRecursionPolicy)recursionPolicy
{
PGContainerAdapter *const p = [self parentAdapter];
return p ? [p descendantRecursionPolicy] : PGRecurseToMaxDepth;
}
- (BOOL)shouldRecursivelyCreateChildren
{
switch([self recursionPolicy]) {
case PGRecurseToMaxDepth: return [self depth] <= [[[NSUserDefaults standardUserDefaults] objectForKey:PGMaxDepthKey] unsignedIntegerValue] + 1;
case PGRecurseToAnyDepth: return YES;
case PGRecurseNoFurther: return NO;
}
PGAssertNotReached(@"Invalid recursion policy.");
return NO;
}
#pragma mark -
- (NSData *)data
{
return [[self dataProvider] data];
}
- (BOOL)canGetData
{
return [[self dataProvider] hasData];
}
- (BOOL)hasNodesWithData
{
return [self canGetData];
}
#pragma mark -
- (BOOL)isContainer
{
return NO;
}
- (BOOL)hasChildren
{
return NO;
}
- (BOOL)isSortedFirstViewableNodeOfFolder
{
PGContainerAdapter *const container = [self containerAdapter];
return !container || [container sortedFirstViewableNodeInFolderFirst:YES] == [self node];
}
- (BOOL)hasRealThumbnail
{
return !!_realThumbnail;
}
- (BOOL)isResolutionIndependent
{
return NO;
}
- (BOOL)canSaveData
{
return NO;
}
- (BOOL)hasSavableChildren
{
return NO;
}
#pragma mark -
- (NSUInteger)viewableNodeIndex
{
return [[self parentAdapter] viewableIndexOfChild:[self node]];
}
- (NSUInteger)viewableNodeCount
{
return [[self node] isViewable] ? 1 : 0;
}
- (BOOL)hasViewableNodeCountGreaterThan:(NSUInteger)anInt
{
return [self viewableNodeCount] > anInt;
}
#pragma mark -
@synthesize error = _error;
- (BOOL)adapterIsViewable
{
return NO;
}
- (void)loadIfNecessary
{
NSAutoreleasePool *const pool = [[NSAutoreleasePool alloc] init]; // We load recursively, so memory use can be a problem.
[self load];
[pool release];
}
- (void)load
{
[[self node] loadFinishedForAdapter:self];
}
- (void)read
{
[[self node] readFinishedWithImageRep:nil];
}
#pragma mark -
- (NSImage *)thumbnail
{
NSImage *const realThumbnail = [self realThumbnail];
if(realThumbnail) return realThumbnail;
if([self canGenerateRealThumbnail] && !_thumbnailGenerationOperation) {
_thumbnailGenerationOperation = [[PGThumbnailGenerationOperation alloc] initWithResourceAdapter:self];
[[self document] addOperation:_thumbnailGenerationOperation];
}
return [self fastThumbnail];
}
- (NSImage *)fastThumbnail
{
NSImage *const thumbnail = [[self dataProvider] icon];
[thumbnail setSize:NSMakeSize(PGThumbnailSize, PGThumbnailSize)];
return thumbnail;
}
- (NSImage *)realThumbnail
{
return [[_realThumbnail retain] autorelease];
}
- (void)setRealThumbnail:(NSImage *)anImage
{
if(anImage != _realThumbnail) {
[_realThumbnail release];
_realThumbnail = [anImage retain];
[[self document] noteNodeThumbnailDidChange:[self node] recursively:NO];
}
[_thumbnailGenerationOperation cancel];
[_thumbnailGenerationOperation release];
_thumbnailGenerationOperation = nil;
}
- (BOOL)canGenerateRealThumbnail
{
return NO;
}
- (void)invalidateThumbnail
{
if(![self canGenerateRealThumbnail]) return;
[_realThumbnail release];
_realThumbnail = nil;
[_thumbnailGenerationOperation cancel];
[_thumbnailGenerationOperation release];
_thumbnailGenerationOperation = nil;
(void)[self thumbnail];
}
#pragma mark -
- (NSDictionary *)imageProperties
{
return nil;
}
- (PGOrientation)orientationWithBase:(BOOL)flag
{
return flag ? [[self document] baseOrientation] : PGUpright;
}
- (void)clearCache {}
- (void)addChildrenToMenu:(NSMenu *)menu {}
#pragma mark -
- (PGNode *)nodeForIdentifier:(PGResourceIdentifier *)ident
{
return PGEqualObjects(ident, [[self node] identifier]) ? [self node] : nil;
}
- (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] inclusive:NO 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] resourceAdapter] sortedViewableNodeNext:flag afterRemovalOfChildren:removedChildren fromNode:changedNode];
}
- (PGNode *)sortedFirstViewableNodeInFolderNext:(BOOL)forward inclusive:(BOOL)inclusive
{
PGNode *const node = [[self parentAdapter] outwardSearchForward:forward fromChild:[self node] inclusive:inclusive withSelector:@selector(sortedFirstViewableNodeInFolderFirst:) context:nil];
return node || forward ? node : [[self rootContainerAdapter] sortedViewableNodeFirst:YES stopAtNode:[self node] includeSelf:YES];
}
- (PGNode *)sortedFirstViewableNodeInFolderFirst:(BOOL)flag
{
return nil;
}
- (PGNode *)sortedViewableNodeInFolderFirst:(BOOL)flag
{
PGContainerAdapter *ancestor = [self parentAdapter];
while(ancestor) {
PGNode *const node = [ancestor sortedViewableNodeFirst:flag];
if([self node] != node) return node;
ancestor = [ancestor parentAdapter];
}
return nil;
}
- (PGNode *)sortedViewableNodeNext:(BOOL)flag matchSearchTerms:(NSArray *)terms
{
PGNode *const node = [[self parentAdapter] outwardSearchForward:flag fromChild:[self node] inclusive:NO 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 node] identifier] displayName] PG_matchesSearchTerms:terms] ? [self node] : nil;
}
#pragma mark -
- (void)noteResourceDidChange {}
#pragma mark -NSObject
- (id)init
{
PGAssertNotReached(@"Invalid initializer, use -initWithNode:dataProvider: instead.");
[self release];
return nil;
}
- (void)dealloc
{
[_activity invalidate];
[_dataProvider release];
[_activity release];
[_error release];
[_realThumbnail release];
[_thumbnailGenerationOperation release];
[super dealloc];
}
#pragma mark -<NSObject>
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@ %p: %@>", [self class], self, [self dataProvider]];
}
#pragma mark -<PGActivityOwner>
- (PGActivity *)activity
{
return [[_activity retain] autorelease];
}
- (NSString *)descriptionForActivity:(PGActivity *)activity
{
return [[[self node] identifier] displayName];
}
#pragma mark -<PGResourceAdapting>
- (PGNode *)parentNode
{
return [[self parentAdapter] node];
}
- (PGContainerAdapter *)parentAdapter
{
return [_node parentAdapter];
}
- (PGNode *)rootNode
{
return [[self node] rootNode];
}
- (PGDocument *)document
{
return [_node document];
}
#pragma mark -
- (void)noteFileEventDidOccurDirect:(BOOL)flag {}
- (void)noteSortOrderDidChange {}
@end
@implementation PGThumbnailGenerationOperation
#pragma mark -PGThumbnailGenerationOperation
- (id)initWithResourceAdapter:(PGResourceAdapter *)adapter
{
if((self = [super init])) {
_adapter = [adapter retain];
}
return self;
}
#pragma mark -NSOperation
- (void)main
{
if([self isCancelled]) return;
NSImageRep *const rep = [_adapter threaded_thumbnailRepWithSize:NSMakeSize(PGThumbnailSize, PGThumbnailSize)];
if(!rep || [self isCancelled]) return;
NSImage *const image = [[[NSImage alloc] initWithSize:NSMakeSize([rep pixelsWide], [rep pixelsHigh])] autorelease];
[image addRepresentation:rep];
if([self isCancelled]) return;
[_adapter performSelectorOnMainThread:@selector(setRealThumbnail:) withObject:image waitUntilDone:NO];
}
#pragma mark -NSObject
- (void)dealloc
{
[_adapter release];
[super dealloc];
}
@end
@implementation PGDataProvider(PGResourceAdapterLoading)
- (NSArray *)adapterClassesForNode:(PGNode *)node
{
NSParameterAssert(node);
NSDictionary *const types = [PGResourceAdapter typesDictionary];
NSMutableDictionary *const adapterByPriority = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInteger:0], [PGResourceAdapter class], nil];
for(NSString *const classString in types) {
Class const class = NSClassFromString(classString);
if(!class) continue;
NSDictionary *const typeDict = [types objectForKey:classString];
NSUInteger const p = [self matchPriorityForTypeDictionary:typeDict];
if(p) [adapterByPriority setObject:[NSNumber numberWithUnsignedInteger:p] forKey:(id<NSCopying>)class];
}
return [adapterByPriority keysSortedByValueUsingSelector:@selector(compare:)];
}
- (NSArray *)adaptersForNode:(PGNode *)node
{
NSMutableArray *const adapters = [NSMutableArray array];
for(Class const class in [self adapterClassesForNode:node]) [adapters addObject:[[[class alloc] initWithNode:node dataProvider:self] autorelease]];
return adapters;
}
- (NSUInteger)matchPriorityForTypeDictionary:(NSDictionary *)dict
{
if([[dict objectForKey:PGBundleTypeFourCCsKey] containsObject:[self fourCCData]]) return 5;
if([[dict objectForKey:PGLSItemContentTypes] containsObject:[self UTIType]]) return 4;
if([[dict objectForKey:PGCFBundleTypeMIMETypesKey] containsObject:[self MIMEType]]) return 3;
if([[dict objectForKey:PGCFBundleTypeOSTypesKey] containsObject:PGOSTypeToStringQuoted([self typeCode], NO)]) return 2;
if([[dict objectForKey:PGCFBundleTypeExtensionsKey] containsObject:[[self extension] lowercaseString]]) return 1;
return 0;
}
@end