Changeset View
Changeset View
Standalone View
Standalone View
plugins/telephony/conversationsdbusinterface.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 "conversationsdbusinterface.h" | ||||
22 | #include "interfaces/dbusinterfaces.h" | ||||
23 | #include "interfaces/conversationmessage.h" | ||||
24 | | ||||
25 | #include <QDBusConnection> | ||||
26 | | ||||
27 | #include <core/device.h> | ||||
28 | #include <core/kdeconnectplugin.h> | ||||
29 | | ||||
30 | #include "telephonyplugin.h" | ||||
31 | | ||||
32 | ConversationsDbusInterface::ConversationsDbusInterface(KdeConnectPlugin* plugin) | ||||
33 | : QDBusAbstractAdaptor(const_cast<Device*>(plugin->device())) | ||||
34 | , m_device(plugin->device()) | ||||
35 | , m_plugin(plugin) | ||||
36 | , m_lastId(0) | ||||
37 | , m_telephonyInterface(m_device->id()) | ||||
38 | { | ||||
39 | ConversationMessage::registerDbusType(); | ||||
40 | } | ||||
41 | | ||||
42 | ConversationsDbusInterface::~ConversationsDbusInterface() | ||||
43 | { | ||||
44 | } | ||||
45 | | ||||
46 | QStringList ConversationsDbusInterface::activeConversations() | ||||
47 | { | ||||
48 | return m_conversations.keys(); | ||||
49 | } | ||||
50 | | ||||
51 | void ConversationsDbusInterface::requestConversation(const QString& conversationID, int start, int end) | ||||
52 | { | ||||
53 | const auto messagesList = m_conversations[conversationID]; | ||||
54 | | ||||
55 | if (messagesList.isEmpty()) | ||||
56 | { | ||||
57 | // Since there are no messages in the conversation, it's likely that it is a junk ID, but go ahead anyway | ||||
58 | qCWarning(KDECONNECT_PLUGIN_TELEPHONY) << "Got a conversationID for a conversation with no messages!" << conversationID; | ||||
59 | } | ||||
60 | | ||||
61 | m_telephonyInterface.requestConversation(conversationID); | ||||
62 | | ||||
63 | for(int i=start; i<end; ++i) { | ||||
64 | if (i<messagesList.size()) { | ||||
65 | Q_EMIT conversationMessageReceived(messagesList.at(i).toVariant(), i); | ||||
66 | } | ||||
67 | } | ||||
68 | } | ||||
69 | | ||||
70 | void ConversationsDbusInterface::addMessage(const ConversationMessage &message) | ||||
71 | { | ||||
72 | const QString& threadId = QString::number(message.threadID()); | ||||
73 | | ||||
74 | if (m_known_messages[threadId].contains(message.uID())) | ||||
75 | { | ||||
76 | // This message has already been processed. Don't do anything. | ||||
77 | return; | ||||
78 | } | ||||
79 | | ||||
80 | // Store the Message in the list corresponding to its thread | ||||
81 | bool newConversation = !m_conversations.contains(threadId); | ||||
82 | m_conversations[threadId].append(message); | ||||
83 | m_known_messages[threadId].insert(message.uID()); | ||||
84 | | ||||
85 | // Tell the world about what just happened | ||||
86 | if (newConversation) | ||||
87 | { | ||||
88 | Q_EMIT conversationCreated(threadId); | ||||
89 | } else | ||||
90 | { | ||||
91 | Q_EMIT conversationUpdated(threadId); | ||||
92 | } | ||||
93 | } | ||||
94 | | ||||
95 | void ConversationsDbusInterface::removeMessage(const QString& internalId) | ||||
96 | { | ||||
97 | // TODO: Delete the specified message from our internal structures | ||||
98 | } | ||||
99 | | ||||
100 | void ConversationsDbusInterface::replyToConversation(const QString& conversationID, const QString& message) | ||||
101 | { | ||||
102 | const auto messagesList = m_conversations[conversationID]; | ||||
103 | if (messagesList.isEmpty()) | ||||
104 | { | ||||
105 | // Since there are no messages in the conversation, we can't do anything sensible | ||||
106 | qCWarning(KDECONNECT_PLUGIN_TELEPHONY) << "Got a conversationID for a conversation with no messages!"; | ||||
107 | return; | ||||
108 | } | ||||
109 | const QString& address = messagesList.front().address(); | ||||
110 | m_telephonyInterface.sendSms(address, message); | ||||
111 | } | ||||
112 | | ||||
113 | void ConversationsDbusInterface::requestAllConversationThreads() | ||||
114 | { | ||||
115 | // Prepare the list of conversations by requesting the first in every thread | ||||
116 | m_telephonyInterface.requestAllConversations(); | ||||
117 | } | ||||
118 | | ||||
119 | QString ConversationsDbusInterface::newId() | ||||
120 | { | ||||
121 | return QString::number(++m_lastId); | ||||
122 | } |