Changeset View
Changeset View
Standalone View
Standalone View
smsapp/conversationlistmodel.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * This file is part of KDE Telepathy Chat | ||||
3 | * | ||||
4 | * Copyright (C) 2018 Aleix Pol Gonzalez <aleixpol@kde.org> | ||||
5 | * | ||||
6 | * This library is free software; you can redistribute it and/or | ||||
7 | * modify it under the terms of the GNU Lesser General Public | ||||
8 | * License as published by the Free Software Foundation; either | ||||
9 | * version 2.1 of the License, or (at your option) any later version. | ||||
10 | * | ||||
11 | * This library is distributed in the hope that it will be useful, | ||||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
14 | * Lesser General Public License for more details. | ||||
15 | * | ||||
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 | ||||
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||||
19 | */ | ||||
20 | | ||||
21 | #ifndef CONVERSATIONLISTMODEL_H | ||||
22 | #define CONVERSATIONLISTMODEL_H | ||||
23 | | ||||
24 | #include <QStandardItemModel> | ||||
25 | #include <QLoggingCategory> | ||||
26 | #include <QQmlParserStatus> | ||||
27 | #include <KPeople/kpeople/personsmodel.h> | ||||
28 | #include <KPeople/kpeople/persondata.h> | ||||
29 | | ||||
30 | #include "interfaces/conversationmessage.h" | ||||
31 | #include "interfaces/dbusinterfaces.h" | ||||
32 | | ||||
33 | #include "interfaces/kdeconnectinterfaces_export.h" | ||||
34 | | ||||
35 | Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) | ||||
36 | | ||||
37 | | ||||
38 | class OurSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParserStatus | ||||
39 | { | ||||
40 | Q_OBJECT | ||||
41 | Q_INTERFACES(QQmlParserStatus) | ||||
42 | Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder) | ||||
43 | public: | ||||
44 | | ||||
45 | Qt::SortOrder sortOrder() const { return m_sortOrder; } | ||||
46 | void setSortOrder(Qt::SortOrder sortOrder) { | ||||
47 | if (m_sortOrder != sortOrder) { | ||||
48 | m_sortOrder = sortOrder; | ||||
49 | sortNow(); | ||||
50 | } | ||||
51 | } | ||||
52 | void classBegin() override {} | ||||
53 | void componentComplete() override { | ||||
54 | m_completed = true; | ||||
55 | sortNow(); | ||||
56 | } | ||||
57 | | ||||
58 | private: | ||||
59 | void sortNow() { | ||||
60 | if (m_completed && dynamicSortFilter()) | ||||
61 | sort(0, m_sortOrder); | ||||
62 | } | ||||
63 | | ||||
64 | bool m_completed = false; | ||||
65 | Qt::SortOrder m_sortOrder = Qt::AscendingOrder; | ||||
66 | }; | ||||
67 | | ||||
68 | class KDECONNECTINTERFACES_EXPORT ConversationListModel | ||||
69 | : public QStandardItemModel | ||||
70 | { | ||||
71 | Q_OBJECT | ||||
72 | Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId) | ||||
73 | | ||||
74 | public: | ||||
75 | ConversationListModel(QObject* parent = nullptr); | ||||
76 | ~ConversationListModel(); | ||||
77 | | ||||
78 | enum Roles { | ||||
79 | FromMeRole = Qt::UserRole, | ||||
80 | PersonUriRole, | ||||
81 | AddressRole, | ||||
82 | ConversationIdRole, | ||||
83 | DateRole, | ||||
84 | }; | ||||
85 | Q_ENUM(Roles) | ||||
86 | | ||||
87 | QString deviceId() const { return m_deviceId; } | ||||
88 | void setDeviceId(const QString &/*deviceId*/); | ||||
89 | | ||||
90 | public Q_SLOTS: | ||||
91 | void handleCreatedConversation(const QString& conversationId); | ||||
92 | void createRowFromMessage(const QVariantMap& message, int row); | ||||
93 | void printDBusError(const QDBusError& error); | ||||
94 | | ||||
95 | private: | ||||
96 | /** | ||||
97 | * Get all conversations currently known by the conversationsInterface, if any | ||||
98 | */ | ||||
99 | void prepareConversationsList(); | ||||
100 | | ||||
101 | /** | ||||
102 | * Get the data for a particular person given their contact address | ||||
103 | */ | ||||
104 | KPeople::PersonData* lookupPersonByAddress(const QString& address); | ||||
105 | | ||||
106 | /** | ||||
107 | * Simplify a phone number to a known form | ||||
108 | */ | ||||
109 | QString canonicalizePhoneNumber(const QString& phoneNumber); | ||||
110 | | ||||
111 | QStandardItem* conversationForThreadId(qint32 threadId); | ||||
112 | | ||||
113 | DeviceConversationsDbusInterface* m_conversationsInterface; | ||||
114 | QString m_deviceId; | ||||
115 | KPeople::PersonsModel m_people; | ||||
116 | }; | ||||
117 | | ||||
118 | #endif // CONVERSATIONLISTMODEL_H |