1 | //
|
---|
2 | // PSBeepAlert.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Sat Oct 26 2002.
|
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import <AppKit/AppKit.h>
|
---|
10 | #import "PSBeepAlert.h"
|
---|
11 | #import "PSAlarmAlertController.h"
|
---|
12 | #import "NSDictionary-NJRExtensions.h"
|
---|
13 |
|
---|
14 | // property list keys
|
---|
15 | static NSString * const PLAlertRepetitions = @"times"; // NSNumber
|
---|
16 |
|
---|
17 | @implementation PSBeepAlert
|
---|
18 |
|
---|
19 | + (PSBeepAlert *)alertWithRepetitions:(unsigned short)numReps;
|
---|
20 | {
|
---|
21 | return [[self alloc] initWithRepetitions: numReps];
|
---|
22 | }
|
---|
23 |
|
---|
24 | - (id)initWithRepetitions:(unsigned short)numReps;
|
---|
25 | {
|
---|
26 | if ( (self = [super init]) != nil) {
|
---|
27 | repetitions = numReps;
|
---|
28 | }
|
---|
29 | return self;
|
---|
30 | }
|
---|
31 |
|
---|
32 | - (void)dealloc;
|
---|
33 | {
|
---|
34 | [[NSNotificationCenter defaultCenter] removeObserver: self];
|
---|
35 | [super dealloc];
|
---|
36 | }
|
---|
37 |
|
---|
38 | - (unsigned short)repetitions;
|
---|
39 | {
|
---|
40 | return repetitions;
|
---|
41 | }
|
---|
42 |
|
---|
43 | - (void)beep;
|
---|
44 | {
|
---|
45 | NSBeep();
|
---|
46 | repetitionsRemaining--;
|
---|
47 | if (repetitionsRemaining == 0) {
|
---|
48 | [self completedForAlarm: alarm];
|
---|
49 | [self release];
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | [self performSelector: @selector(beep) withObject: nil afterDelay: 0.15 inModes: [NSArray arrayWithObject: NSDefaultRunLoopMode]];
|
---|
53 | }
|
---|
54 |
|
---|
55 | - (void)_stopBeeping:(NSNotification *)notification;
|
---|
56 | {
|
---|
57 | repetitionsRemaining = 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | - (void)triggerForAlarm:(PSAlarm *)anAlarm;
|
---|
61 | {
|
---|
62 | alarm = anAlarm;
|
---|
63 | repetitionsRemaining = repetitions;
|
---|
64 | [self retain];
|
---|
65 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_stopBeeping:) name: PSAlarmAlertStopNotification object: nil];
|
---|
66 | [self beep];
|
---|
67 | }
|
---|
68 |
|
---|
69 | - (NSAttributedString *)actionDescription;
|
---|
70 | {
|
---|
71 | return [[@"Play the system alert sound" stringByAppendingString:
|
---|
72 | repetitions == 1 ? @"" : [NSString stringWithFormat: @" %hu times", repetitions]] small];
|
---|
73 | }
|
---|
74 |
|
---|
75 | #pragma mark property list serialization (Pester 1.1)
|
---|
76 |
|
---|
77 | - (NSDictionary *)propertyListRepresentation;
|
---|
78 | {
|
---|
79 | NSMutableDictionary *plAlert = [[super propertyListRepresentation] mutableCopy];
|
---|
80 | [plAlert setObject: [NSNumber numberWithUnsignedShort: repetitions] forKey: PLAlertRepetitions];
|
---|
81 | return [plAlert autorelease];
|
---|
82 | }
|
---|
83 |
|
---|
84 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
85 | {
|
---|
86 | return [self initWithRepetitions: [[dict objectForRequiredKey: PLAlertRepetitions] unsignedShortValue]];
|
---|
87 | }
|
---|
88 |
|
---|
89 | @end
|
---|