-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppController.m
188 lines (154 loc) · 4.9 KB
/
AppController.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
/* Take A Pause!
* Copyright (C) 2007, Fabio Mancinelli <[email protected]>
*
* This program 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#import "AppController.h"
extern double CGSSecondsSinceLastInputEvent(unsigned long eventType);
@interface AppController (Private)
- (void)displayMinutesToNextPause;
- (void)startTimerWithInterval:(int)minutes;
- (void)stopTimer;
@end
@implementation AppController
- (id)init
{
self = [super init];
if(self != nil) {
timer = nil;
minutesToNextPause = 0;
enabled = YES;
[tapWindowController setDelegate:self];
standardUserDefaults = [NSUserDefaults standardUserDefaults];
}
return self;
}
- (void)awakeFromNib
{
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
[statusItem setHighlightMode:YES];
icon = [NSImage imageNamed:@"TAPStatusItemIcon"];
[statusItem setImage:icon];
[statusItem setMenu:menu];
[statusItem setEnabled:YES];
[minutesToNextPauseMenuItem setEnabled:FALSE];
[self startTimerWithInterval:[standardUserDefaults integerForKey:TAPWorkIntervalKey]];
}
- (IBAction)about:(id)sender
{
[[NSApplication sharedApplication] activateIgnoringOtherApps: YES];
[aboutPanel makeKeyAndOrderFront:nil];
}
- (IBAction)enable:(id)sender
{
if(enabled == YES) {
enabled = NO;
[self stopTimer];
[enableMenuItem setTitle:@"Enable"];
}
else {
enabled = YES;
[self startTimerWithInterval:minutesToNextPause];
[enableMenuItem setTitle:@"Disable"];
}
[self displayMinutesToNextPause];
}
- (IBAction)preferences:(id)sender
{
[[NSApplication sharedApplication] activateIgnoringOtherApps: YES];
[preferencesController showWindow:self];
}
- (void)startTimerWithInterval:(int)minutes
{
minutesToNextPause = minutes;
[self displayMinutesToNextPause];
timer = [[NSTimer scheduledTimerWithTimeInterval:60
target:self
selector:@selector(timeout:)
userInfo:nil
repeats:YES] retain];
}
- (void)stopTimer
{
[timer invalidate];
[timer release];
timer = nil;
}
- (void)displayMinutesToNextPause
{
NSString *message;
if(enabled) {
if(minutesToNextPause == 1) {
message = [NSString stringWithFormat:@"%d minute to next pause", minutesToNextPause];
}
else {
message = [NSString stringWithFormat:@"%d minutes to next pause", minutesToNextPause];
}
}
else {
message = @"Take A Pause! is disabled";
}
[minutesToNextPauseMenuItem setTitle:message];
}
- (void)timeout:(NSTimer *)aTimer
{
[self setMinutesToNextPause:([self minutesToNextPause] - 1)];
if([self minutesToNextPause] <= 0) {
[timer invalidate];
[timer release];
timer = nil;
[tapWindowController showWindow:self];
}
else {
int idleTimeInMinutes = (int)(CGSSecondsSinceLastInputEvent(-1) / (double)60.0);
if(idleTimeInMinutes > ([standardUserDefaults integerForKey:TAPIdleIntervalKey])) {
if([standardUserDefaults boolForKey:TAPRestartIfIdleKey] == YES) {
[self setMinutesToNextPause: [standardUserDefaults integerForKey:TAPWorkIntervalKey]];
}
}
}
}
- (void)tapPauseFinishedWithPostpone:(BOOL)postpone
{
if(postpone == YES) {
[self startTimerWithInterval:[standardUserDefaults integerForKey:TAPPostponeIntervalKey]];
}
else {
[self startTimerWithInterval:[standardUserDefaults integerForKey:TAPWorkIntervalKey]];
}
}
- (IBAction)quit:(id)sender
{
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSApplication sharedApplication] terminate:self];
}
- (int)minutesToNextPause
{
return minutesToNextPause;
}
- (void)setMinutesToNextPause:(int)minutes
{
minutesToNextPause = minutes;
[self displayMinutesToNextPause];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
[timer release];
}
- (IBAction)startPauseWithScreenshot:(id)sender
{
[tapWindowController showWindow:self];
}
@end