Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FMDQueueItem.m
- // Free Mac Downloader
- //
- // Created by Diego Massanti on 9/23/09.
- // Copyright 2009 PlusMedia de Argentina LLC. All rights reserved.
- //
- #import "FMDQueueItem.h"
- @implementation FMDQueueItem
- @class FMDHeaders;
- @synthesize itemURL, itemPassword, itemUserName, itemName, itemStatus, itemIcon, itemStatusImage, itemHTTPHeaders, itemSize, itemLoadedBytes, isBusy;
- @synthesize delegate;
- @synthesize itemUISize, itemUILoadedBytes;
- - (id) initWithURL:(NSURL*)theURL
- {
- self = [super init];
- if (self != nil) {
- DLog(@"FMDQueueItem initialized for URL %@", [theURL absoluteString]);
- [self setItemURL:theURL];
- [self setItemName:[[[theURL absoluteString]lastPathComponent]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
- // Set Item representative icon
- self.itemLoadedBytes = 0;
- self.itemSize = 0;
- itemHeadersOperation = [[FMDHeadersOperation alloc]initWithURL:self.itemURL];
- [self addDependency:itemHeadersOperation];
- //[itemHeadersOperation release];
- [itemHeadersOperation addObserver:self
- forKeyPath:@"isFinished"
- options:NSKeyValueObservingOptionNew
- context:NULL];
- [itemHeadersOperation setUserName:self.itemUserName andPassword:self.itemPassword];
- [[[self delegate]mainQueue]addOperation:itemHeadersOperation];
- [self setIsBusy:YES];
- }
- return self;
- }
- - (void) dealloc
- {
- DLog(@"Deallocating queue item... BYE!");
- [super dealloc];
- }
- - (void)setItemName:(NSString *)theName
- {
- [self willChangeValueForKey:@"itemName"];
- itemName = theName;
- [self didChangeValueForKey:@"itemName"];
- [self willChangeValueForKey:@"itemIcon"];
- [self setItemIcon:[[NSWorkspace sharedWorkspace]iconForFileType:[[self itemName]pathExtension]]];
- [self didChangeValueForKey:@"itemIcon"];
- }
- - (void)main
- {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- DLog(@"Queue Item main method called");
- if ([itemHeadersOperation curlError]) {
- NSAlert * headersAlert = [[NSAlert alloc]init];
- [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."];
- [headersAlert setMessageText:[NSString stringWithFormat:@"Download Thingy Error - Code %u", [itemHeadersOperation curlError]]];
- [headersAlert setAlertStyle:NSCriticalAlertStyle];
- [headersAlert beginSheetModalForWindow:[[self delegate]window]
- modalDelegate:self
- didEndSelector:nil
- contextInfo:nil];
- } else {
- DLog(@"No errors so far... proceeding with the download");
- if ([self.itemHTTPHeaders valueForKey:@"Location"]) {
- NSString * newLocation = [[[self.itemHTTPHeaders valueForKey:@"Location"]lastPathComponent]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- [self setItemName:newLocation];
- }
- }
- // We don't need the headers OP anymore.
- [itemHeadersOperation release];
- [self setIsBusy:NO];
- // Configure item params
- [self setItemSize:[[self.itemHTTPHeaders valueForKey:@"Content-Length"]doubleValue]];
- // Initialize FilePackage Instance.
- itemPackage = [[FMDPackage alloc]initWithFileName:self.itemName];
- [itemPackage setDelegate:self];
- // Check package
- if ([itemPackage packageExists]) {
- DLog(@"Package exists for %@...", self.itemName);
- } else {
- DLog(@"Package DOES NOT exists for %@...", self.itemName);
- // Calculate segments.
- itemPackage.segments = [[NSArray alloc]initWithArray:[self getSegments:5 forRange:FMDMakeRange(0, self.itemSize)]];
- [itemPackage createEmptyPackage];
- }
- [pool release];
- }
- #pragma mark -
- #pragma mark Segments Processing Methods
- - (NSArray*)getSegments:(int)howMany forRange:(FMDRange)range
- {
- // At this point we get an FMDRange and we divide it so that we
- // end up with an array of n (howMany) ranges.
- UInt64 maxBytes = range.length; // the total bytes in the range.
- int remainingBytes = lldiv(maxBytes, howMany).rem;
- UInt64 segmentLength = maxBytes / howMany;
- int i = 0;
- NSMutableArray * returnArray = [[NSMutableArray alloc]initWithCapacity:howMany];
- int extraBytes = 0;
- for (i; i < howMany; i++) {
- if (i == howMany - 1) {
- extraBytes = remainingBytes;
- }
- int segmentStartOffset = (i == 0) ? 0 : 1;
- FMDRange segment = FMDMakeRange(i * segmentLength + segmentStartOffset, (i *segmentLength) + segmentLength + extraBytes);
- DLog(@"Created a segment from %qi to %qi", segment.location, segment.length);
- NSDictionary * segmentInfo = [[NSDictionary alloc]initWithObjects:
- [NSArray arrayWithObjects:[NSNumber numberWithLong:segment.location], [NSNumber numberWithLong:segment.length], nil]
- forKeys:[NSArray arrayWithObjects:@"start", @"length", nil]];
- [returnArray addObject:segmentInfo];
- [segmentInfo release];
- }
- return returnArray;
- }
- #pragma mark -
- #pragma mark KVO Observers
- - (void)observeValueForKeyPath:(NSString *)keyPath
- ofObject:(id)object
- change:(NSDictionary *)change
- context:(void *)context
- {
- if ([keyPath isEqual:@"isFinished"]) {
- if ([[change valueForKey:NSKeyValueChangeNewKey]boolValue] == YES) {
- FMDHeadersOperation * headersOperation = object;
- DLog(@"Dependency finished");
- [self setItemHTTPHeaders:headersOperation.headers];
- //[headersOperation release];
- }
- }
- }
- #pragma mark -
- #pragma mark Getters & Setters
- - (void)setItemSize:(double)theSize
- {
- itemSize = theSize;
- self.itemUISize = [[NSNumber numberWithDouble:self.itemSize]humanReadableSize];
- }
- - (void)setItemLoadedBytes:(double)theBytes
- {
- itemLoadedBytes = theBytes;
- self.itemUILoadedBytes = [[NSNumber numberWithDouble:self.itemLoadedBytes]humanReadableSize];
- }
- @end
Add Comment
Please, Sign In to add comment