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)
|
|
|
|
|
|
|
|
@interface BlocklistScheduler (Private)
|
|
|
|
|
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
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
fTimer = [[NSTimer alloc] initWithFireDate:useDate interval:0 target:self selector:@selector(runUpdater) userInfo:nil
|
|
|
|
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;
|
|
|
|
[loop addTimer:fTimer forMode:NSDefaultRunLoopMode];
|
|
|
|
[loop addTimer:fTimer forMode:NSModalPanelRunLoopMode];
|
|
|
|
[loop addTimer: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
|
|
|
{
|
|
|
|
[fTimer invalidate];
|
|
|
|
fTimer = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation BlocklistScheduler (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)runUpdater
|
2008-09-01 15:46:00 +00:00
|
|
|
{
|
|
|
|
fTimer = nil;
|
|
|
|
[BlocklistDownloader downloader];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|