1 | //
|
---|
2 | // PSAlarmSetController.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 "PSAlarmSetController.h"
|
---|
10 | #import "PSAlarmAlertController.h"
|
---|
11 | #import "NJRDateFormatter.h"
|
---|
12 | #import "NJRFSObjectSelector.h"
|
---|
13 | #import "NJRQTMediaPopUpButton.h"
|
---|
14 | #import "NJRVoicePopUpButton.h"
|
---|
15 | #import <Carbon/Carbon.h>
|
---|
16 |
|
---|
17 | #import "PSDockBounceAlert.h"
|
---|
18 | #import "PSScriptAlert.h"
|
---|
19 | #import "PSNotifierAlert.h"
|
---|
20 | #import "PSBeepAlert.h"
|
---|
21 | #import "PSMovieAlert.h"
|
---|
22 | #import "PSSpeechAlert.h"
|
---|
23 |
|
---|
24 | /* Bugs to file:
|
---|
25 |
|
---|
26 | ¥ any trailing spaces: -> exception for +[NSCalendarDate dateWithNaturalLanguageString]:
|
---|
27 | > NSCalendarDate dateWithNaturalLanguageString: '12 '
|
---|
28 | format error: internal error
|
---|
29 |
|
---|
30 | ¥ NSDate natural language stuff in NSCalendarDate (why?), misspelled category name
|
---|
31 | ¥ NSCalendarDate natural language stuff behaves differently from NSDateFormatter (AM/PM has no effect, shouldn't they share code?)
|
---|
32 | ¥ NSDateFormatter doc class description gives two examples for natural language that are incorrect, no link to NSDate doc that describes exactly how natural language dates are parsed
|
---|
33 | ¥ NSTimeFormatString does not include %p when it should, meaning that AM/PM is stripped yet 12-hour time is still used
|
---|
34 | ¥ NSNextDayDesignations, NSNextNextDayDesignations are noted as 'a string' in NSUserDefaults docs, but maybe they are actually an array, or either an array or a string, given their names?
|
---|
35 | ¥ "Setting the Format for Dates" does not document how to get 1:15 AM, the answer is %1I - strftime has no exact equivalent; the closest is %l. strftime does not permit numeric prefixes. It also refers to "NSCalendar" when no such class exists.
|
---|
36 | ¥ none of many mentions of NSAMPMDesignation indicates that they include the leading spaces (" AM", " PM"). In "Setting the Format for Dates", needs to mention that the leading spaces are not included in %p with strftime. But if you use the NSCalendarDate stuff, it appears %p doesn't include the space.
|
---|
37 | ¥ descriptions for %X and %x are reversed (time zone is in %X, not %x)
|
---|
38 | ¥ too hard to implement date-only or time-only formatters
|
---|
39 | ¥ should be able to specify that natural language favors date or time (10 = 10th of month, not 10am)
|
---|
40 | ¥ please expose the iCal controls!
|
---|
41 |
|
---|
42 | */
|
---|
43 |
|
---|
44 | @interface PSAlarmSetController (Private)
|
---|
45 |
|
---|
46 | - (void)_stopUpdateTimer;
|
---|
47 |
|
---|
48 | @end
|
---|
49 |
|
---|
50 | @implementation PSAlarmSetController
|
---|
51 |
|
---|
52 | - (void)awakeFromNib;
|
---|
53 | {
|
---|
54 | // XXX bugs prevent this code from working properly on Jaguar
|
---|
55 | /* NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
56 | [timeOfDay setFormatter: [[NJRDateFormatter alloc] initWithDateFormat: [defaults objectForKey: NSTimeFormatString] allowNaturalLanguage: YES]];
|
---|
57 | [timeDate setFormatter: [[NJRDateFormatter alloc] initWithDateFormat: [defaults objectForKey: NSShortDateFormatString] allowNaturalLanguage: YES]]; */
|
---|
58 | alarm = [[PSAlarm alloc] init];
|
---|
59 | [[self window] center];
|
---|
60 | [self inAtChanged: nil];
|
---|
61 | [self playSoundChanged: nil];
|
---|
62 | [self doScriptChanged: nil];
|
---|
63 | [self doSpeakChanged: nil];
|
---|
64 | [script setFileTypes: [NSArray arrayWithObjects: @"applescript", @"script", NSFileTypeForHFSTypeCode(kOSAFileType), NSFileTypeForHFSTypeCode('TEXT'), nil]];
|
---|
65 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(silence:) name: PSAlarmAlertStopNotification object: nil];
|
---|
66 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(playSoundChanged:) name: NJRQTMediaPopUpButtonMovieChangedNotification object: sound];
|
---|
67 | [voice setDelegate: self];
|
---|
68 | [[self window] makeKeyAndOrderFront: nil];
|
---|
69 | }
|
---|
70 |
|
---|
71 | - (void)setStatus:(NSString *)aString;
|
---|
72 | {
|
---|
73 | // NSLog(@"%@", alarm);
|
---|
74 | if (aString != status) {
|
---|
75 | [status release]; status = nil;
|
---|
76 | status = [aString retain];
|
---|
77 | [timeSummary setStringValue: status];
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | - (id)objectValueForTextField:(NSTextField *)field whileEditing:(id)sender;
|
---|
82 | {
|
---|
83 | if (sender == field) {
|
---|
84 | NSString *stringValue = [[[self window] fieldEditor: NO forObject: field] string];
|
---|
85 | id obj = nil;
|
---|
86 | [[field formatter] getObjectValue: &obj forString: stringValue errorDescription: NULL];
|
---|
87 | // NSLog(@"from field editor: %@", obj);
|
---|
88 | return obj;
|
---|
89 | } else {
|
---|
90 | // NSLog(@"from field: %@", [field objectValue]);
|
---|
91 | return [field objectValue];
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | - (void)setAlarmDateAndInterval:(id)sender;
|
---|
96 | {
|
---|
97 | if (isInterval) {
|
---|
98 | [alarm setInterval:
|
---|
99 | [[self objectValueForTextField: timeInterval whileEditing: sender] intValue] *
|
---|
100 | [timeIntervalUnits selectedTag]];
|
---|
101 | } else {
|
---|
102 | [alarm setForDate: [self objectValueForTextField: timeDate whileEditing: sender]
|
---|
103 | atTime: [self objectValueForTextField: timeOfDay whileEditing: sender]];
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | - (void)_stopUpdateTimer;
|
---|
108 | {
|
---|
109 | [updateTimer invalidate]; [updateTimer release]; updateTimer = nil;
|
---|
110 | }
|
---|
111 |
|
---|
112 | // XXX use OACalendar?
|
---|
113 |
|
---|
114 | - (IBAction)updateDateDisplay:(id)sender;
|
---|
115 | {
|
---|
116 | // NSLog(@"updateDateDisplay: %@", sender);
|
---|
117 | if ([alarm isValid]) {
|
---|
118 | [self setStatus: [[alarm date] descriptionWithCalendarFormat: @"Alarm will be set for %X on %x" timeZone: nil locale: nil]];
|
---|
119 | [setButton setEnabled: YES];
|
---|
120 | if (updateTimer == nil || ![updateTimer isValid]) {
|
---|
121 | // XXX this logic (and the timer) should really go into PSAlarm, to send notifications for status updates instead. Timer starts when people are watching, stops when people aren't.
|
---|
122 | // NSLog(@"setting timer");
|
---|
123 | if (isInterval) {
|
---|
124 | updateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: YES];
|
---|
125 | } else {
|
---|
126 | updateTimer = [NSTimer scheduledTimerWithTimeInterval: [alarm interval] target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: NO];
|
---|
127 | }
|
---|
128 | [updateTimer retain];
|
---|
129 | }
|
---|
130 | } else {
|
---|
131 | [setButton setEnabled: NO];
|
---|
132 | [self setStatus: [alarm invalidMessage]];
|
---|
133 | [self _stopUpdateTimer];
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | // Be careful not to hook up any of the text fields' actions to update: because we handle them in controlTextDidChange: instead. If we could get the active text field somehow via public API (guess we could use controlTextDidBegin/controlTextDidEndEditing) then we'd not need to overload the update sender for this purpose. Or, I guess, we could use another method other than update. It should not be this hard to implement what is essentially standard behavior. Sigh.
|
---|
138 |
|
---|
139 | - (IBAction)update:(id)sender;
|
---|
140 | {
|
---|
141 | // NSLog(@"update: %@", sender);
|
---|
142 | [self setAlarmDateAndInterval: sender];
|
---|
143 | [self updateDateDisplay: sender];
|
---|
144 | }
|
---|
145 |
|
---|
146 | - (IBAction)inAtChanged:(id)sender;
|
---|
147 | {
|
---|
148 | isInterval = ([inAtMatrix selectedTag] == 0);
|
---|
149 | [timeInterval setEnabled: isInterval];
|
---|
150 | [timeIntervalUnits setEnabled: isInterval];
|
---|
151 | [timeIntervalRepeats setEnabled: isInterval];
|
---|
152 | [timeOfDay setEnabled: !isInterval];
|
---|
153 | [timeDate setEnabled: !isInterval];
|
---|
154 | [timeDateCompletions setEnabled: !isInterval];
|
---|
155 | if (sender != nil)
|
---|
156 | [[self window] makeFirstResponder: isInterval ? timeInterval : timeOfDay];
|
---|
157 | // NSLog(@"UPDATING FROM inAtChanged");
|
---|
158 | [self update: nil];
|
---|
159 | }
|
---|
160 |
|
---|
161 | - (IBAction)playSoundChanged:(id)sender;
|
---|
162 | {
|
---|
163 | BOOL playSoundSelected = [playSound intValue];
|
---|
164 | BOOL canRepeat = playSoundSelected ? [sound canRepeat] : NO;
|
---|
165 | [sound setEnabled: playSoundSelected];
|
---|
166 | [soundRepetitions setEnabled: canRepeat];
|
---|
167 | [soundRepetitionStepper setEnabled: canRepeat];
|
---|
168 | [soundRepetitionsLabel setTextColor: canRepeat ? [NSColor controlTextColor] : [NSColor disabledControlTextColor]];
|
---|
169 | if (playSoundSelected && sender != nil)
|
---|
170 | [[self window] makeFirstResponder: sound];
|
---|
171 | }
|
---|
172 |
|
---|
173 | - (IBAction)setSoundRepetitionCount:(id)sender;
|
---|
174 | {
|
---|
175 | int newReps = [sender intValue], oldReps = [soundRepetitions intValue];
|
---|
176 | if (newReps != oldReps)
|
---|
177 | [soundRepetitions setIntValue: newReps];
|
---|
178 | }
|
---|
179 |
|
---|
180 | // XXX should check the 'Do script:' button when someone drops a script on the button
|
---|
181 |
|
---|
182 | - (IBAction)doScriptChanged:(id)sender;
|
---|
183 | {
|
---|
184 | BOOL doScriptSelected = [doScript intValue];
|
---|
185 | [script setEnabled: doScriptSelected];
|
---|
186 | [scriptSelectButton setEnabled: doScriptSelected];
|
---|
187 | if (doScriptSelected && sender != nil)
|
---|
188 | [[self window] makeFirstResponder: scriptSelectButton];
|
---|
189 | }
|
---|
190 |
|
---|
191 | - (IBAction)doSpeakChanged:(id)sender;
|
---|
192 | {
|
---|
193 | BOOL doSpeakSelected = [doSpeak intValue];
|
---|
194 | [voice setEnabled: doSpeakSelected];
|
---|
195 | if (doSpeakSelected && sender != nil)
|
---|
196 | [[self window] makeFirstResponder: voice];
|
---|
197 | }
|
---|
198 |
|
---|
199 | - (IBAction)dateCompleted:(NSPopUpButton *)sender;
|
---|
200 | {
|
---|
201 | [timeDate setStringValue: [sender titleOfSelectedItem]];
|
---|
202 | [self update: sender];
|
---|
203 | }
|
---|
204 |
|
---|
205 | // to ensure proper updating of interval, this should be the only method by which the window is shown (e.g. from the Alarm menu)
|
---|
206 | - (IBAction)showWindow:(id)sender;
|
---|
207 | {
|
---|
208 | if (![[self window] isVisible]) {
|
---|
209 | [self update: self];
|
---|
210 | // XXX otherwise, first responder appears to alternate every time the window is shown?! And if you set the initial first responder, you can't tab in the window. :(
|
---|
211 | [[self window] makeFirstResponder: [[self window] initialFirstResponder]];
|
---|
212 | }
|
---|
213 | [super showWindow: sender];
|
---|
214 | }
|
---|
215 |
|
---|
216 | - (IBAction)setAlarm:(NSButton *)sender;
|
---|
217 | {
|
---|
218 | // set alarm
|
---|
219 | [self setAlarmDateAndInterval: sender];
|
---|
220 | [alarm setMessage: [messageField stringValue]];
|
---|
221 | if (![alarm setTimer]) {
|
---|
222 | [self setStatus: [@"Unable to set alarm. " stringByAppendingString: [alarm invalidMessage]]];
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 | [alarm removeAlerts];
|
---|
227 | // dock bounce alert
|
---|
228 | if ([bounceDockIcon state] == NSOnState)
|
---|
229 | [alarm addAlert: [PSDockBounceAlert alert]];
|
---|
230 | // script alert
|
---|
231 | if ([doScript intValue]) {
|
---|
232 | BDAlias *scriptFileAlias = [script alias];
|
---|
233 | if (scriptFileAlias == nil) {
|
---|
234 | [self setStatus: @"Unable to set script alert (no script specified?)"];
|
---|
235 | return;
|
---|
236 | }
|
---|
237 | [alarm addAlert: [PSScriptAlert alertWithScriptFileAlias: scriptFileAlias]];
|
---|
238 | }
|
---|
239 | // notifier alert
|
---|
240 | if ([displayMessage intValue])
|
---|
241 | [alarm addAlert: [PSNotifierAlert alert]];
|
---|
242 | // sound alerts
|
---|
243 | if ([playSound intValue]) {
|
---|
244 | BDAlias *soundAlias = [sound selectedAlias];
|
---|
245 | unsigned short numReps = [soundRepetitions intValue];
|
---|
246 | if (soundAlias == nil) // beep alert
|
---|
247 | [alarm addAlert: [PSBeepAlert alertWithRepetitions: numReps]];
|
---|
248 | else // movie alert
|
---|
249 | [alarm addAlert: [PSMovieAlert alertWithMovieFileAlias: soundAlias repetitions: numReps]];
|
---|
250 | }
|
---|
251 | // speech alert
|
---|
252 | if ([doSpeak intValue])
|
---|
253 | [alarm addAlert: [PSSpeechAlert alertWithVoice: [voice titleOfSelectedItem]]];
|
---|
254 |
|
---|
255 | [self setStatus: [[alarm date] descriptionWithCalendarFormat: @"Alarm set for %x at %X" timeZone: nil locale: nil]];
|
---|
256 | [[self window] close];
|
---|
257 | [alarm release];
|
---|
258 | alarm = [[PSAlarm alloc] init];
|
---|
259 | }
|
---|
260 |
|
---|
261 | - (IBAction)silence:(id)sender;
|
---|
262 | {
|
---|
263 | [sound stopSoundPreview: self];
|
---|
264 | [voice stopVoicePreview: self];
|
---|
265 | }
|
---|
266 |
|
---|
267 | @end
|
---|
268 |
|
---|
269 | @implementation PSAlarmSetController (NSControlSubclassDelegate)
|
---|
270 |
|
---|
271 | - (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error;
|
---|
272 | {
|
---|
273 | unichar c;
|
---|
274 | int tag;
|
---|
275 | unsigned length = [string length];
|
---|
276 | if (control != timeInterval || length == 0) return;
|
---|
277 | c = [string characterAtIndex: length - 1];
|
---|
278 | switch (c) {
|
---|
279 | case 's': case 'S': tag = 1; break;
|
---|
280 | case 'm': case 'M': tag = 60; break;
|
---|
281 | case 'h': case 'H': tag = 60 * 60; break;
|
---|
282 | default: return;
|
---|
283 | }
|
---|
284 | [timeIntervalUnits selectItemAtIndex:
|
---|
285 | [timeIntervalUnits indexOfItemWithTag: tag]];
|
---|
286 | // NSLog(@"UPDATING FROM validation");
|
---|
287 | [self update: timeInterval]; // make sure we still examine the field editor, otherwise if the existing numeric string is invalid, it'll be cleared
|
---|
288 | }
|
---|
289 |
|
---|
290 | @end
|
---|
291 |
|
---|
292 | @implementation PSAlarmSetController (NSWindowNotifications)
|
---|
293 |
|
---|
294 | - (void)windowWillClose:(NSNotification *)notification;
|
---|
295 | {
|
---|
296 | // NSLog(@"stopping update timer");
|
---|
297 | [self silence: nil];
|
---|
298 | [self _stopUpdateTimer];
|
---|
299 | }
|
---|
300 |
|
---|
301 | @end
|
---|
302 |
|
---|
303 | @implementation PSAlarmSetController (NSControlSubclassNotifications)
|
---|
304 |
|
---|
305 | // called because we're the delegate
|
---|
306 |
|
---|
307 | - (void)controlTextDidChange:(NSNotification *)notification;
|
---|
308 | {
|
---|
309 | // NSLog(@"UPDATING FROM controlTextDidChange");
|
---|
310 | [self update: [notification object]];
|
---|
311 | }
|
---|
312 |
|
---|
313 | @end
|
---|
314 |
|
---|
315 | @implementation PSAlarmSetController (NJRVoicePopUpButtonDelegate)
|
---|
316 |
|
---|
317 | - (NSString *)voicePopUpButton:(NJRVoicePopUpButton *)sender previewStringForVoice:(NSString *)voice;
|
---|
318 | {
|
---|
319 | return [messageField stringValue];
|
---|
320 | }
|
---|
321 |
|
---|
322 | @end |
---|