--- bathyscaphe/trunk/application/source/browser/BSDownloadTask.m 2012/04/29 00:30:11 1421 +++ bathyscaphe/trunk/application/source/browser/BSDownloadTask.m 2012/05/01 13:07:47 1422 @@ -3,7 +3,7 @@ // BathyScaphe // // Created by Hori,Masaki on 06/08/06. -// Copyright 2006-2009 BathyScaphe Project. All rights reserved. +// Copyright 2006-2009,2012 BathyScaphe Project. All rights reserved. // encoding="UTF-8" // @@ -19,18 +19,34 @@ NSString *const BSDownloadTaskFailDownloadNotification = @"BSDownloadTaskFailDownloadNotification"; NSString *const BSDownloadTaskErrorObjectKey = @"BSDownloadTaskErrorObjectKey"; // NSError +@interface BSDownloadTask () +// re-declare override Writability +@property (readwrite, copy) NSString *message; + +@property CGFloat currentLength; +@property CGFloat contLength; +@property (retain) id response; +@end @implementation BSDownloadTask +// message property implementation in BSThreadListTask +@dynamic message; + +@synthesize URL = m_targetURL; +@synthesize currentLength = m_currentLength; +@synthesize contLength = m_contLength; +@synthesize response = _response; + + (id)taskWithURL:(NSURL *)url { return [[[self alloc] initWithURL:url] autorelease]; } -- (id) initWithURL:(NSURL *)url +- (id)initWithURL:(NSURL *)url { if(self = [super init]) { - [self setURL:url]; - [self setIsInProgress:YES]; + self.URL = url; + self.isInProgress = YES; m_contLengthIsUnknown = YES; } @@ -53,7 +69,7 @@ - (void)dealloc { - [self setURL:nil]; + [m_targetURL release]; [con release]; [receivedData release]; [method release]; @@ -63,70 +79,32 @@ } #pragma mark Accessors -- (void)setURL:(NSURL *)url -{ - id temp = m_targetURL; - m_targetURL = [url retain]; - [temp release]; -} - -- (NSURL *)url -{ - return m_targetURL; -} - -- (void)setCurrentLength:(CGFloat)doubleValue -{ - m_currentLength = doubleValue; -} - -- (CGFloat)currentLength -{ - return m_currentLength; -} - -- (void)setContLength:(CGFloat)i -{ - m_contLength = i; -} - -- (CGFloat)contLength -{ - return m_contLength; -} - (NSData *)receivedData { return receivedData; } -- (void)setResponse:(id)response -{ - id temp = _response; - _response = [response retain]; - [temp release]; -} -- (id)response +#pragma mark Overrides +- (void)excute { - return _response; + [self synchronousDownLoad]; } - -#pragma mark Overrides -- (void)doExecuteWithLayout:(CMRThreadLayout *)layout +- (void)synchronousDownLoad { NSRunLoop *loop = [NSRunLoop currentRunLoop]; [receivedData release]; receivedData = nil; - [self setCurrentLength:0]; - [self setContLength:0]; - [self setAmount:-1]; - [self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@)", @"Downloader", @""), [[self url] absoluteString]]]; + self.currentLength = 0; + self.contLength = 0; + self.amount = -1; + self.message = [NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@)", @"Downloader", @""), [self.URL absoluteString]]; NSMutableURLRequest *request; - request = [NSMutableURLRequest requestWithURL:[self url]]; + request = [NSMutableURLRequest requestWithURL:self.URL]; if (!request) { [self postNotificationWithName:BSDownloadTaskInternalErrorNotification userInfo:nil]; return; @@ -142,23 +120,15 @@ return; } - while ([self isInProgress]) { + while (self.isInProgress) { id pool = [[NSAutoreleasePool alloc] init]; - @try { - [loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; - } - @catch(id ex) { - // do nothing. - @throw; - } - @finally { - [pool release]; - } + [loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; + [pool release]; } } #pragma mark CMRTask -- (IBAction)cancel:(id)sender +- (void)cancel:(id)sender { [con cancel]; [self postNotificationWithName:BSDownloadTaskCanceledNotification userInfo:nil]; @@ -166,25 +136,11 @@ [super cancel:sender]; } -- (id)identifier -{ - return [NSString stringWithFormat:@"%@-%p", self, self]; -} - - (NSString *)title { return NSLocalizedStringFromTable(@"Download.", @"Downloader", @""); } -- (double)amount -{ - return m_taskAmount; -} - -- (void)setAmount:(double)doubleValue -{ - m_taskAmount = doubleValue; -} @end @@ -195,7 +151,7 @@ if (!response) { return request; } - [self setResponse:response]; + self.response = response; [self postNotificaionWithResponse:response]; [connection cancel]; return nil; @@ -205,7 +161,7 @@ { BOOL disconnect = NO; - [self setResponse:response]; + self.response = response; if ([[NSUserDefaults standardUserDefaults] boolForKey:BSUserDebugEnabledKey]) { NSLog(@"** USER DEBUG **\n%@", [(NSHTTPURLResponse *)response allHeaderFields]); } @@ -242,12 +198,12 @@ if (length <= 0) { CGFloat assumedLength = [[[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Content-Length"] doubleValue]; if (assumedLength > 0) { - [self setContLength:assumedLength*1.7]; // gzip 圧縮を考慮、適当 + self.contLength = assumedLength*1.7; // gzip 圧縮を考慮、適当 m_contLengthIsUnknown = NO; } } else { m_contLengthIsUnknown = NO; - [self setContLength:length]; + self.contLength = length; } } @@ -266,16 +222,16 @@ } [receivedData appendData:data]; - [self setCurrentLength:[receivedData length]]; + self.currentLength = [receivedData length]; - if (!m_contLengthIsUnknown && ([self contLength] > 0)) { - CGFloat bar = [self currentLength]/[self contLength]*100.0; - [self setAmount:bar]; - [self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk of %.0fk)", @"Downloader", @""), - [[self url] absoluteString], (CGFloat)[self currentLength]/1024, (CGFloat)[self contLength]/1024]]; + if (!m_contLengthIsUnknown && (self.contLength > 0)) { + CGFloat bar = self.currentLength/self.contLength*100.0; + self.amount = bar; + self.message = [NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk of %.0fk)", @"Downloader", @""), + [self.URL absoluteString], (CGFloat)self.currentLength/1024, (CGFloat)self.contLength/1024]; } else { - [self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk)", @"Downloader", @""), - [[self url] absoluteString], (CGFloat)[self currentLength]/1024]]; + self.message = [NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk)", @"Downloader", @""), + [self.URL absoluteString], (CGFloat)self.currentLength/1024]; } } @@ -289,7 +245,7 @@ // abort NSDictionary *userInfo = [NSDictionary dictionaryWithObject:error forKey:BSDownloadTaskErrorObjectKey]; [self postNotificationWithName:BSDownloadTaskFailDownloadNotification userInfo:userInfo]; - [self setIsInProgress:NO]; + self.isInProgress = NO; } @end @@ -298,7 +254,7 @@ - (void)postNotificationWithName:(NSString *)name userInfo:(NSDictionary *)info { [[NSNotificationCenter defaultCenter] postNotificationName:name object:self userInfo:info]; - [self setIsInProgress:NO]; + self.isInProgress = NO; } - (void)postNotificaionWithResponse:(NSURLResponse *)response