source: trunk/Cocoa/Pester/Source/PSAlarmSetController.m@ 28

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

Pester 1.0

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