source: trunk/macosx/BlocklistDownloader.m @ 6273

Last change on this file since 6273 was 6273, checked in by livings124, 14 years ago

fix 2 invalid return types; don't show notices when compiling the xib's

  • Property svn:keywords set to Date Rev Author Id
File size: 6.6 KB
Line 
1/******************************************************************************
2 * $Id: BlocklistDownloader.m 6273 2008-06-30 23:28:17Z livings124 $
3 *
4 * Copyright (c) 2008 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 "PrefsController.h"
27#import "NSStringAdditions.h"
28#import "NSApplicationAdditions.h"
29
30#define LIST_URL @"http://download.m0k.org/transmission/files/level1.gz"
31#define DESTINATION [NSTemporaryDirectory() stringByAppendingPathComponent: @"level1.gz"]
32
33@interface BlocklistDownloader (Private)
34
35- (id) initWithPrefsController: (PrefsController *) prefsController;
36- (void) startDownload;
37- (void) finishDownloadSuccess;
38- (void) updateProcessString;
39- (void) failureSheetClosed: (NSAlert *) alert returnCode: (int) code contextInfo: (void *) info;
40
41@end
42
43@implementation BlocklistDownloader
44
45+ (void) downloadWithPrefsController: (PrefsController *) prefsController
46{
47    BlocklistDownloader * downloader = [[BlocklistDownloader alloc] initWithPrefsController: prefsController];
48    [downloader startDownload];
49}
50
51- (void) awakeFromNib
52{
53    [fButton setTitle: NSLocalizedString(@"Cancel", "Blocklist -> cancel button")];
54    [fTextField setStringValue: [NSLocalizedString(@"Connecting to site", "Blocklist -> message") stringByAppendingEllipsis]];
55   
56    [fProgressBar setUsesThreadedAnimation: YES];
57    [fProgressBar startAnimation: self];
58}
59
60- (void) dealloc
61{
62    [fDownload release];
63    [super dealloc];
64}
65
66- (void) cancelDownload: (id) sender
67{
68    [fDownload cancel];
69   
70    [NSApp endSheet: fStatusWindow];
71    [fStatusWindow orderOut: self];
72    [self release];
73}
74
75- (void) download: (NSURLDownload *) download didReceiveResponse: (NSURLResponse *) response
76{
77    fCurrentSize = 0;
78    fExpectedSize = [response expectedContentLength];
79   
80    //change from indeterminate to progress
81    [fProgressBar setIndeterminate: fExpectedSize == NSURLResponseUnknownLength];
82    [self updateProcessString];
83}
84
85- (void) download: (NSURLDownload *) download didReceiveDataOfLength: (NSUInteger) length
86{
87    fCurrentSize += length;
88    [self updateProcessString];
89}
90
91- (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error
92{
93    [fProgressBar setHidden: YES];
94   
95    [NSApp endSheet: fStatusWindow];
96    [fStatusWindow orderOut: self];
97   
98    NSAlert * alert = [[[NSAlert alloc] init] autorelease];
99    [alert addButtonWithTitle: NSLocalizedString(@"OK", "Blocklist -> button")];
100    [alert setMessageText: NSLocalizedString(@"Download of the blocklist failed.", "Blocklist -> message")];
101    [alert setAlertStyle: NSWarningAlertStyle];
102   
103    [alert setInformativeText: [NSString stringWithFormat: @"%@ - %@", NSLocalizedString(@"Error", "Blocklist -> message"),
104        [error localizedDescription]]];
105   
106    [alert beginSheetModalForWindow: [fPrefsController window] modalDelegate: self
107        didEndSelector: @selector(failureSheetClosed:returnCode:contextInfo:) contextInfo: nil];
108}
109
110- (void) downloadDidFinish: (NSURLDownload *) download
111{
112    if ([NSApp isOnLeopardOrBetter])
113        [self performSelectorInBackground: @selector(finishDownloadSuccess) withObject: nil];
114    else
115        [self finishDownloadSuccess];
116}
117
118@end
119
120@implementation BlocklistDownloader (Private)
121
122- (id) initWithPrefsController: (PrefsController *) prefsController
123{
124    if ((self = [super init]))
125    {
126        fPrefsController = prefsController;
127    }
128   
129    return self;
130}
131
132- (void) startDownload
133{
134    //load window and show as sheet
135    [NSBundle loadNibNamed: @"BlocklistStatusWindow" owner: self];
136    [NSApp beginSheet: fStatusWindow modalForWindow: [fPrefsController window] modalDelegate: nil didEndSelector: nil contextInfo: nil];
137   
138    //start the download
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    //change to indeterminate while processing
148    [fProgressBar setIndeterminate: YES];
149    [fProgressBar startAnimation: self];
150   
151    [fTextField setStringValue: [NSLocalizedString(@"Processing blocklist", "Blocklist -> message") stringByAppendingEllipsis]];
152    [fButton setEnabled: NO];
153    [fStatusWindow display]; //force window to be updated
154   
155    //process data
156    tr_blocklistSetContent([fPrefsController handle], [DESTINATION UTF8String]);
157   
158    //delete downloaded file
159    if ([NSApp isOnLeopardOrBetter])
160        [[NSFileManager defaultManager] removeItemAtPath: DESTINATION error: NULL];
161    else
162        [[NSFileManager defaultManager] removeFileAtPath: DESTINATION handler: nil];
163   
164    [fPrefsController updateBlocklistFields];
165   
166    [NSApp endSheet: fStatusWindow];
167    [fStatusWindow orderOut: self];
168    [self release];
169}
170
171- (void) updateProcessString
172{
173    NSString * string = NSLocalizedString(@"Downloading blocklist", "Blocklist -> message");
174    if (fExpectedSize != NSURLResponseUnknownLength)
175    {
176        NSString * substring = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Blocklist -> message"),
177                                [NSString stringForFileSize: fCurrentSize], [NSString stringForFileSize: fExpectedSize]];
178        string = [string stringByAppendingFormat: @" (%@)",  substring];
179        [fProgressBar setDoubleValue: (double)fCurrentSize / fExpectedSize];
180    }
181   
182    [fTextField setStringValue: string];
183}
184
185- (void) failureSheetClosed: (NSAlert *) alert returnCode: (int) code contextInfo: (void *) info
186{
187    [[alert window] orderOut: nil];
188    [self release];
189}
190
191@end
Note: See TracBrowser for help on using the repository browser.