Changeset View
Changeset View
Standalone View
Standalone View
plugins/sms/requestconversationworker.cpp
- This file was added.
1 | /** | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Simon Redman <simon@ergotech.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "requestconversationworker.h" | ||||
22 | | ||||
23 | #include "conversationsdbusinterface.h" | ||||
24 | | ||||
25 | #include <QObject> | ||||
26 | | ||||
27 | RequestConversationWorker::RequestConversationWorker(const qint32& conversationID, int start, int end, ConversationsDbusInterface* interface) : | ||||
28 | //QObject(interface) | ||||
29 | conversationID(conversationID) | ||||
30 | , start(start) | ||||
31 | , end(end) | ||||
32 | , parent(interface) | ||||
33 | , m_thread(new QThread) | ||||
34 | { | ||||
35 | this->moveToThread(m_thread); | ||||
36 | connect(m_thread, &QThread::started, | ||||
37 | this, &RequestConversationWorker::handleRequestConversation); | ||||
38 | connect(m_thread, &QThread::finished, | ||||
39 | m_thread, &QObject::deleteLater); | ||||
40 | connect(this, &RequestConversationWorker::finished, | ||||
41 | m_thread, &QThread::quit); | ||||
42 | connect(this, &RequestConversationWorker::finished, | ||||
43 | this, &QObject::deleteLater); | ||||
44 | } | ||||
45 | | ||||
46 | void RequestConversationWorker::handleRequestConversation() | ||||
47 | { | ||||
48 | auto messagesList = parent->getConversation(conversationID); | ||||
49 | | ||||
50 | if (messagesList.isEmpty()) { | ||||
51 | // Since there are no messages in the conversation, it's likely that it is a junk ID, but go ahead anyway | ||||
52 | qCWarning(KDECONNECT_CONVERSATIONS) << "Got a conversationID for a conversation with no messages!" << conversationID; | ||||
53 | } | ||||
54 | | ||||
55 | if (messagesList.length() <= end) { | ||||
56 | // If we don't have enough messages in cache, go get some more | ||||
57 | // TODO: Make Android interface capable of requesting small window of messages | ||||
58 | parent->updateConversation(conversationID); | ||||
59 | messagesList = parent->getConversation(conversationID); | ||||
60 | } | ||||
61 | | ||||
62 | // Messages are sorted in ascending order of keys, meaning the front of the list has the oldest | ||||
63 | // messages (smallest timestamp number) | ||||
64 | // Therefore, return the end of the list first (most recent messages) | ||||
65 | int i = start; | ||||
66 | for(auto it = messagesList.crbegin() + start; it != messagesList.crend(); ++it) { | ||||
67 | Q_EMIT conversationMessageRead(it->toVariant()); | ||||
68 | i++; | ||||
69 | if (i >= end) { | ||||
70 | break; | ||||
71 | } | ||||
72 | } | ||||
73 | | ||||
74 | Q_EMIT finished(); | ||||
75 | } | ||||
76 | | ||||
77 | void RequestConversationWorker::work() | ||||
78 | { | ||||
79 | m_thread->start(); | ||||
80 | } |