diff --git a/plugins/sms/conversationsdbusinterface.h b/plugins/sms/conversationsdbusinterface.h --- a/plugins/sms/conversationsdbusinterface.h +++ b/plugins/sms/conversationsdbusinterface.h @@ -72,8 +72,8 @@ 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/plugins/sms/conversationsdbusinterface.cpp b/plugins/sms/conversationsdbusinterface.cpp --- a/plugins/sms/conversationsdbusinterface.cpp +++ b/plugins/sms/conversationsdbusinterface.cpp @@ -96,7 +96,7 @@ Q_EMIT conversationCreated(threadId); } else { - Q_EMIT conversationUpdated(threadId); + Q_EMIT conversationUpdated(message.toVariant()); } } diff --git a/smsapp/conversationmodel.h b/smsapp/conversationmodel.h --- a/smsapp/conversationmodel.h +++ b/smsapp/conversationmodel.h @@ -56,6 +56,7 @@ private Q_SLOTS: void createRowFromMessage(const QVariantMap &msg, int pos); + void handleConversationUpdate(const QVariantMap &msg); private: DeviceConversationsDbusInterface* m_conversationsInterface; diff --git a/smsapp/conversationmodel.cpp b/smsapp/conversationmodel.cpp --- a/smsapp/conversationmodel.cpp +++ b/smsapp/conversationmodel.cpp @@ -67,6 +67,7 @@ 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::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); +}