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 | [string appendAttributedString: [NSLocalizedString(@"* ", "Unordered list label (usually a bullet followed by a space)") small]];
|
---|
98 | [string appendAttributedString: [alert actionDescription]];
|
---|
99 | [string appendAttributedString: [@"\n" small]];
|
---|
100 | }
|
---|
101 | if ( (length = [string length]) == 0) {
|
---|
102 | [string release];
|
---|
103 | return nil;
|
---|
104 | } else {
|
---|
105 | NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
---|
106 | [string deleteCharactersInRange: NSMakeRange(length - 1, 1)]; // remove trailing newline
|
---|
107 | [paraStyle setHeadIndent: [[string attribute: NSFontAttributeName atIndex: 0 effectiveRange: NULL] widthOfString: NSLocalizedString(@"* ", "Unordered list label (usually a bullet followed by a space)")]];
|
---|
108 | [string addAttribute: NSParagraphStyleAttributeName value: paraStyle range: NSMakeRange(0, length - 1)];
|
---|
109 | [paraStyle release]; paraStyle = nil;
|
---|
110 | return [string autorelease];
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | #pragma mark property list serialization (Pester 1.1)
|
---|
115 |
|
---|
116 | - (NSDictionary *)propertyListRepresentation;
|
---|
117 | {
|
---|
118 | NSMutableArray *plAlerts = [[NSMutableArray alloc] initWithCapacity: [alerts count]];
|
---|
119 | NSEnumerator *e = [self alertEnumerator];
|
---|
120 | PSAlert *alert;
|
---|
121 | while ( (alert = [e nextObject]) != nil) {
|
---|
122 | [plAlerts addObject: [alert propertyListRepresentation]];
|
---|
123 | }
|
---|
124 | NSDictionary *dict = [NSDictionary dictionaryWithObject: plAlerts forKey: PLAlerts];
|
---|
125 | [plAlerts release];
|
---|
126 | return dict;
|
---|
127 | }
|
---|
128 |
|
---|
129 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
130 | {
|
---|
131 | if ( (self = [self init]) != nil) {
|
---|
132 | @try {
|
---|
133 | NSArray *plAlerts = [dict objectForKey: PLAlerts];
|
---|
134 | NSEnumerator *e = [plAlerts objectEnumerator];
|
---|
135 | NSDictionary *alertDict;
|
---|
136 | while ( (alertDict = [e nextObject]) != nil) {
|
---|
137 | [self addAlert: [[PSAlert alloc] initWithPropertyList: alertDict]];
|
---|
138 | }
|
---|
139 | } @catch (NSException *e) {
|
---|
140 | [self release];
|
---|
141 | @throw;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return self;
|
---|
145 | }
|
---|
146 |
|
---|
147 | @end
|
---|