source: trunk/Cocoa/Pester/Source/PSAlarmNotifierController.m @ 355

Last change on this file since 355 was 102, checked in by Nicholas Riley, 19 years ago

Pester 1.1b3

File size: 6.1 KB
Line 
1//
2//  PSAlarmNotifierController.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 "PSAlarmNotifierController.h"
10#import "PSAlarmAlertController.h"
11#import "PSAlarm.h"
12#import "PSNotifierAlert.h"
13#import "PSSnoozeUntilController.h"
14#import "NJRIntervalField.h"
15
16static NSString * const PSAlarmSnoozeInterval = @"Pester alarm snooze interval"; // NSUserDefaults key
17
18@interface PSAlarmNotifierController (Private)
19
20- (void)update:(id)sender;
21- (void)updateNextDateDisplay:(id)sender;
22
23@end
24
25@implementation PSAlarmNotifierController
26
27// XXX should use NSNonactivatingPanelMask on 10.2?
28
29- (id)initWithAlarm:(PSAlarm *)anAlarm;
30{
31    if ([self initWithWindowNibName: @"Notifier"]) {
32        NSWindow *window = [self window];
33        NSRect frameRect = [window frame];
34        alarm = [anAlarm retain];
35        [messageField setStringValue: [alarm message]];
36        [dateField setStringValue: [alarm dateTimeString]];
37        if (![self setSnoozeInterval: [alarm snoozeInterval]] &&
38            ![self setSnoozeInterval: [[[NSUserDefaults standardUserDefaults] objectForKey: PSAlarmSnoozeInterval] doubleValue]])
39            [self setSnoozeInterval: 15 * 60]; // 15 minutes
40        if ([alarm isRepeating]) {
41            [intervalField setStringValue:
42                [NSString stringWithFormat: @"every %@", [[alarm intervalString] lowercaseString]]];
43            [self updateNextDateDisplay: nil];
44            updateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateNextDateDisplay:) userInfo: nil repeats: YES];
45            frameRect.size = [window maxSize];
46        } else {
47            frameRect.size = [window minSize];
48        }
49        [window setFrame: frameRect display: NO];
50        [window center];
51        [window makeKeyAndOrderFront: nil];
52        [window orderFrontRegardless];
53    }
54    return self;
55}
56
57- (void)dealloc;
58{
59    [alarm release];
60    [updateTimer invalidate]; updateTimer = nil;
61    [super dealloc];
62}
63
64- (void)updateNextDateDisplay:(id)sender;
65{
66    if (!canSnooze) {
67        NSString *nextDateTimeString = [alarm nextDateTimeString];
68        if (nextDateTimeString == nil) { // no longer repeating
69            [updateTimer invalidate]; updateTimer = nil;
70        } else {
71            [nextDateField setStringValue: nextDateTimeString];
72        }
73    }
74}
75
76- (void)update:(id)sender;
77{
78    snoozeInterval = [snoozeIntervalField interval];
79    canSnooze = (snoozeInterval > 0);
80    if (canSnooze) [nextDateField setStringValue: @"after snooze"];
81    [snoozeButton setEnabled: canSnooze];
82    [canSnooze ? snoozeButton : okButton setKeyEquivalent: @"\r"];
83    [canSnooze ? okButton : snoozeButton setKeyEquivalent: @""];
84}
85
86- (IBAction)close:(id)sender;
87{
88    [PSAlarmAlertController stopAlerts: sender];
89    [self close];
90    [[PSNotifierAlert alert] completedForAlarm: alarm];
91}
92
93- (IBAction)snoozeUntil:(NSMenuItem *)sender;
94{
95    [PSSnoozeUntilController snoozeUntilControllerWithNotifierController: self];
96}
97
98- (IBAction)snoozeIntervalUnitsChanged:(NSPopUpButton *)sender;
99{
100    if ([[sender selectedItem] tag] > 0) [self update: nil];
101}
102
103- (NSTimeInterval)snoozeInterval;
104{
105    return snoozeInterval;
106}
107
108- (BOOL)setSnoozeInterval:(NSTimeInterval)interval;
109{
110    snoozeInterval = interval;
111    return [snoozeIntervalField setInterval: interval];
112}
113
114- (IBAction)snooze:(NSButton *)sender;
115{
116    snoozeInterval = [snoozeIntervalField interval];
117    [alarm setSnoozeInterval: snoozeInterval];
118    [[NSUserDefaults standardUserDefaults] setObject: [NSNumber numberWithDouble: snoozeInterval] forKey: PSAlarmSnoozeInterval];
119    [self close: sender];
120}
121
122- (void)snoozeUntilDate:(NSCalendarDate *)date;
123{
124    [alarm setSnoozeInterval: [date timeIntervalSinceNow]];
125    [self close: self];
126}
127
128- (IBAction)stopRepeating:(NSButton *)sender;
129{
130    NSWindow *window = [self window];
131    NSRect frameRect = [window frame];
132    NSSize newSize = [window minSize];
133   
134    [alarm setRepeating: NO];
135    [sender setEnabled: NO];
136    frameRect.origin.y += frameRect.size.height - newSize.height;
137    frameRect.size = newSize;
138    [window setFrame: frameRect display: YES animate: YES];
139}
140
141@end
142
143@implementation PSAlarmNotifierController (NSControlSubclassDelegate)
144
145- (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error;
146{
147    if (control == snoozeIntervalField)
148        [snoozeIntervalField handleDidFailToFormatString: string errorDescription: error label: @"snooze interval"];
149    return NO;
150}
151
152- (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error;
153{
154    // NSLog(@"UPDATING FROM validation");
155    [self update: control]; // switch to snooze if someone types something weird...
156}
157
158- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector;
159{
160    // NSLog(@"UPDATING from textView: %@", NSStringFromSelector(commandSelector));
161    if (commandSelector == @selector(cancel:)) {
162        // if someone just wants the stupid thing to go away and presses escape, donÕt hinder them
163        [self close: control];
164        return YES;
165    }
166    // if someone invokes the default button or switches fields, donÕt override it
167    if (commandSelector == @selector(insertNewline:) ||
168        commandSelector == @selector(insertTab:) ||
169        commandSelector == @selector(insertBacktab:)) return NO;
170    [self update: control]; // ...or if they type a navigation key...
171    return NO; // we donÕt handle it
172}
173
174@end
175
176@implementation PSAlarmNotifierController (NSControlSubclassNotifications)
177
178- (void)controlTextDidChange:(NSNotification *)notification;
179{
180    // NSLog(@"UPDATING FROM controlTextDidChange: %@", [notification object]);
181    [self update: [notification object]]; // ...or if they modify the snooze interval
182}
183
184@end
185
186@implementation PSAlarmNotifierController (NSWindowNotifications)
187
188- (void)windowWillClose:(NSNotification *)notification;
189{
190    // canÕt rely on dealloc to invalidate the timer, because it retains this object
191    [updateTimer invalidate]; updateTimer = nil;
192    [self release]; // in non-document-based apps, this is needed; see docs
193}
194
195@end
Note: See TracBrowser for help on using the repository browser.