[34] | 1 | //
|
---|
| 2 | // PSSpeechAlert.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Sat Oct 26 2002.
|
---|
[53] | 6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
[34] | 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "PSSpeechAlert.h"
|
---|
| 10 | #import "PSAlarmAlertController.h"
|
---|
[53] | 11 | #import "NSDictionary-NJRExtensions.h"
|
---|
[34] | 12 |
|
---|
[53] | 13 | // property list keys
|
---|
| 14 | static NSString * const PLAlertVoice = @"voice"; // NSString
|
---|
| 15 |
|
---|
[34] | 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) {
|
---|
[53] | 26 | voice = [aVoice retain];
|
---|
[34] | 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 |
|
---|
[53] | 39 | - (NSString *)voice;
|
---|
| 40 | {
|
---|
| 41 | return voice;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[34] | 44 | - (void)_stopSpeaking:(NSNotification *)notification;
|
---|
| 45 | {
|
---|
[53] | 46 | [speaker stopSpeaking]; // triggers didFinishSpeaking:
|
---|
[34] | 47 | }
|
---|
| 48 |
|
---|
[53] | 49 | - (void)triggerForAlarm:(PSAlarm *)anAlarm;
|
---|
[34] | 50 | {
|
---|
| 51 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_stopSpeaking:) name: PSAlarmAlertStopNotification object: nil];
|
---|
| 52 |
|
---|
[364] | 53 | if ( (speaker = [[NSSpeechSynthesizer alloc] initWithVoice: voice]) == nil) return;
|
---|
[53] | 54 | alarm = anAlarm;
|
---|
[34] | 55 | [speaker setDelegate: self];
|
---|
[364] | 56 | [speaker startSpeakingString: [alarm message]];
|
---|
[34] | 57 | }
|
---|
| 58 |
|
---|
[53] | 59 | - (NSAttributedString *)actionDescription;
|
---|
| 60 | {
|
---|
| 61 | NSMutableAttributedString *string = [[@"Speak message with voice " small] mutableCopy];
|
---|
[366] | 62 | NSString *voiceName = [[NSSpeechSynthesizer attributesForVoice: voice] objectForKey: NSVoiceName];
|
---|
| 63 | if (voiceName == nil)
|
---|
| 64 | voiceName = voice;
|
---|
| 65 | [string appendAttributedString: [voiceName underlined]];
|
---|
[53] | 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];
|
---|
[561] | 74 | if (voice == nil) {
|
---|
| 75 | // XXX should pick a different voice, not destroy the alert
|
---|
| 76 | [plAlert release];
|
---|
[364] | 77 | @throw [NSException exceptionWithName: NSInvalidArgumentException
|
---|
| 78 | reason: @"The selected voice is not available." userInfo: nil];
|
---|
[561] | 79 | }
|
---|
[53] | 80 | [plAlert setObject: voice forKey: PLAlertVoice];
|
---|
| 81 | return [plAlert autorelease];
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
| 85 | {
|
---|
| 86 | if ( (self = [self init]) != nil) {
|
---|
[102] | 87 | voice = [[dict objectForRequiredKey: PLAlertVoice] retain];
|
---|
[53] | 88 | }
|
---|
| 89 | return self;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[34] | 92 | @end
|
---|
| 93 |
|
---|
[364] | 94 | @implementation PSSpeechAlert (NSSpeechSynthesizerDelegate)
|
---|
[34] | 95 |
|
---|
[364] | 96 | - (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking;
|
---|
[34] | 97 | {
|
---|
[53] | 98 | [self completedForAlarm: alarm];
|
---|
| 99 | [speaker release]; speaker = nil;
|
---|
[34] | 100 | }
|
---|
| 101 |
|
---|
| 102 | @end |
---|