Desenvolver e Download de Software Open Source

Browse Subversion Repository

Annotation of /bathyscaphe/trunk/application/source/browser/BSDownloadTask.m

Parent Directory Parent Directory | Revision Log Revision Log


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

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26