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 684 - (show annotations) (download)
Sun Feb 11 17:13:48 2007 UTC (17 years, 3 months ago) by tsawada2
File size: 7848 byte(s)
NavigationBar Update

1 //
2 // BSDownloadTask.m
3 // BathyScaphe
4 //
5 // Created by Hori,Masaki on 06/08/06.
6 // Copyright 2006 __MyCompanyName__. 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]//@"Monazilla/1.0 (Starlight Breaker/1.5 SBx)"
125 forHTTPHeaderField:@"User-Agent"];
126 if(method) {
127 [request setHTTPMethod : method];
128 }
129
130 #if 0
131 [self performSelectorOnMainThread:@selector(createURLConnection:)
132 withObject:request
133 waitUntilDone:YES];
134
135 if(con) {
136 NSLog(@"con created.");
137 } else {
138 NSLog(@"con NOT created.");
139 }
140 #else
141 con = [[NSURLConnection alloc] initWithRequest:request
142 delegate:self];
143 if(!con) {
144 [self postNotificationWithName:BSDownloadTaskInternalErrorNotification];
145 return;
146 }
147
148 // -[CMRTaskManager taskWillProgressProcessing:] ���������run loop���fire������������������������������������
149 // ������������������������������������������������������������������������������������������
150 while(!isFinished) {
151 [loop runMode:NSDefaultRunLoopMode
152 beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
153 }
154 #endif
155 }
156
157 - (IBAction)cancel:(id)sender
158 {
159 [con cancel];
160 [self postNotificationWithName:BSDownloadTaskCanceledNotification];
161
162 [super cancel:sender];
163 }
164
165 #pragma mark-
166 - (id)identifier
167 {
168 return [NSString stringWithFormat:@"%@-%p", self, self];
169 }
170 - (NSString *)title
171 {
172 return NSLocalizedString(@"Download.", @"Download.");
173 }
174 - (NSString *) messageInProgress
175 {
176 return [NSString stringWithFormat:NSLocalizedString(@"Download url(%@) (%.1fk)", "Download url(%@) (%.1fk)"),
177 [self url], [self currentLength] / 1024.0];
178 }
179 - (double) amount
180 {
181 if ([self contLength] == 0) return -1;
182 double rate = ((double)[self currentLength] / [self contLength]) * 100.0;
183 return rate >= 100.0 ? 100.0 : rate;
184 }
185 @end
186
187
188 @implementation BSDownloadTask(NSURLConnectionDelegate)
189 - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;
190 {
191 /*
192 if( [(NSHTTPURLResponse *)response statusCode] == 302 ) {
193 // dat���������
194 [self postNotificaionWithResponse:response];
195 }
196 */
197 [self setResponse:response];
198 [self postNotificaionWithResponse:response];
199 [connection cancel];
200 return nil;
201 }
202 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
203 {
204 BOOL disconnect = NO;
205
206 [self setResponse:response];
207
208 switch([(NSHTTPURLResponse *)response statusCode]) {
209 case 200:
210 case 206:
211 break;
212 case 304:
213 NSLog(@"Contents is not modifiered.");
214 disconnect = YES;
215 break;
216 case 404:
217 NSLog(@"Contents has not found.");
218 disconnect = YES;
219 break;
220 case 416:
221 NSLog(@"Range is missmatch.");
222 disconnect = YES;
223 break;
224 default:
225 NSLog(@"Unknown error.");
226 disconnect = YES;
227 break;
228 }
229 if(disconnect) {
230 [connection cancel];
231 [self postNotificaionWithResponse:response];
232
233 return;
234 }
235
236 [self postNotificaionWithResponseDontFinish:response];
237
238 [self setCurrentLength:0];
239 [self setContLength:[response expectedContentLength]];
240 }
241 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
242 {
243 if(!receivedData) {
244 if(currentLength) {
245 receivedData = [[NSMutableData alloc] initWithCapacity:currentLength];
246 } else {
247 receivedData = [[NSMutableData alloc] init];
248 }
249 }
250 if(!receivedData) {
251 // abort
252 [connection cancel];
253 [self postNotificationWithName:BSDownloadTaskInternalErrorNotification];
254
255 return;
256 }
257
258 [receivedData appendData:data];
259 [self setCurrentLength:[self currentLength] + [data length]];
260 }
261 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
262 {
263 // NSLog(@"-->%@",[[[NSString alloc] initWithData:receivedData encoding:NSShiftJISStringEncoding] autorelease]);
264
265 [self postNotificationWithName:BSDownloadTaskFinishDownloadNotification];
266 }
267 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
268 {
269 // abort
270
271 [self postNotificationWithName:BSDownloadTaskFailDownloadNotification];
272 }
273 @end
274
275 @implementation BSDownloadTask(TaskNotification)
276 - (void) postNotificationWithName:(NSString *)name
277 {
278 NSNotificationCenter *nc_;
279
280 nc_ = [NSNotificationCenter defaultCenter];
281 [nc_ postNotificationName : name
282 object : self];
283
284 isFinished = YES;
285 }
286 - (void) postNotificaionWithResponse:(NSURLResponse *)response
287 {
288 NSNotificationCenter *nc_;
289 NSDictionary *info;
290
291 nc_ = [NSNotificationCenter defaultCenter];
292
293 info = [NSDictionary dictionaryWithObjectsAndKeys:response, BSDownloadTaskServerResponseKey,
294 [NSNumber numberWithInt:[(NSHTTPURLResponse *)response statusCode]], BSDownloadTaskStatusCodeKey,
295 nil];
296 [nc_ postNotificationName : BSDownloadTaskAbortDownloadNotification
297 object : self
298 userInfo : info];
299
300 isFinished = YES;
301 }
302 - (void) postNotificaionWithResponseDontFinish:(NSURLResponse *)response
303 {
304 NSNotificationCenter *nc_;
305 NSDictionary *info;
306
307 nc_ = [NSNotificationCenter defaultCenter];
308
309 info = [NSDictionary dictionaryWithObjectsAndKeys:response, BSDownloadTaskServerResponseKey,
310 [NSNumber numberWithInt:[(NSHTTPURLResponse *)response statusCode]], BSDownloadTaskStatusCodeKey,
311 nil];
312 [nc_ postNotificationName : BSDownloadTaskReceiveResponceNotification
313 object : self
314 userInfo : info];
315 }
316
317 @end

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