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-09-01 15:46:00 +00:00
|
|
|
|
|
|
|
#import "BlocklistScheduler.h"
|
|
|
|
#import "BlocklistDownloader.h"
|
|
|
|
|
2008-10-16 04:16:42 +00:00
|
|
|
//thirty second delay before running after option is changed
|
2022-10-19 19:28:21 +00:00
|
|
|
static NSTimeInterval const kSmallDelay = 30;
|
2008-09-01 15:46:00 +00:00
|
|
|
|
|
|
|
//update one week after previous update
|
2022-10-19 19:28:21 +00:00
|
|
|
static NSTimeInterval const kFullWait = 60 * 60 * 24 * 7;
|
2008-09-01 15:46:00 +00:00
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface BlocklistScheduler ()
|
2008-09-01 15:46:00 +00:00
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
@property(nonatomic) NSTimer* fTimer;
|
2022-02-22 16:04:20 +00:00
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistScheduler
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
BlocklistScheduler* fScheduler = nil;
|
|
|
|
|
|
|
|
+ (BlocklistScheduler*)scheduler
|
2008-09-01 15:46:00 +00:00
|
|
|
{
|
|
|
|
if (!fScheduler)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-09-01 15:46:00 +00:00
|
|
|
fScheduler = [[BlocklistScheduler alloc] init];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
return fScheduler;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateSchedule
|
2008-09-01 15:46:00 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
if (BlocklistDownloader.isRunning)
|
2008-09-01 15:51:47 +00:00
|
|
|
return;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-09-01 15:46:00 +00:00
|
|
|
[self cancelSchedule];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* blocklistURL;
|
|
|
|
if (![NSUserDefaults.standardUserDefaults boolForKey:@"BlocklistNew"] ||
|
|
|
|
!((blocklistURL = [NSUserDefaults.standardUserDefaults stringForKey:@"BlocklistURL"]) && ![blocklistURL isEqualToString:@""]) ||
|
|
|
|
![NSUserDefaults.standardUserDefaults boolForKey:@"BlocklistAutoUpdate"])
|
|
|
|
{
|
2008-09-01 15:46:00 +00:00
|
|
|
return;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSDate* lastUpdateDate = [NSUserDefaults.standardUserDefaults objectForKey:@"BlocklistNewLastUpdate"];
|
2008-09-01 15:46:00 +00:00
|
|
|
if (lastUpdateDate)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-10-19 19:28:21 +00:00
|
|
|
lastUpdateDate = [lastUpdateDate dateByAddingTimeInterval:kFullWait];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2022-10-19 19:28:21 +00:00
|
|
|
NSDate* closeDate = [NSDate dateWithTimeIntervalSinceNow:kSmallDelay];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSDate* useDate = lastUpdateDate ? [lastUpdateDate laterDate:closeDate] : closeDate;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTimer = [[NSTimer alloc] initWithFireDate:useDate interval:0 target:self selector:@selector(runUpdater) userInfo:nil
|
2022-03-30 21:52:23 +00:00
|
|
|
repeats:NO];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-09-01 23:17:30 +00:00
|
|
|
//current run loop usually means a second update won't work
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRunLoop* loop = NSRunLoop.mainRunLoop;
|
2022-02-22 16:04:20 +00:00
|
|
|
[loop addTimer:self.fTimer forMode:NSDefaultRunLoopMode];
|
|
|
|
[loop addTimer:self.fTimer forMode:NSModalPanelRunLoopMode];
|
|
|
|
[loop addTimer:self.fTimer forMode:NSEventTrackingRunLoopMode];
|
2008-09-01 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)cancelSchedule
|
2008-09-01 15:46:00 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTimer invalidate];
|
|
|
|
self.fTimer = nil;
|
2008-09-01 15:46:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)runUpdater
|
2008-09-01 15:46:00 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTimer = nil;
|
2008-09-01 15:46:00 +00:00
|
|
|
[BlocklistDownloader downloader];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|