source: trunk/macosx/BlocklistDownloaderViewController.m @ 13250

Last change on this file since 13250 was 13250, checked in by livings124, 10 years ago

replace a method with GCD calls

  • Property svn:keywords set to Date Rev Author Id
File size: 5.5 KB
Line 
1/******************************************************************************
2 * $Id: BlocklistDownloaderViewController.m 13250 2012-03-10 13:59:44Z livings124 $
3 *
4 * Copyright (c) 2008-2012 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
40BlocklistDownloaderViewController * fBLViewController = nil;
41+ (void) downloadWithPrefsController: (PrefsController *) prefsController
42{
43    if (!fBLViewController)
44    {
45        fBLViewController = [[BlocklistDownloaderViewController alloc] initWithPrefsController: prefsController];
46        [fBLViewController startDownload];
47    }
48}
49
50- (void) awakeFromNib
51{
52    [fButton setTitle: NSLocalizedString(@"Cancel", "Blocklist -> cancel button")];
53   
54    const CGFloat oldWidth = NSWidth([fButton frame]);
55    [fButton sizeToFit];
56    NSRect buttonFrame = [fButton frame];
57    buttonFrame.size.width += 12.0; //sizeToFit sizes a bit too small
58    buttonFrame.origin.x -= NSWidth(buttonFrame) - oldWidth;
59    [fButton setFrame: buttonFrame];
60   
61    [fProgressBar setUsesThreadedAnimation: YES];
62    [fProgressBar startAnimation: self];
63}
64
65- (void) cancelDownload: (id) sender
66{
67    [[BlocklistDownloader downloader] cancelDownload];
68}
69
70- (void) setStatusStarting
71{
72    [fTextField setStringValue: [NSLocalizedString(@"Connecting to site", "Blocklist -> message") stringByAppendingEllipsis]];
73    [fProgressBar setIndeterminate: YES];
74}
75
76- (void) setStatusProgressForCurrentSize: (NSUInteger) currentSize expectedSize: (long long) expectedSize
77{
78    NSString * string = NSLocalizedString(@"Downloading blocklist", "Blocklist -> message");
79    if (expectedSize != NSURLResponseUnknownLength)
80    {
81        [fProgressBar setIndeterminate: NO];
82       
83        NSString * substring = [NSString stringForFilePartialSize: currentSize fullSize: expectedSize];
84        string = [string stringByAppendingFormat: @" (%@)",  substring];
85        [fProgressBar setDoubleValue: (double)currentSize / expectedSize];
86    }
87    else
88        string = [string stringByAppendingFormat: @" (%@)",  [NSString stringForFileSize: currentSize]];
89   
90    [fTextField setStringValue: string];
91}
92
93- (void) setStatusProcessing
94{
95    //change to indeterminate while processing
96    [fProgressBar setIndeterminate: YES];
97    [fProgressBar startAnimation: self];
98   
99    [fTextField setStringValue: [NSLocalizedString(@"Processing blocklist", "Blocklist -> message") stringByAppendingEllipsis]];
100    [fButton setEnabled: NO];
101}
102
103- (void) setFinished
104{
105    [NSApp endSheet: fStatusWindow];
106    [fStatusWindow orderOut: self];
107   
108    fBLViewController = nil;
109}
110
111- (void) setFailed: (NSString *) error
112{
113    [NSApp endSheet: fStatusWindow];
114    [fStatusWindow orderOut: self];
115   
116    NSAlert * alert = [[NSAlert alloc] init];
117    [alert addButtonWithTitle: NSLocalizedString(@"OK", "Blocklist -> button")];
118    [alert setMessageText: NSLocalizedString(@"Download of the blocklist failed.", "Blocklist -> message")];
119    [alert setAlertStyle: NSWarningAlertStyle];
120   
121    [alert setInformativeText: error];
122   
123    [alert beginSheetModalForWindow: [fPrefsController window] modalDelegate: self
124        didEndSelector: @selector(failureSheetClosed:returnCode:contextInfo:) contextInfo: nil];
125}
126
127@end
128
129@implementation BlocklistDownloaderViewController (Private)
130
131- (id) initWithPrefsController: (PrefsController *) prefsController
132{
133    if ((self = [super init]))
134    {
135        fPrefsController = prefsController;
136    }
137   
138    return self;
139}
140
141- (void) startDownload
142{
143    //load window and show as sheet
144    [NSBundle loadNibNamed: @"BlocklistStatusWindow" owner: self];
145   
146    BlocklistDownloader * downloader = [BlocklistDownloader downloader];
147    [downloader setViewController: self]; //do before showing the sheet to ensure it doesn't slide out with placeholder text
148   
149    [NSApp beginSheet: fStatusWindow modalForWindow: [fPrefsController window] modalDelegate: nil didEndSelector: nil contextInfo: nil];
150}
151
152- (void) failureSheetClosed: (NSAlert *) alert returnCode: (NSInteger) code contextInfo: (void *) info
153{
154    [[alert window] orderOut: self];
155   
156    fBLViewController = nil;
157}
158
159@end
Note: See TracBrowser for help on using the repository browser.