Added a debug log to investigate #8685, as it worksforme. Refs #8685
[adiumx.git] / Source / ESFileTransferMessagesPlugin.m
blob3f87d815527d462e8d24dceb3f4334dad49565af
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 <Adium/AIChatControllerProtocol.h>
18 #import <Adium/AIContentControllerProtocol.h>
19 #import "ESFileTransferMessagesPlugin.h"
20 #import <Adium/AIChat.h>
21 #import <Adium/AIContentEvent.h>
22 #import <Adium/AIListContact.h>
23 #import <Adium/ESFileTransfer.h>
25 @interface ESFileTransferMessagesPlugin (PRIVATE)
26 - (void)statusMessage:(NSString *)message forContact:(AIListContact *)contact withType:(NSString *)type;
27 @end
29 /*!
30  * @class ESFileTransferMessagesPlugin
31  * @brief Component which handles sending file transfer status messages
32  */
33 @implementation ESFileTransferMessagesPlugin
35 /*!
36  * @brief Install
37  */
38 - (void)installPlugin
40         //Install our observers
41     [[adium notificationCenter] addObserver:self 
42                                                                    selector:@selector(handleFileTransferEvent:) 
43                                                                            name:FILE_TRANSFER_CANCELLED 
44                                                                          object:nil];
46         [[adium notificationCenter] addObserver:self 
47                                                                    selector:@selector(handleFileTransferEvent:) 
48                                                                            name:FILE_TRANSFER_COMPLETE 
49                                                                          object:nil];
50         
51         [[adium notificationCenter] addObserver:self 
52                                                                    selector:@selector(handleFileTransferEvent:) 
53                                                                            name:FILE_TRANSFER_WAITING_REMOTE 
54                                                                          object:nil];
55         
56         [[adium notificationCenter] addObserver:self 
57                                                                    selector:@selector(handleFileTransferEvent:) 
58                                                                            name:FILE_TRANSFER_BEGAN 
59                                                                          object:nil];
61         [[adium notificationCenter] addObserver:self 
62                                                                    selector:@selector(handleFileTransferEvent:) 
63                                                                            name:FILE_TRANSFER_FAILED 
64                                                                          object:nil];
67 /*!
68  * @brief Uninstall
69  */
70 - (void)uninstallPlugin
72         [[adium notificationCenter] removeObserver:self];
75 /*!
76  * @brief A file transfer event occurred
77  */
78 - (void)handleFileTransferEvent:(NSNotification *)notification
80         ESFileTransfer  *fileTransfer = (ESFileTransfer *)[notification userInfo];
82         if ([[fileTransfer account] accountDisplaysFileTransferMessages]) return;
84         AIListContact   *listContact = [notification object];
85         NSString                *notificationName = [notification name];
86         NSString                *filename;
88         if (!(filename = [[fileTransfer localFilename] lastPathComponent])) {
89                 filename = [[fileTransfer remoteFilename] lastPathComponent];
90         }
92         if (filename) {
93                 NSString                *message = nil;
94                 NSString                *type = nil;
96                 if ([notificationName isEqualToString:FILE_TRANSFER_CANCELLED]) {
97                         type = @"fileTransferAborted";
98                         message = [NSString stringWithFormat:AILocalizedString(@"%@ cancelled the transfer of %@",nil),[listContact formattedUID],filename];
100                 } else if ([notificationName isEqualToString:FILE_TRANSFER_FAILED]) {
101                         type = @"fileTransferAborted";
102                         message = [NSString stringWithFormat:AILocalizedString(@"The transfer of %@ failed",nil),filename];
103                 }else if ([notificationName isEqualToString:FILE_TRANSFER_COMPLETE]) {
104                         type = @"fileTransferCompleted";
105                         if ([fileTransfer fileTransferType] == Incoming_FileTransfer) {
106                                 message = [NSString stringWithFormat:AILocalizedString(@"Successfully received %@",nil),filename];
107                         } else {
108                                 message = [NSString stringWithFormat:AILocalizedString(@"Successfully sent %@",nil),filename];                  
109                         }
110                 } else if ([notificationName isEqualToString:FILE_TRANSFER_BEGAN]) {
111                         type = @"fileTransferStarted";
112                         if ([fileTransfer fileTransferType] == Incoming_FileTransfer) {
113                                 message = [NSString stringWithFormat:AILocalizedString(@"Began receiving %@",nil),filename];
114                         } else {
115                                 message = [NSString stringWithFormat:AILocalizedString(@"Began sending %@",nil),filename];                      
116                         }
117                 } else if ([notificationName isEqualToString:FILE_TRANSFER_WAITING_REMOTE]) {
118                         type = @"fileTransferWaitingRemote";
119                         //We should only receive this notification upon sending a file
120                         message = [NSString stringWithFormat:AILocalizedString(@"Offering to send %@ to %@",nil),filename,[listContact formattedUID]];
121                 }
123                 [self statusMessage:message forContact:listContact withType:type];
124         }
128  * @brief Post a status message on all active chats for this object
129  */
130 - (void)statusMessage:(NSString *)message forContact:(AIListContact *)contact withType:(NSString *)type
132     NSEnumerator                *enumerator;
133     AIChat                              *chat;
134         NSAttributedString      *attributedMessage = [[NSAttributedString alloc] initWithString:message
135                                                                                                                                                         attributes:[[adium contentController] defaultFormattingAttributes]];
136         
137     enumerator = [[[adium chatController] allChatsWithContact:contact] objectEnumerator];
138     while ((chat = [enumerator nextObject])) {
139         AIContentEvent  *content;
140                 
141         //Create our content object
142         content = [AIContentEvent statusInChat:chat
143                                                                         withSource:contact
144                                                                    destination:[chat account]
145                                                                                   date:[NSDate date]
146                                                                            message:attributedMessage
147                                                                           withType:type];
148                 
149         //Add the object
150         [[adium contentController] receiveContentObject:content];
151     }
152         
153         [attributedMessage release];
156 @end