source: trunk/macosx/BlocklistDownloaderViewController.m @ 7200

Last change on this file since 7200 was 7200, checked in by charles, 12 years ago

enable keywords properties for $Id$

  • Property svn:keywords set to Date Rev Author Id
File size: 5.5 KB
Line 
1/******************************************************************************
2 * $Id: BlocklistDownloaderViewController.m 7200 2008-11-30 06:10:42Z charles $
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 "BlocklistDownloaderViewController.h"
26#import "BlocklistDownloader.h"
27#import "PrefsController.h"
28#import "NSStringAdditions.h"
29
30@interface BlocklistDownloaderViewController (Private)
31
32- (id) initWithPrefsController: (PrefsController *) prefsController;
33- (void) startDownload;
34- (void) failureSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info;
35
36@end
37
38@implementation BlocklistDownloaderViewController
39
40+ (void) downloadWithPrefsController: (PrefsController *) prefsController
41{
42    BlocklistDownloaderViewController * downloader = [[BlocklistDownloaderViewController alloc] initWithPrefsController: prefsController];
43    [downloader startDownload];
44}
45
46- (void) awakeFromNib
47{
48    [fButton setTitle: NSLocalizedString(@"Cancel", "Blocklist -> cancel button")];
49   
50    CGFloat oldWidth = [fButton frame].size.width;
51    [fButton sizeToFit];
52    NSRect buttonFrame = [fButton frame];
53    buttonFrame.size.width += 12.0f; //sizeToFit sizes a bit too small
54    buttonFrame.origin.x -= buttonFrame.size.width - oldWidth;
55    [fButton setFrame: buttonFrame];
56   
57    [fProgressBar setUsesThreadedAnimation: YES];
58    [fProgressBar startAnimation: self];
59}
60
61- (void) cancelDownload: (id) sender
62{
63    [[BlocklistDownloader downloader] cancelDownload];
64}
65
66- (void) setStatusStarting
67{
68    [fTextField setStringValue: [NSLocalizedString(@"Connecting to site", "Blocklist -> message") stringByAppendingEllipsis]];
69    [fProgressBar setIndeterminate: YES];
70}
71
72- (void) setStatusProgressForCurrentSize: (NSUInteger) currentSize expectedSize: (long long) expectedSize
73{
74    NSString * string = NSLocalizedString(@"Downloading blocklist", "Blocklist -> message");
75    if (expectedSize != NSURLResponseUnknownLength)
76    {
77        [fProgressBar setIndeterminate: NO];
78       
79        NSString * substring = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Blocklist -> message"),
80                                [NSString stringForFileSize: currentSize], [NSString stringForFileSize: expectedSize]];
81        string = [string stringByAppendingFormat: @" (%@)",  substring];
82        [fProgressBar setDoubleValue: (double)currentSize / expectedSize];
83    }
84    else
85        string = [string stringByAppendingFormat: @" (%@)",  [NSString stringForFileSize: currentSize]];
86   
87    [fTextField setStringValue: string];
88}
89
90- (void) setStatusProcessing
91{
92    //change to indeterminate while processing
93    [fProgressBar setIndeterminate: YES];
94    [fProgressBar startAnimation: self];
95   
96    [fTextField setStringValue: [NSLocalizedString(@"Processing blocklist", "Blocklist -> message") stringByAppendingEllipsis]];
97    [fButton setEnabled: NO];
98}
99
100- (void) setFinished
101{
102    [NSApp endSheet: fStatusWindow];
103    [fStatusWindow orderOut: self];
104   
105    [self release];
106}
107
108- (void) setFailed: (NSString *) error
109{
110    [NSApp endSheet: fStatusWindow];
111    [fStatusWindow orderOut: self];
112   
113    NSAlert * alert = [[[NSAlert alloc] init] autorelease];
114    [alert addButtonWithTitle: NSLocalizedString(@"OK", "Blocklist -> button")];
115    [alert setMessageText: NSLocalizedString(@"Download of the blocklist failed.", "Blocklist -> message")];
116    [alert setAlertStyle: NSWarningAlertStyle];
117   
118    [alert setInformativeText: [NSString stringWithFormat: @"%@ - %@", NSLocalizedString(@"Error", "Blocklist -> message"),
119        error]];
120   
121    [alert beginSheetModalForWindow: [fPrefsController window] modalDelegate: self
122        didEndSelector: @selector(failureSheetClosed:returnCode:contextInfo:) contextInfo: nil];
123}
124
125@end
126
127@implementation BlocklistDownloaderViewController (Private)
128
129- (id) initWithPrefsController: (PrefsController *) prefsController
130{
131    if ((self = [super init]))
132    {
133        fPrefsController = prefsController;
134    }
135   
136    return self;
137}
138
139- (void) startDownload
140{
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];
144   
145    BlocklistDownloader * downloader = [BlocklistDownloader downloader];
146    [downloader setViewController: self];
147}
148
149- (void) failureSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info
150{
151    [[alert window] orderOut: self];
152    [self release];
153}
154
155@end
Note: See TracBrowser for help on using the repository browser.