conversationlistmodel.cpp 9.92 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
#define INVALID_THREAD_ID -1
#define INVALID_DATE -1

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
    if (!m_conversationsInterface->isValid()) {
        qCWarning(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Tried to prepareConversationsList with an invalid interface!";
        return;
    }
Nicolas Fella's avatar
Nicolas Fella committed
113
    const QDBusPendingReply<QVariantList> validThreadIDsReply = m_conversationsInterface->activeConversations();
114
115
116
117

    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) {
Nicolas Fella's avatar
Nicolas Fella committed
118
            const QDBusArgument data = headMessage.value<QDBusArgument>();
119
            ConversationMessage message;
120
            data >> message;
121
            createRowFromMessage(message);
122
        }
123
        displayContacts();
124
125
126
    }, this);
}

127
void ConversationListModel::handleCreatedConversation(const QDBusVariant& msg)
128
{
Nicolas Fella's avatar
Nicolas Fella committed
129
    const ConversationMessage message = ConversationMessage::fromDBus(msg);
130
    createRowFromMessage(message);
131
132
}

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

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;
}

154
155
156
157
158
159
160
161
162
163
164
165
QStandardItem * ConversationListModel::getConversationForAddress(const QString& address) {
    for(int i = 0; i < rowCount(); ++i) {
        const auto& it = item(i, 0);
        if (!it->data(MultitargetRole).toBool()) {
            if (SmsHelper::isPhoneNumberMatch(it->data(SenderRole).toString(), address)) {
                return it;
            }
        }
    }
    return nullptr;
}

166
void ConversationListModel::createRowFromMessage(const ConversationMessage& message)
167
{
168
    if (message.type() == -1) {
169
170
171
172
173
        // The Android side currently hacks in -1 if something weird comes up
        // TODO: Remove this hack when MMS support is implemented
        return;
    }

174
    /** The address of everyone involved in this conversation, which we should not display (check if they are known contacts first) */
Nicolas Fella's avatar
Nicolas Fella committed
175
    const QList<ConversationAddress> rawAddresses = message.addresses();
176
177
178
179
180
    if (rawAddresses.isEmpty()) {
        qWarning() << "no addresses!" << message.body();
        return;
    }

181
182
    bool toadd = false;
    QStandardItem* item = conversationForThreadId(message.threadID());
183
184
185
186
187
188
189
190
191
    //Check if we have a contact with which to associate this message, needed if there is no conversation with the contact and we received a message from them
    if (!item && !message.isMultitarget()) {

            item = getConversationForAddress(rawAddresses[0].address());
            if (item) {
                item->setData(message.threadID(), ConversationIdRole);
            }
        }

192
193
194
    if (!item) {
        toadd = true;
        item = new QStandardItem();
195

Nicolas Fella's avatar
Nicolas Fella committed
196
197
        const QString displayNames = SmsHelper::getTitleForAddresses(rawAddresses);
        const QIcon displayIcon = SmsHelper::getIconForAddresses(rawAddresses);
198
199
200

        item->setText(displayNames);
        item->setIcon(displayIcon);
201
        item->setData(message.threadID(), ConversationIdRole);
202
        item->setData(rawAddresses[0].address(), SenderRole);
203
    }
204

205
206
207
208
    // 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)");

209
210
211
212
213
    // 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
Nicolas Fella's avatar
Nicolas Fella committed
214
        const QString senderAddress = item->data(SenderRole).toString();
215
        const auto sender = SmsHelper::lookupPersonByAddress(senderAddress);
Nicolas Fella's avatar
Nicolas Fella committed
216
        const QString senderName = sender == nullptr? senderAddress : SmsHelper::lookupPersonByAddress(senderAddress)->name();
217
        displayBody = i18n("%1: %2", senderName, displayBody);
218
    }
219

220
221
222
223
    // 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;
Nicolas Fella's avatar
Nicolas Fella committed
224
    const qint64 oldDate = item->data(DateRole).toLongLong(&oldDateExists);
225
226
    if (!oldDateExists || message.date() >= oldDate) {
        // If there was no old data or incoming data is newer, update the record
227
228
        item->setData(QVariant::fromValue(message.addresses()), AddressesRole);
        item->setData(message.isOutgoing(), FromMeRole);
229
        item->setData(displayBody, Qt::ToolTipRole);
230
        item->setData(message.date(), DateRole);
231
        item->setData(message.isMultitarget(), MultitargetRole);
232
    }
233
234
235
236

    if (toadd)
        appendRow(item);
}
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

void ConversationListModel::displayContacts() {
    const QList<QSharedPointer<KPeople::PersonData>> personDataList = SmsHelper::getAllPersons();

    for(const auto& person : personDataList) {
        const QVariantList allPhoneNumbers = person->contactCustomProperty(QStringLiteral("all-phoneNumber")).toList();

        for (const QVariant& rawPhoneNumber : allPhoneNumbers) {
            //check for any duplicate phoneNumber and eliminate it
            if (!getConversationForAddress(rawPhoneNumber.toString())) {
                QStandardItem* item = new QStandardItem();
                item->setText(person->name());
                item->setIcon(person->photo());

                QList<ConversationAddress> addresses;
                addresses.append(ConversationAddress(rawPhoneNumber.toString()));
                item->setData(QVariant::fromValue(addresses), AddressesRole);

Nicolas Fella's avatar
Nicolas Fella committed
255
                const QString displayBody = i18n("%1", rawPhoneNumber.toString());
256
257
258
259
260
261
262
263
264
265
                item->setData(displayBody, Qt::ToolTipRole);
                item->setData(false, MultitargetRole);
                item->setData(qint64(INVALID_THREAD_ID), ConversationIdRole);
                item->setData(qint64(INVALID_DATE), DateRole);
                item->setData(rawPhoneNumber.toString(), SenderRole);
                appendRow(item);
            }
        }
    }
}