/
SKDownload.m
386 lines (321 loc) · 13.5 KB
/
SKDownload.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
//
// SKDownload.m
// Skim
//
// Created by Christiaan Hofman on 8/11/07.
/*
This software is Copyright (c) 2007-2012
Christiaan Hofman. 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 Christiaan Hofman nor the names of any
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"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 COPYRIGHT
OWNER OR CONTRIBUTORS 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 "SKDownload.h"
#import <ApplicationServices/ApplicationServices.h>
#import "NSFileManager_SKExtensions.h"
#import "SKDownloadController.h"
#import "SKStringConstants.h"
NSString *SKDownloadFileNameKey = @"fileName";
NSString *SKDownloadStatusKey = @"status";
NSString *SKDownloadProgressIndicatorKey = @"progressIndicator";
#define SKFilePathKey @"filePath"
#define SKFileIconKey @"fileIcon"
#define SKCanCancelKey @"canCancel"
#define SKCanRemoveKey @"canRemove"
#define SKCanResumeKey @"canResume"
#define SKInfoKey @"info"
@interface SKDownload ()
@property (nonatomic) SKDownloadStatus status;
@property (nonatomic, retain) NSString *filePath;
@property (nonatomic, retain) NSImage *fileIcon;
@property (nonatomic) long long expectedContentLength, receivedContentLength;
- (void)handleApplicationWillTerminateNotification:(NSNotification *)notification;
@end
@implementation SKDownload
@synthesize URL, filePath, fileIcon, expectedContentLength, receivedContentLength, status;
@dynamic fileName, fileURL, info, canCancel, canRemove, canResume, scriptingURL, scriptingStatus;
static NSSet *keysAffectedByFilePath = nil;
static NSSet *keysAffectedByDownloadStatus = nil;
static NSSet *filePathSet = nil;
static NSSet *downloadStatusSet = nil;
static NSSet *infoKeys = nil;
+ (void)initialize {
SKINITIALIZE;
keysAffectedByFilePath = [[NSSet alloc] initWithObjects:SKDownloadFileNameKey, SKFileIconKey, nil];
keysAffectedByDownloadStatus = [[NSSet alloc] initWithObjects:SKCanCancelKey, SKCanRemoveKey, SKCanResumeKey, nil];
filePathSet = [[NSSet alloc] initWithObjects:SKFilePathKey, nil];
downloadStatusSet = [[NSSet alloc] initWithObjects:SKDownloadStatusKey, nil];
infoKeys = [[NSSet alloc] initWithObjects:SKDownloadFileNameKey, SKDownloadStatusKey, SKDownloadProgressIndicatorKey, nil];
}
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *set = [super keyPathsForValuesAffectingValueForKey:key];
if ([keysAffectedByFilePath containsObject:key])
return [set count] > 0 ? [set setByAddingObjectsFromSet:filePathSet] : filePathSet;
if ([keysAffectedByDownloadStatus containsObject:key])
return [set count] > 0 ? [set setByAddingObjectsFromSet:downloadStatusSet] : downloadStatusSet;
if ([SKInfoKey isEqualToString:key])
return [set count] > 0 ? [set setByAddingObjectsFromSet:infoKeys] : infoKeys;
return set;
}
- (id)initWithURL:(NSURL *)aURL {
self = [super init];
if (self) {
URL = [aURL retain];
URLDownload = nil;
filePath = nil;
fileIcon = nil;
expectedContentLength = NSURLResponseUnknownLength;
receivedContentLength = 0;
progressIndicator = nil;
status = SKDownloadStatusUndefined;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleApplicationWillTerminateNotification:)
name:NSApplicationWillTerminateNotification object:NSApp];
}
return self;
}
- (id)init {
return [self initWithURL:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self cancel];
SKDESTROY(URL);
SKDESTROY(URLDownload);
SKDESTROY(filePath);
SKDESTROY(fileIcon);
SKDESTROY(progressIndicator);
[super dealloc];
}
- (void)handleApplicationWillTerminateNotification:(NSNotification *)notification {
[self cancel];
}
- (void)resetProgressIndicator {
if (expectedContentLength > 0) {
[progressIndicator setIndeterminate:NO];
[progressIndicator setMaxValue:(double)expectedContentLength];
} else {
[progressIndicator setIndeterminate:YES];
[progressIndicator setMaxValue:1.0];
}
}
- (void)removeProgressIndicatorFromSuperview {
[progressIndicator removeFromSuperview];
}
#pragma mark Accessors
- (void)setStatus:(SKDownloadStatus)newStatus {
if (status != newStatus) {
if (newStatus == SKDownloadStatusDownloading) {
[progressIndicator startAnimation:self];
} else if (status == SKDownloadStatusDownloading) {
[progressIndicator stopAnimation:self];
[progressIndicator removeFromSuperview];
[progressIndicator release];
progressIndicator = nil;
}
status = newStatus;
}
}
- (void)setFilePath:(NSString *)newFilePath {
if (filePath != newFilePath) {
[filePath release];
filePath = [newFilePath retain];
if (fileIcon == nil && filePath) {
fileIcon = [[[NSWorkspace sharedWorkspace] iconForFileType:[filePath pathExtension]] retain];
}
}
}
- (NSString *)fileName {
NSString *fileName = [[NSFileManager defaultManager] displayNameAtPath:filePath];
if (fileName == nil) {
if ([[URL path] length] > 1) {
fileName = [[URL path] lastPathComponent];
} else {
fileName = [URL host];
if (fileName == nil)
fileName = [[[URL resourceSpecifier] lastPathComponent] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
}
return fileName;
}
- (NSURL *)fileURL {
return filePath ? [NSURL fileURLWithPath:filePath] : nil;
}
- (NSImage *)fileIcon {
if (fileIcon == nil && URL)
return [[NSWorkspace sharedWorkspace] iconForFileType:[[[self URL] path] pathExtension]];
return fileIcon;
}
- (void)setExpectedContentLength:(long long)newExpectedContentLength {
if (expectedContentLength != newExpectedContentLength) {
expectedContentLength = newExpectedContentLength;
[self resetProgressIndicator];
}
}
- (void)setReceivedContentLength:(long long)newReceivedContentLength {
if (receivedContentLength != newReceivedContentLength) {
receivedContentLength = newReceivedContentLength;
[progressIndicator setDoubleValue:(double)receivedContentLength];
}
}
- (NSProgressIndicator *)progressIndicator {
if (progressIndicator == nil && [self status] == SKDownloadStatusDownloading) {
progressIndicator = [[NSProgressIndicator alloc] init];
[progressIndicator setStyle:NSProgressIndicatorBarStyle];
[progressIndicator setControlSize:NSSmallControlSize];
[progressIndicator setUsesThreadedAnimation:YES];
[progressIndicator sizeToFit];
[self resetProgressIndicator];
[progressIndicator setDoubleValue:(double)receivedContentLength];
[progressIndicator startAnimation:self];
}
return progressIndicator;
}
- (NSDictionary *)info {
NSMutableDictionary *info = [NSMutableDictionary dictionary];
for (NSString *key in infoKeys)
[info setValue:[self valueForKey:key] forKey:key];
return info;
}
- (id)objectSpecifier {
NSUInteger idx = [[[SKDownloadController sharedDownloadController] downloads] indexOfObjectIdenticalTo:self];
if (idx != NSNotFound) {
NSScriptClassDescription *containerClassDescription = [NSScriptClassDescription classDescriptionForClass:[NSApp class]];
return [[[NSIndexSpecifier allocWithZone:[self zone]] initWithContainerClassDescription:containerClassDescription containerSpecifier:nil key:@"downloads" index:idx] autorelease];
} else {
return nil;
}
}
- (NSString *)scriptingURL {
return [[self URL] absoluteString];
}
- (SKDownloadStatus)scriptingStatus {
return [self status];
}
- (void)setScriptingStatus:(SKDownloadStatus)newStatus {
if (newStatus != status) {
if (newStatus == SKDownloadStatusCanceled && [self canCancel])
[self cancel];
else if ((newStatus == SKDownloadStatusStarting || newStatus == SKDownloadStatusDownloading) && [self canResume])
[self resume];
}
}
#pragma mark Actions
- (void)start {
if (URLDownload || URL == nil) {
NSBeep();
return;
}
[self setExpectedContentLength:NSURLResponseUnknownLength];
[self setReceivedContentLength:0];
URLDownload = [[NSURLDownload alloc] initWithRequest:[NSURLRequest requestWithURL:URL] delegate:self];
[URLDownload setDeletesFileUponFailure:NO];
[self setStatus:SKDownloadStatusStarting];
}
- (void)cancel {
if ([self canCancel]) {
[URLDownload cancel];
[self setStatus:SKDownloadStatusCanceled];
}
}
- (void)resume {
if ([self canResume]) {
NSData *resumeData = nil;
if ([self status] == SKDownloadStatusCanceled)
resumeData = [[[URLDownload resumeData] retain] autorelease];
if (resumeData) {
[URLDownload release];
URLDownload = [[NSURLDownload alloc] initWithResumeData:resumeData delegate:self path:[self filePath]];
[URLDownload setDeletesFileUponFailure:NO];
[self setStatus:SKDownloadStatusDownloading];
} else {
[self cleanup];
[self setFilePath:nil];
[URLDownload release];
URLDownload = nil;
[self start];
}
}
}
- (void)cleanup {
[self cancel];
if (filePath)
[[NSFileManager defaultManager] removeItemAtPath:[filePath stringByDeletingLastPathComponent] error:NULL];
}
- (void)moveToTrash {
if ([self canRemove] && filePath) {
NSString *folderPath = [filePath stringByDeletingLastPathComponent];
NSString *fileName = [filePath lastPathComponent];
NSInteger tag = 0;
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:folderPath destination:nil files:[NSArray arrayWithObjects:fileName, nil] tag:&tag];
}
}
- (BOOL)canCancel {
return [self status] == SKDownloadStatusStarting || [self status] == SKDownloadStatusDownloading;
}
- (BOOL)canRemove {
return [self status] == SKDownloadStatusFinished || [self status] == SKDownloadStatusFailed || [self status] == SKDownloadStatusCanceled;
}
- (BOOL)canResume {
return ([self status] == SKDownloadStatusCanceled || [self status] == SKDownloadStatusFailed) && [self URL];
}
#pragma mark NSURLDownloadDelegate protocol
- (void)downloadDidBegin:(NSURLDownload *)download{
[self setStatus:SKDownloadStatusDownloading];
}
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response {
[self setExpectedContentLength:[response expectedContentLength]];
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (CFStringRef)[response MIMEType], kUTTypeData);
if (UTI) {
NSString *type = [[NSWorkspace sharedWorkspace] preferredFilenameExtensionForType:(NSString *)UTI];
if (type)
[self setFileIcon:[[NSWorkspace sharedWorkspace] iconForFileType:type]];
CFRelease(UTI);
}
}
- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename {
NSString *downloadDir = [[[NSUserDefaults standardUserDefaults] stringForKey:SKDownloadsDirectoryKey] stringByExpandingTildeInPath];
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:downloadDir isDirectory:&isDir] && isDir)
[URLDownload setDestination:[downloadDir stringByAppendingPathComponent:filename] allowOverwrite:NO];
}
- (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path {
[self setFilePath:path];
}
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length {
if (expectedContentLength > 0) {
receivedContentLength += length;
[progressIndicator setDoubleValue:(double)receivedContentLength];
}
}
- (void)downloadDidFinish:(NSURLDownload *)theDownload {
if (expectedContentLength > 0)
[progressIndicator setDoubleValue:(double)expectedContentLength];
[self setStatus:SKDownloadStatusFinished];
}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error {
if (filePath)
[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];
[self setFilePath:nil];
[self setStatus:SKDownloadStatusFailed];
}
@end