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

Last change on this file since 21 was 21, checked in by Nicholas Riley, 20 years ago

Pester 1.0d1

File size: 9.1 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
31@implementation PSAlarmSetController
32
33- (void)awakeFromNib;
34{
35 // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
36 [[self window] center];
37 // XXX bugs prevent these from working (sigh...)
38 // [timeOfDay setFormatter: [[NJRDateFormatter alloc] initWithDateFormat: [defaults objectForKey: NSTimeFormatString] allowNaturalLanguage: YES]];
39 // [timeDate setFormatter: [[NJRDateFormatter alloc] initWithDateFormat: [defaults objectForKey: NSShortDateFormatString] allowNaturalLanguage: YES]];
40 [self inAtChanged: nil];
41}
42
43- (void)setStatus:(NSString *)aString;
44{
45 if (aString != status) {
46 [status release]; status = nil;
47 status = [aString retain];
48 [timeSummary setStringValue: status];
49 }
50}
51
52- (id)objectValueForTextField:(NSTextField *)field whileEditing:(id)sender;
53{
54 if (sender == field) {
55 NSString *stringValue = [[[self window] fieldEditor: NO forObject: field] string];
56 id obj = nil;
57 [[field formatter] getObjectValue: &obj forString: stringValue errorDescription: NULL];
58 // NSLog(@"from field editor: %@", obj);
59 return obj;
60 } else {
61 // NSLog(@"from field: %@", [field objectValue]);
62 return [field objectValue];
63 }
64}
65
66- (void)setAlarmDateAndInterval:(id)sender;
67{
68 [alarmDate release];
69 alarmDate = nil;
70 alarmInterval = 0;
71 if (isIn) {
72 alarmInterval = [[self objectValueForTextField: timeInterval whileEditing: sender] intValue] * [timeIntervalUnits selectedTag];
73 if (alarmInterval == 0) {
74 [self setStatus: @"Please specify an alarm interval."]; return;
75 }
76 alarmDate = [NSCalendarDate dateWithTimeIntervalSinceNow: alarmInterval];
77 [alarmDate retain];
78 } else {
79 NSDate *time = [self objectValueForTextField: timeOfDay whileEditing: sender];
80 NSDate *date = [self objectValueForTextField: timeDate whileEditing: sender];
81 NSCalendarDate *calTime, *calDate;
82 if (time == nil && date == nil) {
83 [self setStatus: @"Please specify an alarm date and time."]; return;
84 }
85 if (time == nil) {
86 [self setStatus: @"Please specify an alarm time."]; return;
87 }
88 if (date == nil) {
89 [self setStatus: @"Please specify an alarm date."]; return;
90 }
91 // XXX if calTime's date is different from the default date, complain
92 calTime = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate: [time timeIntervalSinceReferenceDate]];
93 calDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate: [date timeIntervalSinceReferenceDate]];
94 if (time == nil || date == nil) {
95 [self setStatus: @"Please specify a reasonable date and time."];
96 }
97 alarmDate = [[NSCalendarDate alloc] initWithYear: [calDate yearOfCommonEra]
98 month: [calDate monthOfYear]
99 day: [calDate dayOfMonth]
100 hour: [calTime hourOfDay]
101 minute: [calTime minuteOfHour]
102 second: [calTime secondOfMinute]
103 timeZone: nil];
104 alarmInterval = [alarmDate timeIntervalSinceNow];
105 if (alarmInterval <= 0) {
106 [self setStatus: @"Please specify an alarm time in the future."];
107 [alarmDate release];
108 alarmDate = nil;
109 return;
110 }
111 }
112}
113
114// XXX should set timer to update status every second while configuration is valid, application is in front, and isIn
115
116// XXX use OACalendar?
117
118// 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.
119
120- (IBAction)update:(id)sender;
121{
122 // NSLog(@"update: %@", sender);
123 [self setAlarmDateAndInterval: sender];
124 if (alarmDate != nil) {
125 [self setStatus: [alarmDate descriptionWithCalendarFormat: @"Alarm will be set for %X on %x" timeZone: nil locale: nil]];
126 [setButton setEnabled: YES];
127 } else {
128 [setButton setEnabled: NO];
129 }
130}
131
132- (IBAction)inAtChanged:(id)sender;
133{
134 isIn = ([inAtMatrix selectedTag] == 0);
135 [timeInterval setEnabled: isIn];
136 [timeIntervalUnits setEnabled: isIn];
137 [timeOfDay setEnabled: !isIn];
138 [timeDate setEnabled: !isIn];
139 [timeDateCompletions setEnabled: !isIn];
140 if (sender != nil)
141 [[self window] makeFirstResponder: isIn ? timeInterval : timeOfDay];
142 // NSLog(@"UPDATING FROM inAtChanged");
143 [self update: nil];
144}
145
146- (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error;
147{
148 unichar c;
149 int tag;
150 unsigned length = [string length];
151 if (control != timeInterval || length == 0) return;
152 c = [string characterAtIndex: length - 1];
153 switch (c) {
154 case 's': case 'S': tag = 1; break;
155 case 'm': case 'M': tag = 60; break;
156 case 'h': case 'H': tag = 60 * 60; break;
157 default: return;
158 }
159 [timeIntervalUnits selectItemAtIndex:
160 [timeIntervalUnits indexOfItemWithTag: tag]];
161 // NSLog(@"UPDATING FROM validation");
162 [self update: timeInterval]; // make sure we still examine the field editor, otherwise if the existing numeric string is invalid, it'll be cleared
163}
164
165- (IBAction)dateCompleted:(NSPopUpButton *)sender;
166{
167 [timeDate setStringValue: [sender titleOfSelectedItem]];
168}
169
170// to ensure proper updating of interval, this should be the only method by which the window is shown (e.g. from the Alarm menu)
171- (IBAction)showWindow:(id)sender;
172{
173 if (![[self window] isVisible]) {
174 // NSLog(@"UPDATING FROM showWindow");
175 [self update: self];
176 }
177 [super showWindow: sender];
178
179}
180
181- (IBAction)setAlarm:(NSButton *)sender;
182{
183 PSAlarmNotifierController *notifier = [PSAlarmNotifierController alloc];
184 NSTimer *timer;
185 [self setAlarmDateAndInterval: sender];
186 if (notifier == nil || alarmDate == nil) {
187 [self setStatus: @"Unable to set alarm (time just passed?)"];
188 return;
189 }
190 // XXX should use alarm object instead for userInfo
191 timer = [NSTimer scheduledTimerWithTimeInterval: alarmInterval
192 target: notifier
193 selector: @selector(initWithTimer:)
194 userInfo: [messageField stringValue]
195 repeats: NO];
196 [self setStatus: [alarmDate descriptionWithCalendarFormat: @"Alarm set for %x at %X" timeZone: nil locale: nil]];
197 [[self window] close];
198}
199
200// called because we're the delegate
201
202- (void)controlTextDidChange:(NSNotification *)notification;
203{
204 // NSLog(@"UPDATING FROM controlTextDidChange");
205 [self update: [notification object]];
206}
207
208@end
Note: See TracBrowser for help on using the repository browser.