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 | alarm = [[PSAlarm alloc] init]; |
---|
42 | [[self window] makeKeyAndOrderFront: nil]; |
---|
43 | } |
---|
44 | |
---|
45 | - (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag; |
---|
46 | { |
---|
47 | if (!flag) [self showWindow: self]; |
---|
48 | return YES; |
---|
49 | } |
---|
50 | |
---|
51 | - (void)setStatus:(NSString *)aString; |
---|
52 | { |
---|
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 (isIn) { |
---|
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 | // XXX should set timer to update status every second while configuration is valid, application is in front, and isIn |
---|
87 | |
---|
88 | // XXX use OACalendar? |
---|
89 | |
---|
90 | // 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. |
---|
91 | |
---|
92 | - (IBAction)updateDateDisplay:(id)sender; |
---|
93 | { |
---|
94 | if ([alarm isValid]) { |
---|
95 | [self setStatus: [[alarm date] descriptionWithCalendarFormat: @"Alarm will be set for %X on %x" timeZone: nil locale: nil]]; |
---|
96 | [setButton setEnabled: YES]; |
---|
97 | } else { |
---|
98 | [setButton setEnabled: NO]; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | - (IBAction)update:(id)sender; |
---|
103 | { |
---|
104 | // NSLog(@"update: %@", sender); |
---|
105 | [self setAlarmDateAndInterval: sender]; |
---|
106 | [self updateDateDisplay: sender]; |
---|
107 | } |
---|
108 | |
---|
109 | - (IBAction)inAtChanged:(id)sender; |
---|
110 | { |
---|
111 | isIn = ([inAtMatrix selectedTag] == 0); |
---|
112 | [timeInterval setEnabled: isIn]; |
---|
113 | [timeIntervalUnits setEnabled: isIn]; |
---|
114 | [timeOfDay setEnabled: !isIn]; |
---|
115 | [timeDate setEnabled: !isIn]; |
---|
116 | [timeDateCompletions setEnabled: !isIn]; |
---|
117 | if (sender != nil) |
---|
118 | [[self window] makeFirstResponder: isIn ? timeInterval : timeOfDay]; |
---|
119 | // NSLog(@"UPDATING FROM inAtChanged"); |
---|
120 | [self update: nil]; |
---|
121 | } |
---|
122 | |
---|
123 | - (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error; |
---|
124 | { |
---|
125 | unichar c; |
---|
126 | int tag; |
---|
127 | unsigned length = [string length]; |
---|
128 | if (control != timeInterval || length == 0) return; |
---|
129 | c = [string characterAtIndex: length - 1]; |
---|
130 | switch (c) { |
---|
131 | case 's': case 'S': tag = 1; break; |
---|
132 | case 'm': case 'M': tag = 60; break; |
---|
133 | case 'h': case 'H': tag = 60 * 60; break; |
---|
134 | default: return; |
---|
135 | } |
---|
136 | [timeIntervalUnits selectItemAtIndex: |
---|
137 | [timeIntervalUnits indexOfItemWithTag: tag]]; |
---|
138 | // NSLog(@"UPDATING FROM validation"); |
---|
139 | [self update: timeInterval]; // make sure we still examine the field editor, otherwise if the existing numeric string is invalid, it'll be cleared |
---|
140 | } |
---|
141 | |
---|
142 | - (IBAction)dateCompleted:(NSPopUpButton *)sender; |
---|
143 | { |
---|
144 | [timeDate setStringValue: [sender titleOfSelectedItem]]; |
---|
145 | } |
---|
146 | |
---|
147 | // to ensure proper updating of interval, this should be the only method by which the window is shown (e.g. from the Alarm menu) |
---|
148 | - (IBAction)showWindow:(id)sender; |
---|
149 | { |
---|
150 | if (![[self window] isVisible]) { |
---|
151 | // NSLog(@"UPDATING FROM showWindow"); |
---|
152 | [self update: self]; |
---|
153 | } |
---|
154 | [super showWindow: sender]; |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | - (IBAction)setAlarm:(NSButton *)sender; |
---|
159 | { |
---|
160 | PSAlarmNotifierController *notifier = [PSAlarmNotifierController alloc]; |
---|
161 | NSTimer *timer; |
---|
162 | NSTimeInterval interval; |
---|
163 | [self setAlarmDateAndInterval: sender]; |
---|
164 | if (notifier == nil || ( (interval = [alarm interval]) == 0)) { |
---|
165 | [self setStatus: @"Unable to set alarm (time just passed?)"]; |
---|
166 | return; |
---|
167 | } |
---|
168 | [alarm setMessage: [messageField stringValue]]; |
---|
169 | // XXX should use alarm object instead for userInfo |
---|
170 | timer = [NSTimer scheduledTimerWithTimeInterval: interval |
---|
171 | target: notifier |
---|
172 | selector: @selector(initWithTimer:) |
---|
173 | userInfo: alarm |
---|
174 | repeats: NO]; |
---|
175 | [self setStatus: [[alarm date] descriptionWithCalendarFormat: @"Alarm set for %x at %X" timeZone: nil locale: nil]]; |
---|
176 | [[self window] close]; |
---|
177 | } |
---|
178 | |
---|
179 | // called because we're the delegate |
---|
180 | |
---|
181 | - (void)controlTextDidChange:(NSNotification *)notification; |
---|
182 | { |
---|
183 | // NSLog(@"UPDATING FROM controlTextDidChange"); |
---|
184 | [self update: [notification object]]; |
---|
185 | } |
---|
186 | |
---|
187 | @end |
---|