Guest User

Untitled

a guest
Jul 14th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. //
  2. // FMDQueueItem.m
  3. // Free Mac Downloader
  4. //
  5. // Created by Diego Massanti on 9/23/09.
  6. // Copyright 2009 PlusMedia de Argentina LLC. All rights reserved.
  7. //
  8.  
  9. #import "FMDQueueItem.h"
  10.  
  11.  
  12. @implementation FMDQueueItem
  13.  
  14. @class FMDHeaders;
  15.  
  16. @synthesize itemURL, itemPassword, itemUserName, itemName, itemStatus, itemIcon, itemStatusImage, itemHTTPHeaders, itemSize, itemLoadedBytes, isBusy;
  17. @synthesize delegate;
  18.  
  19. @synthesize itemUISize, itemUILoadedBytes;
  20.  
  21. - (id) initWithURL:(NSURL*)theURL
  22. {
  23. self = [super init];
  24. if (self != nil) {
  25. DLog(@"FMDQueueItem initialized for URL %@", [theURL absoluteString]);
  26. [self setItemURL:theURL];
  27. [self setItemName:[[[theURL absoluteString]lastPathComponent]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  28. // Set Item representative icon
  29.  
  30. self.itemLoadedBytes = 0;
  31. self.itemSize = 0;
  32. itemHeadersOperation = [[FMDHeadersOperation alloc]initWithURL:self.itemURL];
  33.  
  34. [self addDependency:itemHeadersOperation];
  35.  
  36. //[itemHeadersOperation release];
  37. [itemHeadersOperation addObserver:self
  38. forKeyPath:@"isFinished"
  39. options:NSKeyValueObservingOptionNew
  40. context:NULL];
  41.  
  42. [itemHeadersOperation setUserName:self.itemUserName andPassword:self.itemPassword];
  43. [[[self delegate]mainQueue]addOperation:itemHeadersOperation];
  44. [self setIsBusy:YES];
  45.  
  46.  
  47. }
  48. return self;
  49. }
  50.  
  51. - (void) dealloc
  52. {
  53.  
  54.  
  55.  
  56. DLog(@"Deallocating queue item... BYE!");
  57.  
  58. [super dealloc];
  59. }
  60.  
  61.  
  62. - (void)setItemName:(NSString *)theName
  63. {
  64. [self willChangeValueForKey:@"itemName"];
  65. itemName = theName;
  66. [self didChangeValueForKey:@"itemName"];
  67.  
  68. [self willChangeValueForKey:@"itemIcon"];
  69. [self setItemIcon:[[NSWorkspace sharedWorkspace]iconForFileType:[[self itemName]pathExtension]]];
  70. [self didChangeValueForKey:@"itemIcon"];
  71. }
  72.  
  73. - (void)main
  74. {
  75.  
  76. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  77. DLog(@"Queue Item main method called");
  78. if ([itemHeadersOperation curlError]) {
  79.  
  80. NSAlert * headersAlert = [[NSAlert alloc]init];
  81. [headersAlert setInformativeText:@"There was an error when querying the server for preflight info, this could happen if the server is too busy to process your request or due to some other network problems."];
  82. [headersAlert setMessageText:[NSString stringWithFormat:@"Download Thingy Error - Code %u", [itemHeadersOperation curlError]]];
  83. [headersAlert setAlertStyle:NSCriticalAlertStyle];
  84. [headersAlert beginSheetModalForWindow:[[self delegate]window]
  85. modalDelegate:self
  86. didEndSelector:nil
  87. contextInfo:nil];
  88. } else {
  89. DLog(@"No errors so far... proceeding with the download");
  90. if ([self.itemHTTPHeaders valueForKey:@"Location"]) {
  91. NSString * newLocation = [[[self.itemHTTPHeaders valueForKey:@"Location"]lastPathComponent]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  92. [self setItemName:newLocation];
  93. }
  94.  
  95. }
  96.  
  97. // We don't need the headers OP anymore.
  98. [itemHeadersOperation release];
  99.  
  100. [self setIsBusy:NO];
  101.  
  102. // Configure item params
  103.  
  104. [self setItemSize:[[self.itemHTTPHeaders valueForKey:@"Content-Length"]doubleValue]];
  105. // Initialize FilePackage Instance.
  106.  
  107. itemPackage = [[FMDPackage alloc]initWithFileName:self.itemName];
  108. [itemPackage setDelegate:self];
  109.  
  110. // Check package
  111. if ([itemPackage packageExists]) {
  112. DLog(@"Package exists for %@...", self.itemName);
  113. } else {
  114. DLog(@"Package DOES NOT exists for %@...", self.itemName);
  115. // Calculate segments.
  116. itemPackage.segments = [[NSArray alloc]initWithArray:[self getSegments:5 forRange:FMDMakeRange(0, self.itemSize)]];
  117.  
  118.  
  119. [itemPackage createEmptyPackage];
  120. }
  121.  
  122. [pool release];
  123.  
  124. }
  125.  
  126.  
  127. #pragma mark -
  128. #pragma mark Segments Processing Methods
  129.  
  130. - (NSArray*)getSegments:(int)howMany forRange:(FMDRange)range
  131. {
  132. // At this point we get an FMDRange and we divide it so that we
  133. // end up with an array of n (howMany) ranges.
  134.  
  135. UInt64 maxBytes = range.length; // the total bytes in the range.
  136.  
  137. int remainingBytes = lldiv(maxBytes, howMany).rem;
  138. UInt64 segmentLength = maxBytes / howMany;
  139.  
  140. int i = 0;
  141. NSMutableArray * returnArray = [[NSMutableArray alloc]initWithCapacity:howMany];
  142. int extraBytes = 0;
  143. for (i; i < howMany; i++) {
  144. if (i == howMany - 1) {
  145. extraBytes = remainingBytes;
  146. }
  147. int segmentStartOffset = (i == 0) ? 0 : 1;
  148. FMDRange segment = FMDMakeRange(i * segmentLength + segmentStartOffset, (i *segmentLength) + segmentLength + extraBytes);
  149. DLog(@"Created a segment from %qi to %qi", segment.location, segment.length);
  150. NSDictionary * segmentInfo = [[NSDictionary alloc]initWithObjects:
  151. [NSArray arrayWithObjects:[NSNumber numberWithLong:segment.location], [NSNumber numberWithLong:segment.length], nil]
  152. forKeys:[NSArray arrayWithObjects:@"start", @"length", nil]];
  153. [returnArray addObject:segmentInfo];
  154. [segmentInfo release];
  155. }
  156. return returnArray;
  157. }
  158.  
  159.  
  160. #pragma mark -
  161. #pragma mark KVO Observers
  162.  
  163. - (void)observeValueForKeyPath:(NSString *)keyPath
  164. ofObject:(id)object
  165. change:(NSDictionary *)change
  166. context:(void *)context
  167. {
  168. if ([keyPath isEqual:@"isFinished"]) {
  169. if ([[change valueForKey:NSKeyValueChangeNewKey]boolValue] == YES) {
  170. FMDHeadersOperation * headersOperation = object;
  171. DLog(@"Dependency finished");
  172. [self setItemHTTPHeaders:headersOperation.headers];
  173. //[headersOperation release];
  174. }
  175.  
  176.  
  177. }
  178.  
  179. }
  180.  
  181. #pragma mark -
  182. #pragma mark Getters & Setters
  183.  
  184.  
  185. - (void)setItemSize:(double)theSize
  186. {
  187. itemSize = theSize;
  188. self.itemUISize = [[NSNumber numberWithDouble:self.itemSize]humanReadableSize];
  189. }
  190.  
  191. - (void)setItemLoadedBytes:(double)theBytes
  192. {
  193. itemLoadedBytes = theBytes;
  194. self.itemUILoadedBytes = [[NSNumber numberWithDouble:self.itemLoadedBytes]humanReadableSize];
  195. }
  196.  
  197. @end
Add Comment
Please, Sign In to add comment