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 | [super dealloc];
|
---|
44 | }
|
---|
45 |
|
---|
46 | #pragma mark accessing
|
---|
47 |
|
---|
48 | - (void)addAlert:(PSAlert *)alert;
|
---|
49 | {
|
---|
50 | [alerts addObject: alert];
|
---|
51 | if ([alert requiresPesterFrontmost])
|
---|
52 | requirePesterFrontmost = YES;
|
---|
53 | }
|
---|
54 |
|
---|
55 | - (void)removeAlerts;
|
---|
56 | {
|
---|
57 | [alerts removeAllObjects];
|
---|
58 | requirePesterFrontmost = NO;
|
---|
59 | }
|
---|
60 |
|
---|
61 | - (NSEnumerator *)alertEnumerator;
|
---|
62 | {
|
---|
63 | return [alerts objectEnumerator];
|
---|
64 | }
|
---|
65 |
|
---|
66 | - (NSArray *)allAlerts;
|
---|
67 | {
|
---|
68 | return [[alerts copy] autorelease];
|
---|
69 | }
|
---|
70 |
|
---|
71 | - (BOOL)requirePesterFrontmost;
|
---|
72 | {
|
---|
73 | return requirePesterFrontmost;
|
---|
74 | }
|
---|
75 |
|
---|
76 | #pragma mark actions
|
---|
77 |
|
---|
78 | - (void)prepareForAlarm:(PSAlarm *)alarm;
|
---|
79 | {
|
---|
80 | [alerts makeObjectsPerformSelector: @selector(prepareForAlarm:) withObject: alarm];
|
---|
81 | }
|
---|
82 |
|
---|
83 | - (void)triggerForAlarm:(PSAlarm *)alarm;
|
---|
84 | {
|
---|
85 | [alerts makeObjectsPerformSelector: @selector(triggerForAlarm:) withObject: alarm];
|
---|
86 | }
|
---|
87 |
|
---|
88 | #pragma mark printing
|
---|
89 |
|
---|
90 | - (NSAttributedString *)prettyList;
|
---|
91 | {
|
---|
92 | NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
|
---|
93 | NSEnumerator *e = [self alertEnumerator];
|
---|
94 | PSAlert *alert;
|
---|
95 | unsigned int length;
|
---|
96 | while ( (alert = [e nextObject]) != nil) {
|
---|
97 | if ([alert isKindOfClass: [PSDockBounceAlert class]]) // XXX temporary for 1.1b5
|
---|
98 | continue;
|
---|
99 | [string appendAttributedString: [NSLocalizedString(@"* ", "Unordered list label (usually a bullet followed by a space)") small]];
|
---|
100 | [string appendAttributedString: [alert actionDescription]];
|
---|
101 | [string appendAttributedString: [@"\n" small]];
|
---|
102 | }
|
---|
103 | if ( (length = [string length]) == 0) {
|
---|
104 | [string release];
|
---|
105 | return nil;
|
---|
106 | } else {
|
---|
107 | NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
---|
108 | [string deleteCharactersInRange: NSMakeRange(length - 1, 1)]; // remove trailing newline
|
---|
109 | [paraStyle setHeadIndent: [[string attribute: NSFontAttributeName atIndex: 0 effectiveRange: NULL] widthOfString: NSLocalizedString(@"* ", "Unordered list label (usually a bullet followed by a space)")]];
|
---|
110 | [string addAttribute: NSParagraphStyleAttributeName value: paraStyle range: NSMakeRange(0, length - 1)];
|
---|
111 | [paraStyle release]; paraStyle = nil;
|
---|
112 | return [string autorelease];
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | #pragma mark property list serialization (Pester 1.1)
|
---|
117 |
|
---|
118 | - (NSDictionary *)propertyListRepresentation;
|
---|
119 | {
|
---|
120 | NSMutableArray *plAlerts = [[NSMutableArray alloc] initWithCapacity: [alerts count]];
|
---|
121 | NSEnumerator *e = [self alertEnumerator];
|
---|
122 | PSAlert *alert;
|
---|
123 | while ( (alert = [e nextObject]) != nil) {
|
---|
124 | [plAlerts addObject: [alert propertyListRepresentation]];
|
---|
125 | }
|
---|
126 | NSDictionary *dict = [NSDictionary dictionaryWithObject: plAlerts forKey: PLAlerts];
|
---|
127 | [plAlerts release];
|
---|
128 | return dict;
|
---|
129 | }
|
---|
130 |
|
---|
131 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
132 | {
|
---|
133 | if ( (self = [self init]) != nil) {
|
---|
134 | NSArray *plAlerts = [dict objectForKey: PLAlerts];
|
---|
135 | NSEnumerator *e = [plAlerts objectEnumerator];
|
---|
136 | NSDictionary *alertDict;
|
---|
137 | while ( (alertDict = [e nextObject]) != nil) {
|
---|
138 | [self addAlert: [[PSAlert alloc] initWithPropertyList: alertDict]];
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return self;
|
---|
142 | }
|
---|
143 |
|
---|
144 | @end
|
---|