1
1
/******************************************************************************
2
* $Id: BlocklistDownloader.m 6753 2008-09-16 16:39:19Z charles $
2
* $Id: BlocklistDownloader.m 6682 2008-09-01 15:46:00Z livings124 $
4
4
* Copyright (c) 2008 Transmission authors and contributors
23
23
*****************************************************************************/
25
25
#import "BlocklistDownloader.h"
26
#import "BlocklistDownloaderViewController.h"
27
#import "BlocklistScheduler.h"
26
28
#import "PrefsController.h"
27
#import "NSStringAdditions.h"
28
29
#import "NSApplicationAdditions.h"
30
31
#define LIST_URL @"http://download.m0k.org/transmission/files/level1.gz"
33
34
@interface BlocklistDownloader (Private)
35
- (id) initWithPrefsController: (PrefsController *) prefsController;
36
36
- (void) startDownload;
37
37
- (void) finishDownloadSuccess;
38
- (void) updateProcessString;
39
- (void) failureSheetClosed: (NSAlert *) alert returnCode: (int) code contextInfo: (void *) info;
43
41
@implementation BlocklistDownloader
45
+ (void) downloadWithPrefsController: (PrefsController *) prefsController
47
BlocklistDownloader * downloader = [[BlocklistDownloader alloc] initWithPrefsController: prefsController];
48
[downloader startDownload];
53
[fButton setTitle: NSLocalizedString(@"Cancel", "Blocklist -> cancel button")];
55
float oldWidth = [fButton frame].size.width;
57
NSRect buttonFrame = [fButton frame];
58
buttonFrame.origin.x -= buttonFrame.size.width - oldWidth;
59
[fButton setFrame: buttonFrame];
61
[fTextField setStringValue: [NSLocalizedString(@"Connecting to site", "Blocklist -> message") stringByAppendingEllipsis]];
63
[fProgressBar setUsesThreadedAnimation: YES];
64
[fProgressBar startAnimation: self];
43
BlocklistDownloader * fDownloader = nil;
44
+ (BlocklistDownloader *) downloader
48
fDownloader = [[BlocklistDownloader alloc] init];
49
[fDownloader startDownload];
57
return fDownloader != nil;
60
- (void) setViewController: (BlocklistDownloaderViewController *) viewController
62
fViewController = viewController;
67
case BLOCKLIST_DL_START:
68
[fViewController setStatusStarting];
70
case BLOCKLIST_DL_DOWNLOADING:
71
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
73
case BLOCKLIST_DL_PROCESSING:
74
[fViewController setStatusProcessing];
73
- (void) cancelDownload: (id) sender
86
- (void) cancelDownload
88
[fViewController setFinished];
75
90
[fDownload cancel];
77
[NSApp endSheet: fStatusWindow];
78
[fStatusWindow orderOut: self];
92
[[BlocklistScheduler scheduler] updateSchedule];
82
98
- (void) download: (NSURLDownload *) download didReceiveResponse: (NSURLResponse *) response
100
fState = BLOCKLIST_DL_DOWNLOADING;
85
103
fExpectedSize = [response expectedContentLength];
87
//change from indeterminate to progress
88
[fProgressBar setIndeterminate: fExpectedSize == NSURLResponseUnknownLength];
89
[self updateProcessString];
105
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
92
108
- (void) download: (NSURLDownload *) download didReceiveDataOfLength: (NSUInteger) length
94
110
fCurrentSize += length;
95
[self updateProcessString];
111
[fViewController setStatusProgressForCurrentSize: fCurrentSize expectedSize: fExpectedSize];
98
114
- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
100
[fProgressBar setHidden: YES];
102
[NSApp endSheet: fStatusWindow];
103
[fStatusWindow orderOut: self];
105
NSAlert * alert = [[[NSAlert alloc] init] autorelease];
106
[alert addButtonWithTitle: NSLocalizedString(@"OK", "Blocklist -> button")];
107
[alert setMessageText: NSLocalizedString(@"Download of the blocklist failed.", "Blocklist -> message")];
108
[alert setAlertStyle: NSWarningAlertStyle];
110
[alert setInformativeText: [NSString stringWithFormat: @"%@ - %@", NSLocalizedString(@"Error", "Blocklist -> message"),
111
[error localizedDescription]]];
113
[alert beginSheetModalForWindow: [fPrefsController window] modalDelegate: self
114
didEndSelector: @selector(failureSheetClosed:returnCode:contextInfo:) contextInfo: nil];
116
[fViewController setFailed: [error localizedDescription]];
118
[[BlocklistScheduler scheduler] updateSchedule];
117
124
- (void) downloadDidFinish: (NSURLDownload *) download
126
fState = BLOCKLIST_DL_PROCESSING;
119
128
if ([NSApp isOnLeopardOrBetter])
120
129
[self performSelectorInBackground: @selector(finishDownloadSuccess) withObject: nil];
127
136
@implementation BlocklistDownloader (Private)
129
- (id) initWithPrefsController: (PrefsController *) prefsController
131
if ((self = [super init]))
133
fPrefsController = prefsController;
139
138
- (void) startDownload
141
//load window and show as sheet
142
[NSBundle loadNibNamed: @"BlocklistStatusWindow" owner: self];
143
[NSApp beginSheet: fStatusWindow modalForWindow: [fPrefsController window] modalDelegate: nil didEndSelector: nil contextInfo: nil];
140
fState = BLOCKLIST_DL_START;
142
[[BlocklistScheduler scheduler] cancelSchedule];
146
144
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: LIST_URL]];
148
146
fDownload = [[NSURLDownload alloc] initWithRequest: request delegate: self];
154
152
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
156
//change to indeterminate while processing
157
[fProgressBar setIndeterminate: YES];
158
[fProgressBar startAnimation: self];
160
[fTextField setStringValue: [NSLocalizedString(@"Processing blocklist", "Blocklist -> message") stringByAppendingEllipsis]];
161
[fButton setEnabled: NO];
162
[fStatusWindow display]; //force window to be updated
154
[fViewController setStatusProcessing];
165
tr_blocklistSetContent([fPrefsController handle], [DESTINATION UTF8String]);
157
tr_blocklistSetContent([PrefsController handle], [DESTINATION UTF8String]);
167
159
//delete downloaded file
168
160
if ([NSApp isOnLeopardOrBetter])
171
163
[[NSFileManager defaultManager] removeFileAtPath: DESTINATION handler: nil];
173
[fPrefsController updateBlocklistFields];
175
[NSApp endSheet: fStatusWindow];
176
[fStatusWindow orderOut: self];
165
[fViewController setFinished];
167
//update last updated date for schedule
168
[[NSUserDefaults standardUserDefaults] setObject: [NSDate date] forKey: @"BlocklistLastUpdate"];
169
[[BlocklistScheduler scheduler] updateSchedule];
171
[[NSNotificationCenter defaultCenter] postNotificationName: @"BlocklistUpdated" object: nil];
182
- (void) updateProcessString
184
NSString * string = NSLocalizedString(@"Downloading blocklist", "Blocklist -> message");
185
if (fExpectedSize != NSURLResponseUnknownLength)
187
NSString * substring = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Blocklist -> message"),
188
[NSString stringForFileSize: fCurrentSize], [NSString stringForFileSize: fExpectedSize]];
189
string = [string stringByAppendingFormat: @" (%@)", substring];
190
[fProgressBar setDoubleValue: (double)fCurrentSize / fExpectedSize];
193
[fTextField setStringValue: string];
196
- (void) failureSheetClosed: (NSAlert *) alert returnCode: (int) code contextInfo: (void *) info
198
[[alert window] orderOut: nil];