-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPGNode.m
423 lines (371 loc) · 13.2 KB
/
PGNode.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
/* 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 "PGNode.h"
// Models
#import "PGDocument.h"
#import "PGResourceAdapter.h"
#import "PGContainerAdapter.h"
#import "PGResourceIdentifier.h"
#import "PGBookmark.h"
// Controllers
#import "PGDisplayController.h"
// Other Sources
#import "PGAppKitAdditions.h"
#import "PGFoundationAdditions.h"
NSString *const PGNodeLoadingDidProgressNotification = @"PGNodeLoadingDidProgress";
NSString *const PGNodeReadyForViewingNotification = @"PGNodeReadyForViewing";
NSString *const PGImageRepKey = @"PGImageRep";
NSString *const PGNodeErrorDomain = @"PGNodeError";
enum {
PGNodeNothing = 0,
PGNodeLoading = 1 << 0,
PGNodeReading = 1 << 1,
PGNodeLoadingOrReading = PGNodeLoading | PGNodeReading
}; // PGNodeStatus.
@interface PGNode(Private)
- (void)_setResourceAdapter:(PGResourceAdapter *)adapter;
- (void)_stopLoading;
- (void)_updateMenuItem;
- (void)_updateFileAttributes;
@end
@implementation PGNode
#pragma mark +PGNode
+ (NSArray *)pasteboardTypes
{
return [NSArray arrayWithObjects:NSStringPboardType, NSRTFDPboardType, NSFileContentsPboardType, nil];
}
#pragma mark +NSObject
+ (void)initialize
{
srandom(time(NULL)); // Used by our shuffle sort.
}
#pragma mark -PGNode
- (id)initWithParent:(id<PGNodeParenting>)parent identifier:(PGDisplayableIdentifier *)ident
{
if(!(self = [super init])) return nil;
if(!ident) {
[self release];
return nil;
}
_parent = parent;
_identifier = [ident retain];
_menuItem = [[NSMenuItem alloc] init];
[_menuItem setRepresentedObject:[NSValue valueWithNonretainedObject:self]];
[_menuItem setAction:@selector(jumpToPage:)];
_allowMenuItemUpdates = YES;
[self _updateMenuItem];
[_identifier PG_addObserver:self selector:@selector(identifierIconDidChange:) name:PGDisplayableIdentifierIconDidChangeNotification];
[_identifier PG_addObserver:self selector:@selector(identifierDisplayNameDidChange:) name:PGDisplayableIdentifierDisplayNameDidChangeNotification];
return self;
}
- (PGDisplayableIdentifier *)identifier
{
return [[_identifier retain] autorelease];
}
#pragma mark -
- (PGDataProvider *)dataProvider
{
return [[_dataProvider retain] autorelease];
}
- (void)setDataProvider:(PGDataProvider *)dp
{
NSParameterAssert(dp);
if(dp == _dataProvider) return;
[_dataProvider release];
_dataProvider = [dp retain];
[self reload];
}
- (void)reload
{
_status |= PGNodeLoading;
[_potentialAdapters release];
_potentialAdapters = [[_dataProvider adaptersForNode:self] mutableCopy];
[self _setResourceAdapter:[_potentialAdapters lastObject]];
if([_potentialAdapters count]) [_potentialAdapters removeLastObject];
[_adapter loadIfNecessary];
}
- (PGResourceAdapter *)resourceAdapter
{
return [[_adapter retain] autorelease];
}
- (void)loadFinishedForAdapter:(PGResourceAdapter *)adapter
{
NSParameterAssert(PGNodeLoading & _status);
NSParameterAssert(adapter == _adapter);
[self _stopLoading];
[self readIfNecessary];
}
- (void)fallbackFromFailedAdapter:(PGResourceAdapter *)adapter
{
NSParameterAssert(PGNodeLoading & _status);
NSParameterAssert(adapter == _adapter);
[self _setResourceAdapter:[_potentialAdapters lastObject]];
if(![_potentialAdapters count]) return [self _stopLoading];
[_potentialAdapters removeLastObject];
[_adapter loadIfNecessary];
}
#pragma mark -
- (NSImage *)thumbnail
{
return PGNodeLoading & _status ? nil : [[self resourceAdapter] thumbnail];
}
- (BOOL)isViewable
{
return _viewable;
}
- (PGNode *)viewableAncestor
{
return _viewable ? self : [[self parentNode] viewableAncestor];
}
- (NSMenuItem *)menuItem
{
return [[_menuItem retain] autorelease];
}
- (BOOL)canBookmark
{
return [self isViewable] && [[self identifier] hasTarget];
}
- (PGBookmark *)bookmark
{
return [[[PGBookmark alloc] initWithNode:self] autorelease];
}
#pragma mark -
- (void)becomeViewed
{
[[[self resourceAdapter] activity] prioritize:self];
if(PGNodeReading & _status) return;
_status |= PGNodeReading;
[self readIfNecessary];
}
- (void)readIfNecessary
{
if((PGNodeLoadingOrReading & _status) == PGNodeReading) [_adapter read];
}
- (void)readFinishedWithImageRep:(NSImageRep *)aRep
{
NSParameterAssert((PGNodeLoadingOrReading & _status) == PGNodeReading);
_status &= ~PGNodeReading;
[self PG_postNotificationName:PGNodeReadyForViewingNotification userInfo:[NSDictionary dictionaryWithObjectsAndKeys:aRep, PGImageRepKey, nil]];
}
#pragma mark -
- (void)removeFromDocument
{
if([[self document] node] == self) [[self document] close];
else [[self parentAdapter] removeChild:self];
}
- (void)detachFromTree
{
@synchronized(self) {
_parent = nil;
}
}
- (NSComparisonResult)compare:(PGNode *)node
{
NSParameterAssert(node);
NSParameterAssert([self document]);
PGSortOrder const o = [[self document] sortOrder];
NSInteger const d = PGSortDescendingMask & o ? -1 : 1;
PGDataProvider *const dp1 = [[self resourceAdapter] dataProvider];
PGDataProvider *const dp2 = [[node resourceAdapter] dataProvider];
NSComparisonResult r = NSOrderedSame;
switch(PGSortOrderMask & o) {
case PGUnsorted: return NSOrderedSame;
case PGSortByDateModified: r = [[dp1 dateModified] compare:[dp2 dateModified]]; break;
case PGSortByDateCreated: r = [[dp1 dateCreated] compare:[dp2 dateCreated]]; break;
case PGSortBySize: r = [[dp1 dataLength] compare:[dp2 dataLength]]; break;
case PGSortByKind: r = [[dp1 kindString] compare:[dp2 kindString]]; break;
case PGSortShuffle: return random() & 1 ? NSOrderedAscending : NSOrderedDescending;
}
return (NSOrderedSame == r ? [[[self identifier] displayName] PG_localizedCaseInsensitiveNumericCompare:[[node identifier] displayName]] : r) * d; // If the actual sort order doesn't produce a distinct ordering, then sort by name too.
}
- (BOOL)writeToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types
{
BOOL wrote = NO;
if([types containsObject:NSStringPboardType]) {
if(pboard) {
[pboard addTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pboard setString:[[self identifier] displayName] forType:NSStringPboardType];
}
wrote = YES;
}
NSData *const data = [[self resourceAdapter] canGetData] ? [[self resourceAdapter] data] : nil;
if(data) {
if([types containsObject:NSRTFDPboardType]) {
[pboard addTypes:[NSArray arrayWithObject:NSRTFDPboardType] owner:nil];
NSFileWrapper *const wrapper = [[[NSFileWrapper alloc] initRegularFileWithContents:data] autorelease];
[wrapper setPreferredFilename:[[self identifier] displayName]];
NSAttributedString *const string = [NSAttributedString attributedStringWithAttachment:[[[NSTextAttachment alloc] initWithFileWrapper:wrapper] autorelease]];
[pboard setData:[string RTFDFromRange:NSMakeRange(0, [string length]) documentAttributes:nil] forType:NSRTFDPboardType];
wrote = YES;
}
if([types containsObject:NSFileContentsPboardType]) {
if(pboard) {
[pboard addTypes:[NSArray arrayWithObject:NSFileContentsPboardType] owner:nil];
[pboard setData:data forType:NSFileContentsPboardType];
}
wrote = YES;
}
}
return wrote;
}
- (void)addToMenu:(NSMenu *)menu flatten:(BOOL)flatten
{
[_menuItem PG_removeFromMenu];
if(flatten && [[self resourceAdapter] hasChildren]) {
[[self resourceAdapter] addChildrenToMenu:menu];
} else {
[[self resourceAdapter] addChildrenToMenu:[_menuItem submenu]];
[menu addItem:_menuItem];
}
}
#pragma mark -
- (PGNode *)ancestorThatIsChildOfNode:(PGNode *)aNode
{
PGNode *const parent = [self parentNode];
return aNode == parent ? self : [parent ancestorThatIsChildOfNode:aNode];
}
- (BOOL)isDescendantOfNode:(PGNode *)aNode
{
return [self ancestorThatIsChildOfNode:aNode] != nil;
}
#pragma mark -
- (void)identifierIconDidChange:(NSNotification *)aNotif
{
[self _updateMenuItem];
}
- (void)identifierDisplayNameDidChange:(NSNotification *)aNotif
{
[self _updateMenuItem];
if([[self document] isCurrentSortOrder:PGSortByName]) [[self parentAdapter] noteChildValueForCurrentSortOrderDidChange:self];
[[self document] noteNodeDisplayNameDidChange:self];
}
#pragma mark -
- (void)noteIsViewableDidChange
{
BOOL const showsLoadingIndicator = !!(PGNodeLoading & _status);
BOOL const viewable = showsLoadingIndicator || [_adapter adapterIsViewable];
if(viewable == _viewable) return;
_viewable = viewable;
[[self document] noteNodeIsViewableDidChange:self];
}
#pragma mark -PGNode(Private)
- (void)_setResourceAdapter:(PGResourceAdapter *)adapter
{
if(adapter == _adapter) return;
[[_adapter activity] setParentActivity:nil];
[_adapter release];
_adapter = [adapter retain];
PGActivity *const parentActivity = [[self parentAdapter] activity];
[[_adapter activity] setParentActivity:parentActivity ? parentActivity : [[self document] activity]];
[self _updateFileAttributes];
[self noteIsViewableDidChange];
}
- (void)_stopLoading
{
[_potentialAdapters release];
_potentialAdapters = nil;
_status &= ~PGNodeLoading;
[self noteIsViewableDidChange];
[[self document] noteNodeThumbnailDidChange:self recursively:NO];
}
#pragma mark -
- (void)_updateMenuItem
{
if(!_allowMenuItemUpdates) return;
NSMutableAttributedString *const label = [[[[self identifier] attributedStringWithAncestory:NO] mutableCopy] autorelease];
NSString *info = nil;
NSDate *date = nil;
PGDataProvider *const dp = [[self resourceAdapter] dataProvider];
switch(PGSortOrderMask & [[self document] sortOrder]) {
case PGSortByDateModified: date = [dp dateModified]; break;
case PGSortByDateCreated: date = [dp dateCreated]; break;
case PGSortBySize: info = [[dp dataLength] PG_bytesAsLocalizedString]; break;
case PGSortByKind: info = [dp kindString]; break;
}
if(date && !info) info = [date PG_localizedStringWithDateStyle:kCFDateFormatterShortStyle timeStyle:kCFDateFormatterShortStyle];
if(info) [label appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" (%@)", info] attributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSColor grayColor], NSForegroundColorAttributeName, [NSFont boldSystemFontOfSize:12], NSFontAttributeName, nil]] autorelease]];
[_menuItem setAttributedTitle:label];
}
- (void)_updateFileAttributes
{
[[self parentAdapter] noteChildValueForCurrentSortOrderDidChange:self];
[self _updateMenuItem];
}
#pragma mark -NSObject
- (void)dealloc
{
[[_adapter activity] setParentActivity:nil];
// Using our generic -PG_removeObserver is about twice as slow as removing the observer for the specific objects we care about. When closing huge folders of thousands of files, this makes a big difference. Even now it's still the slowest part.
[_identifier PG_removeObserver:self name:PGDisplayableIdentifierIconDidChangeNotification];
[_identifier PG_removeObserver:self name:PGDisplayableIdentifierDisplayNameDidChangeNotification];
[_identifier release];
[_dataProvider release];
[_potentialAdapters release];
[_adapter release];
[_menuItem release];
[super dealloc];
}
#pragma mark -NSObject(NSObject)
- (NSUInteger)hash
{
return [[self class] hash] ^ [[self identifier] hash];
}
- (BOOL)isEqual:(id)anObject
{
return [anObject isMemberOfClass:[self class]] && PGEqualObjects([self identifier], [(PGNode *)anObject identifier]);
}
#pragma mark -
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@(%@) %p: %@>", [self class], [_adapter class], self, [self identifier]];
}
#pragma mark -<PGResourceAdapting>
- (PGNode *)parentNode
{
return [[_parent containerAdapter] node];
}
- (PGContainerAdapter *)parentAdapter
{
return [_parent containerAdapter];
}
- (PGNode *)rootNode
{
return [self parentNode] ? [[self parentNode] rootNode] : self;
}
- (PGDocument *)document
{
return [_parent document];
}
#pragma mark -
- (void)noteFileEventDidOccurDirect:(BOOL)flag
{
[[self identifier] noteNaturalDisplayNameDidChange];
[self _updateFileAttributes];
[_adapter noteFileEventDidOccurDirect:flag];
}
- (void)noteSortOrderDidChange
{
[self _updateMenuItem];
[_adapter noteSortOrderDidChange];
}
@end