Changeset View
Changeset View
Standalone View
Standalone View
plugins/telephony/conversationsdbusinterface.cpp
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Line(s) | |||||
45 | 45 | | |||
46 | QStringList ConversationsDbusInterface::activeConversations() | 46 | QStringList ConversationsDbusInterface::activeConversations() | ||
47 | { | 47 | { | ||
48 | return m_conversations.keys(); | 48 | return m_conversations.keys(); | ||
49 | } | 49 | } | ||
50 | 50 | | |||
51 | void ConversationsDbusInterface::requestConversation(const QString& conversationID, int start, int end) | 51 | void ConversationsDbusInterface::requestConversation(const QString& conversationID, int start, int end) | ||
52 | { | 52 | { | ||
53 | const auto messagesList = m_conversations[conversationID]; | 53 | const auto messagesList = m_conversations[conversationID].values(); | ||
54 | 54 | | |||
55 | if (messagesList.isEmpty()) | 55 | if (messagesList.isEmpty()) | ||
56 | { | 56 | { | ||
57 | // Since there are no messages in the conversation, it's likely that it is a junk ID, but go ahead anyway | 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; | 58 | qCWarning(KDECONNECT_PLUGIN_TELEPHONY) << "Got a conversationID for a conversation with no messages!" << conversationID; | ||
59 | } | 59 | } | ||
60 | 60 | | |||
61 | m_telephonyInterface.requestConversation(conversationID); | 61 | m_telephonyInterface.requestConversation(conversationID); | ||
62 | 62 | | |||
63 | for(int i=start; i<end; ++i) { | 63 | // Messages are sorted in ascending order of keys, meaning the front of the list has the oldest | ||
64 | if (i<messagesList.size()) { | 64 | // messages (smallest timestamp number) | ||
65 | Q_EMIT conversationMessageReceived(messagesList.at(i).toVariant(), i); | 65 | // Therefore, return the end of the list first (most recent messages) | ||
66 | int i = start; | ||||
67 | for(auto it = messagesList.crbegin(); it != messagesList.crend(); ++it) | ||||
68 | { | ||||
69 | Q_EMIT conversationMessageReceived(it->toVariant(), i); | ||||
70 | i++; | ||||
71 | if (i >= end) | ||||
apol: `i >= end` | |||||
72 | { | ||||
73 | break; | ||||
66 | } | 74 | } | ||
67 | } | 75 | } | ||
68 | } | 76 | } | ||
69 | 77 | | |||
70 | void ConversationsDbusInterface::addMessage(const ConversationMessage &message) | 78 | void ConversationsDbusInterface::addMessage(const ConversationMessage &message) | ||
71 | { | 79 | { | ||
72 | const QString& threadId = QString::number(message.threadID()); | 80 | const QString& threadId = QString::number(message.threadID()); | ||
73 | 81 | | |||
74 | if (m_known_messages[threadId].contains(message.uID())) | 82 | if (m_known_messages[threadId].contains(message.uID())) | ||
75 | { | 83 | { | ||
76 | // This message has already been processed. Don't do anything. | 84 | // This message has already been processed. Don't do anything. | ||
77 | return; | 85 | return; | ||
78 | } | 86 | } | ||
79 | 87 | | |||
80 | // Store the Message in the list corresponding to its thread | 88 | // Store the Message in the list corresponding to its thread | ||
81 | bool newConversation = !m_conversations.contains(threadId); | 89 | bool newConversation = !m_conversations.contains(threadId); | ||
82 | m_conversations[threadId].append(message); | 90 | m_conversations[threadId].insert(message.date(), message); | ||
83 | m_known_messages[threadId].insert(message.uID()); | 91 | m_known_messages[threadId].insert(message.uID()); | ||
84 | 92 | | |||
85 | // Tell the world about what just happened | 93 | // Tell the world about what just happened | ||
86 | if (newConversation) | 94 | if (newConversation) | ||
87 | { | 95 | { | ||
88 | Q_EMIT conversationCreated(threadId); | 96 | Q_EMIT conversationCreated(threadId); | ||
89 | } else | 97 | } else | ||
90 | { | 98 | { | ||
Show All 10 Lines | |||||
101 | { | 109 | { | ||
102 | const auto messagesList = m_conversations[conversationID]; | 110 | const auto messagesList = m_conversations[conversationID]; | ||
103 | if (messagesList.isEmpty()) | 111 | if (messagesList.isEmpty()) | ||
104 | { | 112 | { | ||
105 | // Since there are no messages in the conversation, we can't do anything sensible | 113 | // 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!"; | 114 | qCWarning(KDECONNECT_PLUGIN_TELEPHONY) << "Got a conversationID for a conversation with no messages!"; | ||
107 | return; | 115 | return; | ||
108 | } | 116 | } | ||
109 | const QString& address = messagesList.front().address(); | 117 | // Caution: | ||
118 | // This method assumes that the address of any message (in this case, whichever one pops out | ||||
119 | // with .first()) will be the same. This works fine for single-target SMS but might break down | ||||
120 | // for group MMS, etc. | ||||
121 | const QString& address = messagesList.first().address(); | ||||
110 | m_telephonyInterface.sendSms(address, message); | 122 | m_telephonyInterface.sendSms(address, message); | ||
111 | } | 123 | } | ||
112 | 124 | | |||
113 | void ConversationsDbusInterface::requestAllConversationThreads() | 125 | void ConversationsDbusInterface::requestAllConversationThreads() | ||
114 | { | 126 | { | ||
115 | // Prepare the list of conversations by requesting the first in every thread | 127 | // Prepare the list of conversations by requesting the first in every thread | ||
116 | m_telephonyInterface.requestAllConversations(); | 128 | m_telephonyInterface.requestAllConversations(); | ||
117 | } | 129 | } | ||
118 | 130 | | |||
119 | QString ConversationsDbusInterface::newId() | 131 | QString ConversationsDbusInterface::newId() | ||
120 | { | 132 | { | ||
121 | return QString::number(++m_lastId); | 133 | return QString::number(++m_lastId); | ||
122 | } | 134 | } |
i >= end