File indexing completed on 2024-04-14 04:01:47

0001 /*
0002     This file is part of the KDE games library
0003     Copyright (C) 2001 Burkhard Lehner (Burkhard.Lehner@gmx.de)
0004 
0005     This library is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU Library General Public
0007     License either version 2
0008    of the License, or (at your option) any later version.as published by the Free Software Foundation.
0009 
0010     This library is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013     Library General Public License for more details.
0014 
0015     You should have received a copy of the GNU Library General Public License
0016     along with this library; see the file COPYING.LIB.  If not, write to
0017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018     Boston, MA 02110-1301, USA.
0019 */
0020 
0021 /*
0022   subclass KMessageJabber of KMessageIO
0023 */
0024 
0025 #include "kmessagejabber.h"
0026 #include <QDataStream>
0027 #include <QUuid>
0028 
0029 // ----------------------KMessageJabber -----------------------
0030 
0031 KMessageJabber::KMessageJabber (const QString& peerJid, JabberClient* jabberClient, QObject *parent)
0032   : KMessageIO (parent),
0033   mClient(jabberClient),
0034   mPeerJid(peerJid)
0035 {
0036   qCDebug(JABBER_PROTOCOL_LOG) << peerJid;
0037 //   initSocket ();
0038   connect(jabberClient, &JabberClient::messageReceived, this, &KMessageJabber::slotMessageReceived);
0039          
0040   connect(jabberClient, &JabberClient::resourceUnavailable, this, &KMessageJabber::slotResourceUnavailable);
0041                   
0042   connect(jabberClient, &JabberClient::groupChatLeft, this, &KMessageJabber::slotGroupChatLeft);
0043 
0044   connect(jabberClient, &JabberClient::groupChatPresence, this, &KMessageJabber::slotGroupChatPresence);
0045 
0046   
0047   
0048 }
0049 
0050 KMessageJabber::~KMessageJabber ()
0051 {
0052 //   delete mClient;
0053 }
0054 
0055 bool KMessageJabber::isConnected () const
0056 {
0057   return mClient->isConnected ();
0058 }
0059 
0060 void KMessageJabber::send (const QByteArray &msg)
0061 {
0062   XMPP::Message message(mPeerJid);
0063   message.setType("ksirkgame");
0064   message.setId(QUuid::createUuid().toString().remove("{").remove("}").remove("-"));
0065   message.setBody(msg.toBase64());
0066   mClient->sendMessage(message);
0067 }
0068 
0069 void KMessageJabber::slotMessageReceived(const XMPP::Message &message)
0070 {
0071   if (message.from().full() == mPeerJid)
0072   {
0073     QByteArray msg = QByteArray::fromBase64(message.body().toLatin1());
0074     qCDebug(JABBER_PROTOCOL_LOG) << mPeerJid << msg;
0075     emit received (msg);
0076   }
0077 }
0078 
0079 QString KMessageJabber::peerName () const
0080 {
0081   return mPeerJid;
0082 }
0083 
0084 void KMessageJabber::slotGroupChatLeft(const XMPP::Message& msg)
0085 {
0086   qCDebug(JABBER_PROTOCOL_LOG) << msg.from().full() << msg.to().full() << msg.body();
0087 }
0088 
0089 void KMessageJabber::slotResourceUnavailable(const Jid& jid, const Resource& resource)
0090 {
0091   qCDebug(JABBER_PROTOCOL_LOG) << jid.full() << resource.name();
0092 }
0093 
0094 void KMessageJabber::slotGroupChatPresence(const XMPP::Jid& jid, const XMPP::Status& status)
0095 {
0096   qCDebug(JABBER_PROTOCOL_LOG) << jid.full() << status.status();
0097   if (jid.full() == mPeerJid && !status.isAvailable())
0098   {
0099     emit connectionBroken();
0100   }
0101 }
0102 
0103 #include "moc_kmessagejabber.cpp"