-
Notifications
You must be signed in to change notification settings - Fork 1
/
TLFilePhotoSource.m
306 lines (259 loc) · 8.29 KB
/
TLFilePhotoSource.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
//
// TLFilePhotoSource.m
// Geotagalog
//
// Created by Nathan Vander Wilt on 6/5/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLFilePhotoSource.h"
#import "TLFileSourceItem.h"
#import "TLMainThreadPerformer.h"
#import "NSFileManager+TLExtensions.h"
#import "TLRandom.h"
#ifdef __LP64__
static const NSUInteger TLFilePhotoSourceItemLimit = 2500;
#else
static const NSUInteger TLFilePhotoSourceItemLimit = 1000;
#endif
extern NSArray* TLNSArrayShuffle(NSArray* array);
@interface TLFilePhotoSource ()
@property (nonatomic, copy) NSString* name;
@property (nonatomic, retain) NSImage* icon;
@property (nonatomic, retain) NSError* error;
@property (nonatomic, assign, readwrite) BOOL isCurrent;
@property (nonatomic, assign, readwrite) BOOL isWorking;
@property (retain) NSOperationQueue* workQueue;
@property (nonatomic, retain) NSOperation* finalOperation;
@property (nonatomic, retain) NSMutableSet* directlyAddedPaths;
@property (nonatomic, retain) NSMutableSet* allPaths;
- (void)updateName;
- (void)enqueuePaths:(NSSet*)filePaths;
@end
static NSString* const TLFPSObservationContext = @"TLFilePhotoSource_ObservationContext";
@interface TLFPSCreateItemOperation : NSOperation {
@private
TLFilePhotoSource* source;
NSString* path;
}
@property (nonatomic, retain) TLFilePhotoSource* source;
@property (nonatomic, copy) NSString* path;
@end
@implementation TLFilePhotoSource
@synthesize name;
@synthesize icon;
@synthesize error;
@synthesize isCurrent;
@synthesize isWorking;
@synthesize items = mutableItems;
@synthesize workQueue;
@synthesize finalOperation;
@synthesize directlyAddedPaths;
@synthesize allPaths;
- (id)init {
self = [super init];
if (self) {
mutableItems = [NSMutableSet new];
[self setWorkQueue:[[NSOperationQueue new] autorelease]];
[self setDirectlyAddedPaths:[NSMutableSet set]];
[self setAllPaths:[NSMutableSet set]];
[self updateName];
[self addObserver:self
forKeyPath:@"finalOperation.isFinished"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial)
context:TLFPSObservationContext];
}
return self;
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"finalOperation.isFinished"];
[self setName:nil];
[self setIcon:nil];
[mutableItems release];
[self setWorkQueue:nil];
[self setFinalOperation:nil];
[self setDirectlyAddedPaths:nil];
[self setAllPaths:nil];
[super dealloc];
}
- (void)observeValueForKeyPath:(NSString*)keyPath
ofObject:(id)object
change:(NSDictionary*)change
context:(void*)context
{
//NSLog(@"observed %@: %@", keyPath, change);
if (context == TLFPSObservationContext) {
if ([keyPath isEqualToString:@"finalOperation.isFinished"]) {
id newValue = [change objectForKey:NSKeyValueChangeNewKey];
BOOL isFinished = (newValue == [NSNull null]) ? YES : [newValue boolValue];
[[self tlMainThreadProxy] setIsCurrent:isFinished];
[[self tlMainThreadProxy] setIsWorking:!isFinished];
}
}
else {
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}
}
- (void)engage {
[super engage];
[self enqueuePaths:[self directlyAddedPaths]];
}
- (void)disengage {
[mutableItems removeAllObjects];
[[self allPaths] removeAllObjects];
[[self workQueue] cancelAllOperations];
[self setError:nil];
[super disengage];
}
- (void)addPaths:(NSSet*)newFilePaths {
[[self directlyAddedPaths] unionSet:newFilePaths];
[self updateName];
if ([self isEngaged]) {
[self enqueuePaths:newFilePaths];
}
}
- (void)addItemsObject:(TLFileSourceItem*)newItem {
[mutableItems addObject:newItem];
}
- (void)removeItemsObject:(TLFileSourceItem*)oldItem {
// we don't remove items, this just enables automatic KVO
(void)oldItem;
[self doesNotRecognizeSelector:_cmd];
}
- (void)acceptItem:(TLFileSourceItem*)newItem {
NSAssert([NSThread isMainThread], @"Items must be added from main thread");
if (![self isEngaged]) {
// we are disengaged, silently drop item
(void)newItem;
}
else if ([[self items] count] < TLFilePhotoSourceItemLimit) {
[self addItemsObject:newItem];
[[self allPaths] addObject:[[newItem originalURL] path]];
}
else {
NSDictionary* errInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Too many items for source.", NSLocalizedDescriptionKey,
@"Due to resource limitations in the current version of Geotagalog, "
@"you should not add any more files to this session. Please split your "
@"work into smaller sets.", NSLocalizedRecoverySuggestionErrorKey, nil];
NSError* fullError = [NSError errorWithDomain:@"com.calftrail.geotagalog"
code:1000
userInfo:errInfo];
[self setError:fullError];
[[self workQueue] cancelAllOperations];
}
}
- (void)updateName {
NSAssert([NSThread isMainThread], @"Name must be updated from main thread");
NSUInteger numPaths = [[self directlyAddedPaths] count];
NSString* newName = nil;
switch (numPaths) {
case 0:
newName = @"File source";
break;
case 1:
{
NSString* path = [[self directlyAddedPaths] anyObject];
newName = [path lastPathComponent];
break;
}
default:
newName = @"Multiple files/folders";
}
[self setName:newName];
}
- (void)enqueuePaths:(NSSet*)filePaths {
NSAssert([NSThread isMainThread], @"Paths must be enqueued from main thread");
if (![self isEngaged] ||
[[self items] count] >= TLFilePhotoSourceItemLimit)
{
// stop enqueuing paths
return;
}
NSOperation* markerOperation = [[NSOperation new] autorelease];
NSArray* shuffledPaths = TLNSArrayShuffle([filePaths allObjects]);
for (NSString* path in shuffledPaths) {
if ([[self allPaths] containsObject:path]) {
continue;
}
TLFPSCreateItemOperation* createItemOperation = [[TLFPSCreateItemOperation new] autorelease];
[markerOperation addDependency:createItemOperation];
[createItemOperation setSource:self];
[createItemOperation setPath:path];
[[self workQueue] addOperation:createItemOperation];
}
if ([self finalOperation]) {
[markerOperation addDependency:[self finalOperation]];
}
[self setFinalOperation:markerOperation];
[[self workQueue] addOperation:markerOperation];
}
@end
@implementation TLFPSCreateItemOperation
@synthesize source;
@synthesize path;
- (void)dealloc {
[self setSource:nil];
[self setPath:nil];
[super dealloc];
}
- (void)addSuboperationsForFolder:(NSString*)folderPath {
if ([self isCancelled]) return;
//NSLog(@"Adding suboperations for %@", folderPath);
NSArray* filenames = [[NSFileManager tl_threadManager] contentsOfDirectoryAtPath:folderPath
error:NULL];
if (!filenames || [self isCancelled]) return;
NSMutableSet* fullPaths = [NSMutableSet set];
for (NSString* filename in filenames) {
if ([self isCancelled]) return;
if (![filename length] || [filename characterAtIndex:0] == '.') {
// skip hidden files
continue;
}
NSString* fullItemPath = [folderPath stringByAppendingPathComponent:filename];
[fullPaths addObject:fullItemPath];
}
if (![self isCancelled]) {
[[[self source] tlMainThreadProxy] enqueuePaths:fullPaths];
}
}
- (void)addItemForPath:(NSString*)itemPath {
if ([self isCancelled]) return;
BOOL isDirectory = NO;
BOOL exists = [[NSFileManager tl_threadManager] fileExistsAtPath:[self path]
isDirectory:&isDirectory];
if (!exists || [self isCancelled]) return;
if (isDirectory) {
[self addSuboperationsForFolder:itemPath];
}
else {
NSURL* originalURL = [NSURL fileURLWithPath:itemPath isDirectory:NO];
TLFileSourceItem* item = [[[TLFileSourceItem alloc] initWithSource:[self source]
originalURL:originalURL
error:NULL] autorelease];
if (item && ![self isCancelled]) {
[[[self source] tlMainThreadProxy] acceptItem:item];
}
}
}
-(void)main {
if ([self isCancelled]) return;
NSAutoreleasePool* pool = [NSAutoreleasePool new];
[self addItemForPath:[self path]];
[pool drain];
}
@end
NSArray* TLNSArrayShuffle(NSArray* array) {
// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
NSMutableArray* mutableArray = [array mutableCopy];
TLRandomInit();
NSUInteger itemIdx = [mutableArray count];
while (itemIdx-- > 1) {
NSUInteger partnerIdx = NSUIntegerMax;
do { partnerIdx = (itemIdx + 1) * TLRandom(); } while (partnerIdx > itemIdx);
[mutableArray exchangeObjectAtIndex:itemIdx withObjectAtIndex:partnerIdx];
}
return [mutableArray autorelease];
}