forked from bhaller/Jiggler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimedQuitController.m
196 lines (148 loc) · 4.6 KB
/
TimedQuitController.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
//
// TimedQuitController.m
// MM
//
// Created by Ben Haller on Wed Aug 25 2004.
// Copyright (c) 2004 Stick Software. All rights reserved.
//
#import "TimedQuitController.h"
#import "CocoaExtra.h"
static NSString *TimedQuitHoursDefaultsKey = @"TimedQuitHours";
static NSString *TimedQuitMinutesDefaultsKey = @"TimedQuitMinutes";
@interface TimedQuitController (PrivateAPI)
- (int)runForMinutesUntilQuit;
@end
@implementation TimedQuitController
+ (int)runPanelForMinutesUntilQuit
{
static TimedQuitController *sharedController;
if (!sharedController)
sharedController = [[TimedQuitController alloc] init];
return [sharedController runForMinutesUntilQuit];
}
- (void)loadFromDefaults
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
hoursToQuit = (int)[userDefaults integerForKey:TimedQuitHoursDefaultsKey];
minutesToQuit = (int)[userDefaults integerForKey:TimedQuitMinutesDefaultsKey];
if (hoursToQuit < 0)
hoursToQuit = 0;
if (hoursToQuit > 99)
hoursToQuit = 0;
if (minutesToQuit < 0)
minutesToQuit = 0;
if (minutesToQuit > 59)
minutesToQuit = 59;
if ((hoursToQuit == 0) && (minutesToQuit == 0))
hoursToQuit = 2;
}
- (void)saveToDefaults
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:hoursToQuit forKey:TimedQuitHoursDefaultsKey];
[userDefaults setInteger:minutesToQuit forKey:TimedQuitMinutesDefaultsKey];
}
- (id)init
{
if (self = [super init])
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// Set up our default values
[userDefaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:2], TimedQuitHoursDefaultsKey,
[NSNumber numberWithInt:0], TimedQuitMinutesDefaultsKey,
nil]];
[self loadFromDefaults];
}
return self;
}
- (void)readValues
{
hoursToQuit = [hoursTextfield intValue];
minutesToQuit = [minutesTextfield intValue];
}
- (void)fixEnabling
{
if ((hoursToQuit > 0) || (minutesToQuit > 0))
[okButton setEnabled:YES];
else
[okButton setEnabled:NO];
}
- (void)prepareWindow
{
if (!timedQuitPanel)
{
[[NSBundle mainBundle] loadNibNamed:@"TimedQuit" owner:self topLevelObjects:NULL];
[timedQuitPanel retain]; // we own this panel, so we retain it; could make it a retain property instead, whatever
}
[hoursTextfield setIntValue:hoursToQuit];
[minutesTextfield setIntValue:minutesToQuit];
[self fixEnabling];
}
- (int)runForMinutesUntilQuit
{
NSModalResponse resultCode;
[self prepareWindow];
[timedQuitPanel setInitialFirstResponder:hoursTextfield];
[timedQuitPanel makeFirstResponder:hoursTextfield];
[hoursTextfield selectText:nil];
resultCode = [NSApp runModalForWindow:timedQuitPanel];
[timedQuitPanel close];
if (resultCode == NSAlertFirstButtonReturn)
{
[self readValues];
[self saveToDefaults];
return ((hoursToQuit * 60) + minutesToQuit);
}
return 0;
}
- (void)updateTextFieldFromState:(NSTextField *)textfield
{
if (textfield == hoursTextfield)
[textfield setIntValue:hoursToQuit];
if (textfield == minutesTextfield)
[textfield setIntValue:minutesToQuit];
[textfield selectText:nil];
}
- (BOOL)checkString:(NSString *)string againstValue:(int)value
{
NSString *checkString = [NSString stringWithFormat:@"%d", value];
return [checkString isEqualToString:string];
}
- (void)controlTextDidChange:(NSNotification *)aNotification
{
id object = [aNotification object];
if (object == hoursTextfield)
{
//NSLog(@"controlTextDidChange: called for hoursTextfield");
hoursToQuit = [hoursTextfield intValue];
if ((hoursToQuit < 0) || (hoursToQuit > 99) || ![self checkString:[hoursTextfield stringValue] againstValue:hoursToQuit])
{
hoursToQuit = ((hoursToQuit <= 0) ? 0 : 99);
[self performSelector:@selector(updateTextFieldFromState:) withObject:hoursTextfield afterDelay:0.0 inModes:[NSArray allRunLoopModes]];
NSBeep();
}
[self fixEnabling];
}
if (object == minutesTextfield)
{
//NSLog(@"controlTextDidChange: called for minutesTextfield");
minutesToQuit = [minutesTextfield intValue];
if ((minutesToQuit < 0) || (minutesToQuit > 59) || ![self checkString:[minutesTextfield stringValue] againstValue:minutesToQuit])
{
minutesToQuit = ((minutesToQuit <= 0) ? 0 : 59);
[self performSelector:@selector(updateTextFieldFromState:) withObject:minutesTextfield afterDelay:0.0 inModes:[NSArray allRunLoopModes]];
NSBeep();
}
[self fixEnabling];
}
}
- (IBAction)okClicked:(id)sender
{
[NSApp stopModalWithCode:NSAlertFirstButtonReturn];
}
- (IBAction)cancelClicked:(id)sender
{
[NSApp stopModalWithCode:NSAlertSecondButtonReturn];
}
@end