leonho / iphone-libs
- Source
- Commits
- Network (4)
- Issues (1)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
f6ecc6c
iphone-libs / MultipleDownload.m
| 129f245a » | leonho | 2008-10-22 | 1 | // | |
| 2 | // MultipleDownload.m | ||||
| 3 | // | ||||
| c4d72b18 » | leonho | 2008-11-05 | 4 | // Copyright 2008 Stepcase Limited. | |
| 129f245a » | leonho | 2008-10-22 | 5 | // | |
| 6 | |||||
| 7 | #import "MultipleDownload.h" | ||||
| 8 | |||||
| 9 | |||||
| 10 | @implementation MultipleDownload | ||||
| 11 | |||||
| c0dc332c » | leonho | 2008-10-22 | 12 | @synthesize urls, requests, receivedDatas, finishCount; | |
| 129f245a » | leonho | 2008-10-22 | 13 | ||
| 14 | - init { | ||||
| 15 | if ((self = [super init])) { | ||||
| c0dc332c » | leonho | 2008-10-22 | 16 | self.finishCount = 0; | |
| f6ecc6c0 » | leonho | 2008-11-12 | 17 | NSMutableArray *array = [[NSMutableArray alloc] init]; | |
| 18 | self.receivedDatas = array; | ||||
| 19 | [array release]; | ||||
| 20 | |||||
| 21 | NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; | ||||
| 22 | self.requests = dict; | ||||
| 23 | [dict release]; | ||||
| 24 | |||||
| 129f245a » | leonho | 2008-10-22 | 25 | } | |
| 26 | return self; | ||||
| 27 | } | ||||
| 28 | |||||
| 29 | - (void)dealloc { | ||||
| 30 | [urls release]; | ||||
| 31 | [requests release]; | ||||
| f6ecc6c0 » | leonho | 2008-11-12 | 32 | [receivedDatas autorelease]; | |
| 129f245a » | leonho | 2008-10-22 | 33 | [super dealloc]; | |
| 34 | } | ||||
| 35 | |||||
| 36 | - (void)setDelegate:(id)val | ||||
| 37 | { | ||||
| 38 | delegate = val; | ||||
| 39 | } | ||||
| 40 | |||||
| 41 | - (id)delegate | ||||
| 42 | { | ||||
| 43 | return delegate; | ||||
| 44 | } | ||||
| 45 | |||||
| 46 | |||||
| 47 | #pragma mark Methods | ||||
| c0dc332c » | leonho | 2008-10-22 | 48 | - (id)initWithUrls:(NSArray *)aUrls { | |
| 49 | if ((self = [self init])) | ||||
| 50 | [self requestWithUrls:aUrls]; | ||||
| 51 | return self; | ||||
| 52 | } | ||||
| 53 | |||||
| 54 | - (void)requestWithUrls:(NSArray *)aUrls { | ||||
| 55 | |||||
| f6ecc6c0 » | leonho | 2008-11-12 | 56 | [receivedDatas removeAllObjects]; | |
| 57 | [requests removeAllObjects]; | ||||
| 58 | [urls autorelease]; | ||||
| 59 | urls = [aUrls copy]; | ||||
| 60 | |||||
| 129f245a » | leonho | 2008-10-22 | 61 | for(NSInteger i=0; i< [urls count]; i++){ | |
| 62 | NSMutableData *aData = [[NSMutableData alloc] init]; | ||||
| 63 | [receivedDatas addObject: aData]; | ||||
| 64 | [aData release]; | ||||
| f6ecc6c0 » | leonho | 2008-11-12 | 65 | ||
| 129f245a » | leonho | 2008-10-22 | 66 | NSURLRequest *request = [[NSURLRequest alloc] | |
| 671c8872 » | leonho | 2008-10-31 | 67 | initWithURL: [NSURL URLWithString: [urls objectAtIndex:i]] | |
| 68 | cachePolicy: NSURLRequestReloadIgnoringLocalCacheData | ||||
| f6ecc6c0 » | leonho | 2008-11-12 | 69 | timeoutInterval: 10 | |
| 129f245a » | leonho | 2008-10-22 | 70 | ]; | |
| 71 | NSURLConnection *connection = [[NSURLConnection alloc] | ||||
| 72 | initWithRequest:request | ||||
| 73 | delegate:self]; | ||||
| 74 | |||||
| 75 | [requests setObject: [NSNumber numberWithInt: i] forKey: [NSValue valueWithNonretainedObject:connection]]; | ||||
| 76 | [connection release]; | ||||
| 77 | [request release]; | ||||
| 78 | } | ||||
| 79 | } | ||||
| 80 | |||||
| 81 | - (NSData *)dataAtIndex:(NSInteger)idx { | ||||
| 82 | return [receivedDatas objectAtIndex:idx]; | ||||
| 83 | } | ||||
| 84 | |||||
| 85 | - (NSString *)dataAsStringAtIndex:(NSInteger)idx { | ||||
| 86 | return [[[NSString alloc] initWithData:[receivedDatas objectAtIndex:idx] encoding:NSUTF8StringEncoding] autorelease]; | ||||
| 87 | } | ||||
| 88 | |||||
| 89 | #pragma mark NSURLConnection Delegates | ||||
| 90 | - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response | ||||
| 91 | { | ||||
| 92 | NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue]; | ||||
| 93 | [[receivedDatas objectAtIndex:i] setLength:0]; | ||||
| 94 | } | ||||
| 95 | |||||
| 96 | - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data | ||||
| 97 | { | ||||
| 98 | NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue]; | ||||
| 99 | [[receivedDatas objectAtIndex:i] appendData:data]; | ||||
| 100 | } | ||||
| 101 | |||||
| 102 | - (void)connectionDidFinishLoading:(NSURLConnection *)connection | ||||
| 103 | { | ||||
| 104 | NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue]; | ||||
| 105 | finishCount++; | ||||
| 106 | |||||
| 107 | if ([delegate respondsToSelector:@selector(didFinishDownload:)]) | ||||
| 108 | [delegate performSelector:@selector(didFinishDownload:) withObject: [NSNumber numberWithInt: i]]; | ||||
| f6ecc6c0 » | leonho | 2008-11-12 | 109 | ||
| 129f245a » | leonho | 2008-10-22 | 110 | if(finishCount >= [urls count]){ | |
| 111 | if ([delegate respondsToSelector:@selector(didFinishAllDownload)]) | ||||
| 112 | [delegate didFinishAllDownload]; | ||||
| 113 | } | ||||
| 114 | } | ||||
| 115 | |||||
| f6ecc6c0 » | leonho | 2008-11-12 | 116 | - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { | |
| 117 | if ([delegate respondsToSelector:@selector(didFailDownloadWithError:)]) | ||||
| 118 | [delegate performSelector:@selector(didFailDownloadWithError:) withObject: error]; | ||||
| 119 | } | ||||
| 129f245a » | leonho | 2008-10-22 | 120 | ||
| 121 | @end | ||||
