source: trunk/macosx/BlocklistDownloader.m @ 11369

Last change on this file since 11369 was 11369, checked in by livings124, 12 years ago

#3697 make blocklist URL configurable in the Mac UI

  • Property svn:keywords set to Date Rev Author Id
File size: 5.6 KB
Line 
1/******************************************************************************
2 * $Id: BlocklistDownloader.m 11369 2010-10-31 19:05:46Z livings124 $
3 *
4 * Copyright (c) 2008-2010 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 FILE_NAME @"blocklist.tmp"
31
32@interface BlocklistDownloader (Private)
33
34- (void) startDownload;
35- (void) finishDownloadSuccess;
36
37@end
38
39@implementation BlocklistDownloader
40
41BlocklistDownloader * fDownloader = nil;
42+ (BlocklistDownloader *) downloader
43{
44    if (!fDownloader)
45    {
46        fDownloader = [[BlocklistDownloader alloc] init];
47        [fDownloader startDownload];
48    }
49   
50    return fDownloader;
51}
52
53+ (BOOL) isRunning
54{
55    return fDownloader != nil;
56}
57
58- (void) setViewController: (BlocklistDownloaderViewController *) viewController
59{
60    fViewController = viewController;
61    if (fViewController)
62    {
63        switch (fState)
64        {
65            case BLOCKLIST_DL_START:
66                [fViewController setStatusStarting];
67                break;
68            case BLOCKLIST_DL_DOWNLOADING:
69                [fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
70                break;
71            case BLOCKLIST_DL_PROCESSING:
72                [fViewController setStatusProcessing];
73                break;
74        }
75    }
76}
77
78- (void) dealloc
79{
80    [fDownload release];
81    [fDestination 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 didCreateDestination: (NSString *) path
98{
99    fDestination = [path retain];
100}
101
102- (void) download: (NSURLDownload *) download didReceiveResponse: (NSURLResponse *) response
103{
104    fState = BLOCKLIST_DL_DOWNLOADING;
105   
106    fCurrentSize = 0;
107    fExpectedSize = [response expectedContentLength];
108   
109    [fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
110}
111
112- (void) download: (NSURLDownload *) download didReceiveDataOfLength: (NSUInteger) length
113{
114    fCurrentSize += length;
115    [fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
116}
117
118- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
119{
120    [fViewController setFailed: [error localizedDescription]];
121   
122    [[BlocklistScheduler scheduler] updateSchedule];
123   
124    fDownloader = nil;
125    [self release];
126}
127
128- (void) downloadDidFinish: (NSURLDownload *) download
129{
130    fState = BLOCKLIST_DL_PROCESSING;
131    [self performSelectorInBackground: @selector(finishDownloadSuccess) withObject: nil];
132}
133
134@end
135
136@implementation BlocklistDownloader (Private)
137
138- (void) startDownload
139{
140    fState = BLOCKLIST_DL_START;
141   
142    [[BlocklistScheduler scheduler] cancelSchedule];
143   
144    NSString * urlString = [[NSUserDefaults standardUserDefaults] stringForKey: @"BlocklistURL"];
145    if (!urlString)
146        urlString = @"";
147   
148    NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]];
149   
150    fDownload = [[NSURLDownload alloc] initWithRequest: request delegate: self];
151    [fDownload setDestination: [NSTemporaryDirectory() stringByAppendingPathComponent: FILE_NAME] allowOverwrite: YES];
152}
153
154- (void) finishDownloadSuccess
155{
156    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
157   
158    [fViewController setStatusProcessing];
159   
160    //process data
161    NSAssert(fDestination != nil, @"the blocklist file destination has not been specified");
162    const int count = tr_blocklistSetContent([PrefsController handle], [fDestination UTF8String]);
163   
164    //delete downloaded file
165    [[NSFileManager defaultManager] removeItemAtPath: fDestination error: NULL];
166   
167    if (count > 0)
168        [fViewController setFinished];
169    else
170        [fViewController setFailed: NSLocalizedString(@"The specified blocklist file did not contain any valid rules.",
171                                        "blocklist fail message")];
172   
173    //update last updated date for schedule
174    [[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: @"BlocklistLastUpdate"];
175    [[BlocklistScheduler scheduler] updateSchedule];
176   
177    [[NSNotificationCenter defaultCenter] postNotificationName: @"BlocklistUpdated" object: nil];
178   
179    [pool drain];
180   
181    fDownloader = nil;
182    [self release];
183}
184
185@end
Note: See TracBrowser for help on using the repository browser.