Changeset View
Changeset View
Standalone View
Standalone View
smsapp/conversationmodel.cpp
Show All 13 Lines | |||||
14 | * Lesser General Public License for more details. | 14 | * Lesser General Public License for more details. | ||
15 | * | 15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | 16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this library; if not, write to the Free Software | 17 | * License along with this library; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "conversationmodel.h" | 21 | #include "conversationmodel.h" | ||
22 | #include <QLoggingCategory> | ||||
23 | #include "interfaces/conversationmessage.h" | ||||
24 | | ||||
25 | Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATION_MODEL, "kdeconnect.sms.conversation") | ||||
22 | 26 | | |||
23 | ConversationModel::ConversationModel(QObject* parent) | 27 | ConversationModel::ConversationModel(QObject* parent) | ||
24 | : QStandardItemModel(parent) | 28 | : QStandardItemModel(parent) | ||
29 | , m_conversationsInterface(nullptr) | ||||
25 | { | 30 | { | ||
26 | auto roles = roleNames(); | 31 | auto roles = roleNames(); | ||
27 | roles.insert(FromMeRole, "fromMe"); | 32 | roles.insert(FromMeRole, "fromMe"); | ||
33 | roles.insert(DateRole, "date"); | ||||
28 | setItemRoleNames(roles); | 34 | setItemRoleNames(roles); | ||
29 | } | 35 | } | ||
30 | 36 | | |||
37 | ConversationModel::~ConversationModel() | ||||
38 | { | ||||
39 | } | ||||
40 | | ||||
31 | QString ConversationModel::threadId() const | 41 | QString ConversationModel::threadId() const | ||
32 | { | 42 | { | ||
33 | return {}; | 43 | return m_threadId; | ||
34 | } | 44 | } | ||
35 | 45 | | |||
36 | void ConversationModel::setThreadId(const QString &threadId) | 46 | void ConversationModel::setThreadId(const QString &threadId) | ||
37 | { | 47 | { | ||
48 | if (m_threadId == threadId) | ||||
49 | return; | ||||
50 | | ||||
51 | m_threadId = threadId; | ||||
38 | clear(); | 52 | clear(); | ||
39 | appendRow(new QStandardItem(threadId + QStringLiteral(" - A"))); | 53 | if (!threadId.isEmpty()) { | ||
40 | appendRow(new QStandardItem(threadId + QStringLiteral(" - A1"))); | 54 | m_conversationsInterface->requestConversation(threadId, 0, 10); | ||
41 | appendRow(new QStandardItem(threadId + QStringLiteral(" - A2"))); | 55 | } | ||
42 | appendRow(new QStandardItem(threadId + QStringLiteral(" - A3"))); | 56 | } | ||
57 | | ||||
58 | void ConversationModel::setDeviceId(const QString& deviceId) | ||||
59 | { | ||||
60 | if (deviceId == m_deviceId) | ||||
61 | return; | ||||
62 | | ||||
63 | qCDebug(KDECONNECT_SMS_CONVERSATION_MODEL) << "setDeviceId" << "of" << this; | ||||
64 | if (m_conversationsInterface) delete m_conversationsInterface; | ||||
65 | | ||||
66 | m_deviceId = deviceId; | ||||
67 | | ||||
68 | m_conversationsInterface = new DeviceConversationsDbusInterface(deviceId, this); | ||||
69 | connect(m_conversationsInterface, SIGNAL(conversationMessageReceived(QVariantMap, int)), this, SLOT(createRowFromMessage(QVariantMap, int))); | ||||
70 | } | ||||
71 | | ||||
72 | void ConversationModel::sendReplyToConversation(const QString& message) | ||||
73 | { | ||||
74 | qCDebug(KDECONNECT_SMS_CONVERSATION_MODEL) << "Trying to send" << message << "to conversation with ID" << m_threadId; | ||||
75 | m_conversationsInterface->replyToConversation(m_threadId, message); | ||||
76 | } | ||||
77 | | ||||
78 | void ConversationModel::createRowFromMessage(const QVariantMap& msg, int pos) | ||||
79 | { | ||||
80 | const ConversationMessage message(msg); | ||||
81 | auto item = new QStandardItem; | ||||
82 | item->setText(message.body()); | ||||
83 | item->setData(message.type() == ConversationMessage::MessageTypeSent, FromMeRole); | ||||
84 | item->setData(message.date(), DateRole); | ||||
85 | insertRow(pos, item); | ||||
43 | } | 86 | } |