source: releases/Pester/1.0b1/Source/PSAlarmSetController.m@ 632

Last change on this file since 632 was 26, checked in by Nicholas Riley, 22 years ago

Pester 1.0b1

File size: 8.8 KB
Line 
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 "PSAlarmNotifierController.h"
11#import "NJRDateFormatter.h"
12
13// XXX Bugs to file:
14// XXX any trailing spaces: -> exception for +[NSCalendarDate dateWithNaturalLanguageString]:
15// > NSCalendarDate dateWithNaturalLanguageString: '12 '
16// format error: internal error
17
18// XXX NSDate natural language stuff in NSCalendarDate (why?), misspelled category name
19// XXX NSCalendarDate natural language stuff behaves differently from NSDateFormatter (AM/PM has no effect, shouldn't they share code?)
20// XXX 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
21// XXX NSTimeFormatString does not include %p when it should, meaning that AM/PM is stripped yet 12-hour time is still used
22// XXX 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?
23// XXX "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.
24// XXX 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.
25// XXX descriptions for %X and %x are reversed (time zone is in %X, not %x)
26// XXX too hard to implement date-only or time-only formatters
27// XXX should be able to specify that natural language favors date or time (10 = 10th of month, not 10am)
28// XXX please expose the iCal controls!
29
30@interface PSAlarmSetController (Private)
31
32- (void)_stopUpdateTimer;
33
34@end
35
36@implementation PSAlarmSetController
37
38- (void)awakeFromNib;
39{
40 // XXX bugs prevent this code from working properly on Jaguar
41 /* NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
42 [timeOfDay setFormatter: [[NJRDateFormatter alloc] initWithDateFormat: [defaults objectForKey: NSTimeFormatString] allowNaturalLanguage: YES]];
43 [timeDate setFormatter: [[NJRDateFormatter alloc] initWithDateFormat: [defaults objectForKey: NSShortDateFormatString] allowNaturalLanguage: YES]]; */
44 alarm = [[PSAlarm alloc] init];
45 [[self window] center];
46 [self inAtChanged: nil];
47 [[self window] makeKeyAndOrderFront: nil];
48}
49
50- (void)setStatus:(NSString *)aString;
51{
52 // NSLog(@"%@", alarm);
53 if (aString != status) {
54 [status release]; status = nil;
55 status = [aString retain];
56 [timeSummary setStringValue: status];
57 }
58}
59
60- (id)objectValueForTextField:(NSTextField *)field whileEditing:(id)sender;
61{
62 if (sender == field) {
63 NSString *stringValue = [[[self window] fieldEditor: NO forObject: field] string];
64 id obj = nil;
65 [[field formatter] getObjectValue: &obj forString: stringValue errorDescription: NULL];
66 // NSLog(@"from field editor: %@", obj);
67 return obj;
68 } else {
69 // NSLog(@"from field: %@", [field objectValue]);
70 return [field objectValue];
71 }
72}
73
74- (void)setAlarmDateAndInterval:(id)sender;
75{
76 if (isInterval) {
77 [alarm setInterval:
78 [[self objectValueForTextField: timeInterval whileEditing: sender] intValue] *
79 [timeIntervalUnits selectedTag]];
80 } else {
81 [alarm setForDate: [self objectValueForTextField: timeDate whileEditing: sender]
82 atTime: [self objectValueForTextField: timeOfDay whileEditing: sender]];
83 }
84}
85
86- (void)_stopUpdateTimer;
87{
88 if ([updateTimer isValid]) [updateTimer invalidate];
89 [updateTimer release]; updateTimer = nil;
90}
91
92// XXX use OACalendar?
93
94- (IBAction)updateDateDisplay:(id)sender;
95{
96 // NSLog(@"updateDateDisplay: %@", sender);
97 if ([alarm isValid]) {
98 [self setStatus: [[alarm date] descriptionWithCalendarFormat: @"Alarm will be set for %X on %x" timeZone: nil locale: nil]];
99 [setButton setEnabled: YES];
100 if (updateTimer == nil || ![updateTimer isValid]) {
101 // 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.
102 // NSLog(@"setting timer");
103 if (isInterval) {
104 updateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: YES];
105 } else {
106 updateTimer = [NSTimer scheduledTimerWithTimeInterval: [alarm interval] target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: NO];
107 }
108 [updateTimer retain];
109 }
110 } else {
111 [setButton setEnabled: NO];
112 [self setStatus: [alarm invalidMessage]];
113 [self _stopUpdateTimer];
114 }
115}
116
117// 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.
118
119- (IBAction)update:(id)sender;
120{
121 // NSLog(@"update: %@", sender);
122 [self setAlarmDateAndInterval: sender];
123 [self updateDateDisplay: sender];
124}
125
126- (IBAction)inAtChanged:(id)sender;
127{
128 isInterval = ([inAtMatrix selectedTag] == 0);
129 [timeInterval setEnabled: isInterval];
130 [timeIntervalUnits setEnabled: isInterval];
131 [timeOfDay setEnabled: !isInterval];
132 [timeDate setEnabled: !isInterval];
133 [timeDateCompletions setEnabled: !isInterval];
134 if (sender != nil)
135 [[self window] makeFirstResponder: isInterval ? timeInterval : timeOfDay];
136 // NSLog(@"UPDATING FROM inAtChanged");
137 [self update: nil];
138}
139
140- (IBAction)dateCompleted:(NSPopUpButton *)sender;
141{
142 [timeDate setStringValue: [sender titleOfSelectedItem]];
143 [self update: sender];
144}
145
146// to ensure proper updating of interval, this should be the only method by which the window is shown (e.g. from the Alarm menu)
147- (IBAction)showWindow:(id)sender;
148{
149 if (![[self window] isVisible]) {
150 // NSLog(@"UPDATING FROM showWindow");
151 [self update: self];
152 }
153 [super showWindow: sender];
154}
155
156- (IBAction)setAlarm:(NSButton *)sender;
157{
158 PSAlarmNotifierController *notifier = [PSAlarmNotifierController alloc];
159 if (notifier == nil) {
160 [self setStatus: @"Unable to set alarm."];
161 return;
162 }
163 [self setAlarmDateAndInterval: sender];
164 [alarm setMessage: [messageField stringValue]];
165 if (![alarm setTimer]) {
166 [self setStatus: [@"Unable to set alarm. " stringByAppendingString: [alarm invalidMessage]]];
167 return;
168 }
169 [self setStatus: [[alarm date] descriptionWithCalendarFormat: @"Alarm set for %x at %X" timeZone: nil locale: nil]];
170 [[self window] close];
171 [alarm release];
172 alarm = [[PSAlarm alloc] init];
173}
174
175@end
176
177@implementation PSAlarmSetController (NSControlSubclassDelegate)
178
179- (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error;
180{
181 unichar c;
182 int tag;
183 unsigned length = [string length];
184 if (control != timeInterval || length == 0) return;
185 c = [string characterAtIndex: length - 1];
186 switch (c) {
187 case 's': case 'S': tag = 1; break;
188 case 'm': case 'M': tag = 60; break;
189 case 'h': case 'H': tag = 60 * 60; break;
190 default: return;
191 }
192 [timeIntervalUnits selectItemAtIndex:
193 [timeIntervalUnits indexOfItemWithTag: tag]];
194 // NSLog(@"UPDATING FROM validation");
195 [self update: timeInterval]; // make sure we still examine the field editor, otherwise if the existing numeric string is invalid, it'll be cleared
196}
197
198@end
199
200@implementation PSAlarmSetController (NSWindowNotifications)
201
202- (void)windowWillClose:(NSNotification *)notification;
203{
204 // NSLog(@"stopping update timer");
205 [self _stopUpdateTimer];
206}
207
208@end
209
210@implementation PSAlarmSetController (NSControlSubclassNotifications)
211
212// called because we're the delegate
213
214- (void)controlTextDidChange:(NSNotification *)notification;
215{
216 // NSLog(@"UPDATING FROM controlTextDidChange");
217 [self update: [notification object]];
218}
219
220@end
221
222@implementation PSAlarmSetController (NSApplicationDelegate)
223
224- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag;
225{
226 if (!flag) [self showWindow: self];
227 return YES;
228}
229
230@end
Note: See TracBrowser for help on using the repository browser.