forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimerWindowController.m
executable file
·221 lines (172 loc) · 6.8 KB
/
TimerWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// TimerWindowController.m
// SelfControl
//
// Created by Charlie Stigler on 2/15/09.
// Copyright 2009 Eyebeam.
// This file is part of SelfControl.
//
// SelfControl is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#import "TimerWindowController.h"
@implementation TimerWindowController
- (TimerWindowController*) init {
[super init];
unsigned int major, minor, bugfix;
[SelfControlUtilities getSystemVersionMajor: &major minor: &minor bugFix: &bugfix];
if(major <= 10 && minor < 5)
isLeopard = NO;
else
isLeopard = YES;
// We need a block to prevent us from running multiple copies of the "Add to Block"
// sheet.
addToBlockLock = [[NSLock alloc] init];
numStrikes = 0;
return self;
}
- (void)awakeFromNib {
[[self window] center];
[[self window] makeKeyAndOrderFront: self];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSWindow* window = [self window];
[window center];
if([defaults boolForKey:@"TimerWindowFloats"])
[window setLevel: NSFloatingWindowLevel];
else
[window setLevel: NSNormalWindowLevel];
[window setHidesOnDeactivate: NO];
[window makeKeyAndOrderFront: self];
NSDictionary* lockDict = [NSDictionary dictionaryWithContentsOfFile: SelfControlLockFilePath];
NSDate* beginDate = [lockDict objectForKey:@"BlockStartedDate"];
NSTimeInterval blockDuration = [[lockDict objectForKey:@"BlockDuration"] intValue] * 60;
if(beginDate == nil || [beginDate isEqualToDate: [NSDate distantFuture]]
|| blockDuration < 1) {
beginDate = [defaults objectForKey:@"BlockStartedDate"];
blockDuration = [defaults integerForKey:@"BlockDuration"] * 60;
}
// It is KEY to retain the block ending date , if you forget to retain it
// you'll end up with a nasty program crash.
if(blockDuration)
blockEndingDate_ = [[beginDate addTimeInterval: blockDuration] retain];
else
// If the block duration is 0, the ending date is... now!
blockEndingDate_ = [[NSDate date] retain];
[self updateTimerDisplay: nil];
timerUpdater_ = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(updateTimerDisplay:)
userInfo: nil
repeats: YES];
}
- (void)blockEnded {
if(![[NSApp delegate] selfControlLaunchDaemonIsLoaded]) {
[timerUpdater_ invalidate];
timerUpdater_ = nil;
[timerLabel_ setStringValue: NSLocalizedString(@"Block not active", @"block not active string")];
[timerLabel_ setFont: [[NSFontManager sharedFontManager]
convertFont: [timerLabel_ font]
toSize: 37]
];
[timerLabel_ sizeToFit];
[self resetStrikes];
}
}
- (void)updateTimerDisplay:(NSTimer*)timer {
int numSeconds = (int) [blockEndingDate_ timeIntervalSinceNow];
int numHours;
int numMinutes;
if(numSeconds < 0) {
if(isLeopard)
[[NSApp dockTile] setBadgeLabel: nil];
// This increments the strike counter. After four strikes of the timer being
// at or less than 0 seconds, SelfControl will assume something's wrong and run
// scheckup.
numStrikes++;
if(numStrikes >= 4) {
NSLog(@"WARNING: Block should have ended four seconds ago, starting scheckup");
[self runCheckup];
}
return;
}
numHours = (numSeconds / 3600);
numSeconds %= 3600;
numMinutes = (numSeconds / 60);
numSeconds %= 60;
NSString* timeString = [NSString stringWithFormat: @"%0.2d:%0.2d:%0.2d",
numHours,
numMinutes,
numSeconds];
[timerLabel_ setStringValue: timeString];
[timerLabel_ setFont: [[NSFontManager sharedFontManager]
convertFont: [timerLabel_ font]
toSize: 42]
];
[timerLabel_ sizeToFit];
[self resetStrikes];
if(isLeopard && [[NSUserDefaults standardUserDefaults] boolForKey: @"BadgeApplicationIcon"]) {
// We want to round up the minutes--standard when we aren't displaying seconds.
if(numSeconds > 0 && numMinutes != 59)
numMinutes++;
NSString* badgeString = [NSString stringWithFormat: @"%0.2d:%0.2d",
numHours,
numMinutes];
[[NSApp dockTile] setBadgeLabel: badgeString];
} else if(isLeopard) {
// If we're on Leopard but aren't using badging, set the badge string to be
// empty to remove any badge if there is one.
[[NSApp dockTile] setBadgeLabel: nil];
}
}
- (void)windowShouldClose:(NSNotification *)notification {
// Hack to make the application terminate after the last window is closed, but
// INCLUDE the HUD-style timer window.
if(![[[NSApp delegate] initialWindow] isVisible]) {
[NSApp terminate: self];
}
}
- (IBAction) addToBlock:(id)sender {
// Check if there's already a thread trying to add a host. If so, don't make
// another.
if(![addToBlockLock tryLock])
return;
[NSApp beginSheet: addSheet_
modalForWindow: [self window]
modalDelegate: self
didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
contextInfo: nil];
[addToBlockLock unlock];
}
- (IBAction) closeAddSheet:(id)sender {
[NSApp endSheet: addSheet_];
}
- (IBAction) performAdd:(id)sender {
NSString* addToBlockTextFieldContents = [addToBlockTextField_ stringValue];
[[NSApp delegate] addToBlockList: addToBlockTextFieldContents lock: addToBlockLock];
[NSApp endSheet: addSheet_];
}
- (void)didEndSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
[sheet orderOut:self];
}
// see updateTimerDisplay: for an explanation
- (void)resetStrikes {
numStrikes = 0;
}
- (void)runCheckup {
[NSTask launchedTaskWithLaunchPath: @"/Library/PrivilegedHelperTools/scheckup" arguments: [NSArray array]];
[self resetStrikes];
}
- (void)dealloc {
[addToBlockLock release];
[timerUpdater_ invalidate];
timerUpdater_ = nil;
[super dealloc];
}
@end