Unverified Commit bc071e8c authored by Linus Jahn's avatar Linus Jahn Committed by GitHub
Browse files

Fix lag when changing chat (#205)

This also does some clean up and restructuring of the deployment of the
`chatPartner` property to the ClientThread (MessageHandler) and MessageModel.

Before, a thread-safe function of the ClientThread was used, so the mutex of it
blocked the GUI, when the client thread was busy in this moment.
parent 205a246c
...@@ -121,6 +121,8 @@ void ClientThread::run() ...@@ -121,6 +121,8 @@ void ClientThread::run()
// connect slots // connect slots
connect(this, &ClientThread::sendMessageRequested, connect(this, &ClientThread::sendMessageRequested,
messageSessionHandler->getMessageHandler(), &MessageHandler::sendMessage); messageSessionHandler->getMessageHandler(), &MessageHandler::sendMessage);
connect(this, &ClientThread::chatPartnerChanged,
messageSessionHandler->getMessageHandler(), &MessageHandler::setChatPartner);
connect(this, &ClientThread::addContactRequested, connect(this, &ClientThread::addContactRequested,
rosterManager, &RosterManager::addContact); rosterManager, &RosterManager::addContact);
connect(this, &ClientThread::removeContactRequested, connect(this, &ClientThread::removeContactRequested,
...@@ -147,12 +149,8 @@ void ClientThread::setCredentials(Credentials creds) ...@@ -147,12 +149,8 @@ void ClientThread::setCredentials(Credentials creds)
client->setPassword(creds.password.toStdString()); client->setPassword(creds.password.toStdString());
client->unbindResource(client->resource()); client->unbindResource(client->resource());
client->bindResource(creds.jidResource.toStdString()); client->bindResource(creds.jidResource.toStdString());
}
void ClientThread::setCurrentChatPartner(QString *jid) emit messageModel->ownJidChanged(creds.jid);
{
QMutexLocker locker(&mutex); // => locking mutex in this function
messageSessionHandler->getMessageHandler()->setCurrentChatPartner(jid);
} }
void ClientThread::setConnectionState(ConnectionState state) void ClientThread::setConnectionState(ConnectionState state)
......
...@@ -124,11 +124,6 @@ public: ...@@ -124,11 +124,6 @@ public:
*/ */
void setCredentials(Credentials creds); void setCredentials(Credentials creds);
/**
* Applys filters to the database for showing the correct chat.
*/
void setCurrentChatPartner(QString *jid);
/** /**
* Returns if connection state is connected. * Returns if connection state is connected.
*/ */
...@@ -214,6 +209,11 @@ signals: ...@@ -214,6 +209,11 @@ signals:
*/ */
void logInWorked(); void logInWorked();
/**
* Emitted, when a different chat was opened on the GUI
*/
void chatPartnerChanged(QString chatPartner);
private: private:
/** /**
* Emits the signal for a given connection state. * Emits the signal for a given connection state.
......
...@@ -84,7 +84,7 @@ Kaidan::Kaidan(QGuiApplication *app, QObject *parent) : QObject(parent) ...@@ -84,7 +84,7 @@ Kaidan::Kaidan(QGuiApplication *app, QObject *parent) : QObject(parent)
client = new ClientThread(rosterModel, messageModel, avatarStorage, creds, client = new ClientThread(rosterModel, messageModel, avatarStorage, creds,
settings, app); settings, app);
client->start(); client->start();
connect(client, &ClientThread::connectionStateChanged, [=](ConnectionState state) { connect(client, &ClientThread::connectionStateChanged, [=](ConnectionState state) {
emit this->connectionStateChanged((quint8) state); emit this->connectionStateChanged((quint8) state);
}); });
...@@ -93,6 +93,7 @@ Kaidan::Kaidan(QGuiApplication *app, QObject *parent) : QObject(parent) ...@@ -93,6 +93,7 @@ Kaidan::Kaidan(QGuiApplication *app, QObject *parent) : QObject(parent)
}); });
connect(client, &ClientThread::newCredentialsNeeded, this, &Kaidan::newCredentialsNeeded); connect(client, &ClientThread::newCredentialsNeeded, this, &Kaidan::newCredentialsNeeded);
connect(client, &ClientThread::logInWorked, this, &Kaidan::logInWorked); connect(client, &ClientThread::logInWorked, this, &Kaidan::logInWorked);
connect(this, &Kaidan::chatPartnerChanged, client, &ClientThread::chatPartnerChanged);
} }
Kaidan::~Kaidan() Kaidan::~Kaidan()
...@@ -167,14 +168,9 @@ void Kaidan::setChatPartner(QString chatPartner) ...@@ -167,14 +168,9 @@ void Kaidan::setChatPartner(QString chatPartner)
if (this->chatPartner == chatPartner) if (this->chatPartner == chatPartner)
return; return;
// set the new chat partner
this->chatPartner = chatPartner; this->chatPartner = chatPartner;
emit chatPartnerChanged(chatPartner); // -> connected to client
// filter message for this chat partner messageModel->applyRecipientFilter(chatPartner);
messageModel->applyRecipientFilter(&chatPartner, &(creds.jid));
client->setCurrentChatPartner(&chatPartner);
emit chatPartnerChanged();
} }
quint8 Kaidan::getConnectionState() const quint8 Kaidan::getConnectionState() const
......
...@@ -287,7 +287,7 @@ signals: ...@@ -287,7 +287,7 @@ signals:
/** /**
* Emitted when the currently opnened chat has changed * Emitted when the currently opnened chat has changed
*/ */
void chatPartnerChanged(); void chatPartnerChanged(QString chatPartner);
/** /**
* Emitted when there are no (correct) credentials and new are needed * Emitted when there are no (correct) credentials and new are needed
......
...@@ -69,8 +69,7 @@ QDateTime glooxStampToQDateTime(std::string stamp_) ...@@ -69,8 +69,7 @@ QDateTime glooxStampToQDateTime(std::string stamp_)
MessageHandler::MessageHandler(gloox::Client *client, MessageModel *messageModel, MessageHandler::MessageHandler(gloox::Client *client, MessageModel *messageModel,
RosterModel *rosterModel, QObject *parent): RosterModel *rosterModel, QObject *parent):
QObject(parent), client(client), QObject(parent), client(client),
messageModel(messageModel), rosterModel(rosterModel), messageModel(messageModel), rosterModel(rosterModel)
chatPartner("")
{ {
} }
...@@ -78,9 +77,9 @@ MessageHandler::~MessageHandler() ...@@ -78,9 +77,9 @@ MessageHandler::~MessageHandler()
{ {
} }
void MessageHandler::setCurrentChatPartner(QString *chatPartner) void MessageHandler::setChatPartner(QString chatPartner)
{ {
this->chatPartner = *chatPartner; this->chatPartner = chatPartner;
resetUnreadMessagesForJid(this->chatPartner); resetUnreadMessagesForJid(this->chatPartner);
} }
......
...@@ -52,13 +52,13 @@ public: ...@@ -52,13 +52,13 @@ public:
RosterModel *rosterModel, QObject *parent = nullptr); RosterModel *rosterModel, QObject *parent = nullptr);
~MessageHandler(); ~MessageHandler();
void setCurrentChatPartner(QString *chatPartner);
virtual void handleMessage(const gloox::Message &message, gloox::MessageSession *session = 0); virtual void handleMessage(const gloox::Message &message, gloox::MessageSession *session = 0);
void updateLastExchangedOfJid(const QString &jid); void updateLastExchangedOfJid(const QString &jid);
void newUnreadMessageForJid(const QString &jid); void newUnreadMessageForJid(const QString &jid);
void resetUnreadMessagesForJid(const QString &jid); void resetUnreadMessagesForJid(const QString &jid);
public slots: public slots:
void setChatPartner(QString chatPartner);
void sendMessage(QString toJid, QString body); void sendMessage(QString toJid, QString body);
private: private:
......
...@@ -51,16 +51,21 @@ MessageModel::MessageModel(QSqlDatabase *database, QObject *parent): ...@@ -51,16 +51,21 @@ MessageModel::MessageModel(QSqlDatabase *database, QObject *parent):
// Ensures that the model is sorted correctly after submitting a new row. // Ensures that the model is sorted correctly after submitting a new row.
setEditStrategy(QSqlTableModel::OnManualSubmit); setEditStrategy(QSqlTableModel::OnManualSubmit);
connect(this, &MessageModel::chatPartnerChanged,
this, &MessageModel::applyRecipientFilter, Qt::BlockingQueuedConnection);
connect(this, &MessageModel::ownJidChanged, [=](QString &ownJid) {
this->ownJid = ownJid;
});
connect(this, &MessageModel::addMessageRequested, this, &MessageModel::addMessage); connect(this, &MessageModel::addMessageRequested, this, &MessageModel::addMessage);
connect(this, &MessageModel::setMessageAsSentRequested, this, &MessageModel::setMessageAsSent); connect(this, &MessageModel::setMessageAsSentRequested, this, &MessageModel::setMessageAsSent);
connect(this, &MessageModel::setMessageAsDeliveredRequested, this, &MessageModel::setMessageAsDelivered); connect(this, &MessageModel::setMessageAsDeliveredRequested, this, &MessageModel::setMessageAsDelivered);
} }
void MessageModel::applyRecipientFilter(QString *recipient_, QString *author_) void MessageModel::applyRecipientFilter(QString recipient)
{ {
const QString filterString = QString::fromLatin1("(recipient = '%1' AND " const QString filterString = QString::fromLatin1(
"author = '%2') OR (recipient = '%2' AND author = '%1')").arg( "(recipient = '%1' AND author = '%2') OR (recipient = '%2' AND "
*recipient_, *author_); "author = '%1')").arg(recipient, ownJid);
setFilter(filterString); setFilter(filterString);
select(); select();
} }
......
...@@ -42,10 +42,23 @@ public: ...@@ -42,10 +42,23 @@ public:
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE; QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
void applyRecipientFilter(QString *recipient_, QString *author_);
/**
* Applies a filter to the database to only show messages of a certain chat
*/
void applyRecipientFilter(QString recipient);
signals: signals:
void recipientChanged(); /**
* Emitted when the user opens another chat to apply a filter to the db
*/
void chatPartnerChanged(QString &jid);
/**
* Emitted, when connecting
*/
void ownJidChanged(QString &jid);
void addMessageRequested(const QString author, const QString recipient, void addMessageRequested(const QString author, const QString recipient,
const QString timestamp, const QString message, const QString timestamp, const QString message,
const QString msgId, bool sentByMe, const QString msgId, bool sentByMe,
...@@ -64,6 +77,8 @@ private slots: ...@@ -64,6 +77,8 @@ private slots:
private: private:
QSqlDatabase *database; QSqlDatabase *database;
QString ownJid;
}; };
#endif // MESSAGEMODEL_H #endif // MESSAGEMODEL_H
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