1 | //
|
---|
2 | // PSAlarm.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Wed Oct 09 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "PSAlarm.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | @implementation PSAlarm
|
---|
13 |
|
---|
14 | - (void)_setAlarmDate:(NSCalendarDate *)aDate;
|
---|
15 | {
|
---|
16 | if (alarmDate != aDate) {
|
---|
17 | [alarmDate release];
|
---|
18 | alarmDate = nil;
|
---|
19 | alarmDate = [aDate retain];
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | - (void)_invalidate:(NSString *)aMessage;
|
---|
24 | {
|
---|
25 | alarmType = PSAlarmInvalid;
|
---|
26 | if (aMessage != invalidMessage) {
|
---|
27 | [invalidMessage release];
|
---|
28 | invalidMessage = nil;
|
---|
29 | [self _setAlarmDate: nil];
|
---|
30 | alarmInterval = 0;
|
---|
31 | invalidMessage = [aMessage retain];
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | - (void)_validateForType:(PSAlarmType)type;
|
---|
36 | {
|
---|
37 | [invalidMessage release];
|
---|
38 | invalidMessage = nil;
|
---|
39 | alarmType = type;
|
---|
40 | }
|
---|
41 |
|
---|
42 | - (void)_setDateFromInterval;
|
---|
43 | {
|
---|
44 | [alarmDate release]; alarmDate = nil;
|
---|
45 | alarmDate = [NSCalendarDate dateWithTimeIntervalSinceNow: alarmInterval];
|
---|
46 | [alarmDate retain];
|
---|
47 | [self _validateForType: PSAlarmInterval];
|
---|
48 | }
|
---|
49 |
|
---|
50 | - (void)setInterval:(NSTimeInterval)anInterval;
|
---|
51 | {
|
---|
52 | alarmInterval = anInterval;
|
---|
53 | if (alarmInterval <= 0) {
|
---|
54 | [self _invalidate: @"Please specify an alarm interval."]; return;
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | - (void)_setIntervalFromDate;
|
---|
59 | {
|
---|
60 | alarmInterval = [alarmDate timeIntervalSinceNow];
|
---|
61 | if (alarmInterval <= 0) {
|
---|
62 | [self _invalidate: @"Please specify an alarm time in the future."];
|
---|
63 | return;
|
---|
64 | }
|
---|
65 | [self _validateForType: PSAlarmDate];
|
---|
66 | }
|
---|
67 |
|
---|
68 | - (void)setForDateAtTime:(NSDate *)dateTime;
|
---|
69 | {
|
---|
70 | if (dateTime != alarmDate) {
|
---|
71 | [alarmDate release];
|
---|
72 | alarmDate = nil;
|
---|
73 | alarmDate = [dateTime retain];
|
---|
74 | }
|
---|
75 | [self _setIntervalFromDate];
|
---|
76 | }
|
---|
77 |
|
---|
78 | - (void)setForDate:(NSDate *)date atTime:(NSDate *)time;
|
---|
79 | {
|
---|
80 | NSCalendarDate *calTime, *calDate;
|
---|
81 | if (time == nil && date == nil) {
|
---|
82 | [self _invalidate: @"Please specify an alarm date and time."]; return;
|
---|
83 | }
|
---|
84 | if (time == nil) {
|
---|
85 | [self _invalidate: @"Please specify an alarm time."]; return;
|
---|
86 | }
|
---|
87 | if (date == nil) {
|
---|
88 | [self _invalidate: @"Please specify an alarm date."]; return;
|
---|
89 | }
|
---|
90 | // XXX if calTime's date is different from the default date, complain
|
---|
91 | calTime = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate: [time timeIntervalSinceReferenceDate]];
|
---|
92 | calDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate: [date timeIntervalSinceReferenceDate]];
|
---|
93 | if (calTime == nil || calDate == nil) {
|
---|
94 | [self _invalidate: @"Please specify a reasonable date and time."];
|
---|
95 | }
|
---|
96 | [self setForDateAtTime:
|
---|
97 | [[[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] autorelease]];
|
---|
104 | }
|
---|
105 |
|
---|
106 | - (BOOL)isValid;
|
---|
107 | {
|
---|
108 | return (alarmType == PSAlarmInvalid);
|
---|
109 | }
|
---|
110 |
|
---|
111 | - (void)setMessage:(NSString *)aMessage;
|
---|
112 | {
|
---|
113 | if (aMessage != alarmMessage) {
|
---|
114 | [alarmMessage release];
|
---|
115 | alarmMessage = nil;
|
---|
116 | alarmMessage = [aMessage retain];
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | - (NSString *)message;
|
---|
121 | {
|
---|
122 | if (alarmMessage == nil || [alarmMessage isEqualToString: @""])
|
---|
123 | return @"Alarm!";
|
---|
124 | return alarmMessage;
|
---|
125 | }
|
---|
126 |
|
---|
127 | - (NSString *)invalidMessage;
|
---|
128 | {
|
---|
129 | return invalidMessage;
|
---|
130 | }
|
---|
131 |
|
---|
132 | - (NSDate *)date;
|
---|
133 | {
|
---|
134 | if (alarmType == PSAlarmInterval) [self _setDateFromInterval];
|
---|
135 | return alarmDate;
|
---|
136 | }
|
---|
137 |
|
---|
138 | - (NSTimeInterval)interval;
|
---|
139 | {
|
---|
140 | if (alarmType == PSAlarmDate) [self _setIntervalFromDate];
|
---|
141 | return alarmInterval;
|
---|
142 | }
|
---|
143 |
|
---|
144 | @end
|
---|