2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Transmission authors and contributors.
|
|
|
|
// 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
|
|
|
|
#define SMALL_DELAY 30
|
2008-09-01 15:46:00 +00:00
|
|
|
|
|
|
|
//update one week after previous update
|
|
|
|
#define FULL_WAIT (60 * 60 * 24 * 7)
|
|
|
|
|
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
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)runUpdater;
|
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
|
|
|
{
|
|
|
|
lastUpdateDate = [lastUpdateDate dateByAddingTimeInterval:FULL_WAIT];
|
|
|
|
}
|
|
|
|
NSDate* closeDate = [NSDate dateWithTimeIntervalSinceNow:SMALL_DELAY];
|
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
|