...
 
Commits (2)
......@@ -59,7 +59,8 @@ ClientWorker::ClientWorker(Caches *caches, Kaidan *kaidan, bool enableLogging, Q
vCardManager = new VCardManager(client, caches->avatarStorage, this);
rosterManager = new RosterManager(kaidan, client, caches->rosterModel,
caches->avatarStorage, vCardManager, this);
msgHandler = new MessageHandler(kaidan, client, caches->msgModel, this);
msgHandler = new MessageHandler(kaidan, client, caches->msgModel,
caches->settings, this);
discoManager = new DiscoveryManager(client, this);
uploadManager = new UploadManager(kaidan, client, caches->msgModel, rosterManager,
caches->transferCache, this);
......
......@@ -39,7 +39,7 @@
#include <QXmppUtils.h>
#include <QXmppRosterManager.h>
#include <QXmppDiscoveryManager.h>
#if QXMPP_VERSION >= 0x000904
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
#include <QXmppCarbonManager.h>
#endif
// Kaidan
......@@ -48,8 +48,9 @@
#include "Notifications.h"
MessageHandler::MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel *model,
QObject *parent)
: QObject(parent), kaidan(kaidan), client(client), model(model)
QSettings *settings, QObject *parent)
: QObject(parent), kaidan(kaidan), client(client), model(model),
settings(settings)
{
connect(client, &QXmppClient::messageReceived, this, &MessageHandler::handleMessage);
connect(kaidan, &Kaidan::sendMessage, this, &MessageHandler::sendMessage);
......@@ -61,7 +62,7 @@ MessageHandler::MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel
emit model->setMessageAsDeliveredRequested(id);
});
#if QXMPP_VERSION >= 0x000904 // QXmppCarbonManager was added in v0.9.4
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
carbonManager = new QXmppCarbonManager();
client->addExtension(carbonManager);
......@@ -79,13 +80,24 @@ MessageHandler::MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel
connect(discoManager, &QXmppDiscoveryManager::infoReceived,
this, &MessageHandler::handleDiscoInfo);
// MAM manager
mamManager = new QXmppMamManager;
client->addExtension(mamManager);
connect(mamManager, &QXmppMamManager::archivedMessageReceived,
this, &MessageHandler::handleArchiveMessage);
connect(mamManager, &QXmppMamManager::resultsRecieved,
this, &MessageHandler::handleArchiveResults);
connect(&client->rosterManager(), &QXmppRosterManager::rosterReceived,
this, &MessageHandler::requestLatestMessages);
#endif
}
MessageHandler::~MessageHandler()
{
#if QXMPP_VERSION >= 0x000904
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
delete carbonManager;
delete mamManager;
#endif
}
......@@ -141,6 +153,12 @@ void MessageHandler::handleMessage(const QXmppMessage &msg)
stamp = QDateTime::currentDateTime();
entry.timestamp = stamp.toUTC().toString(Qt::ISODate);
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
// if the received message is newer than the currently latest message, update
if (settings->value("cache/latest-message").toDateTime() < stamp)
settings->setValue("cache/latest-message", entry.timestamp);
#endif
// save the message to the database
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
// in case of message correction, replace old message
......@@ -201,6 +219,10 @@ void MessageHandler::sendMessage(QString toJid, QString body)
if (success)
emit model->setMessageAsSentRequested(msg.id);
// TODO: handle error
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
settings->setValue("cache/latest-message", msg.timestamp);
#endif
}
void MessageHandler::correctMessage(QString toJid, QString msgId, QString body)
......@@ -244,7 +266,7 @@ void MessageHandler::correctMessage(QString toJid, QString msgId, QString body)
void MessageHandler::handleDiscoInfo(const QXmppDiscoveryIq &info)
{
#if QXMPP_VERSION >= 0x000904
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
if (info.from() != client->configuration().domain())
return;
// enable carbons, if feature found
......@@ -252,3 +274,56 @@ void MessageHandler::handleDiscoInfo(const QXmppDiscoveryIq &info)
carbonManager->setCarbonsEnabled(true);
#endif
}
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
void MessageHandler::handleArchiveMessage(const QString&,
const QXmppMessage &message)
{
// emit client->messageReceived(message);
handleMessage(message);
}
void MessageHandler::handleArchiveResults(const QString &queryId,
const QXmppResultSetReply &resultSetReply,
bool complete)
{
QDateTime latestMessage = settings->value("cache/latest-message").toDateTime();
if (!jidsToQuery.isEmpty()) {
QTimer::singleShot(500, this, [this]() {
QXmppResultSetQuery queryLimit;
queryLimit.setBefore(""); // query last (newest) first
queryLimit.setMax(10);
QString jid = jidsToQuery.takeFirst();
mamManager->retrieveArchivedMessages(QString(), QString(), jid,
QDateTime(),
QDateTime::currentDateTimeUtc(),
queryLimit);
});
}
// if (resultSetReply.count() > 0)
// mamManager->retrieveArchivedMessages("", "", "", latestMessage, QDateTime({2100, 01, 01}, {00, 00}));
}
void MessageHandler::requestLatestMessages()
{
QDateTime latestMessage = settings->value("cache/latest-message").toDateTime();
if (latestMessage == QDateTime()) {
qDebug() << "[MessageHandler] Loading latest 20 messages for each "
"contact.";
QXmppResultSetQuery queryLimit;
queryLimit.setMax(10);
queryLimit.setBefore(""); // query last (newest) first
jidsToQuery = client->rosterManager().getRosterBareJids();
QString jid = jidsToQuery.takeFirst();
mamManager->retrieveArchivedMessages(QString(), QString(), jid,
QDateTime(),
QDateTime::currentDateTimeUtc(),
queryLimit);
} else {
//mamManager->retrieveArchivedMessages("", "", "", latestMessage, QDateTime({2100, 01, 01}, {00, 00}));
}
}
#endif
......@@ -37,14 +37,18 @@
#include <QXmppGlobal.h>
#include <QXmppMessage.h>
#include <QXmppMessageReceiptManager.h>
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
#include <QXmppMamManager.h>
#endif
// Kaidan
#include "Enums.h"
class Kaidan;
class MessageModel;
class QMimeType;
class QSettings;
class QXmppDiscoveryIq;
#if QXMPP_VERSION >= 0x000904
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
class QXmppCarbonManager;
#endif
......@@ -59,7 +63,7 @@ class MessageHandler : public QObject
public:
MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel *model,
QObject *parent = nullptr);
QSettings *settings, QObject *parent = nullptr);
~MessageHandler();
......@@ -84,13 +88,35 @@ public slots:
*/
void handleDiscoInfo(const QXmppDiscoveryIq &);
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
/**
* Handles incoming archived messages
*/
void handleArchiveMessage(const QString &queryId, const QXmppMessage &message);
/**
* Called when an archive request was handled
*/
void handleArchiveResults(const QString &queryId,
const QXmppResultSetReply &resultSetReply,
bool complete);
/**
* @brief Requests all missed messages
*/
void requestLatestMessages();
#endif
private:
Kaidan *kaidan;
QXmppClient *client;
QXmppMessageReceiptManager receiptManager;
MessageModel *model;
#if QXMPP_VERSION >= 0x000904
QSettings *settings;
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
QXmppCarbonManager *carbonManager;
QXmppMamManager *mamManager;
QStringList jidsToQuery;
#endif
QString chatPartner;
};
......