conversationlistmodel.cpp 7.8 KB
Newer Older
1
/**
2
 * Copyright (C) 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
3
 * Copyright (C) 2018 Simon Redman <simon@ergotech.com>
4
 *
5 6 7 8 9 10 11
 * 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.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 21 22 23
 */

#include "conversationlistmodel.h"

24
#include <QString>
25
#include <QLoggingCategory>
26
#include <QPainter>
27 28 29

#include <KLocalizedString>

30
#include "interfaces/conversationmessage.h"
31
#include "interfaces/dbusinterfaces.h"
32
#include "smshelper.h"
33 34 35

Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL, "kdeconnect.sms.conversations_list")

36 37 38
OurSortFilterProxyModel::OurSortFilterProxyModel(){}
OurSortFilterProxyModel::~OurSortFilterProxyModel(){}

39 40 41 42
ConversationListModel::ConversationListModel(QObject* parent)
    : QStandardItemModel(parent)
    , m_conversationsInterface(nullptr)
{
43
    //qCDebug(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Constructing" << this;
44 45
    auto roles = roleNames();
    roles.insert(FromMeRole, "fromMe");
46 47 48
    roles.insert(SenderRole, "sender");
    roles.insert(DateRole, "date");
    roles.insert(AddressesRole, "addresses");
49
    roles.insert(ConversationIdRole, "conversationId");
50
    roles.insert(MultitargetRole, "isMultitarget");
51 52 53 54 55 56 57 58 59 60 61
    setItemRoleNames(roles);

    ConversationMessage::registerDbusType();
}

ConversationListModel::~ConversationListModel()
{
}

void ConversationListModel::setDeviceId(const QString& deviceId)
{
62
    if (deviceId == m_deviceId) {
63
        return;
64 65
    }

66
    if (deviceId.isEmpty()) {
67 68 69
        return;
    }

70
    qCDebug(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "setDeviceId" << deviceId << "of" << this;
71 72

    if (m_conversationsInterface) {
73 74
        disconnect(m_conversationsInterface, SIGNAL(conversationCreated(QDBusVariant)), this, SLOT(handleCreatedConversation(QDBusVariant)));
        disconnect(m_conversationsInterface, SIGNAL(conversationUpdated(QDBusVariant)), this, SLOT(handleConversationUpdated(QDBusVariant)));
75
        delete m_conversationsInterface;
76
        m_conversationsInterface = nullptr;
77 78
    }

79 80 81 82 83 84 85
    // This method still gets called *with a valid deviceID* when the device is not connected while the component is setting up
    // Detect that case and don't do anything.
    DeviceDbusInterface device(deviceId);
    if (!(device.isValid() && device.isReachable())) {
        return;
    }

86 87 88
    m_deviceId = deviceId;
    Q_EMIT deviceIdChanged();

89
    m_conversationsInterface = new DeviceConversationsDbusInterface(deviceId, this);
90 91
    connect(m_conversationsInterface, SIGNAL(conversationCreated(QDBusVariant)), this, SLOT(handleCreatedConversation(QDBusVariant)));
    connect(m_conversationsInterface, SIGNAL(conversationUpdated(QDBusVariant)), this, SLOT(handleConversationUpdated(QDBusVariant)));
92

93 94 95 96 97 98 99 100 101 102 103
    refresh();
}

void ConversationListModel::refresh()
{
    if (m_deviceId.isEmpty()) {
        qWarning() << "refreshing null device";
        return;
    }

    prepareConversationsList();
104 105 106 107 108
    m_conversationsInterface->requestAllConversationThreads();
}

void ConversationListModel::prepareConversationsList()
{
109 110 111 112 113 114 115 116 117 118
    if (!m_conversationsInterface->isValid()) {
        qCWarning(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Tried to prepareConversationsList with an invalid interface!";
        return;
    }
    QDBusPendingReply<QVariantList> validThreadIDsReply = m_conversationsInterface->activeConversations();

    setWhenAvailable(validThreadIDsReply, [this](const QVariantList& convs) {
        clear(); // If we clear before we receive the reply, there might be a (several second) visual gap!
        for (const QVariant& headMessage : convs) {
            QDBusArgument data = headMessage.value<QDBusArgument>();
119
            ConversationMessage message;
120
            data >> message;
121
            createRowFromMessage(message);
122 123 124 125
        }
    }, this);
}

126
void ConversationListModel::handleCreatedConversation(const QDBusVariant& msg)
127
{
128 129
    ConversationMessage message = ConversationMessage::fromDBus(msg);
    createRowFromMessage(message);
130 131
}

132
void ConversationListModel::handleConversationUpdated(const QDBusVariant& msg)
133
{
134 135
    ConversationMessage message = ConversationMessage::fromDBus(msg);
    createRowFromMessage(message);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
}

void ConversationListModel::printDBusError(const QDBusError& error)
{
    qCWarning(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << error;
}

QStandardItem * ConversationListModel::conversationForThreadId(qint32 threadId)
{
    for(int i=0, c=rowCount(); i<c; ++i) {
        auto it = item(i, 0);
        if (it->data(ConversationIdRole) == threadId)
            return it;
    }
    return nullptr;
}

153
void ConversationListModel::createRowFromMessage(const ConversationMessage& message)
154
{
155
    if (message.type() == -1) {
156 157 158 159 160 161 162 163 164 165
        // The Android side currently hacks in -1 if something weird comes up
        // TODO: Remove this hack when MMS support is implemented
        return;
    }

    bool toadd = false;
    QStandardItem* item = conversationForThreadId(message.threadID());
    if (!item) {
        toadd = true;
        item = new QStandardItem();
166 167 168

        /** The address of everyone involved in this conversation, which we should not display (check if they are known contacts first) */
        QList<ConversationAddress> rawAddresses = message.addresses();
169 170 171 172
        if (rawAddresses.isEmpty()) {
            qWarning() << "no addresses!" << message.body();
            return;
        }
173 174 175 176 177 178

        QString displayNames = SmsHelper::getTitleForAddresses(rawAddresses);
        QIcon displayIcon = SmsHelper::getIconForAddresses(rawAddresses);

        item->setText(displayNames);
        item->setIcon(displayIcon);
179
        item->setData(message.threadID(), ConversationIdRole);
180
        item->setData(rawAddresses[0].address(), SenderRole);
181
    }
182

183 184 185 186
    // 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)");

187 188 189 190 191 192
    // Prepend the sender's name
    if (message.isOutgoing()) {
        displayBody = i18n("You: %1", displayBody);
    } else {
        // If the message is incoming, the sender is the first Address
        QString senderAddress = item->data(SenderRole).toString();
193
        const auto sender = SmsHelper::lookupPersonByAddress(senderAddress);
194 195
        QString senderName = sender == nullptr? senderAddress : SmsHelper::lookupPersonByAddress(senderAddress)->name();
        displayBody = i18n("%1: %2", senderName, displayBody);
196
    }
197

198 199 200 201 202 203 204
    // Update the message if the data is newer
    // This will be true if a conversation receives a new message, but false when the user
    // does something to trigger past conversation history loading
    bool oldDateExists;
    qint64 oldDate = item->data(DateRole).toLongLong(&oldDateExists);
    if (!oldDateExists || message.date() >= oldDate) {
        // If there was no old data or incoming data is newer, update the record
205 206
        item->setData(QVariant::fromValue(message.addresses()), AddressesRole);
        item->setData(message.isOutgoing(), FromMeRole);
207
        item->setData(displayBody, Qt::ToolTipRole);
208
        item->setData(message.date(), DateRole);
209
        item->setData(message.isMultitarget(), MultitargetRole);
210
    }
211 212 213 214

    if (toadd)
        appendRow(item);
}