source: releases/Pester/1.1b4/Source/PSAlerts.m@ 318

Last change on this file since 318 was 103, checked in by Nicholas Riley, 22 years ago

Localization, bug fixes

File size: 3.7 KB
Line 
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
16static 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)prepareForAlarm:(PSAlarm *)alarm;
78{
79 [alerts makeObjectsPerformSelector: @selector(prepareForAlarm:) withObject: alarm];
80}
81
82- (void)triggerForAlarm:(PSAlarm *)alarm;
83{
84 [alerts makeObjectsPerformSelector: @selector(triggerForAlarm:) withObject: alarm];
85}
86
87#pragma mark printing
88
89- (NSAttributedString *)prettyList;
90{
91 NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
92 NSEnumerator *e = [self alertEnumerator];
93 PSAlert *alert;
94 unsigned int length;
95 while ( (alert = [e nextObject]) != nil) {
96 [string appendAttributedString: [NSLocalizedString(@"* ", "Unordered list label (usually a bullet followed by a space)") small]];
97 [string appendAttributedString: [alert actionDescription]];
98 [string appendAttributedString: [@"\n" small]];
99 }
100 if ( (length = [string length]) == 0) {
101 [string release];
102 return nil;
103 } else {
104 NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
105 [string deleteCharactersInRange: NSMakeRange(length - 1, 1)]; // remove trailing newline
106 [paraStyle setHeadIndent: [[string attribute: NSFontAttributeName atIndex: 0 effectiveRange: NULL] widthOfString: NSLocalizedString(@"* ", "Unordered list label (usually a bullet followed by a space)")]];
107 [string addAttribute: NSParagraphStyleAttributeName value: paraStyle range: NSMakeRange(0, length - 1)];
108 [paraStyle release]; paraStyle = nil;
109 return [string autorelease];
110 }
111}
112
113#pragma mark property list serialization (Pester 1.1)
114
115- (NSDictionary *)propertyListRepresentation;
116{
117 NSMutableArray *plAlerts = [[NSMutableArray alloc] initWithCapacity: [alerts count]];
118 NSEnumerator *e = [self alertEnumerator];
119 PSAlert *alert;
120 while ( (alert = [e nextObject]) != nil) {
121 [plAlerts addObject: [alert propertyListRepresentation]];
122 }
123 return [NSDictionary dictionaryWithObject: plAlerts forKey: PLAlerts];
124}
125
126- (id)initWithPropertyList:(NSDictionary *)dict;
127{
128 if ( (self = [self init]) != nil) {
129 NSArray *plAlerts = [dict objectForKey: PLAlerts];
130 NSEnumerator *e = [plAlerts objectEnumerator];
131 NSDictionary *alertDict;
132 while ( (alertDict = [e nextObject]) != nil) {
133 [self addAlert: [[PSAlert alloc] initWithPropertyList: alertDict]];
134 }
135 }
136 return self;
137}
138
139@end
Note: See TracBrowser for help on using the repository browser.