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 "AIStatusChangedMessagesPlugin.h"
18 #import "AIContentController.h"
19 #import "ESContactAlertsController.h"
20 #import <Adium/AIListContact.h>
21 #import <Adium/AIChat.h>
22 #import <Adium/AIContentStatus.h>
24 @interface AIStatusChangedMessagesPlugin (PRIVATE)
25 - (void)statusMessage:(NSString *)message forContact:(AIListContact *)contact
26 withType:(NSString *)type
27 phraseWithoutSubject:(NSString *)statusPhrase
28 inChats:(NSSet *)inChats;
32 * @class AIStatusChangedMessagesPlugin
33 * @brief Generate <tt>AIContentStatus</tt> messages in open chats in response to contact status changes
35 @implementation AIStatusChangedMessagesPlugin
37 static NSDictionary *statusTypeDict = nil;
44 statusTypeDict = [[NSDictionary dictionaryWithObjectsAndKeys:
45 @"away",CONTACT_STATUS_AWAY_YES,
46 @"return_away",CONTACT_STATUS_AWAY_NO,
47 @"online",CONTACT_STATUS_ONLINE_YES,
48 @"offline",CONTACT_STATUS_ONLINE_NO,
49 @"idle",CONTACT_STATUS_IDLE_YES,
50 @"return_idle",CONTACT_STATUS_IDLE_NO,
51 @"away_message",CONTACT_STATUS_MESSAGE,
54 //Observe contact status changes
55 [[adium notificationCenter] addObserver:self selector:@selector(contactStatusChanged:) name:CONTACT_STATUS_ONLINE_YES object:nil];
56 [[adium notificationCenter] addObserver:self selector:@selector(contactStatusChanged:) name:CONTACT_STATUS_ONLINE_NO object:nil];
57 [[adium notificationCenter] addObserver:self selector:@selector(contactStatusChanged:) name:CONTACT_STATUS_IDLE_YES object:nil];
58 [[adium notificationCenter] addObserver:self selector:@selector(contactStatusChanged:) name:CONTACT_STATUS_IDLE_NO object:nil];
60 [[adium notificationCenter] addObserver:self selector:@selector(contactAwayChanged:) name:CONTACT_STATUS_AWAY_YES object:nil];
61 [[adium notificationCenter] addObserver:self selector:@selector(contactAwayChanged:) name:CONTACT_STATUS_AWAY_NO object:nil];
62 [[adium notificationCenter] addObserver:self selector:@selector(contact_statusMessage:) name:CONTACT_STATUS_MESSAGE object:nil];
66 * @brief Notification a changed status message
68 * @param notification <tt>NSNotification</tt> whose object is the AIListContact
70 - (void)contact_statusMessage:(NSNotification *)notification{
72 AIListContact *contact = [notification object];
74 allChats = [[adium contentController] allChatsWithContact:contact];
76 if([contact statusType] != AIAvailableStatusType){
77 NSString *statusMessage = [[contact statusMessage] string];
78 NSString *statusType = [statusTypeDict objectForKey:CONTACT_STATUS_MESSAGE];
80 if(statusMessage && [statusMessage length] != 0){
81 [self statusMessage:[NSString stringWithFormat:AILocalizedString(@"Away Message: %@",nil),statusMessage]
84 phraseWithoutSubject:statusMessage
92 * @brief Contact status changed notification
94 * @param notification <tt>NSNotification</tt> whose object is the AIListContact and whose name is the eventID
96 - (void)contactStatusChanged:(NSNotification *)notification{
98 AIListContact *contact = [notification object];
100 allChats = [[adium contentController] allChatsWithContact:contact];
101 if([allChats count]){
102 NSString *description, *phraseWithoutSubject;
103 NSString *name = [notification name];
104 NSDictionary *userInfo = [notification userInfo];
106 description = [[adium contactAlertsController] naturalLanguageDescriptionForEventID:name
110 phraseWithoutSubject = [[adium contactAlertsController] naturalLanguageDescriptionForEventID:name
114 [self statusMessage:description
116 withType:[statusTypeDict objectForKey:name]
117 phraseWithoutSubject:phraseWithoutSubject
123 * @brief Special handling for away changes
125 * We only display the "Went away" message if a status message for the away hasn't already been printed
127 - (void)contactAwayChanged:(NSNotification *)notification
129 NSDictionary *userInfo = [notification userInfo];
131 if(![[userInfo objectForKey:@"Already Posted StatusMessage"] boolValue]){
132 [self contactStatusChanged:notification];
137 * @brief Post a status message on all active chats for this object
139 - (void)statusMessage:(NSString *)message forContact:(AIListContact *)contact
140 withType:(NSString *)type
141 phraseWithoutSubject:(NSString *)statusPhrase
142 inChats:(NSSet *)inChats
144 NSEnumerator *enumerator;
146 NSAttributedString *attributedMessage = [[[NSAttributedString alloc] initWithString:message
147 attributes:[[adium contentController] defaultFormattingAttributes]] autorelease];
149 enumerator = [inChats objectEnumerator];
150 while((chat = [enumerator nextObject])){
151 AIContentStatus *content;
153 //Create our content object
154 content = [AIContentStatus statusInChat:chat
156 destination:[chat account]
158 message:attributedMessage
162 NSDictionary *userInfo = [NSDictionary dictionaryWithObject:statusPhrase
163 forKey:@"Status Phrase"];
164 [content setUserInfo:userInfo];
168 [[adium contentController] receiveContentObject:content];