2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-08-31 19:47:11 +00:00
|
|
|
|
|
|
|
#import "BlocklistDownloaderViewController.h"
|
|
|
|
#import "BlocklistDownloader.h"
|
|
|
|
#import "PrefsController.h"
|
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface BlocklistDownloaderViewController ()
|
2008-08-31 19:47:11 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic) IBOutlet NSWindow* fStatusWindow;
|
|
|
|
@property(nonatomic) IBOutlet NSProgressIndicator* fProgressBar;
|
|
|
|
@property(nonatomic) IBOutlet NSTextField* fTextField;
|
|
|
|
@property(nonatomic) IBOutlet NSButton* fButton;
|
|
|
|
|
|
|
|
@property(nonatomic, readonly) PrefsController* fPrefsController;
|
|
|
|
|
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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fButton.title = NSLocalizedString(@"Cancel", "Blocklist -> cancel button");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
CGFloat const oldWidth = NSWidth(self.fButton.frame);
|
|
|
|
[self.fButton sizeToFit];
|
|
|
|
NSRect buttonFrame = self.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;
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fButton.frame = buttonFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fProgressBar.usesThreadedAnimation = YES;
|
|
|
|
[self.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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTextField.stringValue = [NSLocalizedString(@"Connecting to site", "Blocklist -> message") stringByAppendingEllipsis];
|
|
|
|
self.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)
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.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];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.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
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.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
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fProgressBar.indeterminate = YES;
|
|
|
|
[self.fProgressBar startAnimation:self];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTextField.stringValue = [NSLocalizedString(@"Processing blocklist", "Blocklist -> message") stringByAppendingEllipsis];
|
|
|
|
self.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
|
|
|
{
|
2019-02-03 04:20:43 +00:00
|
|
|
[self.fPrefsController.window endSheet:self.fStatusWindow];
|
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
|
|
|
{
|
2019-02-03 04:20:43 +00:00
|
|
|
[self.fPrefsController.window endSheet:self.fStatusWindow];
|
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
|
|
|
|
2022-12-21 20:21:16 +00:00
|
|
|
[alert beginSheetModalForWindow:self.fPrefsController.window completionHandler:^(NSModalResponse /*returnCode*/) {
|
2020-11-22 13:02:29 +00:00
|
|
|
fBLViewController = nil;
|
|
|
|
}];
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - 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]))
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_fPrefsController = prefsController;
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
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];
|
2022-06-29 04:20:42 +00:00
|
|
|
downloader.viewController = self; //do before showing the sheet to ensure it doesn't slide out with placeholder text
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fPrefsController.window beginSheet:self.fStatusWindow completionHandler:nil];
|
2008-08-31 19:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|