1 | // |
---|
2 | // PSAlerts.m |
---|
3 | // Pester |
---|
4 | // |
---|
5 | // Created by Nicholas Riley on Sat Dec 21 2002. |
---|
6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved. |
---|
7 | // |
---|
8 | |
---|
9 | #import "PSAlerts.h" |
---|
10 | #import "PSAlert.h" |
---|
11 | #import "PSDockBounceAlert.h" |
---|
12 | #import "PSNotifierAlert.h" |
---|
13 | #import "PSBeepAlert.h" |
---|
14 | |
---|
15 | // property list keys |
---|
16 | static NSString * const PLAlerts = @"alerts"; // NSString |
---|
17 | |
---|
18 | @implementation PSAlerts |
---|
19 | |
---|
20 | #pragma mark initialize-release |
---|
21 | |
---|
22 | - (id)init; |
---|
23 | { |
---|
24 | if ( (self = [super init]) != nil) { |
---|
25 | alerts = [[NSMutableArray alloc] initWithCapacity: 4]; |
---|
26 | } |
---|
27 | return self; |
---|
28 | } |
---|
29 | |
---|
30 | - (id)initWithPesterVersion1Alerts; |
---|
31 | { |
---|
32 | if ( (self = [self init]) != nil) { |
---|
33 | [self addAlert: [PSDockBounceAlert alert]]; |
---|
34 | [self addAlert: [PSNotifierAlert alert]]; |
---|
35 | [self addAlert: [PSBeepAlert alertWithRepetitions: 1]]; |
---|
36 | } |
---|
37 | return self; |
---|
38 | } |
---|
39 | |
---|
40 | - (void)dealloc; |
---|
41 | { |
---|
42 | [alerts release]; alerts = nil; |
---|
43 | } |
---|
44 | |
---|
45 | #pragma mark accessing |
---|
46 | |
---|
47 | - (void)addAlert:(PSAlert *)alert; |
---|
48 | { |
---|
49 | [alerts addObject: alert]; |
---|
50 | if ([alert requiresPesterFrontmost]) |
---|
51 | requirePesterFrontmost = YES; |
---|
52 | } |
---|
53 | |
---|
54 | - (void)removeAlerts; |
---|
55 | { |
---|
56 | [alerts removeAllObjects]; |
---|
57 | requirePesterFrontmost = NO; |
---|
58 | } |
---|
59 | |
---|
60 | - (NSEnumerator *)alertEnumerator; |
---|
61 | { |
---|
62 | return [alerts objectEnumerator]; |
---|
63 | } |
---|
64 | |
---|
65 | - (NSArray *)allAlerts; |
---|
66 | { |
---|
67 | return [[alerts copy] autorelease]; |
---|
68 | } |
---|
69 | |
---|
70 | - (BOOL)requirePesterFrontmost; |
---|
71 | { |
---|
72 | return requirePesterFrontmost; |
---|
73 | } |
---|
74 | |
---|
75 | #pragma mark actions |
---|
76 | |
---|
77 | - (void)triggerForAlarm:(PSAlarm *)alarm; |
---|
78 | { |
---|
79 | [alerts makeObjectsPerformSelector: @selector(triggerForAlarm:) withObject: alarm]; |
---|
80 | } |
---|
81 | |
---|
82 | #pragma mark printing |
---|
83 | |
---|
84 | - (NSAttributedString *)prettyList; |
---|
85 | { |
---|
86 | NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init]; |
---|
87 | NSEnumerator *e = [self alertEnumerator]; |
---|
88 | PSAlert *alert; |
---|
89 | unsigned int length; |
---|
90 | while ( (alert = [e nextObject]) != nil) { |
---|
91 | [string appendAttributedString: [@"¥ " small]]; |
---|
92 | [string appendAttributedString: [alert actionDescription]]; |
---|
93 | [string appendAttributedString: [@"\n" small]]; |
---|
94 | } |
---|
95 | if ( (length = [string length]) == 0) { |
---|
96 | [string release]; |
---|
97 | return nil; |
---|
98 | } else { |
---|
99 | NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
---|
100 | [string deleteCharactersInRange: NSMakeRange(length - 1, 1)]; // remove trailing newline |
---|
101 | [paraStyle setHeadIndent: [[string attribute: NSFontAttributeName atIndex: 0 effectiveRange: NULL] widthOfString: @"¥ "]]; |
---|
102 | [string addAttribute: NSParagraphStyleAttributeName value: paraStyle range: NSMakeRange(0, length - 1)]; |
---|
103 | [paraStyle release]; paraStyle = nil; |
---|
104 | return [string autorelease]; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | #pragma mark property list serialization (Pester 1.1) |
---|
109 | |
---|
110 | - (NSDictionary *)propertyListRepresentation; |
---|
111 | { |
---|
112 | NSMutableArray *plAlerts = [[NSMutableArray alloc] initWithCapacity: [alerts count]]; |
---|
113 | NSEnumerator *e = [self alertEnumerator]; |
---|
114 | PSAlert *alert; |
---|
115 | while ( (alert = [e nextObject]) != nil) { |
---|
116 | [plAlerts addObject: [alert propertyListRepresentation]]; |
---|
117 | } |
---|
118 | return [NSDictionary dictionaryWithObject: plAlerts forKey: PLAlerts]; |
---|
119 | } |
---|
120 | |
---|
121 | - (id)initWithPropertyList:(NSDictionary *)dict; |
---|
122 | { |
---|
123 | if ( (self = [self init]) != nil) { |
---|
124 | NSArray *plAlerts = [dict objectForKey: PLAlerts]; |
---|
125 | NSEnumerator *e = [plAlerts objectEnumerator]; |
---|
126 | NSDictionary *alertDict; |
---|
127 | while ( (alertDict = [e nextObject]) != nil) { |
---|
128 | [self addAlert: [[PSAlert alloc] initWithPropertyList: alertDict]]; |
---|
129 | } |
---|
130 | } |
---|
131 | return self; |
---|
132 | } |
---|
133 | |
---|
134 | @end |
---|