source: trunk/macosx/BlocklistScheduler.m @ 7200

Last change on this file since 7200 was 7200, checked in by charles, 12 years ago

enable keywords properties for $Id$

  • Property svn:keywords set to Date Rev Author Id
File size: 3.2 KB
Line 
1/******************************************************************************
2 * $Id: BlocklistScheduler.m 7200 2008-11-30 06:10:42Z charles $
3 *
4 * Copyright (c) 2008 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "BlocklistScheduler.h"
26#import "BlocklistDownloader.h"
27#import "NSApplicationAdditions.h"
28
29//thirty second delay before running after option is changed
30#define SMALL_DELAY 30
31
32//update one week after previous update
33#define FULL_WAIT (60 * 60 * 24 * 7)
34
35@interface BlocklistScheduler (Private)
36
37- (void) runUpdater;
38
39@end
40
41@implementation BlocklistScheduler
42
43BlocklistScheduler * fScheduler = nil;
44+ (BlocklistScheduler *) scheduler
45{
46    if (!fScheduler)
47        fScheduler = [[BlocklistScheduler alloc] init];
48   
49    return fScheduler;
50}
51
52- (void) updateSchedule
53{
54    if ([BlocklistDownloader isRunning])
55        return;
56   
57    [self cancelSchedule];
58   
59    if (![[NSUserDefaults standardUserDefaults] boolForKey: @"Blocklist"]
60        || ![[NSUserDefaults standardUserDefaults] boolForKey: @"BlocklistAutoUpdate"])
61        return;
62   
63    NSDate * lastUpdateDate = [[NSUserDefaults standardUserDefaults] objectForKey: @"BlocklistLastUpdate"];
64    if (lastUpdateDate)
65        lastUpdateDate = [lastUpdateDate addTimeInterval: FULL_WAIT];
66    NSDate * closeDate = [NSDate dateWithTimeIntervalSinceNow: SMALL_DELAY];
67   
68    NSDate * useDate = lastUpdateDate ? [lastUpdateDate laterDate: closeDate] : closeDate;
69   
70    fTimer = [[NSTimer alloc] initWithFireDate: useDate interval: 0 target: self selector: @selector(runUpdater)
71                userInfo: nil repeats: NO];
72   
73    //current run loop usually means a second update won't work
74    NSRunLoop * loop = [NSApp isOnLeopardOrBetter] ? [NSRunLoop mainRunLoop] : [NSRunLoop currentRunLoop];
75    [loop addTimer: fTimer forMode: NSDefaultRunLoopMode];
76    [loop addTimer: fTimer forMode: NSModalPanelRunLoopMode];
77    [loop addTimer: fTimer forMode: NSEventTrackingRunLoopMode];
78}
79
80- (void) cancelSchedule
81{
82    [fTimer invalidate];
83    fTimer = nil;
84}
85
86@end
87
88@implementation BlocklistScheduler (Private)
89
90- (void) runUpdater
91{
92    fTimer = nil;
93    [BlocklistDownloader downloader];
94}
95
96@end
Note: See TracBrowser for help on using the repository browser.