File indexing completed on 2024-12-01 04:33:08

0001 /**
0002  * SPDX-FileCopyrightText: 2018 Simon Redman <simon@ergotech.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "requestconversationworker.h"
0008 
0009 #include "conversationsdbusinterface.h"
0010 
0011 #include <QObject>
0012 
0013 RequestConversationWorker::RequestConversationWorker(const qint64 &conversationID, int start, int end, ConversationsDbusInterface *interface)
0014     : // QObject(interface)
0015     conversationID(conversationID)
0016     , start(start)
0017     , parent(interface)
0018     , m_thread(new QThread)
0019 {
0020     Q_ASSERT(end >= start && "Not allowed to have a negative-length range");
0021     howMany = end - start;
0022 
0023     this->moveToThread(m_thread);
0024     connect(m_thread, &QThread::started, this, &RequestConversationWorker::handleRequestConversation);
0025     connect(m_thread, &QThread::finished, m_thread, &QObject::deleteLater);
0026     connect(this, &RequestConversationWorker::finished, m_thread, &QThread::quit);
0027     connect(this, &RequestConversationWorker::finished, this, &QObject::deleteLater);
0028 }
0029 
0030 void RequestConversationWorker::handleRequestConversation()
0031 {
0032     auto messagesList = parent->getConversation(conversationID);
0033 
0034     if (messagesList.isEmpty()) {
0035         // Since there are no messages in the conversation, it's likely that it is a junk ID, but go ahead anyway
0036         qCWarning(KDECONNECT_CONVERSATIONS) << "Got a conversationID for a conversation with no messages!" << conversationID;
0037     }
0038 
0039     // In case the remote takes awhile to respond, we should go ahead and do anything we can from the cache
0040     size_t numHandled = replyForConversation(messagesList, start, howMany);
0041 
0042     if (numHandled < howMany) {
0043         // In this case, the cache wasn't able to satisfy the request fully. Get more.
0044 
0045         size_t numRemaining = howMany - numHandled;
0046         parent->updateConversation(conversationID);
0047         messagesList = parent->getConversation(conversationID);
0048         // ConversationsDbusInterface::updateConversation blocks until it sees new messages in the requested conversation
0049         replyForConversation(messagesList, start + numHandled, numRemaining);
0050     } else {
0051         // The cache was able to fully satisfy the request but we need to check that it isn't running dry
0052 
0053         size_t numCachedMessages = messagesList.count();
0054         size_t requestEnd = start + numHandled;
0055         size_t numRemainingMessages = numCachedMessages - requestEnd;
0056         double percentRemaining = ((double)numRemainingMessages / numCachedMessages) * 100;
0057 
0058         if (percentRemaining < CACHE_LOW_WATER_MARK_PERCENT || numRemainingMessages < MIN_NUMBER_TO_REQUEST) {
0059             parent->updateConversation(conversationID);
0060         }
0061     }
0062 
0063     Q_EMIT finished();
0064 }
0065 
0066 size_t RequestConversationWorker::replyForConversation(const QList<ConversationMessage> &conversation, int start, size_t howMany)
0067 {
0068     Q_ASSERT(start >= 0);
0069     // Messages are sorted in ascending order of keys, meaning the front of the list has the oldest
0070     // messages (smallest timestamp number)
0071     // Therefore, return the end of the list first (most recent messages)
0072     size_t i = 0;
0073     for (auto it = conversation.crbegin() + start; it != conversation.crend(); ++it) {
0074         if (i >= howMany) {
0075             break;
0076         }
0077         Q_EMIT conversationMessageRead(QDBusVariant(QVariant::fromValue(*it)));
0078         i++;
0079     }
0080 
0081     return i;
0082 }
0083 
0084 void RequestConversationWorker::work()
0085 {
0086     m_thread->start();
0087 }
0088 
0089 #include "moc_requestconversationworker.cpp"