source: trunk/Cocoa/Pester/Source/PSSpeechAlert.m@ 573

Last change on this file since 573 was 561, checked in by Nicholas Riley, 15 years ago

PSSpeechAlert.m: Remove memory leak when voice not found; note that even this behavior isn't ideal.

File size: 2.7 KB
Line 
1//
2// PSSpeechAlert.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 "PSSpeechAlert.h"
10#import "PSAlarmAlertController.h"
11#import "NSDictionary-NJRExtensions.h"
12
13// property list keys
14static NSString * const PLAlertVoice = @"voice"; // NSString
15
16@implementation PSSpeechAlert
17
18+ (PSSpeechAlert *)alertWithVoice:(NSString *)aVoice;
19{
20 return [[[self alloc] initWithVoice: aVoice] autorelease];
21}
22
23- (id)initWithVoice:(NSString *)aVoice;
24{
25 if ( (self = [super init]) != nil) {
26 voice = [aVoice retain];
27 }
28 return self;
29}
30
31- (void)dealloc;
32{
33 [[NSNotificationCenter defaultCenter] removeObserver: self];
34 [speaker release]; speaker = nil;
35 [voice release]; voice = nil;
36 [super dealloc];
37}
38
39- (NSString *)voice;
40{
41 return voice;
42}
43
44- (void)_stopSpeaking:(NSNotification *)notification;
45{
46 [speaker stopSpeaking]; // triggers didFinishSpeaking:
47}
48
49- (void)triggerForAlarm:(PSAlarm *)anAlarm;
50{
51 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_stopSpeaking:) name: PSAlarmAlertStopNotification object: nil];
52
53 if ( (speaker = [[NSSpeechSynthesizer alloc] initWithVoice: voice]) == nil) return;
54 alarm = anAlarm;
55 [speaker setDelegate: self];
56 [speaker startSpeakingString: [alarm message]];
57}
58
59- (NSAttributedString *)actionDescription;
60{
61 NSMutableAttributedString *string = [[@"Speak message with voice " small] mutableCopy];
62 NSString *voiceName = [[NSSpeechSynthesizer attributesForVoice: voice] objectForKey: NSVoiceName];
63 if (voiceName == nil)
64 voiceName = voice;
65 [string appendAttributedString: [voiceName underlined]];
66 return [string autorelease];
67}
68
69#pragma mark property list serialization (Pester 1.1)
70
71- (NSDictionary *)propertyListRepresentation;
72{
73 NSMutableDictionary *plAlert = [[super propertyListRepresentation] mutableCopy];
74 if (voice == nil) {
75 // XXX should pick a different voice, not destroy the alert
76 [plAlert release];
77 @throw [NSException exceptionWithName: NSInvalidArgumentException
78 reason: @"The selected voice is not available." userInfo: nil];
79 }
80 [plAlert setObject: voice forKey: PLAlertVoice];
81 return [plAlert autorelease];
82}
83
84- (id)initWithPropertyList:(NSDictionary *)dict;
85{
86 if ( (self = [self init]) != nil) {
87 voice = [[dict objectForRequiredKey: PLAlertVoice] retain];
88 }
89 return self;
90}
91
92@end
93
94@implementation PSSpeechAlert (NSSpeechSynthesizerDelegate)
95
96- (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking;
97{
98 [self completedForAlarm: alarm];
99 [speaker release]; speaker = nil;
100}
101
102@end
Note: See TracBrowser for help on using the repository browser.