オープンソース・ソフトウェアの開発とダウンロード

Subversion リポジトリの参照

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1421 by masakih, Tue Mar 23 11:56:22 2010 UTC revision 1422 by masakih, Tue May 1 13:07:47 2012 UTC
# Line 3  Line 3 
3  //  BathyScaphe  //  BathyScaphe
4  //  //
5  //  Created by Hori,Masaki on 06/08/06.  //  Created by Hori,Masaki on 06/08/06.
6  //  Copyright 2006-2009 BathyScaphe Project. All rights reserved.  //  Copyright 2006-2009,2012 BathyScaphe Project. All rights reserved.
7  //  encoding="UTF-8"  //  encoding="UTF-8"
8  //  //
9    
# Line 19  NSString *const BSDownloadTaskStatusCode Line 19  NSString *const BSDownloadTaskStatusCode
19  NSString *const BSDownloadTaskFailDownloadNotification = @"BSDownloadTaskFailDownloadNotification";  NSString *const BSDownloadTaskFailDownloadNotification = @"BSDownloadTaskFailDownloadNotification";
20  NSString *const BSDownloadTaskErrorObjectKey = @"BSDownloadTaskErrorObjectKey"; // NSError  NSString *const BSDownloadTaskErrorObjectKey = @"BSDownloadTaskErrorObjectKey"; // NSError
21    
22    @interface BSDownloadTask ()
23    // re-declare override Writability
24    @property (readwrite, copy) NSString *message;
25    
26    @property CGFloat currentLength;
27    @property CGFloat contLength;
28    @property (retain) id response;
29    @end
30    
31  @implementation BSDownloadTask  @implementation BSDownloadTask
32    // message property implementation in BSThreadListTask
33    @dynamic message;
34    
35    @synthesize URL = m_targetURL;
36    @synthesize currentLength = m_currentLength;
37    @synthesize contLength = m_contLength;
38    @synthesize response = _response;
39    
40  + (id)taskWithURL:(NSURL *)url  + (id)taskWithURL:(NSURL *)url
41  {  {
42          return [[[self alloc] initWithURL:url] autorelease];          return [[[self alloc] initWithURL:url] autorelease];
43  }  }
44    
45  - (id) initWithURL:(NSURL *)url  - (id)initWithURL:(NSURL *)url
46  {  {
47          if(self = [super init]) {          if(self = [super init]) {
48                  [self setURL:url];                  self.URL = url;
49                  [self setIsInProgress:YES];                  self.isInProgress = YES;
50          m_contLengthIsUnknown = YES;          m_contLengthIsUnknown = YES;
51          }          }
52                    
# Line 53  NSString *const BSDownloadTaskErrorObjec Line 69  NSString *const BSDownloadTaskErrorObjec
69    
70  - (void)dealloc  - (void)dealloc
71  {  {
72          [self setURL:nil];          [m_targetURL release];
73          [con release];          [con release];
74          [receivedData release];          [receivedData release];
75          [method release];          [method release];
# Line 63  NSString *const BSDownloadTaskErrorObjec Line 79  NSString *const BSDownloadTaskErrorObjec
79  }  }
80    
81  #pragma mark Accessors  #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;  
 }  
82    
83  - (NSData *)receivedData  - (NSData *)receivedData
84  {  {
85          return receivedData;          return receivedData;
86  }  }
87    
 - (void)setResponse:(id)response  
 {  
         id temp = _response;  
         _response = [response retain];  
         [temp release];  
 }  
88    
89  - (id)response  #pragma mark Overrides
90    - (void)excute
91  {  {
92          return _response;          [self synchronousDownLoad];
93  }  }
94    - (void)synchronousDownLoad
 #pragma mark Overrides  
 - (void)doExecuteWithLayout:(CMRThreadLayout *)layout  
95  {  {
96          NSRunLoop *loop = [NSRunLoop currentRunLoop];          NSRunLoop *loop = [NSRunLoop currentRunLoop];
97                                    
98          [receivedData release];          [receivedData release];
99          receivedData = nil;          receivedData = nil;
100          [self setCurrentLength:0];          self.currentLength = 0;
101          [self setContLength:0];          self.contLength = 0;
102          [self setAmount:-1];          self.amount = -1;
103      [self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@)", @"Downloader", @""), [[self url] absoluteString]]];          self.message = [NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@)", @"Downloader", @""), [self.URL absoluteString]];
104    
105          NSMutableURLRequest *request;          NSMutableURLRequest *request;
106                    
107          request = [NSMutableURLRequest requestWithURL:[self url]];          request = [NSMutableURLRequest requestWithURL:self.URL];
108          if (!request) {          if (!request) {
109                  [self postNotificationWithName:BSDownloadTaskInternalErrorNotification userInfo:nil];                  [self postNotificationWithName:BSDownloadTaskInternalErrorNotification userInfo:nil];
110                  return;                  return;
# Line 142  NSString *const BSDownloadTaskErrorObjec Line 120  NSString *const BSDownloadTaskErrorObjec
120                  return;                  return;
121          }          }
122                    
123          while ([self isInProgress]) {          while (self.isInProgress) {
124                  id pool = [[NSAutoreleasePool alloc] init];                  id pool = [[NSAutoreleasePool alloc] init];
125                  @try {                  [loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
126                          [loop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];                  [pool release];
                 }  
                 @catch(id ex) {  
                         // do nothing.  
                         @throw;  
                 }  
                 @finally {  
                         [pool release];  
                 }  
127          }          }
128  }  }
129    
130  #pragma mark CMRTask  #pragma mark CMRTask
131  - (IBAction)cancel:(id)sender  - (void)cancel:(id)sender
132  {  {
133          [con cancel];          [con cancel];
134          [self postNotificationWithName:BSDownloadTaskCanceledNotification userInfo:nil];          [self postNotificationWithName:BSDownloadTaskCanceledNotification userInfo:nil];
# Line 166  NSString *const BSDownloadTaskErrorObjec Line 136  NSString *const BSDownloadTaskErrorObjec
136          [super cancel:sender];          [super cancel:sender];
137  }  }
138    
 - (id)identifier  
 {  
         return [NSString stringWithFormat:@"%@-%p", self, self];  
 }  
   
139  - (NSString *)title  - (NSString *)title
140  {  {
141          return NSLocalizedStringFromTable(@"Download.", @"Downloader", @"");          return NSLocalizedStringFromTable(@"Download.", @"Downloader", @"");
142  }  }
143    
 - (double)amount  
 {  
         return m_taskAmount;  
 }  
   
 - (void)setAmount:(double)doubleValue  
 {  
         m_taskAmount = doubleValue;  
 }  
144  @end  @end
145    
146    
# Line 195  NSString *const BSDownloadTaskErrorObjec Line 151  NSString *const BSDownloadTaskErrorObjec
151          if (!response) {          if (!response) {
152                  return request;                  return request;
153          }          }
154          [self setResponse:response];          self.response = response;
155          [self postNotificaionWithResponse:response];          [self postNotificaionWithResponse:response];
156          [connection cancel];          [connection cancel];
157          return nil;          return nil;
# Line 205  NSString *const BSDownloadTaskErrorObjec Line 161  NSString *const BSDownloadTaskErrorObjec
161  {  {
162          BOOL disconnect = NO;          BOOL disconnect = NO;
163                    
164          [self setResponse:response];          self.response = response;
165      if ([[NSUserDefaults standardUserDefaults] boolForKey:BSUserDebugEnabledKey]) {      if ([[NSUserDefaults standardUserDefaults] boolForKey:BSUserDebugEnabledKey]) {
166          NSLog(@"** USER DEBUG **\n%@", [(NSHTTPURLResponse *)response allHeaderFields]);          NSLog(@"** USER DEBUG **\n%@", [(NSHTTPURLResponse *)response allHeaderFields]);
167          }          }
# Line 242  NSString *const BSDownloadTaskErrorObjec Line 198  NSString *const BSDownloadTaskErrorObjec
198      if (length <= 0) {      if (length <= 0) {
199          CGFloat assumedLength = [[[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Content-Length"] doubleValue];          CGFloat assumedLength = [[[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Content-Length"] doubleValue];
200          if (assumedLength > 0) {          if (assumedLength > 0) {
201              [self setContLength:assumedLength*1.7]; // gzip 圧縮を考慮、適当                          self.contLength = assumedLength*1.7; // gzip 圧縮を考慮、適当
202              m_contLengthIsUnknown = NO;              m_contLengthIsUnknown = NO;
203          }          }
204      } else {      } else {
205          m_contLengthIsUnknown = NO;          m_contLengthIsUnknown = NO;
206          [self setContLength:length];                  self.contLength = length;
207      }      }
208  }  }
209    
# Line 266  NSString *const BSDownloadTaskErrorObjec Line 222  NSString *const BSDownloadTaskErrorObjec
222          }          }
223                    
224          [receivedData appendData:data];          [receivedData appendData:data];
225          [self setCurrentLength:[receivedData length]];          self.currentLength = [receivedData length];
226    
227          if (!m_contLengthIsUnknown && ([self contLength] > 0)) {          if (!m_contLengthIsUnknown && (self.contLength > 0)) {
228                  CGFloat bar = [self currentLength]/[self contLength]*100.0;                  CGFloat bar = self.currentLength/self.contLength*100.0;
229                  [self setAmount:bar];                  self.amount = bar;
230                  [self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk of %.0fk)", @"Downloader", @""),                  self.message = [NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk of %.0fk)", @"Downloader", @""),
231                                                                            [[self url] absoluteString], (CGFloat)[self currentLength]/1024, (CGFloat)[self contLength]/1024]];                                                  [self.URL absoluteString], (CGFloat)self.currentLength/1024, (CGFloat)self.contLength/1024];
232      } else {      } else {
233          [self setMessage:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk)", @"Downloader", @""),                  self.message = [NSString stringWithFormat:NSLocalizedStringFromTable(@"Download url(%@) (%.0fk)", @"Downloader", @""),
234                                      [[self url] absoluteString], (CGFloat)[self currentLength]/1024]];                                                  [self.URL absoluteString], (CGFloat)self.currentLength/1024];
235      }      }
236  }  }
237    
# Line 289  NSString *const BSDownloadTaskErrorObjec Line 245  NSString *const BSDownloadTaskErrorObjec
245          // abort          // abort
246      NSDictionary *userInfo = [NSDictionary dictionaryWithObject:error forKey:BSDownloadTaskErrorObjectKey];      NSDictionary *userInfo = [NSDictionary dictionaryWithObject:error forKey:BSDownloadTaskErrorObjectKey];
247      [self postNotificationWithName:BSDownloadTaskFailDownloadNotification userInfo:userInfo];      [self postNotificationWithName:BSDownloadTaskFailDownloadNotification userInfo:userInfo];
248          [self setIsInProgress:NO];          self.isInProgress = NO;
249  }  }
250  @end  @end
251    
# Line 298  NSString *const BSDownloadTaskErrorObjec Line 254  NSString *const BSDownloadTaskErrorObjec
254  - (void)postNotificationWithName:(NSString *)name userInfo:(NSDictionary *)info  - (void)postNotificationWithName:(NSString *)name userInfo:(NSDictionary *)info
255  {  {
256          [[NSNotificationCenter defaultCenter] postNotificationName:name object:self userInfo:info];          [[NSNotificationCenter defaultCenter] postNotificationName:name object:self userInfo:info];
257          [self setIsInProgress:NO];          self.isInProgress = NO;
258  }  }
259    
260  - (void)postNotificaionWithResponse:(NSURLResponse *)response  - (void)postNotificaionWithResponse:(NSURLResponse *)response

Legend:
Removed from v.1421  
changed lines
  Added in v.1422

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