conversationsdbusinterface.cpp 8.48 KB
Newer Older
1
/**
2
 * SPDX-FileCopyrightText: 2018 Simon Redman <simon@ergotech.com>
3
 *
4
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 6 7 8 9 10
 */

#include "conversationsdbusinterface.h"
#include "interfaces/dbusinterfaces.h"
#include "interfaces/conversationmessage.h"

11 12
#include "requestconversationworker.h"

13 14 15 16 17
#include <QDBusConnection>

#include <core/device.h>
#include <core/kdeconnectplugin.h>

18
#include "kdeconnect_conversations_debug.h"
19

20 21
QMap<QString, ConversationsDbusInterface*> ConversationsDbusInterface::liveConversationInterfaces;

22 23
ConversationsDbusInterface::ConversationsDbusInterface(KdeConnectPlugin* plugin)
    : QDBusAbstractAdaptor(const_cast<Device*>(plugin->device()))
Nicolas Fella's avatar
Nicolas Fella committed
24
    , m_device(plugin->device()->id())
25
    , m_lastId(0)
Nicolas Fella's avatar
Nicolas Fella committed
26
    , m_smsInterface(m_device)
27 28
{
    ConversationMessage::registerDbusType();
29 30 31

    // Check for an existing interface for the same device
    // If there is already an interface for this device, we can safely delete is since we have just replaced it
Nicolas Fella's avatar
Nicolas Fella committed
32
    const auto& oldInterfaceItr = ConversationsDbusInterface::liveConversationInterfaces.find(m_device);
33 34 35 36 37 38
    if (oldInterfaceItr != ConversationsDbusInterface::liveConversationInterfaces.end()) {
        ConversationsDbusInterface* oldInterface = oldInterfaceItr.value();
        oldInterface->deleteLater();
        ConversationsDbusInterface::liveConversationInterfaces.erase(oldInterfaceItr);
    }

Nicolas Fella's avatar
Nicolas Fella committed
39
    ConversationsDbusInterface::liveConversationInterfaces[m_device] = this;
40 41 42 43
}

ConversationsDbusInterface::~ConversationsDbusInterface()
{
44 45 46 47 48 49
    // Wake all threads which were waiting for a reply from this interface
    // This might result in some noise on dbus, but it's better than leaking a bunch of resources!
    waitingForMessagesLock.lock();
    conversationsWaitingForMessages.clear();
    waitingForMessages.wakeAll();
    waitingForMessagesLock.unlock();
50 51

    // Erase this interface from the list of known interfaces
Nicolas Fella's avatar
Nicolas Fella committed
52
    const auto myIterator = ConversationsDbusInterface::liveConversationInterfaces.find(m_device);
53
    ConversationsDbusInterface::liveConversationInterfaces.erase(myIterator);
54 55
}

56
QVariantList ConversationsDbusInterface::activeConversations()
57
{
58 59 60 61 62 63 64 65 66 67 68 69
    QList<QVariant> toReturn;
    toReturn.reserve(m_conversations.size());

    for (auto it = m_conversations.cbegin(); it != m_conversations.cend(); ++it) {
        const auto& conversation = it.value().values();
        if (conversation.isEmpty()) {
            // This should really never happen because we create a conversation at the same time
            // as adding a message, but better safe than sorry
            qCWarning(KDECONNECT_CONVERSATIONS)
                    << "Conversation with ID" << it.key() << "is unexpectedly empty";
            break;
        }
70
        const QVariant& message = QVariant::fromValue<ConversationMessage>(*conversation.crbegin());
71 72 73 74
        toReturn.append(message);
    }

    return toReturn;
75 76
}

77
void ConversationsDbusInterface::requestConversation(const qint64& conversationID, int start, int end)
78
{
79 80 81 82 83 84 85 86 87 88
    if (start < 0 || end < 0) {
        qCWarning(KDECONNECT_CONVERSATIONS) << "requestConversation" << "Start and end must be >= 0";
        return;
    }

    if (end - start < 0) {
        qCWarning(KDECONNECT_CONVERSATIONS) << "requestConversation" <<"Start must be before end";
        return;
    }

89 90 91 92 93 94
    RequestConversationWorker* worker = new RequestConversationWorker(conversationID, start, end, this);
    connect(worker, &RequestConversationWorker::conversationMessageRead,
            this, &ConversationsDbusInterface::conversationUpdated,
            Qt::QueuedConnection);
    worker->work();
}
95

96 97 98 99 100
void ConversationsDbusInterface::requestAttachmentFile(const qint64& partID, const QString& uniqueIdentifier)
{
    m_smsInterface.getAttachment(partID, uniqueIdentifier);
}

101 102
void ConversationsDbusInterface::addMessages(const QList<ConversationMessage> &messages)
{
103
    QSet<qint64> updatedConversationIDs;
104

105 106
    for (const auto& message : messages) {
        const qint32& threadId = message.threadID();
107

108 109 110 111 112 113 114
        // We might discover that there are no new messages in this conversation, thus calling it
        // "updated" might turn out to be a bit misleading
        // However, we need to report it as updated regardless, for the case where we have already
        // cached every message of the conversation but we have received a request for more, otherwise
        // we will never respond to that request
        updatedConversationIDs.insert(message.threadID());

115 116 117
        if (m_known_messages[threadId].contains(message.uID())) {
            // This message has already been processed. Don't do anything.
            continue;
118 119
        }

120 121 122 123
        // Store the Message in the list corresponding to its thread
        bool newConversation = !m_conversations.contains(threadId);
        const auto& threadPosition = m_conversations[threadId].insert(message.date(), message);
        m_known_messages[threadId].insert(message.uID());
124

125 126
        // If this message was inserted at the end of the list, it is the latest message in the conversation
        bool latestMessage = threadPosition == m_conversations[threadId].end() - 1;
127

128 129
        // Tell the world about what just happened
        if (newConversation) {
130
            Q_EMIT conversationCreated(QDBusVariant(QVariant::fromValue(message)));
131
        } else if (latestMessage) {
132
            Q_EMIT conversationUpdated(QDBusVariant(QVariant::fromValue(message)));
133
        }
134
    }
135

136 137 138 139 140 141 142
    // It feels bad to go through the set of updated conversations again,
    // but also there are not many times that updatedConversationIDs will be more than one
    for (qint64 conversationID : updatedConversationIDs) {
        quint64 numMessages = m_known_messages[conversationID].size();
        Q_EMIT conversationLoaded(conversationID, numMessages);
    }

143 144 145 146 147
    waitingForMessagesLock.lock();
    // Remove the waiting flag for all conversations which we just processed
    conversationsWaitingForMessages.subtract(updatedConversationIDs);
    waitingForMessages.wakeAll();
    waitingForMessagesLock.unlock();
148 149 150 151 152
}

void ConversationsDbusInterface::removeMessage(const QString& internalId)
{
    // TODO: Delete the specified message from our internal structures
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
153
    Q_UNUSED(internalId);
154 155
}

156 157 158 159 160
QList<ConversationMessage> ConversationsDbusInterface::getConversation(const qint64& conversationID) const
{
    return m_conversations.value(conversationID).values();
}

161
void ConversationsDbusInterface::updateConversation(const qint64& conversationID)
162 163
{
    waitingForMessagesLock.lock();
164 165 166 167 168 169
    if (conversationsWaitingForMessages.contains(conversationID)) {
        // This conversation is already being waited on, don't allow more than one thread to wait at a time
        qCDebug(KDECONNECT_CONVERSATIONS) << "Not allowing two threads to wait for conversationID" << conversationID;
        waitingForMessagesLock.unlock();
        return;
    }
170 171 172 173 174 175 176 177 178
    qCDebug(KDECONNECT_CONVERSATIONS) << "Requesting conversation with ID" << conversationID << "from remote";
    conversationsWaitingForMessages.insert(conversationID);
    m_smsInterface.requestConversation(conversationID);
    while (conversationsWaitingForMessages.contains(conversationID)) {
        waitingForMessages.wait(&waitingForMessagesLock);
    }
    waitingForMessagesLock.unlock();
}

179
void ConversationsDbusInterface::replyToConversation(const qint64& conversationID, const QString& message, const QVariantList& attachmentUrls)
180 181
{
    const auto messagesList = m_conversations[conversationID];
182
    if (messagesList.isEmpty()) {
183
        qCWarning(KDECONNECT_CONVERSATIONS) << "Got a conversationID for a conversation with no messages!";
184 185
        return;
    }
186

187
    const QList<ConversationAddress>& addressList = messagesList.first().addresses();
188
    QVariantList addresses;
189

190
    for (const auto& address : addressList) {
191
        addresses << QVariant::fromValue(address);
192 193
    }

194
    m_smsInterface.sendSms(addresses, message, attachmentUrls, messagesList.first().subID());
195 196
}

197 198
void ConversationsDbusInterface::sendWithoutConversation(const QVariantList& addresses, const QString& message, const QVariantList& attachmentUrls) {
    m_smsInterface.sendSms(addresses, message, attachmentUrls);
199 200
}

201 202 203
void ConversationsDbusInterface::requestAllConversationThreads()
{
    // Prepare the list of conversations by requesting the first in every thread
204
    m_smsInterface.requestAllConversations();
205 206 207 208 209 210
}

QString ConversationsDbusInterface::newId()
{
    return QString::number(++m_lastId);
}
211 212 213 214

void ConversationsDbusInterface::attachmentDownloaded(const QString& filePath, const QString& fileName) {
    Q_EMIT attachmentReceived(filePath, fileName);
}