Verified Commit a4945e3a authored by Linus Jahn's avatar Linus Jahn 🍙

MessageHandler: Use Carbons in QXmpp v0.9.4 or later

parent 04275bdf
......@@ -36,6 +36,10 @@
#include <QXmppClient.h>
#include <QXmppUtils.h>
#include <QXmppRosterManager.h>
#include <QXmppDiscoveryManager.h>
#if QXMPP_VERSION >= 0x000904
#include <QXmppCarbonManager.h>
#endif
// Kaidan
#include "Kaidan.h"
#include "MessageModel.h"
......@@ -53,6 +57,33 @@ MessageHandler::MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel
[=] (const QString&, const QString &id) {
emit model->setMessageAsDeliveredRequested(id);
});
#if QXMPP_VERSION >= 0x000904 // QXmppCarbonManager was added in v0.9.4
carbonManager = new QXmppCarbonManager();
client->addExtension(carbonManager);
// messages sent to our account (forwarded from another client)
connect(carbonManager, &QXmppCarbonManager::messageReceived,
client, &QXmppClient::messageReceived);
// messages sent from our account (but another client)
connect(carbonManager, &QXmppCarbonManager::messageSent,
client, &QXmppClient::messageReceived);
// carbons discovery
QXmppDiscoveryManager *discoManager = client->findExtension<QXmppDiscoveryManager>();
if (!discoManager)
return;
connect(discoManager, &QXmppDiscoveryManager::infoReceived,
this, &MessageHandler::handleDiscoInfo);
#endif
}
MessageHandler::~MessageHandler()
{
#if QXMPP_VERSION >= 0x000904
delete carbonManager;
#endif
}
void MessageHandler::handleMessage(const QXmppMessage &msg)
......@@ -126,3 +157,13 @@ void MessageHandler::sendMessage(QString toJid, QString body)
client->sendPacket(m);
}
void MessageHandler::handleDiscoInfo(const QXmppDiscoveryIq &info)
{
#if QXMPP_VERSION >= 0x000904
if (info.from() != client->configuration().domain())
return;
// enable carbons, if feature found
if (info.features().contains(QString("urn:xmpp:carbons:2")))
carbonManager->setCarbonsEnabled(true);
#endif
}
......@@ -34,6 +34,7 @@
// Qt
#include <QObject>
// QXmpp
#include <QXmppGlobal.h>
#include <QXmppMessage.h>
#include <QXmppMessageReceiptManager.h>
// Kaidan
......@@ -42,6 +43,10 @@
class Kaidan;
class MessageModel;
class QMimeType;
class QXmppDiscoveryIq;
#if QXMPP_VERSION >= 0x000904
class QXmppCarbonManager;
#endif
using namespace Enums;
......@@ -56,6 +61,8 @@ public:
MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel *model,
QObject *parent = nullptr);
~MessageHandler();
public slots:
/**
* Handles incoming messages from the server
......@@ -67,11 +74,19 @@ public slots:
*/
void sendMessage(QString toJid, QString body);
/**
* Handles service discovery info and enables carbons if feature was found
*/
void handleDiscoInfo(const QXmppDiscoveryIq &);
private:
Kaidan *kaidan;
QXmppClient *client;
QXmppMessageReceiptManager receiptManager;
MessageModel *model;
#if QXMPP_VERSION >= 0x000904
QXmppCarbonManager *carbonManager;
#endif
QString chatPartner;
};
......
......@@ -164,8 +164,10 @@ void RosterManager::handleMessage(const QXmppMessage &msg)
QString dateTime = QDateTime::currentDateTime().toUTC().toString(Qt::ISODate);
emit model->setLastExchangedRequested(contactJid, dateTime);
// update unread message counter
if (!sentByMe && chatPartner != contactJid)
// when we sent a message we can ignore all unread message notifications
if (sentByMe)
emit model->setUnreadMessageCountRequested(contactJid, 0);
// update unread message counter, if chat is not active
else if (!sentByMe && chatPartner != contactJid)
emit model->newUnreadMessageRequested(contactJid);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment