diff --git a/plugins/sms/conversationsdbusinterface.cpp b/plugins/sms/conversationsdbusinterface.cpp index 88d573686..3ed907cf2 100644 --- a/plugins/sms/conversationsdbusinterface.cpp +++ b/plugins/sms/conversationsdbusinterface.cpp @@ -96,7 +96,7 @@ void ConversationsDbusInterface::addMessage(const ConversationMessage &message) Q_EMIT conversationCreated(threadId); } else { - Q_EMIT conversationUpdated(threadId); + Q_EMIT conversationUpdated(message.toVariant()); } } diff --git a/plugins/sms/conversationsdbusinterface.h b/plugins/sms/conversationsdbusinterface.h index f69c84963..fbde4f3ee 100644 --- a/plugins/sms/conversationsdbusinterface.h +++ b/plugins/sms/conversationsdbusinterface.h @@ -72,8 +72,8 @@ public Q_SLOTS: Q_SIGNALS: Q_SCRIPTABLE void conversationCreated(const QString& threadID); Q_SCRIPTABLE void conversationRemoved(const QString& threadID); - Q_SCRIPTABLE void conversationUpdated(const QString& threadID); - Q_SCRIPTABLE void conversationMessageReceived(const QVariantMap & msg, int pos) const; + Q_SCRIPTABLE void conversationUpdated(const QVariantMap& msg) const; + Q_SCRIPTABLE void conversationMessageReceived(const QVariantMap& msg, int pos) const; private /*methods*/: QString newId(); //Generates successive identifitiers to use as public ids diff --git a/smsapp/conversationmodel.cpp b/smsapp/conversationmodel.cpp index 19aa3c505..b1a8e2c73 100644 --- a/smsapp/conversationmodel.cpp +++ b/smsapp/conversationmodel.cpp @@ -67,6 +67,7 @@ void ConversationModel::setDeviceId(const QString& deviceId) m_conversationsInterface = new DeviceConversationsDbusInterface(deviceId, this); connect(m_conversationsInterface, SIGNAL(conversationMessageReceived(QVariantMap, int)), this, SLOT(createRowFromMessage(QVariantMap, int))); + connect(m_conversationsInterface, SIGNAL(conversationUpdated(QVariantMap)), this, SLOT(handleConversationUpdate(QVariantMap))); } void ConversationModel::sendReplyToConversation(const QString& message) @@ -78,9 +79,35 @@ void ConversationModel::sendReplyToConversation(const QString& message) void ConversationModel::createRowFromMessage(const QVariantMap& msg, int pos) { const ConversationMessage message(msg); + + if (!(message.threadID() == m_threadId.toInt())) { + // Because of the asynchronous nature of the current implementation of this model, if the + // user clicks quickly between threads or for some other reason a message comes when we're + // not expecting it, we should not display it in the wrong place + qCDebug(KDECONNECT_SMS_CONVERSATION_MODEL) + << "Got a message for a thread" << message.threadID() + << "but we are currently viewing" << m_threadId + << "Discarding."; + return; + } auto item = new QStandardItem; item->setText(message.body()); item->setData(message.type() == ConversationMessage::MessageTypeSent, FromMeRole); item->setData(message.date(), DateRole); insertRow(pos, item); } + +void ConversationModel::handleConversationUpdate(const QVariantMap& msg) +{ + const ConversationMessage message(msg); + + if (!(message.threadID() == m_threadId.toInt())) { + // If a conversation which we are not currently viewing was updated, discard the information + qCDebug(KDECONNECT_SMS_CONVERSATION_MODEL) + << "Saw update for thread" << message.threadID() + << "but we are currently viewing" << m_threadId; + return; + } + + createRowFromMessage(msg, 0); +} diff --git a/smsapp/conversationmodel.h b/smsapp/conversationmodel.h index c8e5e1203..0de265c4f 100644 --- a/smsapp/conversationmodel.h +++ b/smsapp/conversationmodel.h @@ -56,6 +56,7 @@ public: private Q_SLOTS: void createRowFromMessage(const QVariantMap &msg, int pos); + void handleConversationUpdate(const QVariantMap &msg); private: DeviceConversationsDbusInterface* m_conversationsInterface;