Updated Address Book scripts to work with new AppleScript API.
[adiumx.git] / Source / OWSpellingPerContactPlugin.m
blob98be76abf114ffcfdfba9d6bdd81376fadfa5494
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 "OWSpellingPerContactPlugin.h"
18 #import <Adium/AIChat.h>
19 #import <Adium/AIListContact.h>
21 #define GROUP_LAST_USED_SPELLING        @"Last Used Spelling"
22 #define KEY_LAST_USED_SPELLING          @"Last Used Spelling Languge"
24 /*!
25  * @class OWSpellingPerContactPlugin
26  * @brief Component to save and restore spelling dictionary language preferences on a per-contact basis for chats
27  *
28  * Language settings on a group chat basis are not currently saved.
29  */
30 @implementation OWSpellingPerContactPlugin
32 /*!
33  * @brief Install
34  */
35 - (void)installPlugin
37         NSNotificationCenter *notificationCenter = [adium notificationCenter];
38         
39         [notificationCenter addObserver:self
40                                                    selector:@selector(chatBecameActive:)
41                                                            name:Chat_BecameActive
42                                                          object:nil];
43         
44         [notificationCenter addObserver:self
45                                                    selector:@selector(chatWillClose:)
46                                                            name:Chat_WillClose
47                                                          object:nil];
48         
49         languageDict = [[NSMutableDictionary alloc] init];
52 /*!
53  * @brief Uninstall
54  */
55 - (void)uninstallPlugin
57         [languageDict release]; languageDict = nil;
59         [[adium notificationCenter] removeObserver:self];
62 /*!
63  * @brief A chat became active; set the spelling language preference appropriately
64  *
65  * If a chat was previously active, as indicated by the PreviouslyActiveChat key in the notification's userInfo,
66  * its language preference is stored before switching.
67  */
68 - (void)chatBecameActive:(NSNotification *)notification
70         @try
71         {
72                 AIChat   *newChat = [notification object];
73                 AIChat   *previousChat = [[notification userInfo] objectForKey:@"PreviouslyActiveChat"];
75                 if (previousChat) {
76                         NSString *language = [[NSSpellChecker sharedSpellChecker] language];
77                         NSString *chatID = [previousChat uniqueChatID];
79                         if (language &&
80                                 ![[languageDict objectForKey:chatID] isEqualToString:language]) {
81                                 //If this chat is not known to be in the current language, store its setting in our languageDict
82                                 [languageDict setObject:language
83                                                                  forKey:chatID];
84                         }
85                 }
86                 
87                 if (newChat) {
88                         NSString *chatID = [newChat uniqueChatID];
89                         NSString *newChatLanguage = [languageDict objectForKey:chatID];
90                         
91                         //If we don't have a previously noted language, try to load one from a preference
92                         if (!newChatLanguage) {
93                                 AIListObject *listObject = [newChat listObject];
95                                 if (listObject) {
96                                         //Load the preference if possible
97                                         newChatLanguage = [listObject preferenceForKey:KEY_LAST_USED_SPELLING group:GROUP_LAST_USED_SPELLING];
98                                 }
100                                 if (!newChatLanguage) {
101                                         //If no preference, set to @"" so we won't keep trying to load the preference
102                                         newChatLanguage = @"";
103                                 }
105                                 [languageDict setObject:newChatLanguage
106                                                                  forKey:chatID];
107                         }
108                         
109                         if ([newChatLanguage length]) {
110                                 //Only set the language if we have one specified
111                                 [[NSSpellChecker sharedSpellChecker] setLanguage:newChatLanguage];
112                         }
113                 }
114         }
115         @catch(id exc) {}
119  * @brief Chat will close; save the language preference for its contact before it closes
120  */
121 - (void)chatWillClose:(NSNotification *)notification
123         AIChat           *chat = [notification object];
124         AIListObject *listObject = [chat listObject];
126         if (listObject) {
127                 NSString         *chatID = [chat uniqueChatID];
128                 NSString         *chatLanguage = [languageDict objectForKey:chatID];
130                 //If we didn't cache a language for this chat, we might just never have made it inactive; save the current language
131                 if (!chatLanguage) chatLanguage = [[NSSpellChecker sharedSpellChecker] language];
132                 
133                 //Save the last used language for this chat as it closes
134                 [listObject setPreference:chatLanguage
135                                                    forKey:KEY_LAST_USED_SPELLING
136                                                         group:GROUP_LAST_USED_SPELLING];
138                 [languageDict removeObjectForKey:chatID];
139         }
142 @end