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

« back to all changes in this revision

Viewing changes to musicstreaming/utilities/operations/AlbumArtLoadingOperation.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
1
//
2
 
//  ImageLoadingOperation.m
 
2
//  AlbumArtLoadingOperation.m
3
3
//  iSub
4
4
//
5
5
//  Created by Aaron Brethorst on 5/4/11.
8
8
 
9
9
// adapted from http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent_operations/
10
10
 
11
 
#import "ImageLoadingOperation.h"
12
 
 
13
 
@interface ImageLoadingOperation ()
14
 
- (void)finish;
15
 
@end
16
 
 
17
 
@implementation ImageLoadingOperation
 
11
#import "AlbumArtLoadingOperation.h"
 
12
 
 
13
@implementation AlbumArtLoadingOperation
18
14
@synthesize delegate;
19
 
@synthesize url = _url;
20
 
@synthesize statusCode = _statusCode;
21
15
@synthesize data = _data;
22
 
@synthesize error = _error;
23
 
@synthesize isExecuting = _isExecuting;
24
 
@synthesize isFinished = _isFinished;
25
16
 
26
 
+ (id)imageLoaderWithUrlString:(NSString *)urlString
 
17
+ (id)AlbumArtLoaderWithUrlString:(NSString *)urlString
27
18
{
28
19
    NSURL * url = [NSURL URLWithString:urlString];
29
 
    ImageLoadingOperation * operation = [[self alloc] initWithUrl:url];
 
20
    AlbumArtLoadingOperation * operation = [[self alloc] initWithUrl:url];
30
21
    return [operation autorelease];
31
22
}
32
23
 
33
 
- (id)initWithUrl:(NSURL *)url
 
24
- (id)initWithArtId:(NSString *)artId URL:(NSURL *)url
34
25
{
35
 
    self = [super init];
 
26
    self = [super initWithUrl:url];
36
27
    if (self == nil)
37
28
        return nil;
38
29
    
39
 
    _url = [url copy];
40
 
    _isExecuting = NO;
41
 
    _isFinished = NO;
42
 
    
 
30
    _artId = [artId retain];
43
31
    return self;
 
32
 
44
33
}
45
34
 
46
35
- (void)dealloc
47
36
{
48
 
    [_url release];
49
 
    [_connection release];
50
 
    [_data release];
51
 
    [_error release];
52
 
    [super dealloc];
53
 
}
54
 
 
55
 
- (BOOL)isConcurrent
56
 
{
57
 
    return YES;
58
 
}
59
 
 
60
 
- (void)start
61
 
{
62
 
    if (![NSThread isMainThread])
63
 
    {
64
 
        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
65
 
        return;
66
 
    }
67
 
    
68
 
    NSLog(@"opeartion for <%@> started.", _url);
69
 
    
70
 
    [self willChangeValueForKey:@"isExecuting"];
71
 
    _isExecuting = YES;
72
 
    [self didChangeValueForKey:@"isExecuting"];
73
 
        
74
 
    NSURLRequest * request = [NSURLRequest requestWithURL:_url];
75
 
    _connection = [[NSURLConnection alloc] initWithRequest:request
76
 
                                                  delegate:self];
77
 
    if (_connection == nil)
78
 
        [self finish];
79
 
}
80
 
 
81
 
- (void)finish
82
 
{
83
 
    NSLog(@"operation for <%@> finished. "
84
 
          @"status code: %d, error: %@, data size: %u",
85
 
          _url, _statusCode, _error, [_data length]);
86
 
    
87
 
    [_connection release];
88
 
    _connection = nil;
89
 
    
90
 
    [self willChangeValueForKey:@"isExecuting"];
91
 
    [self willChangeValueForKey:@"isFinished"];
92
 
        
93
 
    _isExecuting = NO;
94
 
    _isFinished = YES;
95
 
        
96
 
    [self didChangeValueForKey:@"isExecuting"];
97
 
    [self didChangeValueForKey:@"isFinished"];
 
37
        [_data release];
 
38
    [_artId release];
 
39
        [super dealloc];
98
40
}
99
41
 
100
42
#pragma mark -
101
43
#pragma mark NSURLConnection delegate
102
44
 
103
 
- (void)connection:(NSURLConnection *)connection
104
 
didReceiveResponse:(NSURLResponse *)response
 
45
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
105
46
{
 
47
        [super connection:connection didReceiveResponse:response];
 
48
        
106
49
    [_data release];
107
50
    _data = [[NSMutableData alloc] init];
108
 
        
109
 
    NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
110
 
    _statusCode = [httpResponse statusCode];
111
51
}
112
52
 
113
 
- (void)connection:(NSURLConnection *)connection
114
 
    didReceiveData:(NSData *)data
 
53
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
115
54
{
 
55
        [super connection:connection didReceiveData:data];
 
56
        
116
57
    [_data appendData:data];
117
58
}
118
59
 
119
60
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
120
61
{
121
 
        UIImage *image = [[UIImage alloc] initWithData:_data];
 
62
        UIImage *image = [[[UIImage alloc] initWithData:_data] autorelease];
122
63
        if (image)
123
64
        {
124
 
                [self.delegate imageLoaded:image forURL:_url];
 
65
                [self.delegate imageLoaded:image forArtId:_artId];
125
66
        }
126
 
    [self finish];
 
67
        [super connectionDidFinishLoading:connection];
127
68
}
128
69
 
129
 
- (void)connection:(NSURLConnection *)connection
130
 
  didFailWithError:(NSError *)error
 
70
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
131
71
{
132
 
    _error = [error copy];
133
 
    [self finish];
 
72
        [super connection:connection didFailWithError:error];
134
73
}
135
74
 
136
 
 
137
 
 
138
75
@end