source: trunk/macosx/BlocklistDownloader.m @ 8022

Last change on this file since 8022 was 8022, checked in by livings124, 13 years ago

update "update" urls

  • Property svn:keywords set to Date Rev Author Id
File size: 5.0 KB
Line 
1/******************************************************************************
2 * $Id: BlocklistDownloader.m 8022 2009-03-04 22:38:49Z livings124 $
3 *
4 * Copyright (c) 2008-2009 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "BlocklistDownloader.h"
26#import "BlocklistDownloaderViewController.h"
27#import "BlocklistScheduler.h"
28#import "PrefsController.h"
29
30#define LIST_URL @"http://update.transmissionbt.com/files/level1.gz"
31#define DESTINATION [NSTemporaryDirectory() stringByAppendingPathComponent: @"level1.gz"]
32
33@interface BlocklistDownloader (Private)
34
35- (void) startDownload;
36- (void) finishDownloadSuccess;
37
38@end
39
40@implementation BlocklistDownloader
41
42BlocklistDownloader * fDownloader = nil;
43+ (BlocklistDownloader *) downloader
44{
45    if (!fDownloader)
46    {
47        fDownloader = [[BlocklistDownloader alloc] init];
48        [fDownloader startDownload];
49    }
50   
51    return fDownloader;
52}
53
54+ (BOOL) isRunning
55{
56    return fDownloader != nil;
57}
58
59- (void) setViewController: (BlocklistDownloaderViewController *) viewController
60{
61    fViewController = viewController;
62    if (fViewController)
63    {
64        switch (fState)
65        {
66            case BLOCKLIST_DL_START:
67                [fViewController setStatusStarting];
68                break;
69            case BLOCKLIST_DL_DOWNLOADING:
70                [fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
71                break;
72            case BLOCKLIST_DL_PROCESSING:
73                [fViewController setStatusProcessing];
74                break;
75        }
76    }
77}
78
79- (void) dealloc
80{
81    [fDownload release];
82    [super dealloc];
83}
84
85- (void) cancelDownload
86{
87    [fViewController setFinished];
88   
89    [fDownload cancel];
90   
91    [[BlocklistScheduler scheduler] updateSchedule];
92   
93    fDownloader = nil;
94    [self release];
95}
96
97- (void) download: (NSURLDownload *) download didReceiveResponse: (NSURLResponse *) response
98{
99    fState = BLOCKLIST_DL_DOWNLOADING;
100   
101    fCurrentSize = 0;
102    fExpectedSize = [response expectedContentLength];
103   
104    [fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
105}
106
107- (void) download: (NSURLDownload *) download didReceiveDataOfLength: (NSUInteger) length
108{
109    fCurrentSize += length;
110    [fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
111}
112
113- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
114{
115    [fViewController setFailed: [error localizedDescription]];
116   
117    [[BlocklistScheduler scheduler] updateSchedule];
118   
119    fDownloader = nil;
120    [self release];
121}
122
123- (void) downloadDidFinish: (NSURLDownload *) download
124{
125    fState = BLOCKLIST_DL_PROCESSING;
126    [self performSelectorInBackground: @selector(finishDownloadSuccess) withObject: nil];
127}
128
129@end
130
131@implementation BlocklistDownloader (Private)
132
133- (void) startDownload
134{
135    fState = BLOCKLIST_DL_START;
136   
137    [[BlocklistScheduler scheduler] cancelSchedule];
138   
139    NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: LIST_URL]];
140   
141    fDownload = [[NSURLDownload alloc] initWithRequest: request delegate: self];
142    [fDownload setDestination: DESTINATION allowOverwrite: YES];
143}
144
145- (void) finishDownloadSuccess
146{
147    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
148   
149    [fViewController setStatusProcessing];
150   
151    //process data
152    tr_blocklistSetContent([PrefsController handle], [DESTINATION UTF8String]);
153   
154    //delete downloaded file
155    [[NSFileManager defaultManager] removeItemAtPath: DESTINATION error: NULL];
156   
157    [fViewController setFinished];
158   
159    //update last updated date for schedule
160    [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: @"BlocklistLastUpdate"];
161    [[BlocklistScheduler scheduler] updateSchedule];
162   
163    [[NSNotificationCenter defaultCenter] postNotificationName: @"BlocklistUpdated" object: nil];
164   
165    [pool release];
166   
167    fDownloader = nil;
168    [self release];
169}
170
171@end
Note: See TracBrowser for help on using the repository browser.