~ubuntuone-ios-client-team/ubuntuone-ios-contacts/trunk

« back to all changes in this revision

Viewing changes to musicstreaming/utilities/operations/DownloadOperation.m

  • Committer: Jason Foreman
  • Date: 2011-06-16 18:33:42 UTC
  • mfrom: (191.1.60 master)
  • Revision ID: jason.foreman@canonical.com-20110616183342-vl7a4804xsf0s1b5
Merging branches for v 2.0.

lp:~threeve/ubuntuone-ios-client/master
lp:~urbanape/ubuntuone-ios-client/downloader

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  DownloadOperation.m
 
3
//  iSub
 
4
//
 
5
//  Created by Aaron Brethorst on 5/12/11.
 
6
//  Copyright 2011 Canonical. All rights reserved.
 
7
//
 
8
 
 
9
#import "DownloadOperation.h"
 
10
 
 
11
@implementation DownloadOperation
 
12
@synthesize filePath = _filePath;
 
13
@synthesize delegate = _delegate;
 
14
 
 
15
+ (id)downloadOperationWithURL:(NSURL*)anURL filePath:(NSString*)aFilePath
 
16
{
 
17
        DownloadOperation *operation = [[[DownloadOperation alloc] initWithUrl:anURL filePath:aFilePath] autorelease];
 
18
        return operation;
 
19
}
 
20
 
 
21
- (id)initWithUrl:(NSURL*)anUrl filePath:(NSString*)aFilePath
 
22
{
 
23
        if ((self = [super initWithUrl:anUrl]))
 
24
        {
 
25
                self.delegate = nil;
 
26
                
 
27
                self.filePath = aFilePath;
 
28
                
 
29
                [[NSFileManager defaultManager] createFileAtPath:self.filePath contents:[NSData data] attributes:nil];
 
30
                _fileHandle = [[NSFileHandle fileHandleForWritingAtPath:self.filePath] retain];
 
31
        }
 
32
        return self;
 
33
}
 
34
 
 
35
- (void)dealloc
 
36
{
 
37
        self.delegate = nil;
 
38
        self.filePath = nil;
 
39
        [_fileHandle release];
 
40
        [super dealloc];
 
41
}
 
42
 
 
43
#pragma mark -
 
44
#pragma mark NSURLConnection delegate
 
45
 
 
46
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 
47
{
 
48
        [super connection:connection didReceiveResponse:response];
 
49
}
 
50
 
 
51
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 
52
{
 
53
        [super connection:connection didReceiveData:data];
 
54
        
 
55
        [_fileHandle writeData:data];
 
56
}
 
57
 
 
58
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
 
59
{
 
60
        [_fileHandle closeFile];
 
61
        
 
62
        if (nil != self.delegate)
 
63
        {
 
64
                [self.delegate performSelectorOnMainThread:@selector(songDownloadedToPath:) withObject:self.filePath waitUntilDone:NO];
 
65
        }
 
66
        
 
67
        [super connectionDidFinishLoading:connection];
 
68
}
 
69
 
 
70
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 
71
{
 
72
        [_fileHandle closeFile];
 
73
        [[NSFileManager defaultManager] removeItemAtPath:self.filePath error:nil];
 
74
        
 
75
        [super connection:connection didFailWithError:error];
 
76
}
 
77
 
 
78
@end
 
79