forked from karelia/KSFileUtilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KSFilePromise.m
274 lines (213 loc) · 8.53 KB
/
KSFilePromise.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
//
// KSFilePromise.m
// Sandvox
//
// Created by Mike Abdullah on 19/10/2012.
// Copyright © 2012 Karelia Software
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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.
//
#import "KSFilePromise.h"
#import "KSUniformType.h"
@interface KSFilePromiseDestination : NSObject
{
@private
NSURL *_destinationURL;
}
- (id)initForDocument:(NSDocument *)doc;
@property(nonatomic, readonly) NSURL *destinationURL;
@end
#pragma mark -
@implementation KSFilePromise
#pragma mark Lifecycle
+ (NSArray *)promisesFromDraggingInfo:(id<NSDraggingInfo>)info forDocument:(NSDocument *)doc;
{
KSFilePromiseDestination *destination = [[KSFilePromiseDestination alloc] initForDocument:doc];
if (!destination) return nil;
NSArray *names = [info namesOfPromisedFilesDroppedAtDestination:[destination destinationURL]];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[names count]];
for (NSString *aName in names)
{
KSFilePromise *promise = [[KSFilePromise alloc] initWithName:aName destination:destination];
[result addObject:promise];
[promise release];
}
[destination release];
return result;
}
+ (NSArray *)promisesFromDraggingInfo:(id <NSDraggingInfo>)info forDocument:(NSDocument *)doc
waitUntilFilesAreReachable:(BOOL)waitTillReachable timeout:(NSTimeInterval)timeout;
{
NSArray *result = [self promisesFromDraggingInfo:info forDocument:doc];
if (waitTillReachable)
{
for (KSFilePromise *aPromise in result)
{
[aPromise waitUntilFileIsReachableWithTimeout:timeout error:NULL];
}
}
return result;
}
- (id)initWithName:(NSString *)name destination:(KSFilePromiseDestination *)destination;
{
NSParameterAssert([name length] > 0);
NSParameterAssert(destination);
if (self = [self init])
{
_fileURL = [[[destination destinationURL] URLByAppendingPathComponent:name] copy];
_destination = [destination retain]; // when last promise for this destination is dealloced, so will the destination be
}
return self;
}
- (void)dealloc;
{
// Delete underlying file
[[self class] tryToDeleteFilePromiseURL:[self fileURL] destination:_destination retryInterval:2];
[_fileURL release];
[_destination release]; // if we were last reference to it, the directory is automatically deleted
[super dealloc];
}
+ (BOOL)tryToDeleteFilePromiseURL:(NSURL *)url destination:(KSFilePromiseDestination *)destination retryInterval:(int64_t)delayInSeconds;
{
NSError *error;
BOOL result = [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
if (!result)
{
NSLog(@"File promise deletion failed: %@", error);
NSLog(@"Maybe the file hasn't arrived yet, or will become removable soon. Retrying later");
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
BOOL retryResult = [self tryToDeleteFilePromiseURL:url
destination:destination
retryInterval:(2 * delayInSeconds)]; // keep extending to minimise interruptions
if (retryResult) NSLog(@"Retried deletion of file promise succeded: %@", [url path]);
});
}
return result;
}
#pragma mark Pasteboard Introspection
+ (BOOL)canReadFilePromiseConformingToTypes:(NSArray *)types fromPasteboard:(NSPasteboard *)pasteboard;
{
if ([pasteboard canReadItemWithDataConformingToTypes:[NSArray arrayWithObject:(NSString *)kPasteboardTypeFilePromiseContent]])
{
for (NSPasteboardItem *anItem in [pasteboard pasteboardItems])
{
NSString *type = [anItem stringForType:(NSString *)kPasteboardTypeFilePromiseContent];
if ([KSUniformType type:type conformsToOneOfTypes:types])
{
return YES;
}
}
}
return NO;
}
#pragma mark Properties
@synthesize fileURL = _fileURL;
#pragma mark Waiting for File Existance
- (BOOL)waitUntilFileIsReachableWithTimeout:(NSTimeInterval)timeout error:(NSError **)error;
{
NSURL *url = self.fileURL;
NSTimeInterval start = [NSProcessInfo processInfo].systemUptime;
BOOL result;
while (!(result = [url checkResourceIsReachableAndReturnError:error]))
{
if ([NSProcessInfo processInfo].systemUptime - start > timeout) break;
[NSThread sleepForTimeInterval:0.1f]; // yes it's horriffic. Think of something better before you judge me
}
return result;
}
#pragma mark NSCopying
- (id)copyWithZone:(NSZone *)zone;
{
// Immutable
return [self retain];
}
@end
#pragma mark -
@implementation KSFilePromiseDestination
- (id)initForDocument:(NSDocument *)doc;
{
if (self = [self init])
{
NSURL *docURL = [doc fileURL];
if (!docURL) docURL = [doc autosavedContentsFileURL];
NSError *error;
if (!docURL)
{
// For completely unsaved docs, chances are they'll be going into NSAutosavedInformationDirectory next
docURL = [[NSFileManager defaultManager] URLForDirectory:NSAutosavedInformationDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:&error];
if (!docURL)
{
NSLog(@"Failed to retrieve autosave directory's lcoation: %@", error);
docURL = [NSURL fileURLWithPath:NSHomeDirectory() isDirectory:YES];
}
}
_destinationURL = [[NSFileManager defaultManager] URLForDirectory:NSItemReplacementDirectory
inDomain:NSUserDomainMask
appropriateForURL:docURL
create:YES
error:&error];
if (_destinationURL)
{
_destinationURL = [_destinationURL copy];
[[NSProcessInfo processInfo] disableSuddenTermination];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appWillTerminate:)
name:NSApplicationWillTerminateNotification
object:NSApp];
}
else
{
NSLog(@"Failed to create temp directory for file promises: %@", error);
}
}
return self;
}
- (void)close;
{
if ([self destinationURL])
{
// Cleanup the directory
NSFileManager *manager = [[NSFileManager alloc] init];
NSError *error;
if (![manager removeItemAtURL:[self destinationURL] error:&error])
{
NSLog(@"File promise temp directory deletion failed: %@", error);
}
[manager release];
[[NSProcessInfo processInfo] enableSuddenTermination];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:NSApp];
[_destinationURL release]; _destinationURL = nil;
}
}
- (void)dealloc;
{
[self close];
[super dealloc];
}
@synthesize destinationURL = _destinationURL;
- (void)appWillTerminate:(NSNotification *)notification;
{
[self close];
}
@end