1 | //
|
---|
2 | // PSAlarmNotifierController.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Tue Oct 08 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "PSAlarmNotifierController.h"
|
---|
10 | #import "PSAlarmAlertController.h"
|
---|
11 | #import "PSAlarm.h"
|
---|
12 | #import "PSApplication.h"
|
---|
13 | #import "PSNotifierAlert.h"
|
---|
14 | #import "PSSnoozeUntilController.h"
|
---|
15 | #import "NJRIntervalField.h"
|
---|
16 |
|
---|
17 | static NSString * const PSAlarmSnoozeInterval = @"Pester alarm snooze interval"; // NSUserDefaults key
|
---|
18 |
|
---|
19 | @interface PSAlarmNotifierController (Private)
|
---|
20 |
|
---|
21 | - (void)update:(id)sender;
|
---|
22 | - (void)updateNextDateDisplay:(id)sender;
|
---|
23 |
|
---|
24 | @end
|
---|
25 |
|
---|
26 | @implementation PSAlarmNotifierController
|
---|
27 |
|
---|
28 | // XXX should use NSNonactivatingPanelMask on 10.2?
|
---|
29 |
|
---|
30 | - (id)initWithAlarm:(PSAlarm *)anAlarm;
|
---|
31 | {
|
---|
32 | if ([self initWithWindowNibName: @"Notifier"]) {
|
---|
33 | NSWindow *window = [self window];
|
---|
34 | NSRect frameRect = [window frame];
|
---|
35 | alarm = [anAlarm retain];
|
---|
36 | [messageField setStringValue: [alarm message]];
|
---|
37 | [dateField setStringValue: [alarm dateTimeString]];
|
---|
38 | if (![self setSnoozeInterval: [alarm snoozeInterval]] &&
|
---|
39 | ![self setSnoozeInterval: [[[NSUserDefaults standardUserDefaults] objectForKey: PSAlarmSnoozeInterval] doubleValue]])
|
---|
40 | [self setSnoozeInterval: 15 * 60]; // 15 minutes
|
---|
41 | if ([alarm isRepeating]) {
|
---|
42 | [intervalField setStringValue:
|
---|
43 | [NSString stringWithFormat: @"every %@", [[alarm intervalString] lowercaseString]]];
|
---|
44 | [self updateNextDateDisplay: nil];
|
---|
45 | updateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateNextDateDisplay:) userInfo: nil repeats: YES];
|
---|
46 | frameRect.size = [window maxSize];
|
---|
47 | } else {
|
---|
48 | frameRect.size = [window minSize];
|
---|
49 | }
|
---|
50 | [window setFrame: frameRect display: NO];
|
---|
51 | [window center];
|
---|
52 | [window makeKeyAndOrderFront: nil];
|
---|
53 | [window orderFrontRegardless];
|
---|
54 | [(PSApplication *)NSApp orderOutSetAlarmPanelIfHidden];
|
---|
55 | }
|
---|
56 | return self;
|
---|
57 | }
|
---|
58 |
|
---|
59 | - (void)dealloc;
|
---|
60 | {
|
---|
61 | [alarm release]; alarm = nil;
|
---|
62 | [updateTimer invalidate]; updateTimer = nil;
|
---|
63 | [super dealloc];
|
---|
64 | }
|
---|
65 |
|
---|
66 | - (void)updateNextDateDisplay:(id)sender;
|
---|
67 | {
|
---|
68 | if (!canSnooze) {
|
---|
69 | NSString *nextDateTimeString = [alarm nextDateTimeString];
|
---|
70 | if (nextDateTimeString == nil) { // no longer repeating
|
---|
71 | [updateTimer invalidate]; updateTimer = nil;
|
---|
72 | } else {
|
---|
73 | [nextDateField setStringValue: nextDateTimeString];
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | - (void)update:(id)sender;
|
---|
79 | {
|
---|
80 | snoozeInterval = [snoozeIntervalField interval];
|
---|
81 | canSnooze = (snoozeInterval > 0);
|
---|
82 | if (canSnooze) [nextDateField setStringValue: @"after snooze"];
|
---|
83 | [snoozeButton setEnabled: canSnooze];
|
---|
84 | [canSnooze ? snoozeButton : okButton setKeyEquivalent: @"\r"];
|
---|
85 | [canSnooze ? okButton : snoozeButton setKeyEquivalent: @""];
|
---|
86 | }
|
---|
87 |
|
---|
88 | - (IBAction)close:(id)sender;
|
---|
89 | {
|
---|
90 | [PSAlarmAlertController stopAlerts: sender];
|
---|
91 | [self retain];
|
---|
92 | [self close]; // releases self in windowWillClose:
|
---|
93 | [[PSNotifierAlert alert] completedForAlarm: alarm];
|
---|
94 | [self release];
|
---|
95 | }
|
---|
96 |
|
---|
97 | - (IBAction)snoozeUntil:(NSMenuItem *)sender;
|
---|
98 | {
|
---|
99 | [PSSnoozeUntilController snoozeUntilControllerWithNotifierController: self];
|
---|
100 | }
|
---|
101 |
|
---|
102 | - (IBAction)snoozeIntervalUnitsChanged:(NSPopUpButton *)sender;
|
---|
103 | {
|
---|
104 | if ([[sender selectedItem] tag] > 0) [self update: nil];
|
---|
105 | }
|
---|
106 |
|
---|
107 | - (NSTimeInterval)snoozeInterval;
|
---|
108 | {
|
---|
109 | return snoozeInterval;
|
---|
110 | }
|
---|
111 |
|
---|
112 | - (BOOL)setSnoozeInterval:(NSTimeInterval)interval;
|
---|
113 | {
|
---|
114 | snoozeInterval = interval;
|
---|
115 | return [snoozeIntervalField setInterval: interval];
|
---|
116 | }
|
---|
117 |
|
---|
118 | - (IBAction)snooze:(NSButton *)sender;
|
---|
119 | {
|
---|
120 | snoozeInterval = [snoozeIntervalField interval];
|
---|
121 | [alarm setSnoozeInterval: snoozeInterval];
|
---|
122 | [[NSUserDefaults standardUserDefaults] setObject: [NSNumber numberWithDouble: snoozeInterval] forKey: PSAlarmSnoozeInterval];
|
---|
123 | [self close: sender];
|
---|
124 | }
|
---|
125 |
|
---|
126 | - (void)snoozeUntilDate:(NSCalendarDate *)date;
|
---|
127 | {
|
---|
128 | [alarm setSnoozeInterval: [date timeIntervalSinceNow]];
|
---|
129 | [self close: self];
|
---|
130 | }
|
---|
131 |
|
---|
132 | - (IBAction)stopRepeating:(NSButton *)sender;
|
---|
133 | {
|
---|
134 | NSWindow *window = [self window];
|
---|
135 | NSRect frameRect = [window frame];
|
---|
136 | NSSize newSize = [window minSize];
|
---|
137 |
|
---|
138 | [alarm setRepeating: NO];
|
---|
139 | [sender setEnabled: NO];
|
---|
140 | frameRect.origin.y += frameRect.size.height - newSize.height;
|
---|
141 | frameRect.size = newSize;
|
---|
142 | [window setFrame: frameRect display: YES animate: YES];
|
---|
143 | }
|
---|
144 |
|
---|
145 | @end
|
---|
146 |
|
---|
147 | @implementation PSAlarmNotifierController (NSControlSubclassDelegate)
|
---|
148 |
|
---|
149 | - (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error;
|
---|
150 | {
|
---|
151 | if (control == snoozeIntervalField)
|
---|
152 | [snoozeIntervalField handleDidFailToFormatString: string errorDescription: error label: @"snooze interval"];
|
---|
153 | return NO;
|
---|
154 | }
|
---|
155 |
|
---|
156 | - (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error;
|
---|
157 | {
|
---|
158 | // NSLog(@"UPDATING FROM validation");
|
---|
159 | [self update: control]; // switch to snooze if someone types something weird...
|
---|
160 | }
|
---|
161 |
|
---|
162 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector;
|
---|
163 | {
|
---|
164 | // NSLog(@"UPDATING from textView: %@", NSStringFromSelector(commandSelector));
|
---|
165 | if (commandSelector == @selector(cancel:)) {
|
---|
166 | // if someone just wants the stupid thing to go away and presses escape, donÕt hinder them
|
---|
167 | [self close: control];
|
---|
168 | return YES;
|
---|
169 | }
|
---|
170 | // if someone invokes the default button or switches fields, donÕt override it
|
---|
171 | if (commandSelector == @selector(insertNewline:) ||
|
---|
172 | commandSelector == @selector(insertTab:) ||
|
---|
173 | commandSelector == @selector(insertBacktab:)) return NO;
|
---|
174 | [self update: control]; // ...or if they type a navigation key...
|
---|
175 | return NO; // we donÕt handle it
|
---|
176 | }
|
---|
177 |
|
---|
178 | @end
|
---|
179 |
|
---|
180 | @implementation PSAlarmNotifierController (NSControlSubclassNotifications)
|
---|
181 |
|
---|
182 | - (void)controlTextDidChange:(NSNotification *)notification;
|
---|
183 | {
|
---|
184 | // NSLog(@"UPDATING FROM controlTextDidChange: %@", [notification object]);
|
---|
185 | [self update: [notification object]]; // ...or if they modify the snooze interval
|
---|
186 | }
|
---|
187 |
|
---|
188 | @end
|
---|
189 |
|
---|
190 | @implementation PSAlarmNotifierController (NSWindowNotifications)
|
---|
191 |
|
---|
192 | - (void)windowWillClose:(NSNotification *)notification;
|
---|
193 | {
|
---|
194 | // canÕt rely on dealloc to invalidate the timer, because it retains this object
|
---|
195 | [updateTimer invalidate]; updateTimer = nil;
|
---|
196 | [self release]; // in non-document-based apps, this is needed; see docs
|
---|
197 | }
|
---|
198 |
|
---|
199 | @end |
---|