5
// Created by Aaron Brethorst on 5/12/11.
6
// Copyright 2011 Canonical. All rights reserved.
9
#import "DownloadOperation.h"
11
@implementation DownloadOperation
12
@synthesize filePath = _filePath;
13
@synthesize delegate = _delegate;
15
+ (id)downloadOperationWithURL:(NSURL*)anURL filePath:(NSString*)aFilePath
17
DownloadOperation *operation = [[[DownloadOperation alloc] initWithUrl:anURL filePath:aFilePath] autorelease];
21
- (id)initWithUrl:(NSURL*)anUrl filePath:(NSString*)aFilePath
23
if ((self = [super initWithUrl:anUrl]))
27
self.filePath = aFilePath;
29
[[NSFileManager defaultManager] createFileAtPath:self.filePath contents:[NSData data] attributes:nil];
30
_fileHandle = [[NSFileHandle fileHandleForWritingAtPath:self.filePath] retain];
39
[_fileHandle release];
44
#pragma mark NSURLConnection delegate
46
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
48
[super connection:connection didReceiveResponse:response];
51
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
53
[super connection:connection didReceiveData:data];
55
[_fileHandle writeData:data];
58
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
60
[_fileHandle closeFile];
62
if (nil != self.delegate)
64
[self.delegate performSelectorOnMainThread:@selector(songDownloadedToPath:) withObject:self.filePath waitUntilDone:NO];
67
[super connectionDidFinishLoading:connection];
70
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
72
[_fileHandle closeFile];
73
[[NSFileManager defaultManager] removeItemAtPath:self.filePath error:nil];
75
[super connection:connection didFailWithError:error];