Commit 5fdf2e7a authored by Linus Jahn's avatar Linus Jahn

Clean up Message/RosterController

I separated them again, so the RosterController will also connect to the
Message received event, and will upsate the unread message counter on its
own.

I also removed many `emit xxxModelChanged`. They are unnecessary and I don't
know why I added them at all (in these cases).

There were also some unused #includes, that I have removed.

But the QML API has also changed through this and sendMessage and chatPartner
are now on the top level of `kaidan`.
parent f56b8d86
......@@ -78,8 +78,6 @@ Kaidan::Kaidan(Swift::NetworkFactories* networkFactories, QObject *parent) : QOb
vCardController = new VCardController();
serviceDiscoveryManager = new ServiceDiscoveryManager();
messageController->setRosterController(rosterController);
storages = new Swift::MemoryStorages(Swift::PlatformCryptoProvider::create());
}
......@@ -158,11 +156,21 @@ void Kaidan::handleDisconnected()
emit connectionStateDisconnected();
}
bool Kaidan::newLoginNeeded()
{
return (jid == "") || (password == "");
}
void Kaidan::updateFullJid()
{
fullJid = jid + QString("/") + jidResource;
}
QString Kaidan::getJid()
{
return jid;
}
void Kaidan::setJid(QString jid_)
{
jid = jid_; // set new jid for mainConnect
......@@ -170,6 +178,11 @@ void Kaidan::setJid(QString jid_)
updateFullJid();
}
QString Kaidan::getJidResource()
{
return jidResource;
}
void Kaidan::setJidResource(QString jidResource_)
{
jidResource = jidResource_;
......@@ -177,12 +190,44 @@ void Kaidan::setJidResource(QString jidResource_)
updateFullJid(); // update the full jid (the resource part has changed)
}
QString Kaidan::getPassword()
{
return password;
}
void Kaidan::setPassword(QString password_)
{
password = password_; // set new password for
settings->setValue("auth/password", password_); // save to settings
}
QString Kaidan::getChatPartner()
{
return chatPartner;
}
void Kaidan::setChatPartner(QString chatPartner)
{
// check if different
if (this->chatPartner == chatPartner)
return;
// set the new chat partner
this->chatPartner = chatPartner;
// upsate message controller
messageController->setChatPartner(&chatPartner);
rosterController->setChatPartner(&chatPartner);
emit chatPartnerChanged();
}
void Kaidan::sendMessage(QString jid, QString message)
{
messageController->sendMessage(&jid, &message);
rosterController->updateLastExchangedOfJid(&jid);
}
QString Kaidan::getResourcePath(QString name_)
{
// get the standard app data locations for current platform
......@@ -205,12 +250,6 @@ QString Kaidan::getResourcePath(QString name_)
return QString("");
}
// if no jid or password, return true
bool Kaidan::newLoginNeeded()
{
return (jid == "") || (password == "");
}
RosterController* Kaidan::getRosterController()
{
return rosterController;
......@@ -231,16 +270,3 @@ QString Kaidan::getVersionString()
{
return QString(VERSION_STRING);
}
QString Kaidan::getJid()
{
return jid;
}
QString Kaidan::getJidResource()
{
return jidResource;
}
QString Kaidan::getPassword()
{
return password;
}
......@@ -48,6 +48,7 @@ class Kaidan : public QObject
Q_PROPERTY(QString jid READ getJid WRITE setJid NOTIFY jidChanged)
Q_PROPERTY(QString jidResource READ getJidResource WRITE setJidResource NOTIFY jidResourceChanged)
Q_PROPERTY(QString password READ getPassword WRITE setPassword NOTIFY passwordChanged)
Q_PROPERTY(QString chatPartner READ getChatPartner WRITE setChatPartner NOTIFY chatPartnerChanged)
public:
Kaidan(Swift::NetworkFactories* networkFactories, QObject *parent = 0);
......@@ -55,21 +56,23 @@ public:
Q_INVOKABLE void mainDisconnect();
Q_INVOKABLE void mainConnect();
bool getConnectionState() const;
Q_INVOKABLE bool newLoginNeeded();
bool getConnectionState() const;
QString getJid();
QString getJidResource();
QString getPassword();
void setJid(QString);
QString getJidResource();
void setJidResource(QString);
QString getPassword();
void setPassword(QString);
QString getChatPartner();
void setChatPartner(QString);
RosterController* getRosterController();
MessageController* getMessageController();
VCardController* getVCardController();
Q_INVOKABLE void sendMessage(QString jid, QString message);
Q_INVOKABLE QString getResourcePath(QString);
Q_INVOKABLE QString getVersionString();
......@@ -82,6 +85,7 @@ signals:
void jidChanged();
void jidResourceChanged();
void passwordChanged();
void chatPartnerChanged();
private:
void handleConnected();
......@@ -108,6 +112,7 @@ private:
QString jidResource;
QString fullJid;
QString password;
QString chatPartner;
};
#endif
......@@ -23,9 +23,6 @@
#include <QDateTime>
#include <QDebug>
#include <QString>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlQuery>
// Boost
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
......@@ -59,37 +56,16 @@ void MessageController::setClient(Swift::Client* client_)
client->onMessageReceived.connect(boost::bind(&MessageController::handleMessageReceived, this, _1));
}
void MessageController::setRosterController(RosterController* rosterController_)
{
rosterController = rosterController_;
}
MessageModel* MessageController::getMessageModel()
{
return messageModel;
}
void MessageController::setRecipient(QString recipient_)
void MessageController::setChatPartner(QString *recipient)
{
// update the recipient
if (recipient == recipient_)
return;
recipient = recipient_;
emit recipientChanged();
// we have to use ownJid here, because this should also be usable when
// we're offline or we haven't connected already.
messageModel->applyRecipientFilter(recipient, *ownJid);
emit messageModelChanged();
// reset the unread message counter for this contact
rosterController->resetUnreadMessagesForJid(recipient_);
}
QString MessageController::getRecipient()
{
return recipient;
messageModel->applyRecipientFilter(recipient, ownJid);
}
void MessageController::handleMessageReceived(Swift::Message::ref message_)
......@@ -120,8 +96,6 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_)
messageModel->addMessage(&author, &author_resource, ownJid,
&recipient_resource, &timestamp, &message, &msgId, false);
emit messageModelChanged();
// send a new notification | TODO: Resolve nickname from JID
Notifications::sendMessageNotification(
message_->getFrom().toBare().toString(),
......@@ -158,23 +132,9 @@ void MessageController::handleMessageReceived(Swift::Message::ref message_)
messageModel->setMessageAsDelivered(QString::fromStdString(receivedId));
}
}
//
// Update last exchanged, unread message count in roster controller
//
if (bodyOpt) {
const QString msgAuthor = QString::fromStdString(message_->getFrom().toBare().toString());
rosterController->updateLastExchangedOfJid(msgAuthor);
if (msgAuthor != this->recipient) {
rosterController->newUnreadMessageForJid(msgAuthor);
}
}
}
void MessageController::sendMessage(const QString recipient_, const QString message_)
void MessageController::sendMessage(QString *recipient_, QString *message_)
{
// generate a new message id
Swift::IDGenerator idGenerator;
......@@ -185,24 +145,21 @@ void MessageController::sendMessage(const QString recipient_, const QString mess
//
const QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate);
const QString author_resource = QString(client->getJID().getResource().c_str());
const QString author_resource = QString::fromStdString(client->getJID().getResource());
const QString recipient_resource = QString("");
const QString qmsgId = QString::fromStdString(msgId);
messageModel->addMessage(ownJid, &author_resource, &recipient_,
&recipient_resource, &timestamp, &message_, &qmsgId, true);
emit messageModelChanged();
messageModel->addMessage(ownJid, &author_resource, recipient_,
&recipient_resource, &timestamp, message_, &qmsgId, true);
//
// send the message
//
boost::shared_ptr<Swift::Message> newMessage(new Swift::Message());
newMessage->setTo(Swift::JID(recipient_.toStdString()));
newMessage->setTo(Swift::JID(recipient_->toStdString()));
newMessage->setFrom(client->getJID());
newMessage->setBody(message_.toStdString());
newMessage->setBody(message_->toStdString());
newMessage->setID(msgId);
// XEP-0184: Message Delivery Receipts
......@@ -211,10 +168,4 @@ void MessageController::sendMessage(const QString recipient_, const QString mess
// send the message
client->sendMessage(newMessage);
//
// Update lastExchanged in roster controller
//
rosterController->updateLastExchangedOfJid(recipient);
}
......@@ -22,7 +22,6 @@
// Qt
#include <QObject>
#include <QSqlTableModel>
// Swiften
#include <Swiften/Client/Client.h>
#include <Swiften/Elements/Message.h>
......@@ -33,21 +32,17 @@
class MessageController : public QObject
{
Q_OBJECT
Q_PROPERTY(MessageModel* messageModel READ getMessageModel NOTIFY messageModelChanged)
Q_PROPERTY(QString recipient READ getRecipient WRITE setRecipient NOTIFY recipientChanged)
Q_PROPERTY(MessageModel *messageModel READ getMessageModel NOTIFY messageModelChanged)
public:
MessageController(QString* ownJid_, QObject *parent = 0);
MessageController(QString *ownJid_, QObject *parent = 0);
~MessageController();
void setClient(Swift::Client* client_);
void setRosterController(RosterController* rosterController_);
void setClient(Swift::Client *client_);
MessageModel* getMessageModel();
void setRecipient(QString recipient_);
QString getRecipient();
Q_INVOKABLE void sendMessage(const QString recipient_, const QString message_);
void setChatPartner(QString *recipient);
void sendMessage(QString *recipient_, QString *message_);
signals:
void messageModelChanged();
......@@ -56,11 +51,9 @@ signals:
private:
void handleMessageReceived(Swift::Message::ref message);
Swift::Client* client;
MessageModel* messageModel;
RosterController* rosterController;
QString recipient;
QString* ownJid;
Swift::Client *client;
MessageModel *messageModel;
QString *ownJid;
};
#endif // MESSAGECONTROLLER_H
......@@ -69,11 +69,11 @@ MessageModel::MessageModel(QObject *parent) : QSqlTableModel(parent)
setEditStrategy(QSqlTableModel::OnManualSubmit);
}
void MessageModel::applyRecipientFilter(QString recipient_, QString author_)
void MessageModel::applyRecipientFilter(QString *recipient_, QString *author_)
{
const QString filterString = QString::fromLatin1("(recipient = '%1' AND "
"author = '%2') OR (recipient = '%2' AND author = '%1')").arg(
recipient_, author_);
*recipient_, *author_);
setFilter(filterString);
select();
}
......
......@@ -32,7 +32,7 @@ public:
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
void applyRecipientFilter(QString recipient_, QString author_);
void applyRecipientFilter(QString *recipient_, QString *author_);
void addMessage(const QString* author, const QString* author_resource,
const QString* recipient, const QString* recipient_resource,
const QString* timestamp, const QString* message,
......
......@@ -24,12 +24,8 @@
#include <iostream>
#include <string.h>
// Qt 5
#include <QQmlContext>
#include <QDateTime>
#include <QDebug>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlQuery>
// Kaidan
#include "RosterModel.h"
// Swiften
......@@ -42,10 +38,12 @@
#include <Swiften/Queries/IQRouter.h>
// Boost
#include <boost/bind.hpp>
#include <boost/optional.hpp>
RosterController::RosterController(QObject *parent) : QObject(parent)
{
rosterModel = new RosterModel();
chatPartner = QString("");
}
RosterController::~RosterController()
......@@ -56,10 +54,15 @@ RosterController::~RosterController()
void RosterController::setClient(Swift::Client *client_)
{
client = client_;
client->onConnected.connect(boost::bind(&RosterController::requestRosterFromClient, this));
iqRouter = client->getIQRouter();
client->onConnected.connect(
boost::bind(&RosterController::requestRosterFromClient, this));
client->onMessageReceived.connect(
boost::bind(&RosterController::handleMessageReceived, this, _1));
xmppRoster = client->getRoster();
xmppRoster->onInitialRosterPopulated.connect(boost::bind(&RosterController::handleInitialRosterPopulated, this));
xmppRoster->onInitialRosterPopulated.connect(
boost::bind(&RosterController::handleInitialRosterPopulated, this));
}
RosterModel* RosterController::getRosterModel()
......@@ -67,6 +70,14 @@ RosterModel* RosterController::getRosterModel()
return rosterModel;
}
void RosterController::setChatPartner(QString *chatPartner)
{
this->chatPartner = *chatPartner;
// reset the unread message counter for this contact
resetUnreadMessagesForJid(chatPartner);
}
void RosterController::requestRosterFromClient()
{
client->requestRoster();
......@@ -127,9 +138,6 @@ void RosterController::handleRosterReceived(Swift::ErrorPayload::ref error_)
rosterModel->insertContact(jid, name);
}
}
// send signal for updating the GUI
emit rosterModelChanged();
}
}
......@@ -151,8 +159,6 @@ void RosterController::handleJidAdded(const Swift::JID &jid_)
);
rosterModel->submitAll();
emit rosterModelChanged();
}
void RosterController::handleJidRemoved(const Swift::JID &jid_)
......@@ -160,8 +166,6 @@ void RosterController::handleJidRemoved(const Swift::JID &jid_)
rosterModel->removeContactByJid(
QString::fromStdString(jid_.toBare().toString())
);
emit rosterModelChanged();
}
void RosterController::handleJidUpdated(const Swift::JID &jid_, const std::string &name_,
......@@ -171,16 +175,12 @@ void RosterController::handleJidUpdated(const Swift::JID &jid_, const std::strin
QString::fromStdString(jid_.toBare().toString()),
QString::fromStdString(name_)
);
emit rosterModelChanged();
}
void RosterController::handleRosterCleared()
{
// remove all contacts
rosterModel->clearData();
emit rosterModelChanged();
}
void RosterController::addContact(const QString jid_, const QString name_)
......@@ -228,12 +228,30 @@ void RosterController::removeContact(const QString jid_)
);
}
void RosterController::updateLastExchangedOfJid(const QString jid_)
void RosterController::handleMessageReceived(Swift::Message::ref message)
{
rosterModel->setLastExchangedOfJid(jid_, QDateTime::currentDateTime().toString(Qt::ISODate));
//
// Update last exchanged and unread message count
//
boost::optional<std::string> optionalMessageBody = message->getBody();
if (optionalMessageBody) {
QString msgAuthor = QString::fromStdString(message->getFrom()
.toBare().toString());
// send signal for updating the GUI
emit rosterModelChanged();
updateLastExchangedOfJid(&msgAuthor);
if (msgAuthor != chatPartner) {
newUnreadMessageForJid(msgAuthor);
}
}
}
void RosterController::updateLastExchangedOfJid(QString *jid_)
{
QString dateTime = QDateTime::currentDateTime().toString(Qt::ISODate);
rosterModel->setLastExchangedOfJid(jid_, &dateTime);
}
void RosterController::newUnreadMessageForJid(const QString jid_)
......@@ -246,8 +264,7 @@ void RosterController::newUnreadMessageForJid(const QString jid_)
rosterModel->setUnreadMessageCountOfJid(&jid_, msgCount);
}
void RosterController::resetUnreadMessagesForJid(const QString jid_)
void RosterController::resetUnreadMessagesForJid(QString *jid)
{
rosterModel->setUnreadMessageCountOfJid(&jid_, 0);
emit rosterModelChanged();
rosterModel->setUnreadMessageCountOfJid(jid, 0);
}
......@@ -24,10 +24,10 @@
// Qt
#include <QObject>
#include <QSqlTableModel>
#include <QQmlListProperty>
// Swiften
#include <Swiften/Client/Client.h>
#include <Swiften/Elements/ErrorPayload.h>
#include <Swiften/Elements/Message.h>
// Kaidan
#include "RosterModel.h"
......@@ -43,9 +43,10 @@ public:
void setClient(Swift::Client *client_);
void requestRosterFromClient();
RosterModel* getRosterModel();
void updateLastExchangedOfJid(const QString jid_);
void setChatPartner(QString *chatPartner);
void updateLastExchangedOfJid(QString *jid_);
void newUnreadMessageForJid(const QString jid_);
void resetUnreadMessagesForJid(const QString jid_);
void resetUnreadMessagesForJid(QString *jid_);
Q_INVOKABLE void addContact(const QString jid_, const QString name_);
Q_INVOKABLE void removeContact(const QString);
......@@ -53,16 +54,18 @@ signals:
void rosterModelChanged();
private:
void handleMessageReceived(Swift::Message::ref message);
void handleRosterReceived(Swift::ErrorPayload::ref error);
void handleInitialRosterPopulated();
void handleJidAdded(const Swift::JID &jid_);
void handleJidRemoved(const Swift::JID &jid_);
void handleJidUpdated(const Swift::JID &jid_, const std::string &name_, const std::vector<std::string>&);
void handleRosterCleared();
Swift::Client* client;
Swift::IQRouter* iqRouter;
Swift::XMPPRoster* xmppRoster;
RosterModel* rosterModel;
Swift::Client *client;
Swift::IQRouter *iqRouter;
Swift::XMPPRoster *xmppRoster;
RosterModel *rosterModel;
QString chatPartner;
};
#endif // ROSTERCONTROLLER_H
......@@ -156,10 +156,10 @@ void RosterModel::removeListOfJids(QStringList* jidList)
submitAll();
}
void RosterModel::setLastExchangedOfJid(const QString jid_, const QString date_)
void RosterModel::setLastExchangedOfJid(QString *jid_, QString *date_)
{
QSqlQuery newQuery;
newQuery.exec(QString("UPDATE 'Roster' SET lastExchanged = '%1' WHERE jid = '%2'").arg(date_, jid_));
newQuery.exec(QString("UPDATE 'Roster' SET lastExchanged = '%1' WHERE jid = '%2'").arg(*date_, *jid_));
submitAll();
}
......
......@@ -40,7 +40,7 @@ public:
void updateContactName(QString, QString);
QStringList getJidList();
void removeListOfJids(QStringList*);
void setLastExchangedOfJid(const QString, const QString);
void setLastExchangedOfJid(QString*, QString*);
int getUnreadMessageCountOfJid(const QString* jid_);
void setUnreadMessageCountOfJid(const QString* jid_, const int unreadMessageCount);
};
......
......@@ -153,7 +153,7 @@ Kirigami.Page {
sendButton.enabled = false;
// send the message
kaidan.messageController.sendMessage(recipientJid, messageField.text);
kaidan.sendMessage(recipientJid, messageField.text);
// clean up the text field
messageField.text = "";
......
......@@ -82,7 +82,7 @@ Kirigami.ScrollablePage {
// this will update the unread message count,
// which will update the roster and will reset the
// model variable
kaidan.messageController.recipient = model.jid;
kaidan.chatPartner = model.jid;
}
actions: [
......
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