source: trunk/macosx/BlocklistDownloaderViewController.m @ 13249

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

less crashing with ARC

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