Changeset View
Changeset View
Standalone View
Standalone View
plugins/sms/conversationsdbusinterface.cpp
Show All 37 Lines | |||||
38 | { | 38 | { | ||
39 | ConversationMessage::registerDbusType(); | 39 | ConversationMessage::registerDbusType(); | ||
40 | } | 40 | } | ||
41 | 41 | | |||
42 | ConversationsDbusInterface::~ConversationsDbusInterface() | 42 | ConversationsDbusInterface::~ConversationsDbusInterface() | ||
43 | { | 43 | { | ||
44 | } | 44 | } | ||
45 | 45 | | |||
46 | QStringList ConversationsDbusInterface::activeConversations() | 46 | QVariantList ConversationsDbusInterface::activeConversations() | ||
47 | { | 47 | { | ||
48 | return m_conversations.keys(); | 48 | QList<QVariant> toReturn; | ||
49 | toReturn.reserve(m_conversations.size()); | ||||
50 | | ||||
51 | for (auto it = m_conversations.cbegin(); it != m_conversations.cend(); ++it) { | ||||
52 | const auto& conversation = it.value().values(); | ||||
53 | if (conversation.isEmpty()) { | ||||
apol: Looks like it's casting twice? How about `toReturn += conversation.cbegin()->toVariant()`?
Also… | |||||
The double cast was because I was trying to resolve a (different) issue. Good catch on the "null" dereference, thank you! sredman: The double cast was because I was trying to resolve a (different) issue. Good catch on the… | |||||
54 | // This should really never happen because we create a conversation at the same time | ||||
55 | // as adding a message, but better safe than sorry | ||||
56 | qCWarning(KDECONNECT_CONVERSATIONS) | ||||
57 | << "Conversation with ID" << it.key() << "is unexpectedly empty"; | ||||
58 | break; | ||||
59 | } | ||||
60 | const QVariantMap& message = (*conversation.crbegin()).toVariant(); | ||||
61 | toReturn.append(message); | ||||
62 | } | ||||
63 | | ||||
64 | return toReturn; | ||||
49 | } | 65 | } | ||
50 | 66 | | |||
51 | void ConversationsDbusInterface::requestConversation(const QString& conversationID, int start, int end) | 67 | void ConversationsDbusInterface::requestConversation(const QString& conversationID, int start, int end) | ||
52 | { | 68 | { | ||
53 | const auto messagesList = m_conversations[conversationID].values(); | 69 | const auto messagesList = m_conversations[conversationID].values(); | ||
54 | 70 | | |||
55 | if (messagesList.isEmpty()) { | 71 | if (messagesList.isEmpty()) { | ||
56 | // Since there are no messages in the conversation, it's likely that it is a junk ID, but go ahead anyway | 72 | // Since there are no messages in the conversation, it's likely that it is a junk ID, but go ahead anyway | ||
57 | qCWarning(KDECONNECT_CONVERSATIONS) << "Got a conversationID for a conversation with no messages!" << conversationID; | 73 | qCWarning(KDECONNECT_CONVERSATIONS) << "Got a conversationID for a conversation with no messages!" << conversationID; | ||
58 | } | 74 | } | ||
59 | 75 | | |||
76 | // TODO: Check local cache before requesting new messages | ||||
77 | // TODO: Make Android interface capable of requesting small window of messages | ||||
60 | m_smsInterface.requestConversation(conversationID); | 78 | m_smsInterface.requestConversation(conversationID); | ||
61 | 79 | | |||
62 | // Messages are sorted in ascending order of keys, meaning the front of the list has the oldest | 80 | // Messages are sorted in ascending order of keys, meaning the front of the list has the oldest | ||
63 | // messages (smallest timestamp number) | 81 | // messages (smallest timestamp number) | ||
64 | // Therefore, return the end of the list first (most recent messages) | 82 | // Therefore, return the end of the list first (most recent messages) | ||
65 | int i = start; | 83 | int i = start; | ||
66 | for(auto it = messagesList.crbegin(); it != messagesList.crend(); ++it) { | 84 | for(auto it = messagesList.crbegin() + start; it != messagesList.crend(); ++it) { | ||
apol: Should probably offset it by start? | |||||
I forgot I could use a different dbus viewer to make the call: Now I have been able to test this and it appears to work! sredman: I forgot I could use a different dbus viewer to make the call: Now I have been able to test… | |||||
67 | Q_EMIT conversationMessageReceived(it->toVariant(), i); | 85 | Q_EMIT conversationMessageReceived(it->toVariant(), i); | ||
68 | i++; | 86 | i++; | ||
69 | if (i >= end) { | 87 | if (i >= end) { | ||
70 | break; | 88 | break; | ||
71 | } | 89 | } | ||
72 | } | 90 | } | ||
73 | } | 91 | } | ||
74 | 92 | | |||
75 | void ConversationsDbusInterface::addMessage(const ConversationMessage &message) | 93 | void ConversationsDbusInterface::addMessage(const ConversationMessage &message) | ||
76 | { | 94 | { | ||
77 | const QString& threadId = QString::number(message.threadID()); | 95 | const QString& threadId = QString::number(message.threadID()); | ||
78 | 96 | | |||
79 | if (m_known_messages[threadId].contains(message.uID())) { | 97 | if (m_known_messages[threadId].contains(message.uID())) { | ||
80 | // This message has already been processed. Don't do anything. | 98 | // This message has already been processed. Don't do anything. | ||
81 | return; | 99 | return; | ||
82 | } | 100 | } | ||
83 | 101 | | |||
84 | // Store the Message in the list corresponding to its thread | 102 | // Store the Message in the list corresponding to its thread | ||
85 | bool newConversation = !m_conversations.contains(threadId); | 103 | bool newConversation = !m_conversations.contains(threadId); | ||
86 | m_conversations[threadId].insert(message.date(), message); | 104 | m_conversations[threadId].insert(message.date(), message); | ||
87 | m_known_messages[threadId].insert(message.uID()); | 105 | m_known_messages[threadId].insert(message.uID()); | ||
88 | 106 | | |||
89 | // Tell the world about what just happened | 107 | // Tell the world about what just happened | ||
90 | if (newConversation) { | 108 | if (newConversation) { | ||
91 | Q_EMIT conversationCreated(threadId); | 109 | Q_EMIT conversationCreated(message.toVariant()); | ||
92 | } else { | 110 | } else { | ||
93 | Q_EMIT conversationUpdated(message.toVariant()); | 111 | Q_EMIT conversationUpdated(message.toVariant()); | ||
94 | } | 112 | } | ||
95 | } | 113 | } | ||
96 | 114 | | |||
97 | void ConversationsDbusInterface::removeMessage(const QString& internalId) | 115 | void ConversationsDbusInterface::removeMessage(const QString& internalId) | ||
98 | { | 116 | { | ||
99 | // TODO: Delete the specified message from our internal structures | 117 | // TODO: Delete the specified message from our internal structures | ||
Show All 28 Lines |
Looks like it's casting twice? How about toReturn += conversation.cbegin()->toVariant()?
Also we probably should make sure it's not empty.