2008-08-31 19:47:11 +00:00
|
|
|
/******************************************************************************
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2008-2012 Transmission authors and contributors
|
2008-08-31 19:47:11 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "BlocklistDownloaderViewController.h"
|
|
|
|
#import "BlocklistDownloader.h"
|
|
|
|
#import "PrefsController.h"
|
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
|
|
|
@interface BlocklistDownloaderViewController (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithPrefsController:(PrefsController*)prefsController;
|
|
|
|
- (void)startDownload;
|
2008-08-31 19:47:11 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistDownloaderViewController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
BlocklistDownloaderViewController* fBLViewController = nil;
|
|
|
|
+ (void)downloadWithPrefsController:(PrefsController*)prefsController
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2012-03-13 03:20:09 +00:00
|
|
|
if (!fBLViewController)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
fBLViewController = [[BlocklistDownloaderViewController alloc] initWithPrefsController:prefsController];
|
2012-03-13 03:20:09 +00:00
|
|
|
[fBLViewController startDownload];
|
|
|
|
}
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
fButton.title = NSLocalizedString(@"Cancel", "Blocklist -> cancel button");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const oldWidth = NSWidth(fButton.frame);
|
2008-08-31 19:47:11 +00:00
|
|
|
[fButton sizeToFit];
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect buttonFrame = fButton.frame;
|
2009-12-23 02:02:11 +00:00
|
|
|
buttonFrame.size.width += 12.0; //sizeToFit sizes a bit too small
|
|
|
|
buttonFrame.origin.x -= NSWidth(buttonFrame) - oldWidth;
|
2021-08-07 07:27:56 +00:00
|
|
|
fButton.frame = buttonFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
fProgressBar.usesThreadedAnimation = YES;
|
2021-08-15 09:41:48 +00:00
|
|
|
[fProgressBar startAnimation:self];
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)cancelDownload:(id)sender
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2008-08-31 20:48:10 +00:00
|
|
|
[[BlocklistDownloader downloader] cancelDownload];
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setStatusStarting
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
fTextField.stringValue = [NSLocalizedString(@"Connecting to site", "Blocklist -> message") stringByAppendingEllipsis];
|
|
|
|
fProgressBar.indeterminate = YES;
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setStatusProgressForCurrentSize:(NSUInteger)currentSize expectedSize:(long long)expectedSize
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* string = NSLocalizedString(@"Downloading blocklist", "Blocklist -> message");
|
2008-08-31 19:47:11 +00:00
|
|
|
if (expectedSize != NSURLResponseUnknownLength)
|
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
fProgressBar.indeterminate = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* substring = [NSString stringForFilePartialSize:currentSize fullSize:expectedSize];
|
|
|
|
string = [string stringByAppendingFormat:@" (%@)", substring];
|
2021-08-07 07:27:56 +00:00
|
|
|
fProgressBar.doubleValue = (double)currentSize / expectedSize;
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
string = [string stringByAppendingFormat:@" (%@)", [NSString stringForFileSize:currentSize]];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
fTextField.stringValue = string;
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setStatusProcessing
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
|
|
|
//change to indeterminate while processing
|
2021-08-07 07:27:56 +00:00
|
|
|
fProgressBar.indeterminate = YES;
|
2021-08-15 09:41:48 +00:00
|
|
|
[fProgressBar startAnimation:self];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
fTextField.stringValue = [NSLocalizedString(@"Processing blocklist", "Blocklist -> message") stringByAppendingEllipsis];
|
|
|
|
fButton.enabled = NO;
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setFinished
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSApp endSheet:fStatusWindow];
|
|
|
|
[fStatusWindow orderOut:self];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-03-13 03:20:09 +00:00
|
|
|
fBLViewController = nil;
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setFailed:(NSString*)error
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSApp endSheet:fStatusWindow];
|
|
|
|
[fStatusWindow orderOut:self];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"OK", "Blocklist -> button")];
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.messageText = NSLocalizedString(@"Download of the blocklist failed.", "Blocklist -> message");
|
2021-10-31 15:18:27 +00:00
|
|
|
alert.alertStyle = NSAlertStyleWarning;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.informativeText = error;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[alert beginSheetModalForWindow:fPrefsController.window completionHandler:^(NSModalResponse returnCode) {
|
2021-08-15 09:41:48 +00:00
|
|
|
[alert.window orderOut:self];
|
2020-11-22 13:02:29 +00:00
|
|
|
|
|
|
|
fBLViewController = nil;
|
|
|
|
}];
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistDownloaderViewController (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithPrefsController:(PrefsController*)prefsController
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
fPrefsController = prefsController;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-08-31 19:47:11 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)startDownload
|
2008-08-31 19:47:11 +00:00
|
|
|
{
|
|
|
|
//load window and show as sheet
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSBundle.mainBundle loadNibNamed:@"BlocklistStatusWindow" owner:self topLevelObjects:NULL];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
BlocklistDownloader* downloader = [BlocklistDownloader downloader];
|
|
|
|
[downloader setViewController:self]; //do before showing the sheet to ensure it doesn't slide out with placeholder text
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[fPrefsController.window beginSheet:fStatusWindow completionHandler:nil];
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|