Desenvolver e Download de Software Open Source

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 792 - (show annotations) (download)
Mon Aug 13 14:56:02 2007 UTC (16 years, 9 months ago) by masakih
File size: 7538 byte(s)
corrected all file's format of file header comment.

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

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