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

« back to all changes in this revision

Viewing changes to musicstreaming/utilities/operations/AbstractNetworkOperation.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
//  AbstractNetworkOperation.m
 
3
//  iSub
 
4
//
 
5
//  Created by Aaron Brethorst on 5/12/11.
 
6
//  Copyright 2011 Canonical. All rights reserved.
 
7
//
 
8
 
 
9
#import "AbstractNetworkOperation.h"
 
10
 
 
11
#import "UONetworkStatusCoordinator.h"
 
12
 
 
13
 
 
14
@interface AbstractNetworkOperation ()
 
15
- (void)finish;
 
16
@end
 
17
 
 
18
@implementation AbstractNetworkOperation
 
19
@synthesize url = _url;
 
20
@synthesize statusCode = _statusCode;
 
21
@synthesize error = _error;
 
22
@synthesize isExecuting = _isExecuting;
 
23
@synthesize isFinished = _isFinished;
 
24
 
 
25
- (id)initWithUrl:(NSURL *)url
 
26
{
 
27
    self = [super init];
 
28
    if (self == nil)
 
29
        return nil;
 
30
    
 
31
    _url = [url copy];
 
32
    _isExecuting = NO;
 
33
    _isFinished = NO;
 
34
    
 
35
    return self;
 
36
}
 
37
 
 
38
- (void)dealloc
 
39
{
 
40
    [_url release];
 
41
    [_connection release];
 
42
    [_error release];
 
43
    [super dealloc];
 
44
}
 
45
 
 
46
- (BOOL)isConcurrent
 
47
{
 
48
    return YES;
 
49
}
 
50
 
 
51
- (void)start
 
52
{
 
53
    if (![NSThread isMainThread])
 
54
    {
 
55
        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
 
56
        return;
 
57
    }
 
58
    
 
59
    NSLog(@"operation for <%@> started.", _url);
 
60
    
 
61
    [self willChangeValueForKey:@"isExecuting"];
 
62
    _isExecuting = YES;
 
63
    [self didChangeValueForKey:@"isExecuting"];
 
64
        
 
65
    NSURLRequest * request = [NSURLRequest requestWithURL:_url];
 
66
    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
 
67
        [UONetworkStatusCoordinator addNetworkActivity];
 
68
    if (nil == _connection)
 
69
        {
 
70
        [self finish];
 
71
        }
 
72
}
 
73
 
 
74
- (void)finish
 
75
{
 
76
    NSLog(@"operation for <%@> finished. "
 
77
          @"status code: %d, error: %@",
 
78
          _url, _statusCode, _error);
 
79
    
 
80
        [UONetworkStatusCoordinator removeNetworkActivity];
 
81
        
 
82
    [_connection release];
 
83
    _connection = nil;
 
84
    
 
85
    [self willChangeValueForKey:@"isExecuting"];
 
86
    [self willChangeValueForKey:@"isFinished"];
 
87
        
 
88
    _isExecuting = NO;
 
89
    _isFinished = YES;
 
90
        
 
91
    [self didChangeValueForKey:@"isExecuting"];
 
92
    [self didChangeValueForKey:@"isFinished"];
 
93
}
 
94
 
 
95
#pragma mark -
 
96
#pragma mark NSURLConnection delegate
 
97
 
 
98
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 
99
{       
 
100
    NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
 
101
    _statusCode = [httpResponse statusCode];
 
102
}
 
103
 
 
104
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 
105
{
 
106
    //no-op, subclasses must implement this.
 
107
}
 
108
 
 
109
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
 
110
{
 
111
    [self finish];
 
112
}
 
113
 
 
114
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 
115
{
 
116
    _error = [error copy];
 
117
    [self finish];
 
118
}
 
119
 
 
120
 
 
121
 
 
122
@end