1 | //
|
---|
2 | // PSAlarms.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Fri Oct 11 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "PSAlarms.h"
|
---|
10 | #import "PSAlarm.h"
|
---|
11 | #import "PSTimer.h"
|
---|
12 | #import "NSDictionary-NJRExtensions.h"
|
---|
13 |
|
---|
14 | NSString * const PSAlarmImportException = @"PSAlarmImportException";
|
---|
15 |
|
---|
16 | NSString * const PSAlarmsDidChangeNotification = @"PSAlarmsDidChangeNotification";
|
---|
17 | NSString * const PSAlarmsNextAlarmDidChangeNotification = @"PSAlarmsNextAlarmDidChangeNotification";
|
---|
18 |
|
---|
19 | // NSUserDefaults key
|
---|
20 | static NSString * const PSPendingAlarms = @"Pester pending alarms"; // 1.0 Ð 1.1a3
|
---|
21 | static NSString * const PSAllAlarms = @"Pester alarms"; // 1.1a4 Ð
|
---|
22 |
|
---|
23 | // property list keys
|
---|
24 | static NSString * const PLAlarmsPending = @"pending";
|
---|
25 | static NSString * const PLAlarmsExpired = @"expired";
|
---|
26 |
|
---|
27 | static PSAlarms *PSAlarmsAllAlarms = nil;
|
---|
28 |
|
---|
29 | @interface PSAlarms (Private)
|
---|
30 |
|
---|
31 | - (void)_updateNextAlarm;
|
---|
32 |
|
---|
33 | @end
|
---|
34 |
|
---|
35 | @implementation PSAlarms
|
---|
36 |
|
---|
37 | + (void)setUp;
|
---|
38 | {
|
---|
39 | if (PSAlarmsAllAlarms == nil) {
|
---|
40 | NSDictionary *plAlarms = [[NSUserDefaults standardUserDefaults] objectForKey: PSAllAlarms];
|
---|
41 | if (plAlarms == nil) {
|
---|
42 | PSAlarmsAllAlarms = [[self alloc] init];
|
---|
43 | } else {
|
---|
44 | PSAlarmsAllAlarms = [[self alloc] initWithPropertyList: plAlarms];
|
---|
45 | }
|
---|
46 | [PSAlarmsAllAlarms _updateNextAlarm]; // only generate notifications after singleton established
|
---|
47 | [PSTimer setUp];
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | + (PSAlarms *)allAlarms;
|
---|
52 | {
|
---|
53 | NSAssert(PSAlarmsAllAlarms != nil, @"Attempt to use +[PSAlarms allAlarms] before setup complete");
|
---|
54 | return PSAlarmsAllAlarms;
|
---|
55 | }
|
---|
56 |
|
---|
57 | #pragma mark private
|
---|
58 |
|
---|
59 | - (void)_changed;
|
---|
60 | {
|
---|
61 | NSMutableArray *alarmsData = [[NSMutableArray alloc] init];
|
---|
62 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
63 | [self _updateNextAlarm];
|
---|
64 | // NSLog(@"PSAlarms _changed:\n%@", alarms);
|
---|
65 | [defaults setObject: [self propertyListRepresentation] forKey: PSAllAlarms];
|
---|
66 | [defaults synchronize];
|
---|
67 | [alarmsData release];
|
---|
68 | [[NSNotificationCenter defaultCenter] postNotificationName: PSAlarmsDidChangeNotification object: self];
|
---|
69 | }
|
---|
70 |
|
---|
71 | - (void)_alarmTimerExpired:(NSNotification *)notification;
|
---|
72 | {
|
---|
73 | PSAlarm *alarm = [notification object];
|
---|
74 | // NSLog(@"timer expired: %@ retainCount %d", alarm, [alarm retainCount]);
|
---|
75 | [expiredAlarms addObject: alarm];
|
---|
76 | // NSLog(@"expired alarms: %@", [expiredAlarms description]);
|
---|
77 | [alarms removeObject: alarm];
|
---|
78 | [self _changed];
|
---|
79 | }
|
---|
80 |
|
---|
81 | - (void)_alarmTimerSet:(NSNotification *)notification;
|
---|
82 | {
|
---|
83 | PSAlarm *alarm = [notification object];
|
---|
84 | // NSLog(@"timer set: %@ retainCount %d", alarm, [alarm retainCount]);
|
---|
85 | [alarms addObject: alarm];
|
---|
86 | [expiredAlarms removeObject: alarm];
|
---|
87 | [self _changed];
|
---|
88 | }
|
---|
89 |
|
---|
90 | - (void)_alarmDied:(NSNotification *)notification;
|
---|
91 | {
|
---|
92 | PSAlarm *alarm = [notification object];
|
---|
93 | // NSLog(@"alarm died: %@ retainCount %d", alarm, [alarm retainCount]);
|
---|
94 | [alarms removeObject: alarm];
|
---|
95 | [expiredAlarms removeObject: alarm];
|
---|
96 | [self _changed];
|
---|
97 | }
|
---|
98 |
|
---|
99 | - (void)_updateNextAlarm;
|
---|
100 | {
|
---|
101 | NSEnumerator *e;
|
---|
102 | PSAlarm *alarm, *oldNextAlarm = nextAlarm;
|
---|
103 | [nextAlarm release];
|
---|
104 | nextAlarm = nil;
|
---|
105 | // sort alarms so earliest is first
|
---|
106 | [alarms sortUsingSelector: @selector(compareDate:)];
|
---|
107 | // find first un-expired alarm
|
---|
108 | e = [alarms objectEnumerator];
|
---|
109 | while ( (alarm = [e nextObject]) != nil) {
|
---|
110 | if ([alarm isValid]) {
|
---|
111 | nextAlarm = [alarm retain];
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | if (oldNextAlarm != nextAlarm)
|
---|
116 | [[NSNotificationCenter defaultCenter] postNotificationName: PSAlarmsNextAlarmDidChangeNotification object: nextAlarm];
|
---|
117 | }
|
---|
118 |
|
---|
119 | - (void)_setUpNotifications;
|
---|
120 | {
|
---|
121 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_alarmTimerSet:) name: PSAlarmTimerSetNotification object: nil];
|
---|
122 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_alarmTimerExpired:) name: PSAlarmTimerExpiredNotification object: nil];
|
---|
123 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_alarmDied:) name: PSAlarmDiedNotification object: nil];
|
---|
124 | }
|
---|
125 |
|
---|
126 | #pragma mark initialize-release
|
---|
127 |
|
---|
128 | - (id)init;
|
---|
129 | {
|
---|
130 | if ( (self = [super init]) != nil) {
|
---|
131 | alarms = [[NSMutableArray alloc] init];
|
---|
132 | expiredAlarms = [[NSMutableSet alloc] init];
|
---|
133 | [self _setUpNotifications];
|
---|
134 | }
|
---|
135 | return self;
|
---|
136 | }
|
---|
137 |
|
---|
138 | - (void)dealloc;
|
---|
139 | {
|
---|
140 | [alarms release];
|
---|
141 | [expiredAlarms release];
|
---|
142 | [[NSNotificationCenter defaultCenter] removeObserver: self];
|
---|
143 | [super dealloc];
|
---|
144 | }
|
---|
145 |
|
---|
146 | #pragma mark accessing
|
---|
147 |
|
---|
148 | - (NSArray *)alarms;
|
---|
149 | {
|
---|
150 | return alarms;
|
---|
151 | }
|
---|
152 |
|
---|
153 | - (PSAlarm *)nextAlarm;
|
---|
154 | {
|
---|
155 | return nextAlarm;
|
---|
156 | }
|
---|
157 |
|
---|
158 | - (int)alarmCount;
|
---|
159 | {
|
---|
160 | return [alarms count];
|
---|
161 | }
|
---|
162 |
|
---|
163 | - (PSAlarm *)alarmAtIndex:(int)index;
|
---|
164 | {
|
---|
165 | return [alarms objectAtIndex: index];
|
---|
166 | }
|
---|
167 |
|
---|
168 | - (void)removeAlarmAtIndex:(int)index;
|
---|
169 | {
|
---|
170 | [(PSAlarm *)[alarms objectAtIndex: index] cancelTimer];
|
---|
171 | [alarms removeObjectAtIndex: index];
|
---|
172 | }
|
---|
173 |
|
---|
174 | - (void)removeAlarmsAtIndices:(NSArray *)indices;
|
---|
175 | {
|
---|
176 | NSEnumerator *e = [indices objectEnumerator];
|
---|
177 | NSNumber *n;
|
---|
178 | int indexCount = [indices count], i = 0, alarmIndex;
|
---|
179 | int *indexArray = (int *)malloc(indexCount * sizeof(int));
|
---|
180 | NS_DURING
|
---|
181 | while ( (n = [e nextObject]) != nil) {
|
---|
182 | alarmIndex = [n intValue];
|
---|
183 | [(PSAlarm *)[alarms objectAtIndex: alarmIndex] cancelTimer];
|
---|
184 | indexArray[i] = alarmIndex;
|
---|
185 | i++;
|
---|
186 | }
|
---|
187 | [alarms removeObjectsFromIndices: indexArray numIndices: indexCount];
|
---|
188 | free(indexArray); indexArray = NULL;
|
---|
189 | [self _changed];
|
---|
190 | NS_HANDLER
|
---|
191 | free(indexArray);
|
---|
192 | [self _changed];
|
---|
193 | [localException raise];
|
---|
194 | NS_ENDHANDLER
|
---|
195 | }
|
---|
196 |
|
---|
197 | - (void)removeAlarms:(NSSet *)alarmsToRemove;
|
---|
198 | {
|
---|
199 | NSEnumerator *e = [alarms objectEnumerator];
|
---|
200 | PSAlarm *alarm;
|
---|
201 | NSMutableArray *indices = [NSMutableArray arrayWithCapacity: [alarmsToRemove count]];
|
---|
202 | int alarmIndex = 0;
|
---|
203 |
|
---|
204 | while ( (alarm = [e nextObject]) != nil) {
|
---|
205 | if ([alarmsToRemove containsObject: alarm])
|
---|
206 | [indices addObject: [NSNumber numberWithInt: alarmIndex]];
|
---|
207 | alarmIndex++;
|
---|
208 | }
|
---|
209 | [self removeAlarmsAtIndices: indices];
|
---|
210 | }
|
---|
211 |
|
---|
212 | - (BOOL)alarmsExpiring;
|
---|
213 | {
|
---|
214 | return [expiredAlarms count] != 0;
|
---|
215 | }
|
---|
216 |
|
---|
217 | #pragma mark property list serialization (Pester 1.1)
|
---|
218 |
|
---|
219 | - (NSDictionary *)propertyListRepresentation;
|
---|
220 | {
|
---|
221 | NSMutableArray *plPendingAlarms = [[NSMutableArray alloc] initWithCapacity: [alarms count]];
|
---|
222 | NSMutableArray *plExpiredAlarms = [[NSMutableArray alloc] initWithCapacity: [expiredAlarms count]];
|
---|
223 | NSDictionary *plAllAlarms, *plAlarm;
|
---|
224 | NSEnumerator *e;
|
---|
225 | PSAlarm *alarm;
|
---|
226 |
|
---|
227 | e = [alarms objectEnumerator];
|
---|
228 | while ( (alarm = [e nextObject]) != nil) {
|
---|
229 | plAlarm = [alarm propertyListRepresentation];
|
---|
230 | if (plAlarm != nil)
|
---|
231 | [plPendingAlarms addObject: plAlarm];
|
---|
232 | }
|
---|
233 |
|
---|
234 | e = [expiredAlarms objectEnumerator];
|
---|
235 | while ( (alarm = [e nextObject]) != nil) {
|
---|
236 | plAlarm = [alarm propertyListRepresentation];
|
---|
237 | if (plAlarm != nil)
|
---|
238 | [plExpiredAlarms addObject: plAlarm];
|
---|
239 | }
|
---|
240 |
|
---|
241 | plAllAlarms = [NSDictionary dictionaryWithObjectsAndKeys:
|
---|
242 | plPendingAlarms, PLAlarmsPending, plExpiredAlarms, PLAlarmsExpired, nil];
|
---|
243 | [plPendingAlarms release];
|
---|
244 | [plExpiredAlarms release];
|
---|
245 |
|
---|
246 | return plAllAlarms;
|
---|
247 | }
|
---|
248 |
|
---|
249 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
250 | {
|
---|
251 | if ( (self = [super init]) != nil) {
|
---|
252 | NSArray *plPendingAlarms = [dict objectForRequiredKey: PLAlarmsPending];
|
---|
253 | NSArray *plExpiredAlarms = [dict objectForRequiredKey: PLAlarmsExpired];
|
---|
254 | NSEnumerator *e;
|
---|
255 | NSDictionary *plAlarm;
|
---|
256 | PSAlarm *alarm;
|
---|
257 |
|
---|
258 | alarms = [[NSMutableArray alloc] initWithCapacity: [plPendingAlarms count]];
|
---|
259 | e = [plPendingAlarms objectEnumerator];
|
---|
260 | while ( (plAlarm = [e nextObject]) != nil) {
|
---|
261 | [alarms addObject: [[PSAlarm alloc] initWithPropertyList: plAlarm]];
|
---|
262 | }
|
---|
263 |
|
---|
264 | e = [plExpiredAlarms objectEnumerator];
|
---|
265 | while ( (plAlarm = [e nextObject]) != nil) {
|
---|
266 | // expired alarms may be just that, or they may have outstanding repeats - if the latter, PSAlarm will reschedule the alarm.
|
---|
267 | if ( (alarm = [[PSAlarm alloc] initWithPropertyList: plAlarm]) != nil)
|
---|
268 | [alarms addObject: alarm];
|
---|
269 | }
|
---|
270 | expiredAlarms = [[NSMutableSet alloc] init];
|
---|
271 |
|
---|
272 | [self _setUpNotifications];
|
---|
273 | }
|
---|
274 | return self;
|
---|
275 | }
|
---|
276 |
|
---|
277 | #pragma mark archiving (Pester 1.0)
|
---|
278 |
|
---|
279 | - (unsigned)countOfVersion1Alarms;
|
---|
280 | {
|
---|
281 | return [[[NSUserDefaults standardUserDefaults] objectForKey: PSPendingAlarms] count];
|
---|
282 | }
|
---|
283 |
|
---|
284 | - (void)discardVersion1Alarms;
|
---|
285 | {
|
---|
286 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
287 | [defaults removeObjectForKey: PSPendingAlarms];
|
---|
288 | [defaults synchronize];
|
---|
289 | }
|
---|
290 |
|
---|
291 | - (void)importVersion1Alarms;
|
---|
292 | {
|
---|
293 | NSArray *alarmsData = [[NSUserDefaults standardUserDefaults] arrayForKey: PSPendingAlarms];
|
---|
294 | NSEnumerator *e = [alarmsData objectEnumerator];
|
---|
295 | NSData *alarmData;
|
---|
296 | PSAlarm *alarm;
|
---|
297 | while ( (alarmData = [e nextObject]) != nil) {
|
---|
298 | NS_DURING
|
---|
299 | alarm = [NSUnarchiver unarchiveObjectWithData: alarmData];
|
---|
300 | NS_HANDLER
|
---|
301 | alarm = nil;
|
---|
302 | // XXX
|
---|
303 | NS_ENDHANDLER
|
---|
304 | if (alarm != nil)
|
---|
305 | [alarms addObject: alarm];
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | @end
|
---|