kdeconnect-kde/smsapp/conversationmodel.cpp
Simon Redman ee9547ed89 [SMS App] Basic plain-text MMS support
## Summary

Desktop companion to https://invent.kde.org/kde/kdeconnect-android/merge_requests/78

Give desktop SMS app a basic understanding of the MMSes coming from Android:
- Show a fake body if we get an attachment we can't display (for now, any attachment)
- Display a fake contact header for multi-target messages since Android does not yet export multi-target address information
- Disable attempting to reply to multi-target messages

BUG: 398889

## Test Plan
### Before:
MMS messages were silently dropped, meaning:
 - Group MMS conversations were not visible
 - Single-target conversations with the most-recent message an MMS were not visible

### After:
 - Install https://invent.kde.org/kde/kdeconnect-android/merge_requests/78
 - Multi-target conversations are displayed (kind of ugly, since they have no contact information
 - Single-target conversations which end with an MMS are displayed
 - Plain-text MMS is displayed nicely
 - MMS attachments don't show
   - MMS which are only an attachment with no body are displayed with a dummy body
2019-07-19 15:29:28 +00:00

141 lines
4.8 KiB
C++

/**
* Copyright (C) 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
* Copyright (C) 2018 Simon Redman <simon@ergotech.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "conversationmodel.h"
#include <QLoggingCategory>
#include <KLocalizedString>
#include "interfaces/conversationmessage.h"
Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATION_MODEL, "kdeconnect.sms.conversation")
ConversationModel::ConversationModel(QObject* parent)
: QStandardItemModel(parent)
, m_conversationsInterface(nullptr)
{
auto roles = roleNames();
roles.insert(FromMeRole, "fromMe");
roles.insert(DateRole, "date");
setItemRoleNames(roles);
}
ConversationModel::~ConversationModel()
{
}
qint64 ConversationModel::threadId() const
{
return m_threadId;
}
void ConversationModel::setThreadId(const qint64& threadId)
{
if (m_threadId == threadId)
return;
m_threadId = threadId;
clear();
knownMessageIDs.clear();
if (m_threadId != INVALID_THREAD_ID && !m_deviceId.isEmpty()) {
requestMoreMessages();
}
}
void ConversationModel::setDeviceId(const QString& deviceId)
{
if (deviceId == m_deviceId)
return;
qCDebug(KDECONNECT_SMS_CONVERSATION_MODEL) << "setDeviceId" << "of" << this;
if (m_conversationsInterface) {
disconnect(m_conversationsInterface, SIGNAL(conversationUpdated(QVariantMap)), this, SLOT(handleConversationUpdate(QVariantMap)));
delete m_conversationsInterface;
}
m_deviceId = deviceId;
m_conversationsInterface = new DeviceConversationsDbusInterface(deviceId, this);
connect(m_conversationsInterface, SIGNAL(conversationUpdated(QVariantMap)), this, SLOT(handleConversationUpdate(QVariantMap)));
}
void ConversationModel::sendReplyToConversation(const QString& message)
{
//qCDebug(KDECONNECT_SMS_CONVERSATION_MODEL) << "Trying to send" << message << "to conversation with ID" << m_threadId;
m_conversationsInterface->replyToConversation(m_threadId, message);
}
void ConversationModel::requestMoreMessages(const quint32& howMany)
{
if (m_threadId == INVALID_THREAD_ID) {
return;
}
const auto& numMessages = rowCount();
m_conversationsInterface->requestConversation(m_threadId, numMessages, numMessages + howMany);
}
void ConversationModel::createRowFromMessage(const QVariantMap& msg, int pos)
{
const ConversationMessage message(msg);
if (message.threadID() != m_threadId) {
// 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;
}
if (knownMessageIDs.contains(message.uID())) {
qCDebug(KDECONNECT_SMS_CONVERSATION_MODEL)
<< "Ignoring duplicate message with ID" << message.uID();
return;
}
// TODO: Upgrade to support other kinds of media
// Get the body that we should display
QString displayBody = message.containsTextBody() ? message.body() : i18n("(Unsupported Message Type)");
auto item = new QStandardItem;
item->setText(displayBody);
item->setData(message.type() == ConversationMessage::MessageTypeSent, FromMeRole);
item->setData(message.date(), DateRole);
insertRow(pos, item);
knownMessageIDs.insert(message.uID());
}
void ConversationModel::handleConversationUpdate(const QVariantMap& msg)
{
const ConversationMessage message(msg);
if (message.threadID() != m_threadId) {
// 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);
}