AIHyperlinks universal building
[adiumx.git] / Source / ESAnnouncerAbstractDetailPane.m
blob5390633095d4e8cbd4e603d06c3214250c169b68
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "AIPreferenceController.h"
18 #import "AISoundController.h"
19 #import "ESAnnouncerAbstractDetailPane.h"
20 #import "ESAnnouncerPlugin.h"
21 #import "ESContactAlertsController.h"
22 #import <AIUtilities/AIMenuAdditions.h>
23 #import <Adium/AILocalizationButton.h>
25 @interface ESAnnouncerAbstractDetailPane (PRIVATE)
26 - (NSMenu *)voicesMenu;
27 @end
29 /*!
30  * @class ESAnnouncerAbstractDetailPane
31  * @brief Abstract superclass for Announcer action (Speak Event and Speak Text) detail panes
32  */
33 @implementation ESAnnouncerAbstractDetailPane
35 /*!
36  * @brief View did load
37  */
38 - (void)viewDidLoad
40         [super viewDidLoad];
41         
42         [checkBox_speakEventTime setTitle:SPEAK_EVENT_TIME];
43         [checkBox_speakContactName setLocalizedString:AILocalizedString(@"Speak Name",nil)];
44         [checkBox_customPitch setLocalizedString:AILocalizedString(@"Use custom pitch:",nil)];
45         [checkBox_customRate setLocalizedString:AILocalizedString(@"Use custom rate:",nil)];
47         [popUp_voices setMenu:[self voicesMenu]];
50 /*!
51  * @brief Configure for the action
52  */
53 - (void)configureForActionDetails:(NSDictionary *)inDetails listObject:(AIListObject *)inObject
55         BOOL            speakTime, speakContactName;
56         NSString        *voice;
57         NSNumber        *pitchNumber, *rateNumber;
59         if(!inDetails) inDetails = [[adium preferenceController] preferenceForKey:[self defaultDetailsKey]
60                                                                                                                                                 group:PREF_GROUP_ANNOUNCER];
62         speakTime = [[inDetails objectForKey:KEY_ANNOUNCER_TIME] boolValue];
63         speakContactName = [[inDetails objectForKey:KEY_ANNOUNCER_SENDER] boolValue];
65     if(voice = [inDetails objectForKey:KEY_VOICE_STRING]) {
66         [popUp_voices selectItemWithTitle:voice];
67     } else {
68         [popUp_voices selectItemAtIndex:0]; //"Default"
69     }
70         
71     if(pitchNumber = [inDetails objectForKey:KEY_PITCH]) {
72                 [slider_pitch setFloatValue:[pitchNumber floatValue]];
73     } else {
74                 [slider_pitch setFloatValue:[[adium soundController] defaultPitch]];
75     }
76         
77         [checkBox_customPitch setState:[[inDetails objectForKey:KEY_PITCH_CUSTOM] boolValue]];
78         
79     if(rateNumber = [inDetails objectForKey:KEY_RATE]) {
80                 [slider_rate setFloatValue:[rateNumber floatValue]];
81     } else {
82                 [slider_rate setFloatValue:[[adium soundController] defaultRate]];
83     }
85         [checkBox_customRate setState:[[inDetails objectForKey:KEY_RATE_CUSTOM] boolValue]];
87         [checkBox_speakEventTime setState:speakTime];
88         [checkBox_speakContactName setState:speakContactName];
89         
90         [self configureControlDimming];
93 - (void)configureControlDimming
95         [super configureControlDimming];
96         
97         [slider_rate setEnabled:[checkBox_customRate state]];
98         [slider_pitch setEnabled:[checkBox_customPitch state]];
102  * @brief Configure controls specially for message events.
104  * Speaking of the name is only disable-able for message events.
105  */
106 - (void)configureForEventID:(NSString *)eventID listObject:(AIListObject *)inObject
108         if([[adium contactAlertsController] isMessageEvent:eventID]){
109                 [checkBox_speakContactName setEnabled:YES];
110         }else{
111                 [checkBox_speakContactName setEnabled:NO];
112                 [checkBox_speakContactName setState:NSOnState];
113         }
117  * @brief Return action details
119  * Should be overridden, with the subclass returning [self actionDetailsDromDict:actionDetails]
120  * where actionDetails is the dictionary of what it itself needs to store
121  */
122 - (NSDictionary *)actionDetails
124         NSDictionary    *actionDetails = [self actionDetailsFromDict:nil];
126         //Save the preferred settings for future use as defaults
127         [[adium preferenceController] setPreference:actionDetails
128                                                                                  forKey:[self defaultDetailsKey]
129                                                                                   group:PREF_GROUP_ANNOUNCER];
131         return(actionDetails);
135  * @brief Used by subclasses; adds the general information managed by the superclass to the details dictionary.
136  */
137 - (NSDictionary *)actionDetailsFromDict:(NSMutableDictionary *)actionDetails
139         NSNumber                *speakTime, *speakContactName, *pitch, *rate;
140         NSString                *voice;
142         if(!actionDetails) actionDetails = [NSMutableDictionary dictionary];
144         speakTime = [NSNumber numberWithBool:([checkBox_speakEventTime state] == NSOnState)];
145         speakContactName = [NSNumber numberWithBool:([checkBox_speakContactName state] == NSOnState)];
147         voice = [[popUp_voices selectedItem] representedObject];        
148         pitch = [NSNumber numberWithFloat:[slider_pitch floatValue]];
149         rate = [NSNumber numberWithFloat:[slider_rate floatValue]];
150         
151         if(voice){
152                 [actionDetails setObject:voice
153                                                   forKey:KEY_VOICE_STRING];
154         }
156         if([pitch floatValue] != [[adium soundController] defaultPitch]){
157                 [actionDetails setObject:pitch
158                                                   forKey:KEY_PITCH];
159         }
160         
161         if([rate floatValue] != [[adium soundController] defaultRate]){
162                 [actionDetails setObject:rate
163                                                   forKey:KEY_RATE];
164         }
165         [actionDetails setObject:[NSNumber numberWithBool:[checkBox_customRate state]]
166                                           forKey:KEY_RATE_CUSTOM];
167         [actionDetails setObject:[NSNumber numberWithBool:[checkBox_customPitch state]]
168                                           forKey:KEY_PITCH_CUSTOM];
169         
170         [actionDetails setObject:speakTime
171                                           forKey:KEY_ANNOUNCER_TIME];
172         [actionDetails setObject:speakContactName
173                                           forKey:KEY_ANNOUNCER_SENDER];
174         
175         return actionDetails;
179  * @brief Key on which to store our defaults
181  * Must be overridden by subclasses
182  */
183 - (NSString *)defaultDetailsKey
185         return nil;
189  * @brief Speech voices menu
190  */
191 - (NSMenu *)voicesMenu
193         NSArray                 *voicesArray;
194         NSMenu                  *voicesMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] init];
195         NSMenuItem              *menuItem;
196         NSEnumerator    *enumerator;
197         NSString                *voice;
198         
199         menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:AILocalizedString(@"Use System Default",nil)
200                                                                                                                                          target:nil
201                                                                                                                                          action:nil
202                                                                                                                           keyEquivalent:@""] autorelease];
203         [voicesMenu addItem:menuItem];
204         [voicesMenu addItem:[NSMenuItem separatorItem]];
206         voicesArray = [[[adium soundController] voices] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
207         enumerator = [voicesArray objectEnumerator];
208         while(voice = [enumerator nextObject]){
209                 menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:voice
210                                                                                                                                                                           target:nil
211                                                                                                                                                                           action:nil
212                                                                                                                                                            keyEquivalent:@""] autorelease];
213                 [menuItem setRepresentedObject:voice];
214                 [voicesMenu addItem:menuItem];
215         }
216         
217         return(voicesMenu);
221  * @brief Preference changed
222  */
223 -(IBAction)changePreference:(id)sender
225         //If the Default voice is selected, also set the pitch and rate to defaults
226         if(sender == popUp_voices){
227                 if(![[popUp_voices selectedItem] representedObject]){
228                         [slider_pitch setFloatValue:[[adium soundController] defaultPitch]];
229                         [slider_rate setFloatValue:[[adium soundController] defaultRate]];
230                 }
231         }
233         if(sender == popUp_voices ||
234            sender == slider_pitch ||
235            sender == slider_rate){
236                 [[adium soundController] speakDemoTextForVoice:[[popUp_voices selectedItem] representedObject]
237                                                                                          withPitch:[slider_pitch floatValue]
238                                                                                            andRate:[slider_rate floatValue]];
239         }
240         
241         [super changePreference:sender];
244 @end