Commit 8b36766f authored by Linus Jahn's avatar Linus Jahn

RosterUpdater: Move lastMessage, unreadMessageCount into MessageHandler

The management of the lastMessage and the unreadMessageCount for each contact are now
located in the MessageHandler. This fixes that these functions were not called and the
features were not usable.
parent c9ef84e2
......@@ -97,8 +97,8 @@ void Kaidan::mainConnect()
client->registerConnectionListener(this);
// Message receiving/sending
messageSessionHandler = new MessageSessionHandler(client, messageModel);
client->registerMessageSessionHandler((gloox::MessageSessionHandler*) messageSessionHandler);
messageSessionHandler = new MessageSessionHandler(client, messageModel, rosterModel);
client->registerMessageSessionHandler(messageSessionHandler);
// Roster
rosterManager = new RosterManager(rosterModel, client);
......@@ -216,8 +216,7 @@ void Kaidan::setChatPartner(QString chatPartner)
// filter message for this chat partner
messageModel->applyRecipientFilter(&chatPartner, &jid);
rosterManager->setChatPartner(&chatPartner);
messageSessionHandler->getMessageHandler()->setCurrentChatPartner(&chatPartner);
emit chatPartnerChanged();
}
......@@ -226,7 +225,6 @@ void Kaidan::sendMessage(QString jid, QString message)
{
if (connected) {
messageSessionHandler->getMessageHandler()->sendMessage(&(this->jid), &jid, &message);
rosterManager->handleMessageSent(&jid, &message);
}
}
......
......@@ -19,7 +19,6 @@
#include "MessageHandler.h"
// Std
#include <exception>
#include <iostream>
// Qt
#include <QDateTime>
......@@ -55,9 +54,11 @@ QDateTime glooxStampToQDateTime(std::string stamp_)
return dateTime;
}
MessageHandler::MessageHandler(gloox::Client *client, MessageModel *model)
MessageHandler::MessageHandler(gloox::Client *client, MessageModel *messageModel,
RosterModel *rosterModel)
{
messageModel = model;
this->messageModel = messageModel;
this->rosterModel = rosterModel;
this->client = client;
}
......@@ -65,10 +66,17 @@ MessageHandler::~MessageHandler()
{
}
void MessageHandler::setCurrentChatPartner(QString* chatPartner)
{
this->chatPartner = chatPartner;
resetUnreadMessagesForJid(chatPartner);
}
void MessageHandler::handleMessage(const gloox::Message &message, gloox::MessageSession *session)
{
QString body = QString::fromStdString(message.body());
if (body.size() > 0) {
//
// add the message to the db
......@@ -92,11 +100,23 @@ void MessageHandler::handleMessage(const gloox::Message &message, gloox::Message
const QString msgId = QString::fromStdString(message.id());
// add the message to the database
messageModel->addMessage(&author, &recipient, &timestamp, &body, &msgId,
false, &author_resource, &recipient_resource);
// send a new notification | TODO: Resolve nickname from JID
Notifications::sendMessageNotification(message.from().full(), body.toStdString());
// update the last message for this contact
rosterModel->setLastMessageForJid(&author, &body);
// update the last exchanged for this contact
updateLastExchangedOfJid(&author);
// if chat is not opened, add a new unread message
if (author != *chatPartner) {
newUnreadMessageForJid(&author);
}
}
// XEP-0184: Message Delivery Receipts
......@@ -145,4 +165,30 @@ void MessageHandler::sendMessage(QString *fromJid, QString *toJid, QString *body
// send the message
client->send(message);
// update the last message for this contact
rosterModel->setLastMessageForJid(toJid, body);
// update the last exchanged date
updateLastExchangedOfJid(toJid);
}
void MessageHandler::updateLastExchangedOfJid(const QString *jid)
{
QString dateTime = QDateTime::currentDateTime().toString(Qt::ISODate);
rosterModel->setLastExchangedOfJid(jid, &dateTime);
}
void MessageHandler::newUnreadMessageForJid(const QString *jid)
{
// get the current unread message count
int msgCount = rosterModel->getUnreadMessageCountOfJid(jid);
// increase it by one
msgCount++;
// set the new increased count
rosterModel->setUnreadMessageCountOfJid(jid, msgCount);
}
void MessageHandler::resetUnreadMessagesForJid(const QString *jid)
{
rosterModel->setUnreadMessageCountOfJid(jid, 0);
}
......@@ -30,19 +30,28 @@
#include <gloox/messagesession.h>
// Kaidan
#include "MessageModel.h"
#include "RosterModel.h"
class MessageHandler : public gloox::MessageHandler
{
public:
MessageHandler(gloox::Client *client, MessageModel *model);
MessageHandler(gloox::Client *client, MessageModel *messageModel, RosterModel *rosterModel);
~MessageHandler();
void setCurrentChatPartner(QString *chatPartner);
void sendMessage(QString *fromJid, QString *toJid, QString *body);
virtual void handleMessage(const gloox::Message &message, gloox::MessageSession *session = 0);
void updateLastExchangedOfJid(const QString *jid);
void newUnreadMessageForJid(const QString *jid);
void resetUnreadMessagesForJid(const QString *jid);
private:
gloox::Client *client;
MessageModel *messageModel;
RosterModel *rosterModel;
QString *chatPartner;
};
#endif // MESSAGEHANDLER_H
......@@ -20,11 +20,12 @@
#include "MessageSessionHandler.h"
#include "MessageHandler.h"
MessageSessionHandler::MessageSessionHandler(gloox::Client *client, MessageModel *messageModel)
MessageSessionHandler::MessageSessionHandler(gloox::Client *client,
MessageModel *messageModel, RosterModel *rosterModel)
{
this->client = client;
this->messageModel = messageModel;
messageHandler = new MessageHandler(client, messageModel);
messageHandler = new MessageHandler(client, messageModel, rosterModel);
}
MessageSessionHandler::~MessageSessionHandler()
......@@ -34,7 +35,7 @@ MessageSessionHandler::~MessageSessionHandler()
void MessageSessionHandler::handleMessageSession(gloox::MessageSession *session)
{
session->registerMessageHandler((gloox::MessageHandler*) messageHandler);
session->registerMessageHandler(messageHandler);
}
MessageHandler* MessageSessionHandler::getMessageHandler()
......
......@@ -28,10 +28,10 @@
#include "MessageHandler.h"
#include "MessageModel.h"
class MessageSessionHandler : gloox::MessageSessionHandler
class MessageSessionHandler : public gloox::MessageSessionHandler
{
public:
MessageSessionHandler(gloox::Client*, MessageModel*);
MessageSessionHandler(gloox::Client *client, MessageModel *messageModel, RosterModel *rosterModel);
~MessageSessionHandler();
virtual void handleMessageSession(gloox::MessageSession*);
......
......@@ -50,13 +50,3 @@ void RosterManager::removeContact(const QString jid)
// remove contact from roster
rosterManager->remove(jid.toStdString());
}
void RosterManager::setChatPartner(QString* jid)
{
rosterUpdater->setChatPartner(jid);
}
void RosterManager::handleMessageSent(QString* jid, QString* message)
{
rosterUpdater->handleMessageSent(jid, message);
}
......@@ -36,9 +36,6 @@ public:
void addContact(const QString jid_, const QString name_);
void removeContact(const QString);
void setChatPartner(QString *jid);
void handleMessageSent(QString *jid, QString *message);
private:
RosterModel *rosterModel;
RosterUpdater *rosterUpdater;
......
......@@ -144,7 +144,7 @@ void RosterModel::removeListOfJids(QStringList* jidList)
submitAll();
}
void RosterModel::setLastExchangedOfJid(QString *jid_, QString *date_)
void RosterModel::setLastExchangedOfJid(const QString *jid_, QString *date_)
{
QSqlQuery newQuery(*database);
newQuery.exec(QString("UPDATE 'Roster' SET lastExchanged = '%1' WHERE jid = '%2'").arg(*date_, *jid_));
......@@ -182,7 +182,7 @@ void RosterModel::setUnreadMessageCountOfJid(const QString* jid_, const int coun
submit();
}
void RosterModel::setLastMessageForJid(QString *jid, QString *message)
void RosterModel::setLastMessageForJid(const QString *jid, QString *message)
{
QSqlQuery query(*database);
query.prepare(QString("UPDATE Roster SET lastMessage = '%1' WHERE jid = '%2'")
......
......@@ -41,10 +41,10 @@ public:
void updateContactName(QString, QString);
QStringList getJidList();
void removeListOfJids(QStringList*);
void setLastExchangedOfJid(QString*, QString*);
void setLastExchangedOfJid(const QString *jid, QString *date);
int getUnreadMessageCountOfJid(const QString* jid_);
void setUnreadMessageCountOfJid(const QString* jid_, const int unreadMessageCount);
void setLastMessageForJid(QString*, QString*);
void setLastMessageForJid(const QString *jid, QString *message);
private:
QSqlDatabase *database;
......
......@@ -38,12 +38,6 @@ RosterUpdater::~RosterUpdater()
{
}
void RosterUpdater::setChatPartner(QString* jid)
{
chatPartner = jid;
resetUnreadMessagesForJid(chatPartner);
}
void RosterUpdater::handleRoster(const gloox::Roster &roster)
{
//
......@@ -161,55 +155,3 @@ void RosterUpdater::handleRosterError(const gloox::IQ& iq)
qWarning() << "[RosterUpdater] Error occured in IQ"
<< QString::fromStdString(iq.id());
}
// handle message received
void RosterUpdater::handleMessage(const gloox::Message &msg, gloox::MessageSession *session)
{
// Update last exchanged and unread message count
std::string body = msg.body();
if (body.length() > 0) {
QString msgAuthor = QString::fromStdString(msg.from().bare());
QString message = QString::fromStdString(body);
// update the last message for this contact
rosterModel->setLastMessageForJid(&msgAuthor, &message);
updateLastExchangedOfJid(&msgAuthor);
if (msgAuthor != chatPartner) {
newUnreadMessageForJid(&msgAuthor);
}
}
}
// handle message sent
void RosterUpdater::handleMessageSent(QString *jid, QString *message)
{
// update the last message for this contact
rosterModel->setLastMessageForJid(jid, message);
// update the last exchanged date
updateLastExchangedOfJid(jid);
}
void RosterUpdater::updateLastExchangedOfJid(QString *jid)
{
QString dateTime = QDateTime::currentDateTime().toString(Qt::ISODate);
rosterModel->setLastExchangedOfJid(jid, &dateTime);
}
void RosterUpdater::newUnreadMessageForJid(QString *jid)
{
// get the current unread message count
int msgCount = rosterModel->getUnreadMessageCountOfJid(jid);
// increase it by one
msgCount++;
// set the new increased count
rosterModel->setUnreadMessageCountOfJid(jid, msgCount);
}
void RosterUpdater::resetUnreadMessagesForJid(QString *jid)
{
rosterModel->setUnreadMessageCountOfJid(jid, 0);
}
......@@ -25,19 +25,16 @@
#include <QObject>
#include <QSqlTableModel>
// gloox
#include <gloox/message.h>
#include <gloox/messagehandler.h>
#include <gloox/rosterlistener.h>
#include <gloox/rostermanager.h>
// Kaidan
#include "RosterModel.h"
class RosterUpdater : public gloox::RosterListener, gloox::MessageHandler
class RosterUpdater : public gloox::RosterListener
{
public:
RosterUpdater(RosterModel* rosterModel, gloox::RosterManager *rosterManager);
~RosterUpdater();
void setChatPartner(QString *jid);
// gloox::RosterListener
virtual void handleItemAdded(const gloox::JID& jid);
......@@ -55,17 +52,8 @@ public:
virtual void handleNonrosterPresence(const gloox::Presence& presence);
virtual void handleRosterError(const gloox::IQ& iq);
// gloox::MessageHandler
virtual void handleMessage(const gloox::Message &msg, gloox::MessageSession *session = 0);
void handleMessageSent(QString *jid, QString *message);
void updateLastExchangedOfJid(QString *jid);
void newUnreadMessageForJid(QString *jid);
void resetUnreadMessagesForJid(QString *jid);
private:
RosterModel *rosterModel;
QString *chatPartner;
gloox::RosterManager *rosterManager;
};
......
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