[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];
|
---|
[364] | 74 | if (voice == nil)
|
---|
| 75 | @throw [NSException exceptionWithName: NSInvalidArgumentException
|
---|
| 76 | reason: @"The selected voice is not available." userInfo: nil];
|
---|
[53] | 77 | [plAlert setObject: voice forKey: PLAlertVoice];
|
---|
| 78 | return [plAlert autorelease];
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
| 82 | {
|
---|
| 83 | if ( (self = [self init]) != nil) {
|
---|
[102] | 84 | voice = [[dict objectForRequiredKey: PLAlertVoice] retain];
|
---|
[53] | 85 | }
|
---|
| 86 | return self;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[34] | 89 | @end
|
---|
| 90 |
|
---|
[364] | 91 | @implementation PSSpeechAlert (NSSpeechSynthesizerDelegate)
|
---|
[34] | 92 |
|
---|
[364] | 93 | - (void)speechSynthesizer:(NSSpeechSynthesizer *)sender didFinishSpeaking:(BOOL)finishedSpeaking;
|
---|
[34] | 94 | {
|
---|
[53] | 95 | [self completedForAlarm: alarm];
|
---|
| 96 | [speaker release]; speaker = nil;
|
---|
[34] | 97 | }
|
---|
| 98 |
|
---|
| 99 | @end |
---|