forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerWindowController.m
executable file
·338 lines (263 loc) · 9.55 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
// 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"
@interface TimerWindowController ()
@property(nonatomic, readonly) AppController* appController;
@end
@implementation TimerWindowController
- (TimerWindowController*) init {
if(self = [super init]) {
// 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];
killBlockButton_.hidden = YES;
addToBlockButton_.hidden = NO;
NSDictionary* lockDict = [NSDictionary dictionaryWithContentsOfFile: SelfControlLockFilePath];
NSDate* beginDate = lockDict[@"BlockStartedDate"];
NSTimeInterval blockDuration = [lockDict[@"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 dateByAddingTimeInterval: blockDuration];
else
// If the block duration is 0, the ending date is... now!
blockEndingDate_ = [NSDate date];
[self updateTimerDisplay: nil];
timerUpdater_ = [NSTimer timerWithTimeInterval: 1.0
target: self
selector: @selector(updateTimerDisplay:)
userInfo: nil
repeats: YES];
//If the dialog isn't focused, instead of getting a NSTimer, we get null.
//Scheduling the timer from the main thread seems to work.
[self performSelectorOnMainThread: @selector(hackAroundMainThreadtimer:) withObject: timerUpdater_ waitUntilDone: YES];
}
- (void)blockEnded {
if(![self.appController 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)hackAroundMainThreadtimer:(NSTimer*)timer{
[[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
}
- (void)updateTimerDisplay:(NSTimer*)timer {
// update UI for the whole app, in case the block is done with
[self.appController performSelectorOnMainThread:@selector(refreshUserInterface)
withObject:nil
waitUntilDone:NO];
int numSeconds = (int) [blockEndingDate_ timeIntervalSinceNow];
int numHours;
int numMinutes;
if(numSeconds < 0) {
[[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 == 2) {
NSLog(@"WARNING: Block should have ended two seconds ago, starting scheckup");
[self runCheckup];
} else if(numStrikes > 10) {
// OK, so apparently scheckup couldn't remove the block either. Enable manual block removal.
if (numStrikes == 10) NSLog(@"WARNING: Block should have ended a minute ago! Probable permablock.");
addToBlockButton_.hidden = YES;
killBlockButton_.hidden = NO;
}
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];
[timerLabel_ setFrame:NSRectFromCGRect(CGRectMake(0, timerLabel_.frame.origin.y, self.window.frame.size.width, timerLabel_.frame.size.height))];
[self resetStrikes];
if([[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 we 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(![[self.appController 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];
[self.appController 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 {
@try {
[NSTask launchedTaskWithLaunchPath: @"/Library/PrivilegedHelperTools/scheckup" arguments: @[]];
}
@catch (NSException* exception) {
NSLog(@"ERROR: exception %@ caught while trying to launch scheckup", exception);
}
}
- (IBAction)killBlock:(id)sender {
AuthorizationRef authorizationRef;
char* helperToolPath = [self selfControlKillerHelperToolPathUTF8String];
int helperToolPathSize = strlen(helperToolPath);
AuthorizationItem right = {
kAuthorizationRightExecute,
helperToolPathSize,
helperToolPath,
0
};
AuthorizationRights authRights = {
1,
&right
};
AuthorizationFlags myFlags = kAuthorizationFlagDefaults |
kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed;
OSStatus status;
status = AuthorizationCreate (&authRights,
kAuthorizationEmptyEnvironment,
myFlags,
&authorizationRef);
if(status) {
NSLog(@"ERROR: Failed to authorize block kill.");
return;
}
char uidString[10];
snprintf(uidString, sizeof(uidString), "%d", getuid());
char* args[] = { uidString, NULL };
status = AuthorizationExecuteWithPrivileges(authorizationRef,
helperToolPath,
kAuthorizationFlagDefaults,
args,
NULL);
if(status) {
NSLog(@"WARNING: Authorized execution of helper tool returned failure status code %d", status);
NSError* err = [NSError errorWithDomain: @"org.eyebeam.SelfControl-Killer" code: status userInfo: @{NSLocalizedDescriptionKey: @"Error executing privileged helper tool."}];
[NSApp presentError: err];
return;
} else {
NSAlert* alert = [[NSAlert alloc] init];
[alert setMessageText: @"Success!"];
[alert setInformativeText:@"The block was cleared successfully. You can find the log file, named SelfControl-Killer.log, in your Documents folder. If you're still having issues, please check out the SelfControl FAQ on GitHub."];
[alert addButtonWithTitle: @"OK"];
[alert runModal];
}
}
- (NSString*)selfControlKillerHelperToolPath {
static NSString* path;
// Cache the path so it doesn't have to be searched for again.
if(!path) {
NSBundle* thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForAuxiliaryExecutable: @"SCKillerHelper"];
}
return path;
}
- (char*)selfControlKillerHelperToolPathUTF8String {
static char* path;
// Cache the converted path so it doesn't have to be converted again
if(!path) {
path = malloc(512);
[[self selfControlKillerHelperToolPath] getCString: path
maxLength: 512
encoding: NSUTF8StringEncoding];
}
return path;
}
- (void)dealloc {
[timerUpdater_ invalidate];
}
#pragma mark - Properties
- (AppController *)appController
{
AppController* controller = (AppController *)[NSApp delegate];
return controller;
}
@end