2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
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.
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.
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.
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"
25 * @class OWSpellingPerContactPlugin
26 * @brief Component to save and restore spelling dictionary language preferences on a per-contact basis for chats
28 * Language settings on a group chat basis are not currently saved.
30 @implementation OWSpellingPerContactPlugin
37 NSNotificationCenter *notificationCenter = [adium notificationCenter];
39 [notificationCenter addObserver:self
40 selector:@selector(chatBecameActive:)
41 name:Chat_BecameActive
44 [notificationCenter addObserver:self
45 selector:@selector(chatWillClose:)
49 languageDict = [[NSMutableDictionary alloc] init];
55 - (void)uninstallPlugin
57 [languageDict release]; languageDict = nil;
59 [[adium notificationCenter] removeObserver:self];
63 * @brief A chat became active; set the spelling language preference appropriately
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.
68 - (void)chatBecameActive:(NSNotification *)notification
72 AIChat *newChat = [notification object];
73 AIChat *previousChat = [[notification userInfo] objectForKey:@"PreviouslyActiveChat"];
76 NSString *language = [[NSSpellChecker sharedSpellChecker] language];
77 NSString *chatID = [previousChat uniqueChatID];
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
88 NSString *chatID = [newChat uniqueChatID];
89 NSString *newChatLanguage = [languageDict objectForKey:chatID];
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];
96 //Load the preference if possible
97 newChatLanguage = [listObject preferenceForKey:KEY_LAST_USED_SPELLING group:GROUP_LAST_USED_SPELLING];
100 if (!newChatLanguage) {
101 //If no preference, set to @"" so we won't keep trying to load the preference
102 newChatLanguage = @"";
105 [languageDict setObject:newChatLanguage
109 if ([newChatLanguage length]) {
110 //Only set the language if we have one specified
111 [[NSSpellChecker sharedSpellChecker] setLanguage:newChatLanguage];
119 * @brief Chat will close; save the language preference for its contact before it closes
121 - (void)chatWillClose:(NSNotification *)notification
123 AIChat *chat = [notification object];
124 AIListObject *listObject = [chat 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];
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];