source: releases/Pester/1.0b1/Source/PSAlarms.m@ 614

Last change on this file since 614 was 26, checked in by Nicholas Riley, 22 years ago

Pester 1.0b1

File size: 3.2 KB
Line 
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
12NSString * const PSAlarmsDidChangeNotification = @"PSAlarmsDidChangeNotification";
13
14static NSString * const PSPendingAlarms = @"Pester pending alarms"; // NSUserDefaults key
15
16static PSAlarms *PSAlarmsAllAlarms = nil;
17
18@implementation PSAlarms
19
20+ (void)setUp; // XXX if you can think of a better name, please be my guest.
21{
22 if (PSAlarmsAllAlarms == nil) {
23 PSAlarmsAllAlarms = [[self alloc] init];
24 }
25}
26
27+ (PSAlarms *)allAlarms;
28{
29 return PSAlarmsAllAlarms;
30}
31
32- (id)init;
33{
34 if ( (self = [super init]) != nil) {
35 NS_DURING
36 NSData *alarmData = [[NSUserDefaults standardUserDefaults] dataForKey: PSPendingAlarms];
37 if (alarmData != nil) {
38 alarms = [[NSUnarchiver unarchiveObjectWithData: alarmData] retain];
39 }
40 NS_HANDLER
41 // XXX need better error handling here, don't stomp on data
42 NSLog(@"An error occurred while attempting to restore the alarm list: %@", localException);
43 NS_ENDHANDLER
44 if (alarms == nil) alarms = [[NSMutableArray alloc] init];
45 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_alarmTimerSet:) name: PSAlarmTimerSetNotification object: nil];
46 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_alarmTimerExpired:) name: PSAlarmTimerExpiredNotification object: nil];
47 }
48 return self;
49}
50
51- (void)dealloc;
52{
53 [alarms dealloc];
54 [[NSNotificationCenter defaultCenter] removeObserver: self];
55 [super dealloc];
56}
57
58- (void)_changed;
59{
60 NSData *alarmData;
61 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
62 [alarms sortUsingSelector: @selector(compare:)];
63 alarmData = [NSArchiver archivedDataWithRootObject: alarms];
64 [defaults setObject: alarmData forKey: PSPendingAlarms];
65 [defaults synchronize];
66 // NSLog(@"PSAlarms changed: %@", alarms);
67 [[NSNotificationCenter defaultCenter] postNotificationName: PSAlarmsDidChangeNotification object: self];
68}
69
70- (void)_alarmTimerExpired:(NSNotification *)notification;
71{
72 [alarms removeObject: [notification object]];
73 [self _changed];
74}
75
76- (void)_alarmTimerSet:(NSNotification *)notification;
77{
78 [alarms addObject: [notification object]];
79 [self _changed];
80}
81
82- (int)alarmCount;
83{
84 return [alarms count];
85}
86
87- (PSAlarm *)alarmAtIndex:(int)index;
88{
89 return [alarms objectAtIndex: index];
90}
91
92- (void)removeAlarmAtIndex:(int)index;
93{
94 [alarms removeObjectAtIndex: index];
95}
96
97- (void)removeAlarmsAtIndices:(NSArray *)indices;
98{
99 NSEnumerator *e = [indices objectEnumerator];
100 NSNumber *n;
101 int indexCount = [indices count], i = 0;
102 int *indexArray = (int *)malloc(indexCount * sizeof(int));
103 NS_DURING
104 while ( (n = [e nextObject]) != nil) {
105 indexArray[i] = [n intValue];
106 i++;
107 }
108 [alarms removeObjectsFromIndices: indexArray numIndices: indexCount];
109 [self _changed];
110 NS_HANDLER
111 free(indexArray);
112 [self _changed];
113 [localException raise];
114 NS_ENDHANDLER
115}
116
117@end
Note: See TracBrowser for help on using the repository browser.