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 "AdiumMessageEvents.h"
18 #import <Adium/AIContactAlertsControllerProtocol.h>
19 #import <Adium/AIChatControllerProtocol.h>
20 #import <Adium/AIContentControllerProtocol.h>
21 #import <Adium/AIChat.h>
22 #import <Adium/AIListContact.h>
23 #import <AIUtilities/AIAttributedStringAdditions.h>
24 #import <AIUtilities/AIImageAdditions.h>
26 @implementation AdiumMessageEvents
30 if ((self = [super init])) {
37 //Requires contactAlertController loaded
38 - (void)controllerDidLoad
40 //Register the events we generate
41 [[adium contactAlertsController] registerEventID:CONTENT_MESSAGE_SENT withHandler:self inGroup:AIMessageEventHandlerGroup globalOnly:NO];
42 [[adium contactAlertsController] registerEventID:CONTENT_MESSAGE_RECEIVED withHandler:self inGroup:AIMessageEventHandlerGroup globalOnly:NO];
43 [[adium contactAlertsController] registerEventID:CONTENT_MESSAGE_RECEIVED_FIRST withHandler:self inGroup:AIMessageEventHandlerGroup globalOnly:NO];
44 [[adium contactAlertsController] registerEventID:CONTENT_MESSAGE_RECEIVED_BACKGROUND withHandler:self inGroup:AIMessageEventHandlerGroup globalOnly:NO];
46 //Observe chat changes
47 [[adium chatController] registerChatObserver:self];
52 [[adium chatController] unregisterChatObserver:self];
57 #pragma mark Message event handling
58 - (NSSet *)updateChat:(AIChat *)inChat keys:(NSSet *)inModifiedKeys silent:(BOOL)silent
60 if (inModifiedKeys == nil ||
61 [inModifiedKeys containsObject:KEY_CHAT_TIMED_OUT] ||
62 [inModifiedKeys containsObject:KEY_CHAT_CLOSED_WINDOW] ||
63 [inModifiedKeys containsObject:KEY_CHAT_ERROR]) {
65 NSString *message = nil;
67 AIListContact *listObject = [inChat listObject];
69 if ([inChat statusObjectForKey:KEY_CHAT_ERROR] != nil) {
71 AIChatErrorType errorType = [inChat integerStatusObjectForKey:KEY_CHAT_ERROR];
75 case AIChatUnknownError:
76 message = [NSString stringWithFormat:AILocalizedString(@"Unknown conversation error.",nil)];
79 case AIChatMessageSendingUserNotAvailable:
80 message = [NSString stringWithFormat:AILocalizedString(@"Could not send because %@ is not available.",nil),[listObject formattedUID]];
83 case AIChatMessageSendingUserIsBlocked:
84 message = [NSString stringWithFormat:AILocalizedString(@"Could not send because %@ is blocked.",nil),[listObject formattedUID]];
87 case AIChatMessageSendingTooLarge:
88 message = AILocalizedString(@"Could not send the last message because it was too large.",nil);
91 case AIChatMessageSendingTimeOutOccurred:
92 message = AILocalizedString(@"A message may not have been sent; a timeout occurred.",nil);
95 case AIChatMessageSendingMissedRateLimitExceeded:
96 message = AILocalizedString(@"Could not send the last message because the rate limit has been exceeded. Please wait a moment and then try again.",nil);
99 case AIChatMessageReceivingMissedTooLarge:
100 message = AILocalizedString(@"Could not receive the last message because it was too large.",nil);
103 case AIChatMessageReceivingMissedInvalid:
104 message = AILocalizedString(@"Could not receive the last message because it was invalid.",nil);
107 case AIChatMessageReceivingMissedRateLimitExceeded:
108 message = AILocalizedString(@"Could not receive the last message because the rate limit has been exceeded. Please wait a moment and then try again.",nil);
111 case AIChatMessageReceivingMissedRemoteIsTooEvil:
112 message = [NSString stringWithFormat:AILocalizedString(@"Could not receive; %@ is too evil.",nil),[listObject formattedUID]];
115 case AIChatMessageReceivingMissedLocalIsTooEvil:
116 message = AILocalizedString(@"Could not receive: you are too evil.",nil);
119 case AIChatCommandFailed:
120 message = AILocalizedString(@"Command failed.",nil);
123 case AIChatInvalidNumberOfArguments:
124 message = AILocalizedString(@"Incorrect number of command argments.",nil);
127 case AIChatMessageSendingConnectionError:
128 message = AILocalizedString(@"Could not send; a connection error occurred.",nil);
131 case AIChatMessageSendingNotAllowedWhileInvisible:
132 message = AILocalizedString(@"Could not send; not allowed while invisible.",nil);
136 } else if ([inChat integerStatusObjectForKey:KEY_CHAT_CLOSED_WINDOW] && listObject) {
137 message = [NSString stringWithFormat:AILocalizedString(@"%@ closed the conversation window.",nil),[listObject displayName]];
139 } else if ([inChat integerStatusObjectForKey:KEY_CHAT_TIMED_OUT] && listObject) {
140 message = [NSString stringWithFormat:AILocalizedString(@"The conversation with %@ timed out.",nil),[listObject displayName]];
145 [[adium contentController] displayEvent:message
154 #pragma mark Event descriptions
155 - (NSString *)shortDescriptionForEventID:(NSString *)eventID
157 NSString *description;
159 if ([eventID isEqualToString:CONTENT_MESSAGE_SENT]) {
160 description = AILocalizedString(@"Is sent a message",nil);
161 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED]) {
162 description = AILocalizedString(@"Sends a message",nil);
163 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_FIRST]) {
164 description = AILocalizedString(@"Sends an initial message",nil);
165 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_BACKGROUND]) {
166 description = AILocalizedString(@"Sends a message in a background chat",nil);
174 - (NSString *)globalShortDescriptionForEventID:(NSString *)eventID
176 NSString *description;
178 if ([eventID isEqualToString:CONTENT_MESSAGE_SENT]) {
179 description = AILocalizedString(@"Message sent",nil);
180 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED]) {
181 description = AILocalizedString(@"Message received",nil);
182 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_FIRST]) {
183 description = AILocalizedString(@"Message received (Initial)",nil);
184 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_BACKGROUND]) {
185 description = AILocalizedString(@"Message received (Background chat)",nil);
193 //Evan: This exists because old X(tras) relied upon matching the description of event IDs, and I don't feel like making
194 //a converter for old packs. If anyone wants to fix this situation, please feel free :)
195 - (NSString *)englishGlobalShortDescriptionForEventID:(NSString *)eventID
197 NSString *description;
199 if ([eventID isEqualToString:CONTENT_MESSAGE_SENT]) {
200 description = @"Message Sent";
201 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED]) {
202 description = @"Message Received";
203 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_FIRST]) {
204 description = @"Message Received (New)";
205 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_BACKGROUND]) {
206 description = @"Message Received (Background Chat)";
214 - (NSString *)longDescriptionForEventID:(NSString *)eventID forListObject:(AIListObject *)listObject
216 NSString *description = nil;
222 if ([eventID isEqualToString:CONTENT_MESSAGE_SENT]) {
223 format = AILocalizedString(@"When you send %@ a message",nil);
224 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED]) {
225 format = AILocalizedString(@"When %@ sends a message to you",nil);
226 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_FIRST]) {
227 format = AILocalizedString(@"When %@ sends an initial message to you",nil);
228 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_BACKGROUND]) {
229 format = AILocalizedString(@"When %@ sends a message to you in a background chat",nil);
235 name = ([listObject isKindOfClass:[AIListGroup class]] ?
236 [NSString stringWithFormat:AILocalizedString(@"a member of %@",nil),[listObject displayName]] :
237 [listObject displayName]);
239 description = [NSString stringWithFormat:format, name];
243 if ([eventID isEqualToString:CONTENT_MESSAGE_SENT]) {
244 description = AILocalizedString(@"When you send a message",nil);
245 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED]) {
246 description = AILocalizedString(@"When you receive any message",nil);
247 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_FIRST]) {
248 description = AILocalizedString(@"When you receive an initial message",nil);
249 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_BACKGROUND]) {
250 description = AILocalizedString(@"When you receive a message in a background chat",nil);
259 - (NSString *)naturalLanguageDescriptionForEventID:(NSString *)eventID
260 listObject:(AIListObject *)listObject
261 userInfo:(id)userInfo
262 includeSubject:(BOOL)includeSubject
264 NSString *description = nil;
265 AIContentObject *contentObject;
266 NSString *messageText;
267 NSString *displayName;
269 NSParameterAssert([userInfo isKindOfClass:[NSDictionary class]]);
271 contentObject = [(NSDictionary *)userInfo objectForKey:@"AIContentObject"];
272 messageText = [[[contentObject message] attributedStringByConvertingAttachmentsToStrings] string];
274 if (includeSubject) {
276 if ([eventID isEqualToString:CONTENT_MESSAGE_SENT]) {
277 displayName = (listObject ? [listObject displayName] : [[contentObject chat] name]);
279 if (messageText && [messageText length]) {
280 description = [NSString stringWithFormat:
281 AILocalizedString(@"You said %@ to %@","You said Message to Contact"),
286 description = [NSString stringWithFormat:
287 AILocalizedString(@"You sent a message to %@","You sent a message to Contact"),
291 } else if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED] ||
292 [eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_FIRST] ||
293 [eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_BACKGROUND]) {
294 displayName = (listObject ? [listObject displayName] : [[contentObject source] displayName]);
296 if (messageText && [messageText length]) {
297 description = [NSString stringWithFormat:
298 AILocalizedString(@"%@ said %@","Contact said Message"),
303 description = [NSString stringWithFormat:
304 AILocalizedString(@"%@ sent you a message","Contact sent you a message"),
310 if (messageText && [messageText length]) {
311 description = messageText;
313 if ([eventID isEqualToString:CONTENT_MESSAGE_RECEIVED] ||
314 [eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_FIRST] ||
315 [eventID isEqualToString:CONTENT_MESSAGE_RECEIVED_BACKGROUND]) {
316 //Use the message received text for all message received events if we don't have a message
317 description = [self globalShortDescriptionForEventID:CONTENT_MESSAGE_RECEIVED];
319 description = [self globalShortDescriptionForEventID:eventID];
327 - (NSImage *)imageForEventID:(NSString *)eventID
329 static NSImage *eventImage = nil;
330 //Use the message icon from the main bundle
331 if (!eventImage) eventImage = [[NSImage imageNamed:@"message"] retain];