Port things from MSN to WLM plugin:
[kdenetwork.git] / kopete / protocols / telepathy / telepathychatsession.cpp
blob76204d96e111958175255393c5dbdf0f35e2e0af
1 /*
2 * telepathychatsession.cpp - Telepathy Chat Session.
4 * Copyright (c) 2006 by Michaƫl Larouche <larouche@kde.org>
5 *
6 * Kopete (c) 2002-2006 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 *************************************************************************
17 #include "telepathychatsession.h"
19 // Qt includes
20 #include <QtCore/QPointer>
22 // KDE includes
23 #include <kdebug.h>
24 #include <klocale.h>
26 // Kopete includes
27 #include <kopetechatsessionmanager.h>
29 // QtTapioca includes
30 #include <QtTapioca/Contact>
32 // Local includes
33 #include "telepathyprotocol.h"
34 #include "telepathyaccount.h"
35 #include "telepathycontact.h"
37 using namespace QtTapioca;
39 class TelepathyChatSession::Private
41 public:
42 QPointer<QtTapioca::TextChannel> textChannel;
45 TelepathyChatSession::TelepathyChatSession(const Kopete::Contact *user, Kopete::ContactPtrList others, Kopete::Protocol *protocol)
46 : Kopete::ChatSession(user, others, protocol), d(new Private)
48 Kopete::ChatSessionManager::self()->registerChatSession(this);
50 connect(this, SIGNAL(messageSent(Kopete::Message&, Kopete::ChatSession*)), this, SLOT(sendMessage(Kopete::Message&)));
53 TelepathyChatSession::~TelepathyChatSession()
55 kDebug(TELEPATHY_DEBUG_AREA) ;
57 // End text channel session
58 d->textChannel->close();
60 delete d;
63 QtTapioca::TextChannel* TelepathyChatSession::textChannel()
65 Q_ASSERT( !d->textChannel.isNull() );
67 return d->textChannel;
70 void TelepathyChatSession::setTextChannel(QtTapioca::TextChannel *textChannel)
72 // Disconnect previous signals connection
73 if( !d->textChannel.isNull() )
75 d->textChannel->disconnect();
78 d->textChannel = textChannel;
80 // Connect signal/slots
81 connect(d->textChannel, SIGNAL(messageReceived(QtTapioca::TextChannel::Message)), this, SLOT(telepathyMessageReceived(QtTapioca::TextChannel::Message)));
83 connect(d->textChannel, SIGNAL(messageDeliveryError(QtTapioca::TextChannel::Message, QtTapioca::TextChannel::Message::DeliveryError)), this, SLOT(telepathyMessageDeliveryError(QtTapioca::TextChannel::Message, QtTapioca::TextChannel::Message::DeliveryError)));
85 connect(d->textChannel, SIGNAL(messageSent(QtTapioca::TextChannel::Message)), this, SLOT(telepathyMessageSent(QtTapioca::TextChannel::Message)));
88 void TelepathyChatSession::telepathyMessageReceived(const QtTapioca::TextChannel::Message &message)
90 kDebug(TELEPATHY_DEBUG_AREA) ;
92 // Create a new Kopete::Message
93 Kopete::Message::MessageType messageType = Kopete::Message::TypeNormal;
95 if( message.type() == QtTapioca::TextChannel::Message::Action )
97 messageType = Kopete::Message::TypeAction;
100 Kopete::Message newMessage( members().first(), myself() );
101 newMessage.setPlainBody( message.contents() );
102 newMessage.setDirection( Kopete::Message::Inbound );
103 newMessage.setType( messageType );
105 appendMessage( newMessage );
108 void TelepathyChatSession::telepathyMessageSent(const QtTapioca::TextChannel::Message &message)
110 kDebug(TELEPATHY_DEBUG_AREA) << "Message contents: " << message.contents();
112 Kopete::Message::MessageType messageType = Kopete::Message::TypeNormal;
114 if( message.type() == QtTapioca::TextChannel::Message::Action )
116 messageType = Kopete::Message::TypeAction;
119 Kopete::Message newMessage( myself(), members() );
120 newMessage.setPlainBody( message.contents() );
121 newMessage.setDirection( Kopete::Message::Outbound );
122 newMessage.setType( messageType );
124 // Append successfully sent message to chat window and notify other components of success
125 appendMessage( newMessage );
126 messageSucceeded();
129 void TelepathyChatSession::telepathyMessageDeliveryError(const QtTapioca::TextChannel::Message &message, QtTapioca::TextChannel::Message::DeliveryError error)
131 kDebug(TELEPATHY_DEBUG_AREA) ;
132 QString internalErrorMessage, errorMessageText;
133 switch(error)
135 case TextChannel::Message::ContactOffline:
136 internalErrorMessage = i18n("Contact is offline.");
137 break;
138 case TextChannel::Message::InvalidContact:
139 internalErrorMessage = i18n("Contact is invalid.");
140 break;
141 case TextChannel::Message::PermissionDenied:
142 internalErrorMessage = i18n("You do not have permission to send a message to this contact.");
143 break;
144 case TextChannel::Message::MessageTooLong:
145 internalErrorMessage = i18n("Message is too long.");
146 break;
147 case TextChannel::Message::Unknown:
148 internalErrorMessage = i18n("Unknown reason");
149 break;
152 // The following message:
153 // "test
154 // testsfaefe"
155 // could not be delivered. Reason: Contact is offline.
156 errorMessageText = i18n("The following message:\n \"%1\"\ncould not be delivered. Reason: %2", message.contents(), internalErrorMessage);
158 Kopete::Message errorMessage( myself(), members() );
159 errorMessage.setPlainBody( errorMessageText );
160 errorMessage.setDirection( Kopete::Message::Internal );
162 appendMessage( errorMessage );
165 void TelepathyChatSession::sendMessage(Kopete::Message &message)
167 kDebug(TELEPATHY_DEBUG_AREA) << "Sending: " << message.plainBody();
169 // TODO: Support other type of message (when QtTapioca will support it)
170 QtTapioca::TextChannel::Message messageSend( message.plainBody() );
172 textChannel()->sendMessage( messageSend );
175 #include "telepathychatsession.moc"