[24] | 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"
|
---|
[34] | 10 | #import "PSAlert.h"
|
---|
[53] | 11 | #import "PSAlerts.h"
|
---|
[61] | 12 | #import "PSTimer.h"
|
---|
[53] | 13 | #import "NSCalendarDate-NJRExtensions.h"
|
---|
| 14 | #import "NSDictionary-NJRExtensions.h"
|
---|
| 15 | #import "NSString-NJRExtensions.h"
|
---|
[24] | 16 |
|
---|
[26] | 17 | NSString * const PSAlarmTimerSetNotification = @"PSAlarmTimerSetNotification";
|
---|
| 18 | NSString * const PSAlarmTimerExpiredNotification = @"PSAlarmTimerExpiredNotification";
|
---|
[53] | 19 | NSString * const PSAlarmDiedNotification = @"PSAlarmDiedNotification";
|
---|
[24] | 20 |
|
---|
[53] | 21 | // property list keys
|
---|
| 22 | static NSString * const PLAlarmType = @"type"; // NSString
|
---|
| 23 | static NSString * const PLAlarmDate = @"date"; // NSNumber
|
---|
| 24 | static NSString * const PLAlarmInterval = @"interval"; // NSNumber
|
---|
| 25 | static NSString * const PLAlarmSnoozeInterval = @"snooze interval"; // NSNumber
|
---|
| 26 | static NSString * const PLAlarmMessage = @"message"; // NSString
|
---|
| 27 | static NSString * const PLAlarmAlerts = @"alerts"; // NSDictionary
|
---|
| 28 | static NSString * const PLAlarmRepeating = @"repeating"; // NSNumber
|
---|
| 29 |
|
---|
[358] | 30 | static NSDateFormatter *dateFormatter, *shortDateFormatter, *timeFormatter;
|
---|
[43] | 31 |
|
---|
[24] | 32 | @implementation PSAlarm
|
---|
| 33 |
|
---|
[51] | 34 | #pragma mark initialize-release
|
---|
| 35 |
|
---|
[358] | 36 | + (void)initialize;
|
---|
[43] | 37 | {
|
---|
[358] | 38 | [NSDateFormatter setDefaultFormatterBehavior: NSDateFormatterBehavior10_4];
|
---|
| 39 | dateFormatter = [[NSDateFormatter alloc] init];
|
---|
| 40 | [dateFormatter setTimeStyle: NSDateFormatterNoStyle];
|
---|
| 41 | [dateFormatter setDateStyle: NSDateFormatterFullStyle];
|
---|
| 42 | shortDateFormatter = [[NSDateFormatter alloc] init];
|
---|
| 43 | [shortDateFormatter setTimeStyle: NSDateFormatterNoStyle];
|
---|
| 44 | [shortDateFormatter setDateStyle: NSDateFormatterShortStyle];
|
---|
| 45 | timeFormatter = [[NSDateFormatter alloc] init];
|
---|
| 46 | [timeFormatter setTimeStyle: NSDateFormatterMediumStyle];
|
---|
| 47 | [timeFormatter setDateStyle: NSDateFormatterNoStyle];
|
---|
[43] | 48 | }
|
---|
| 49 |
|
---|
[26] | 50 | - (void)dealloc;
|
---|
| 51 | {
|
---|
| 52 | // NSLog(@"DEALLOC %@", self);
|
---|
| 53 | alarmType = PSAlarmInvalid;
|
---|
| 54 | [alarmDate release]; alarmDate = nil;
|
---|
| 55 | [alarmMessage release]; alarmMessage = nil;
|
---|
| 56 | [invalidMessage release]; invalidMessage = nil;
|
---|
[28] | 57 | [timer invalidate]; [timer release]; timer = nil;
|
---|
[34] | 58 | [alerts release]; alerts = nil;
|
---|
[26] | 59 | [super dealloc];
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[51] | 62 | #pragma mark private
|
---|
| 63 |
|
---|
[24] | 64 | - (void)_setAlarmDate:(NSCalendarDate *)aDate;
|
---|
| 65 | {
|
---|
| 66 | if (alarmDate != aDate) {
|
---|
| 67 | [alarmDate release];
|
---|
| 68 | alarmDate = nil;
|
---|
| 69 | alarmDate = [aDate retain];
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[51] | 73 | - (void)_beInvalid:(NSString *)aMessage;
|
---|
[24] | 74 | {
|
---|
| 75 | alarmType = PSAlarmInvalid;
|
---|
| 76 | if (aMessage != invalidMessage) {
|
---|
| 77 | [invalidMessage release];
|
---|
| 78 | invalidMessage = nil;
|
---|
| 79 | [self _setAlarmDate: nil];
|
---|
| 80 | alarmInterval = 0;
|
---|
| 81 | invalidMessage = [aMessage retain];
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[51] | 85 | - (void)_beValidWithType:(PSAlarmType)type;
|
---|
[24] | 86 | {
|
---|
[28] | 87 | if (alarmType == PSAlarmSet) return; // already valid
|
---|
[24] | 88 | [invalidMessage release];
|
---|
| 89 | invalidMessage = nil;
|
---|
| 90 | alarmType = type;
|
---|
[53] | 91 | if (type != PSAlarmInterval) [self setRepeating: NO];
|
---|
[24] | 92 | }
|
---|
| 93 |
|
---|
| 94 | - (void)_setDateFromInterval;
|
---|
| 95 | {
|
---|
[53] | 96 | [self _setAlarmDate: [NSCalendarDate dateWithTimeIntervalSinceNow: alarmInterval]];
|
---|
[51] | 97 | [self _beValidWithType: PSAlarmInterval];
|
---|
[24] | 98 | }
|
---|
| 99 |
|
---|
[51] | 100 | - (void)_setIntervalFromDate;
|
---|
[24] | 101 | {
|
---|
[53] | 102 | alarmInterval = [alarmDate timeIntervalSinceNow];
|
---|
[24] | 103 | if (alarmInterval <= 0) {
|
---|
[51] | 104 | [self _beInvalid: @"Please specify an alarm time in the future."];
|
---|
| 105 | return;
|
---|
[24] | 106 | }
|
---|
[51] | 107 | [self _beValidWithType: PSAlarmDate];
|
---|
[24] | 108 | }
|
---|
| 109 |
|
---|
[53] | 110 | - (PSAlarmType)_alarmTypeForString:(NSString *)string;
|
---|
| 111 | {
|
---|
| 112 | if ([string isEqualToString: @"PSAlarmDate"]) return PSAlarmDate;
|
---|
| 113 | if ([string isEqualToString: @"PSAlarmInterval"]) return PSAlarmInterval;
|
---|
| 114 | if ([string isEqualToString: @"PSAlarmSet"]) return PSAlarmSet;
|
---|
| 115 | if ([string isEqualToString: @"PSAlarmInvalid"]) return PSAlarmInvalid;
|
---|
| 116 | if ([string isEqualToString: @"PSAlarmSnooze"]) return PSAlarmSnooze;
|
---|
| 117 | if ([string isEqualToString: @"PSAlarmExpired"]) return PSAlarmExpired;
|
---|
| 118 | NSLog(@"unknown alarm type string: %@", string);
|
---|
| 119 | return nil;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[51] | 122 | - (NSString *)_alarmTypeString;
|
---|
[24] | 123 | {
|
---|
[51] | 124 | switch (alarmType) {
|
---|
| 125 | case PSAlarmDate: return @"PSAlarmDate";
|
---|
| 126 | case PSAlarmInterval: return @"PSAlarmInterval";
|
---|
| 127 | case PSAlarmSet: return @"PSAlarmSet";
|
---|
| 128 | case PSAlarmInvalid: return @"PSAlarmInvalid";
|
---|
[53] | 129 | case PSAlarmSnooze: return @"PSAlarmSnooze";
|
---|
| 130 | case PSAlarmExpired: return @"PSAlarmExpired";
|
---|
[51] | 131 | default: return [NSString stringWithFormat: @"<unknown: %u>", alarmType];
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[53] | 135 | - (NSString *)_stringForInterval:(unsigned long long)interval;
|
---|
| 136 | {
|
---|
| 137 | const unsigned long long minute = 60, hour = minute * 60, day = hour * 24, year = day * 365.26;
|
---|
| 138 | // +[NSString stringWithFormat:] in 10.1 does not support long longs: work around it by converting to unsigned ints or longs for display
|
---|
| 139 | if (interval == 0) return nil;
|
---|
| 140 | if (interval < minute) return [NSString stringWithFormat: @"%us", (unsigned)interval];
|
---|
[113] | 141 | if (interval < hour) return [NSString stringWithFormat: @"%um", (unsigned)(interval / minute)];
|
---|
[53] | 142 | if (interval < day) return [NSString stringWithFormat: @"%uh %um", (unsigned)(interval / hour), (unsigned)((interval % hour) / minute)];
|
---|
| 143 | if (interval < 2 * day) return @"One day";
|
---|
| 144 | if (interval < year) return [NSString stringWithFormat: @"%u days", (unsigned)(interval / day)];
|
---|
| 145 | if (interval < 2 * year) return @"One year";
|
---|
| 146 | return [NSString stringWithFormat: @"%lu years", (unsigned long)(interval / year)];
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[61] | 149 | - (void)_timerExpired:(PSTimer *)aTimer;
|
---|
[51] | 150 | {
|
---|
[53] | 151 | NSLog(@"expired: %@; now %@", [[aTimer fireDate] description], [[NSDate date] description]);
|
---|
| 152 | alarmType = PSAlarmExpired;
|
---|
[51] | 153 | [[NSNotificationCenter defaultCenter] postNotificationName: PSAlarmTimerExpiredNotification object: self];
|
---|
| 154 | [timer release]; timer = nil;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | #pragma mark alarm setting
|
---|
| 158 |
|
---|
| 159 | - (void)setInterval:(NSTimeInterval)anInterval;
|
---|
| 160 | {
|
---|
| 161 | alarmInterval = anInterval;
|
---|
[24] | 162 | if (alarmInterval <= 0) {
|
---|
[51] | 163 | [self _beInvalid: @"Please specify an alarm interval."]; return;
|
---|
[24] | 164 | }
|
---|
[51] | 165 | [self _setDateFromInterval];
|
---|
[24] | 166 | }
|
---|
| 167 |
|
---|
[26] | 168 | - (void)setForDateAtTime:(NSCalendarDate *)dateTime;
|
---|
[24] | 169 | {
|
---|
[26] | 170 | [self _setAlarmDate: dateTime];
|
---|
[24] | 171 | [self _setIntervalFromDate];
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | - (void)setForDate:(NSDate *)date atTime:(NSDate *)time;
|
---|
| 175 | {
|
---|
[53] | 176 | NSCalendarDate *dateTime;
|
---|
[24] | 177 | if (time == nil && date == nil) {
|
---|
[51] | 178 | [self _beInvalid: @"Please specify an alarm date and time."]; return;
|
---|
[24] | 179 | }
|
---|
| 180 | if (time == nil) {
|
---|
[51] | 181 | [self _beInvalid: @"Please specify an alarm time."]; return;
|
---|
[24] | 182 | }
|
---|
| 183 | if (date == nil) {
|
---|
[51] | 184 | [self _beInvalid: @"Please specify an alarm date."]; return;
|
---|
[24] | 185 | }
|
---|
| 186 | // XXX if calTime's date is different from the default date, complain
|
---|
[53] | 187 | dateTime = [NSCalendarDate dateWithDate: date atTime: time];
|
---|
| 188 | if (dateTime == nil) {
|
---|
[105] | 189 | [self _beInvalid: @"Please specify a reasonable date and time."]; return;
|
---|
[24] | 190 | }
|
---|
[53] | 191 | [self setForDateAtTime: dateTime];
|
---|
[24] | 192 | }
|
---|
| 193 |
|
---|
[53] | 194 | - (void)setRepeating:(BOOL)isRepeating;
|
---|
| 195 | {
|
---|
| 196 | repeating = isRepeating;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | - (void)setSnoozeInterval:(NSTimeInterval)anInterval;
|
---|
| 200 | {
|
---|
| 201 | snoozeInterval = anInterval;
|
---|
[103] | 202 | NSAssert(alarmType == PSAlarmExpired, NSLocalizedString(@"Can't snooze an alarm that hasn't expired", "Assertion for PSAlarm snooze setting"));
|
---|
[53] | 203 | alarmType = PSAlarmSnooze;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[61] | 206 | - (void)setWakeUp:(BOOL)doWake;
|
---|
| 207 | {
|
---|
| 208 | [timer setWakeUp: doWake];
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[51] | 211 | #pragma mark accessing
|
---|
| 212 |
|
---|
| 213 | - (NSString *)message;
|
---|
[24] | 214 | {
|
---|
[51] | 215 | if (alarmMessage == nil || [alarmMessage isEqualToString: @""])
|
---|
| 216 | return @"Alarm!";
|
---|
| 217 | return alarmMessage;
|
---|
[24] | 218 | }
|
---|
| 219 |
|
---|
| 220 | - (void)setMessage:(NSString *)aMessage;
|
---|
| 221 | {
|
---|
| 222 | if (aMessage != alarmMessage) {
|
---|
| 223 | [alarmMessage release];
|
---|
| 224 | alarmMessage = nil;
|
---|
| 225 | alarmMessage = [aMessage retain];
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[51] | 229 | - (BOOL)isValid;
|
---|
[24] | 230 | {
|
---|
[51] | 231 | if (alarmType == PSAlarmDate) [self _setIntervalFromDate];
|
---|
[53] | 232 | if (alarmType == PSAlarmInvalid ||
|
---|
| 233 | (alarmType == PSAlarmExpired && ![self isRepeating])) return NO;
|
---|
| 234 | return YES;
|
---|
[24] | 235 | }
|
---|
| 236 |
|
---|
| 237 | - (NSString *)invalidMessage;
|
---|
| 238 | {
|
---|
[26] | 239 | if (invalidMessage == nil) return @"";
|
---|
[24] | 240 | return invalidMessage;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[28] | 243 | - (NSCalendarDate *)date;
|
---|
[24] | 244 | {
|
---|
| 245 | if (alarmType == PSAlarmInterval) [self _setDateFromInterval];
|
---|
| 246 | return alarmDate;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[366] | 249 | - (NSDate *)time;
|
---|
[51] | 250 | {
|
---|
[366] | 251 | // XXX this works, but the result is unlikely to be useful until we move away from NSCalendarDate elsewhere
|
---|
[51] | 252 | if (alarmType == PSAlarmInterval) [self _setDateFromInterval];
|
---|
[366] | 253 |
|
---|
| 254 | NSCalendar *calendar = [NSCalendar currentCalendar];
|
---|
| 255 |
|
---|
| 256 | return [calendar dateFromComponents:
|
---|
| 257 | [calendar components: NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate: alarmDate]];
|
---|
[51] | 258 | }
|
---|
| 259 |
|
---|
| 260 | - (NSTimeInterval)interval;
|
---|
| 261 | {
|
---|
[53] | 262 | if (alarmType == PSAlarmDate) [self _setIntervalFromDate];
|
---|
[51] | 263 | return alarmInterval;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[53] | 266 | - (NSTimeInterval)snoozeInterval;
|
---|
[51] | 267 | {
|
---|
[53] | 268 | return snoozeInterval;
|
---|
[51] | 269 | }
|
---|
| 270 |
|
---|
[53] | 271 | - (NSTimeInterval)timeRemaining;
|
---|
[51] | 272 | {
|
---|
[103] | 273 | NSAssert1(alarmType == PSAlarmSet, NSLocalizedString(@"Can't get time remaining on alarm with no timer set: %@", "Assertion for PSAlarm time remaining, internal error; %@ replaced by alarm description"), self);
|
---|
[53] | 274 | return -[[NSDate date] timeIntervalSinceDate: alarmDate];
|
---|
[51] | 275 | }
|
---|
| 276 |
|
---|
[53] | 277 | - (void)setAlerts:(PSAlerts *)theAlerts;
|
---|
[51] | 278 | {
|
---|
[53] | 279 | [alerts release]; alerts = nil;
|
---|
| 280 | alerts = [theAlerts retain];
|
---|
[51] | 281 | }
|
---|
| 282 |
|
---|
[53] | 283 | - (PSAlerts *)alerts;
|
---|
| 284 | {
|
---|
| 285 | if (alerts == nil) alerts = [[PSAlerts alloc] init];
|
---|
| 286 | return alerts;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | - (BOOL)isRepeating;
|
---|
| 290 | {
|
---|
| 291 | return repeating;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[43] | 294 | - (NSString *)dateString;
|
---|
| 295 | {
|
---|
[358] | 296 | return [dateFormatter stringFromDate: [self date]];
|
---|
[43] | 297 | }
|
---|
| 298 |
|
---|
[28] | 299 | - (NSString *)shortDateString;
|
---|
| 300 | {
|
---|
[358] | 301 | return [shortDateFormatter stringFromDate: [self date]];
|
---|
[28] | 302 | }
|
---|
| 303 |
|
---|
| 304 | - (NSString *)timeString;
|
---|
| 305 | {
|
---|
[358] | 306 | return [timeFormatter stringFromDate: [self date]];
|
---|
[28] | 307 | }
|
---|
| 308 |
|
---|
[53] | 309 | - (NSString *)dateTimeString;
|
---|
[28] | 310 | {
|
---|
[53] | 311 | return [NSString stringWithFormat: @"%@ at %@", [self dateString], [self timeString]];
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | - (NSString *)nextDateTimeString;
|
---|
| 315 | {
|
---|
| 316 | if (![self isRepeating]) {
|
---|
| 317 | return nil;
|
---|
| 318 | } else {
|
---|
| 319 | NSCalendarDate *date = [[NSCalendarDate alloc] initWithTimeIntervalSinceNow: [self interval]];
|
---|
| 320 | NSString *nextDateTimeString = [NSString stringWithFormat: @"%@ at %@",
|
---|
[358] | 321 | [dateFormatter stringFromDate: date],
|
---|
| 322 | [timeFormatter stringFromDate: date]];
|
---|
[53] | 323 | [date release];
|
---|
| 324 | return nextDateTimeString;
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | - (NSString *)intervalString;
|
---|
| 329 | {
|
---|
[551] | 330 | const unsigned long long mval = 999, minute = 60, hour = minute * 60, day = hour * 24, week = day * 7;
|
---|
[28] | 331 | unsigned long long interval = [self interval];
|
---|
[53] | 332 | if (interval == 0) return nil;
|
---|
[551] | 333 |
|
---|
[53] | 334 | if (interval == 1) return @"One second";
|
---|
| 335 | if (interval == minute) return @"One minute";
|
---|
[551] | 336 | if (interval == hour) return @"One hour";
|
---|
| 337 | if (interval == day) return @"One day";
|
---|
| 338 | if (interval == week) return @"One week";
|
---|
| 339 |
|
---|
| 340 | if (interval % week == 0) return [NSString stringWithFormat: @"%u weeks", (unsigned)(interval / week)];
|
---|
| 341 | if (interval % day == 0) return [NSString stringWithFormat: @"%u days", (unsigned)(interval / day)];
|
---|
| 342 | if (interval % hour == 0) return [NSString stringWithFormat: @"%u hours", (unsigned)(interval / hour)];
|
---|
[53] | 343 | if (interval % minute == 0) return [NSString stringWithFormat: @"%u minutes", (unsigned)(interval / minute)];
|
---|
[551] | 344 |
|
---|
[53] | 345 | if (interval <= mval) return [NSString stringWithFormat: @"%u seconds", (unsigned)interval];
|
---|
| 346 | if (interval <= mval * minute) return [NSString stringWithFormat: @"%u minutes", (unsigned)(interval / minute)];
|
---|
| 347 | if (interval <= mval * hour) return [NSString stringWithFormat: @"%u hours", (unsigned)(interval / hour)];
|
---|
[551] | 348 | if (interval <= mval * day) return [NSString stringWithFormat: @"%u days", (unsigned)(interval / day)];
|
---|
| 349 | return [NSString stringWithFormat: @"%u weeks", (unsigned)(interval / week)];
|
---|
| 350 |
|
---|
[53] | 351 | return [self _stringForInterval: interval];
|
---|
[28] | 352 | }
|
---|
| 353 |
|
---|
[53] | 354 | - (NSString *)timeRemainingString;
|
---|
| 355 | {
|
---|
| 356 | NSString *timeRemainingString = [self _stringForInterval: llround([self timeRemaining])];
|
---|
| 357 |
|
---|
[355] | 358 | if (timeRemainingString == nil) return @"«expired»";
|
---|
[53] | 359 | return timeRemainingString;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | - (NSAttributedString *)prettyDescription;
|
---|
| 363 | {
|
---|
| 364 | NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
|
---|
| 365 | NSAttributedString *alertList = [alerts prettyList];
|
---|
| 366 |
|
---|
| 367 | [string appendAttributedString:
|
---|
[364] | 368 | [[NSString stringWithFormat: NSLocalizedString(@"At alarm time for '%@':\n", "Alert list title in pretty description, %@ replaced with message"), [self message]] small]];
|
---|
[53] | 369 | if (alertList != nil) {
|
---|
| 370 | [string appendAttributedString: alertList];
|
---|
| 371 | } else {
|
---|
| 372 | [string appendAttributedString: [@"Do nothing." small]];
|
---|
| 373 | }
|
---|
| 374 | if ([self isRepeating]) {
|
---|
| 375 | [string appendAttributedString:
|
---|
| 376 | [[NSString stringWithFormat: @"\nAlarm repeats every %@.", [[self intervalString] lowercaseString]] small]];
|
---|
| 377 | }
|
---|
| 378 | return [string autorelease];
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[51] | 381 | #pragma mark actions
|
---|
[24] | 382 |
|
---|
[26] | 383 | - (BOOL)setTimer;
|
---|
| 384 | {
|
---|
[53] | 385 | if (alarmType == PSAlarmExpired) {
|
---|
| 386 | if ([self isRepeating]) {
|
---|
| 387 | [self _setDateFromInterval];
|
---|
| 388 | } else {
|
---|
| 389 | [[NSNotificationCenter defaultCenter] postNotificationName: PSAlarmDiedNotification object: self];
|
---|
[26] | 390 | return NO;
|
---|
[53] | 391 | }
|
---|
| 392 | } else if (alarmType == PSAlarmDate) {
|
---|
| 393 | if (![self isValid]) return NO;
|
---|
| 394 | } else if (alarmType == PSAlarmSnooze) {
|
---|
| 395 | [self _setAlarmDate: [NSCalendarDate dateWithTimeIntervalSinceNow: snoozeInterval]];
|
---|
| 396 | } else if (alarmType != PSAlarmInterval) {
|
---|
| 397 | return NO;
|
---|
[26] | 398 | }
|
---|
[61] | 399 | timer = [PSTimer scheduledTimerWithTimeInterval: (alarmType == PSAlarmSnooze ? snoozeInterval : alarmInterval) target: self selector: @selector(_timerExpired:) userInfo: nil repeats: NO];
|
---|
[53] | 400 | if (timer == nil) return NO;
|
---|
| 401 | [timer retain];
|
---|
| 402 | alarmType = PSAlarmSet;
|
---|
[61] | 403 | [alerts prepareForAlarm: self];
|
---|
| 404 |
|
---|
[53] | 405 | [[NSNotificationCenter defaultCenter] postNotificationName: PSAlarmTimerSetNotification object: self];
|
---|
| 406 | // NSLog(@"set: %@; now %@; remaining %@", [[timer fireDate] description], [[NSDate date] description], [self timeRemainingString]);
|
---|
| 407 | return YES;
|
---|
[26] | 408 | }
|
---|
| 409 |
|
---|
[51] | 410 | - (void)cancelTimer;
|
---|
[26] | 411 | {
|
---|
[28] | 412 | [timer invalidate]; [timer release]; timer = nil;
|
---|
[26] | 413 | }
|
---|
| 414 |
|
---|
[113] | 415 | - (void)resetTimer;
|
---|
| 416 | {
|
---|
| 417 | if (timer != nil || alarmType != PSAlarmSet)
|
---|
| 418 | return;
|
---|
| 419 |
|
---|
| 420 | alarmType = PSAlarmDate;
|
---|
| 421 | if (![self isRepeating]) {
|
---|
| 422 | [self setTimer];
|
---|
| 423 | } else {
|
---|
| 424 | // don't want to put this logic in setTimer or isValid because it can cause invalid alarms to be set (consider when someone clicks the "repeat" checkbox, then switches to a [nonrepeating, by design] date alarm, and enters a date that has passed: we do -not- want the alarm to magically morph into a repeating interval alarm)
|
---|
| 425 | NSTimeInterval savedInterval = alarmInterval;
|
---|
[364] | 426 | if ([self setTimer]) {
|
---|
| 427 | // alarm is set, but not repeating - and the interval is wrong because it was computed from the date
|
---|
| 428 | alarmInterval = savedInterval;
|
---|
| 429 | [self setRepeating: YES];
|
---|
| 430 | } else {
|
---|
| 431 | // alarm is now invalid: expired in the past, so we start the timer over again
|
---|
| 432 | // We could potentially start counting from the expiration date (or expiration date + n * interval), but this doesn't match our existing behavior.
|
---|
[113] | 433 | alarmType = PSAlarmInterval;
|
---|
| 434 | [self setInterval: savedInterval];
|
---|
| 435 | [self setTimer];
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 |
|
---|
[51] | 440 | #pragma mark comparing
|
---|
[26] | 441 |
|
---|
[51] | 442 | - (NSComparisonResult)compareDate:(PSAlarm *)otherAlarm;
|
---|
[26] | 443 | {
|
---|
| 444 | return [[self date] compare: [otherAlarm date]];
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[51] | 447 | - (NSComparisonResult)compareMessage:(PSAlarm *)otherAlarm;
|
---|
[34] | 448 | {
|
---|
[51] | 449 | return [[self message] caseInsensitiveCompare: [otherAlarm message]];
|
---|
[34] | 450 | }
|
---|
| 451 |
|
---|
[51] | 452 | #pragma mark printing
|
---|
[34] | 453 |
|
---|
[26] | 454 | - (NSString *)description;
|
---|
| 455 | {
|
---|
[105] | 456 | return [NSString stringWithFormat: @"%@: type %@ date %@ interval %.1f%@%@",
|
---|
[26] | 457 | [super description], [self _alarmTypeString], alarmDate, alarmInterval,
|
---|
[105] | 458 | (repeating ? @" repeating" : @""),
|
---|
[26] | 459 | (alarmType == PSAlarmInvalid ?
|
---|
| 460 | [NSString stringWithFormat: @"\ninvalid message: %@", invalidMessage]
|
---|
| 461 | : (alarmType == PSAlarmSet ?
|
---|
| 462 | [NSString stringWithFormat: @"\ntimer: %@", timer] : @""))];
|
---|
| 463 | }
|
---|
| 464 |
|
---|
[53] | 465 | #pragma mark property list serialization (Pester 1.1)
|
---|
[51] | 466 |
|
---|
[53] | 467 | - (NSDictionary *)propertyListRepresentation;
|
---|
| 468 | {
|
---|
| 469 | NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 5];
|
---|
| 470 | if (![self isValid]) return nil;
|
---|
| 471 | [dict setObject: [self _alarmTypeString] forKey: PLAlarmType];
|
---|
| 472 | switch (alarmType) {
|
---|
| 473 | case PSAlarmDate:
|
---|
| 474 | case PSAlarmSet:
|
---|
| 475 | [dict setObject: [NSNumber numberWithDouble: [alarmDate timeIntervalSinceReferenceDate]] forKey: PLAlarmDate];
|
---|
| 476 | case PSAlarmSnooze:
|
---|
| 477 | case PSAlarmInterval:
|
---|
| 478 | case PSAlarmExpired:
|
---|
| 479 | break;
|
---|
| 480 | default:
|
---|
[103] | 481 | NSAssert1(NO, NSLocalizedString(@"Can't save alarm type %@", "Assertion for invalid PSAlarm type on string; %@ replaced with alarm type string"), [self _alarmTypeString]);
|
---|
[53] | 482 | break;
|
---|
| 483 | }
|
---|
[105] | 484 | if ((alarmType != PSAlarmSet || repeating) && alarmType != PSAlarmDate) {
|
---|
| 485 | [dict setObject: [NSNumber numberWithBool: repeating] forKey: PLAlarmRepeating];
|
---|
| 486 | [dict setObject: [NSNumber numberWithDouble: alarmInterval] forKey: PLAlarmInterval];
|
---|
| 487 | }
|
---|
[53] | 488 | if (snoozeInterval != 0)
|
---|
| 489 | [dict setObject: [NSNumber numberWithDouble: snoozeInterval] forKey: PLAlarmSnoozeInterval];
|
---|
| 490 | [dict setObject: alarmMessage forKey: PLAlarmMessage];
|
---|
| 491 | if (alerts != nil) {
|
---|
| 492 | [dict setObject: [alerts propertyListRepresentation] forKey: PLAlarmAlerts];
|
---|
| 493 | }
|
---|
| 494 | return dict;
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
| 498 | {
|
---|
| 499 | if ( (self = [self init]) != nil) {
|
---|
| 500 | PSAlerts *alarmAlerts;
|
---|
| 501 | alarmType = [self _alarmTypeForString: [dict objectForRequiredKey: PLAlarmType]];
|
---|
| 502 | switch (alarmType) {
|
---|
| 503 | case PSAlarmDate:
|
---|
| 504 | case PSAlarmSet:
|
---|
| 505 | { NSCalendarDate *date = [[NSCalendarDate alloc] initWithTimeIntervalSinceReferenceDate: [[dict objectForRequiredKey: PLAlarmDate] doubleValue]];
|
---|
| 506 | [self _setAlarmDate: date];
|
---|
| 507 | [date release];
|
---|
| 508 | }
|
---|
| 509 | break;
|
---|
| 510 | case PSAlarmSnooze: // snooze interval set but not confirmed; ignore
|
---|
| 511 | alarmType = PSAlarmExpired;
|
---|
| 512 | case PSAlarmInterval:
|
---|
| 513 | case PSAlarmExpired:
|
---|
| 514 | break;
|
---|
| 515 | default:
|
---|
[103] | 516 | NSAssert1(NO, NSLocalizedString(@"Can't load alarm type %@", "Assertion for invalid PSAlarm type on load; %@ replaced with alarm type string"), [self _alarmTypeString]);
|
---|
[53] | 517 | break;
|
---|
| 518 | }
|
---|
[105] | 519 | repeating = [[dict objectForKey: PLAlarmRepeating] boolValue];
|
---|
| 520 | if ((alarmType != PSAlarmSet || repeating) && alarmType != PSAlarmDate)
|
---|
| 521 | alarmInterval = [[dict objectForRequiredKey: PLAlarmInterval] doubleValue];
|
---|
[53] | 522 | snoozeInterval = [[dict objectForKey: PLAlarmSnoozeInterval] doubleValue];
|
---|
| 523 | [self setMessage: [dict objectForRequiredKey: PLAlarmMessage]];
|
---|
| 524 | alarmAlerts = [[PSAlerts alloc] initWithPropertyList: [dict objectForRequiredKey: PLAlarmAlerts]];
|
---|
| 525 | [self setAlerts: alarmAlerts];
|
---|
| 526 | [alarmAlerts release];
|
---|
[113] | 527 | [self resetTimer];
|
---|
[53] | 528 | if (alarmType == PSAlarmExpired) {
|
---|
| 529 | [self setTimer];
|
---|
| 530 | if (alarmType == PSAlarmExpired) { // failed to restart
|
---|
| 531 | [self release];
|
---|
| 532 | self = nil;
|
---|
| 533 | }
|
---|
| 534 | }
|
---|
| 535 | }
|
---|
| 536 | return self;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | #pragma mark archiving (Pester 1.0)
|
---|
| 540 |
|
---|
[26] | 541 | - (void)encodeWithCoder:(NSCoder *)coder;
|
---|
| 542 | {
|
---|
| 543 | if (![self isValid]) return;
|
---|
| 544 | [coder encodeValueOfObjCType: @encode(PSAlarmType) at: &alarmType];
|
---|
| 545 | switch (alarmType) {
|
---|
| 546 | case PSAlarmDate:
|
---|
| 547 | case PSAlarmSet:
|
---|
| 548 | [coder encodeObject: alarmDate];
|
---|
| 549 | break;
|
---|
| 550 | case PSAlarmInterval:
|
---|
| 551 | [coder encodeValueOfObjCType: @encode(NSTimeInterval) at: &alarmInterval];
|
---|
| 552 | break;
|
---|
| 553 | default:
|
---|
| 554 | break;
|
---|
| 555 | }
|
---|
| 556 | [coder encodeObject: alarmMessage];
|
---|
[28] | 557 | // NSLog(@"encoded: %@", self); // XXX happening twice, gdb refuses to show proper backtrace, grr
|
---|
[26] | 558 | return;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | - (id)initWithCoder:(NSCoder *)coder;
|
---|
| 562 | {
|
---|
[53] | 563 | if ( (self = [self init]) != nil) {
|
---|
| 564 | PSAlerts *legacyAlerts = [[PSAlerts alloc] initWithPesterVersion1Alerts];
|
---|
| 565 | [self setAlerts: legacyAlerts];
|
---|
| 566 | [legacyAlerts release];
|
---|
[26] | 567 | [coder decodeValueOfObjCType: @encode(PSAlarmType) at: &alarmType];
|
---|
| 568 | switch (alarmType) {
|
---|
| 569 | case PSAlarmDate:
|
---|
| 570 | case PSAlarmSet:
|
---|
| 571 | [self _setAlarmDate: [coder decodeObject]];
|
---|
| 572 | break;
|
---|
| 573 | case PSAlarmInterval:
|
---|
| 574 | [coder decodeValueOfObjCType: @encode(NSTimeInterval) at: &alarmInterval];
|
---|
| 575 | break;
|
---|
| 576 | default:
|
---|
| 577 | break;
|
---|
| 578 | }
|
---|
| 579 | [self setMessage: [coder decodeObject]];
|
---|
[364] | 580 | if (alarmType == PSAlarmSet)
|
---|
[26] | 581 | alarmType = PSAlarmDate;
|
---|
[364] | 582 | // Note: the timer is not set here, so these alarms are inert.
|
---|
| 583 | // This helps make importing atomic (see -[PSAlarms importVersion1Alarms])
|
---|
[26] | 584 | }
|
---|
| 585 | return self;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
[24] | 588 | @end
|
---|