1 |
// |
2 |
// BSDownloadTask.m |
3 |
// BathyScaphe |
4 |
// |
5 |
// Created by Hori,Masaki on 06/08/06. |
6 |
// Copyright 2006-2009 BathyScaphe Project. All rights reserved. |
7 |
// encoding="UTF-8" |
8 |
// |
9 |
|
10 |
#import "BSDownloadTask.h" |
11 |
|
12 |
NSString *const BSDownloadTaskFinishDownloadNotification = @"BSDownloadTaskFinishDownloadNotification"; |
13 |
NSString *const BSDownloadTaskReceiveResponseNotification = @"BSDownloadTaskReceiveResponseNotification"; |
14 |
NSString *const BSDownloadTaskCanceledNotification = @"BSDownloadTaskCanceledNotification"; |
15 |
NSString *const BSDownloadTaskInternalErrorNotification = @"BSDownloadTaskInternalErrorNotification"; |
16 |
NSString *const BSDownloadTaskAbortDownloadNotification = @"BSDownloadTaskAbortDownloadNotification"; |
17 |
NSString *const BSDownloadTaskServerResponseKey = @"BSDownloadTaskServerResponseKey"; // NSURLResponse |
18 |
NSString *const BSDownloadTaskStatusCodeKey = @"BSDownloadTaskStatusCodeKey"; // NSNumber (int) |
19 |
NSString *const BSDownloadTaskFailDownloadNotification = @"BSDownloadTaskFailDownloadNotification"; |
20 |
NSString *const BSDownloadTaskErrorObjectKey = @"BSDownloadTaskErrorObjectKey"; // NSError |
21 |
|
22 |
|
23 |
@implementation BSDownloadTask |
24 |
+ (id)taskWithURL:(NSURL *)url |
25 |
{ |
26 |
return [[[self alloc] initWithURL:url] autorelease]; |
27 |
} |
28 |
|
29 |
- (id) initWithURL:(NSURL *)url |
30 |
{ |
31 |
if(self = [super init]) { |
32 |
[self setURL:url]; |
33 |
[self setIsInProgress:YES]; |
34 |
m_contLengthIsUnknown = YES; |
35 |
} |
36 |
|
37 |
return self; |
38 |
} |
39 |
|
40 |
+ (id)taskWithURL:(NSURL *)url method:(NSString *)method |
41 |
{ |
42 |
return [[[self alloc] initWithURL:url method:method] autorelease]; |
43 |
} |
44 |
|
45 |
- (id)initWithURL:(NSURL *)url method:(NSString *)inMethod |
46 |
{ |
47 |
if(self = [self initWithURL:url]) { |
48 |
method = [inMethod retain]; |
49 |
} |
50 |
|
51 |
return self; |
52 |
} |
53 |
|
54 |
- (void)dealloc |
55 |
{ |
56 |
[self setURL:nil]; |
57 |
[con release]; |
58 |
[receivedData release]; |
59 |
[method release]; |
60 |
[_response release]; |
61 |
|
62 |
[super dealloc]; |
63 |
} |
64 |
|
65 |
#pragma mark Accessors |
66 |
- (void)setURL:(NSURL *)url |
67 |
{ |
68 |
id temp = m_targetURL; |
69 |
m_targetURL = [url retain]; |
70 |
[temp release]; |
71 |
} |
72 |
|
73 |
- (NSURL *)url |
74 |
{ |
75 |
return m_targetURL; |
76 |
} |
77 |
|
78 |
- (void)setCurrentLength:(double)doubleValue |
79 |
{ |
80 |
m_currentLength = doubleValue; |
81 |
} |
82 |
|
83 |
- (double)currentLength |
84 |
{ |
85 |
return m_currentLength; |
86 |
} |
87 |
|
88 |
- (void)setContLength:(double)i |
89 |
{ |
90 |
m_contLength = i; |
91 |
} |
92 |
|
93 |
- (double)contLength |
94 |
{ |
95 |
return m_contLength; |
96 |
} |
97 |
|
98 |
- (NSData *)receivedData |
99 |
{ |
100 |
return receivedData; |
101 |
} |
102 |
|
103 |
- (void)setResponse:(id)response |
104 |
{ |
105 |
id temp = _response; |
106 |
_response = [response retain]; |
107 |
[temp release]; |
108 |
} |
109 |
|
110 |
- (id)response |
111 |
{ |
112 |
return _response; |
113 |
} |
114 |
|
115 |
#pragma mark Overrides |
116 |
- (void)doExecuteWithLayout:(CMRThreadLayout *)layout |
117 |
{ |
118 |
NSRunLoop *loop = [NSRunLoop currentRunLoop]; |
119 |
|
120 |
[receivedData release]; |
121 |
receivedData = nil; |
122 |
[self setCurrentLength:0]; |
123 |
[self setContLength:0]; |
124 |
[self setAmount:-1]; |
125 |
[self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@)", @"Downloader", @""), [[self url] absoluteString]]]; |
126 |
|
127 |
NSMutableURLRequest *request; |
128 |
|
129 |
request = [NSMutableURLRequest requestWithURL:[self url]]; |
130 |
if (!request) { |
131 |
[self postNotificationWithName:BSDownloadTaskInternalErrorNotification userInfo:nil]; |
132 |
return; |
133 |
} |
134 |
[request setValue:[NSBundle monazillaUserAgent] forHTTPHeaderField:@"User-Agent"]; |
135 |
if (method) { |
136 |
[request setHTTPMethod:method]; |
137 |
} |
138 |
|
139 |
con = [[NSURLConnection alloc] initWithRequest:request delegate:self]; |
140 |
if (!con) { |
141 |
[self postNotificationWithName:BSDownloadTaskInternalErrorNotification userInfo:nil]; |
142 |
return; |
143 |
} |
144 |
|
145 |
while ([self isInProgress]) { |
146 |
id pool = [[NSAutoreleasePool alloc] init]; |
147 |
@try { |
148 |
[loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; |
149 |
} |
150 |
@catch(id ex) { |
151 |
// do nothing. |
152 |
@throw; |
153 |
} |
154 |
@finally { |
155 |
[pool release]; |
156 |
} |
157 |
} |
158 |
} |
159 |
|
160 |
#pragma mark CMRTask |
161 |
- (IBAction)cancel:(id)sender |
162 |
{ |
163 |
[con cancel]; |
164 |
[self postNotificationWithName:BSDownloadTaskCanceledNotification userInfo:nil]; |
165 |
|
166 |
[super cancel:sender]; |
167 |
} |
168 |
|
169 |
- (id)identifier |
170 |
{ |
171 |
return [NSString stringWithFormat:@"%@-%p", self, self]; |
172 |
} |
173 |
|
174 |
- (NSString *)title |
175 |
{ |
176 |
return NSLocalizedStringFromTable(@"Download.", @"Downloader", @""); |
177 |
} |
178 |
|
179 |
- (double)amount |
180 |
{ |
181 |
return m_taskAmount; |
182 |
} |
183 |
|
184 |
- (void)setAmount:(double)doubleValue |
185 |
{ |
186 |
m_taskAmount = doubleValue; |
187 |
} |
188 |
@end |
189 |
|
190 |
|
191 |
@implementation BSDownloadTask(NSURLConnectionDelegate) |
192 |
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response; |
193 |
{ |
194 |
// Leopard |
195 |
if (!response) { |
196 |
return request; |
197 |
} |
198 |
[self setResponse:response]; |
199 |
[self postNotificaionWithResponse:response]; |
200 |
[connection cancel]; |
201 |
return nil; |
202 |
} |
203 |
|
204 |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response |
205 |
{ |
206 |
BOOL disconnect = NO; |
207 |
|
208 |
[self setResponse:response]; |
209 |
if ([[NSUserDefaults standardUserDefaults] boolForKey:BSUserDebugEnabledKey]) { |
210 |
NSLog(@"** USER DEBUG **\n%@", [(NSHTTPURLResponse *)response allHeaderFields]); |
211 |
} |
212 |
switch ([(NSHTTPURLResponse *)response statusCode]) { |
213 |
case 200: |
214 |
case 206: |
215 |
break; |
216 |
case 304: |
217 |
NSLog(@"Content is not modified."); |
218 |
disconnect = YES; |
219 |
break; |
220 |
case 404: |
221 |
NSLog(@"Content has not found."); |
222 |
disconnect = YES; |
223 |
break; |
224 |
case 416: |
225 |
NSLog(@"Range is mismatch."); |
226 |
disconnect = YES; |
227 |
break; |
228 |
default: |
229 |
NSLog(@"Unknown error."); |
230 |
disconnect = YES; |
231 |
break; |
232 |
} |
233 |
if (disconnect) { |
234 |
[connection cancel]; |
235 |
[self postNotificaionWithResponse:response]; |
236 |
|
237 |
return; |
238 |
} |
239 |
|
240 |
// [self postNotificaionWithResponseDontFinish:response]; // 2009-03-24 ��������������������������������������������������������������� |
241 |
double length = [response expectedContentLength]; |
242 |
if (length <= 0) { |
243 |
double assumedLength = [[[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Content-Length"] doubleValue]; |
244 |
if (assumedLength > 0) { |
245 |
[self setContLength:assumedLength*1.7]; // gzip ������������������������ |
246 |
m_contLengthIsUnknown = NO; |
247 |
} |
248 |
} else { |
249 |
m_contLengthIsUnknown = NO; |
250 |
[self setContLength:length]; |
251 |
} |
252 |
} |
253 |
|
254 |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data |
255 |
{ |
256 |
if (!receivedData) { |
257 |
receivedData = [[NSMutableData alloc] init]; |
258 |
} |
259 |
|
260 |
if (!receivedData) { |
261 |
// abort |
262 |
[connection cancel]; |
263 |
[self postNotificationWithName:BSDownloadTaskInternalErrorNotification userInfo:nil]; |
264 |
|
265 |
return; |
266 |
} |
267 |
|
268 |
[receivedData appendData:data]; |
269 |
[self setCurrentLength:[receivedData length]]; |
270 |
|
271 |
if (!m_contLengthIsUnknown && ([self contLength] > 0)) { |
272 |
double bar = [self currentLength]/[self contLength]*100.0; |
273 |
[self setAmount:bar]; |
274 |
[self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk of %.0fk)", @"Downloader", @""), |
275 |
[[self url] absoluteString], (float)[self currentLength]/1024, (float)[self contLength]/1024]]; |
276 |
} else { |
277 |
[self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk)", @"Downloader", @""), |
278 |
[[self url] absoluteString], (float)[self currentLength]/1024]]; |
279 |
} |
280 |
} |
281 |
|
282 |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection |
283 |
{ |
284 |
[self postNotificationWithName:BSDownloadTaskFinishDownloadNotification userInfo:nil]; |
285 |
} |
286 |
|
287 |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error |
288 |
{ |
289 |
// abort |
290 |
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:error forKey:BSDownloadTaskErrorObjectKey]; |
291 |
[self postNotificationWithName:BSDownloadTaskFailDownloadNotification userInfo:userInfo]; |
292 |
[self setIsInProgress:NO]; |
293 |
} |
294 |
@end |
295 |
|
296 |
|
297 |
@implementation BSDownloadTask(TaskNotification) |
298 |
- (void)postNotificationWithName:(NSString *)name userInfo:(NSDictionary *)info |
299 |
{ |
300 |
[[NSNotificationCenter defaultCenter] postNotificationName:name object:self userInfo:info]; |
301 |
[self setIsInProgress:NO]; |
302 |
} |
303 |
|
304 |
- (void)postNotificaionWithResponse:(NSURLResponse *)response |
305 |
{ |
306 |
NSDictionary *info; |
307 |
|
308 |
info = [NSDictionary dictionaryWithObjectsAndKeys:response, BSDownloadTaskServerResponseKey, |
309 |
[NSNumber numberWithInt:[(NSHTTPURLResponse *)response statusCode]], BSDownloadTaskStatusCodeKey, |
310 |
NULL]; |
311 |
[self postNotificationWithName:BSDownloadTaskAbortDownloadNotification userInfo:info]; |
312 |
} |
313 |
|
314 |
- (void)postNotificaionWithResponseDontFinish:(NSURLResponse *)response |
315 |
{ |
316 |
NSDictionary *info; |
317 |
|
318 |
info = [NSDictionary dictionaryWithObjectsAndKeys:response, BSDownloadTaskServerResponseKey, |
319 |
[NSNumber numberWithInt:[(NSHTTPURLResponse *)response statusCode]], BSDownloadTaskStatusCodeKey, |
320 |
NULL]; |
321 |
[self postNotificationWithName:BSDownloadTaskReceiveResponseNotification userInfo:info]; |
322 |
} |
323 |
@end |