conversationlistmodel.h 3.64 KB
Newer Older
1
/**
2
 * Copyright (C) 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
3
 * Copyright (C) 2018 Simon Redman <simon@ergotech.com>
4
 *
5 6 7 8 9 10 11
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */

#ifndef CONVERSATIONLISTMODEL_H
#define CONVERSATIONLISTMODEL_H

#include <QStandardItemModel>
#include <QLoggingCategory>
#include <QQmlParserStatus>

#include "interfaces/conversationmessage.h"
#include "interfaces/dbusinterfaces.h"

Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL)


Simon Redman's avatar
Simon Redman committed
35
class OurSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParserStatus
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
    Q_OBJECT
    Q_INTERFACES(QQmlParserStatus)
    Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder)
public:

    Qt::SortOrder sortOrder() const { return m_sortOrder; }
    void setSortOrder(Qt::SortOrder sortOrder) {
        if (m_sortOrder != sortOrder) {
            m_sortOrder = sortOrder;
            sortNow();
        }
    }
    void classBegin() override {}
    void componentComplete() override {
        m_completed = true;
        sortNow();
    }

Nicolas Fella's avatar
Nicolas Fella committed
55 56
    OurSortFilterProxyModel();
    ~OurSortFilterProxyModel();
57

58 59 60 61 62 63 64 65 66 67
private:
    void sortNow() {
        if (m_completed && dynamicSortFilter())
            sort(0, m_sortOrder);
    }

    bool m_completed = false;
    Qt::SortOrder m_sortOrder = Qt::AscendingOrder;
};

Simon Redman's avatar
Simon Redman committed
68
class ConversationListModel
69 70 71
    : public QStandardItemModel
{
    Q_OBJECT
72
    Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged)
73 74

public:
Nicolas Fella's avatar
Nicolas Fella committed
75 76
    ConversationListModel(QObject* parent = nullptr);
    ~ConversationListModel();
77 78

    enum Roles {
79
        /* Roles which apply while working as a single message */
80
        FromMeRole = Qt::UserRole,
81 82 83 84 85 86
        SenderRole,      // The sender of the message. Undefined if this is an outgoing message
        DateRole,        // The date of this message
        /* Roles which apply while working as the head of a conversation */
        AddressesRole,      // The Addresses involved in the conversation
        ConversationIdRole, // The ThreadID of the conversation
        MultitargetRole,    // Indicate that this conversation is multitarget
87 88 89 90
    };
    Q_ENUM(Roles)

    QString deviceId() const { return m_deviceId; }
Nicolas Fella's avatar
Nicolas Fella committed
91
    void setDeviceId(const QString &/*deviceId*/);
92

93 94
    Q_SCRIPTABLE void refresh();

95
public Q_SLOTS:
96 97 98
    void handleCreatedConversation(const QDBusVariant& msg);
    void handleConversationUpdated(const QDBusVariant& msg);
    void createRowFromMessage(const ConversationMessage& message);
Nicolas Fella's avatar
Nicolas Fella committed
99
    void printDBusError(const QDBusError& error);
100

101 102 103
Q_SIGNALS:
    void deviceIdChanged();

104 105 106 107
private:
    /**
     * Get all conversations currently known by the conversationsInterface, if any
     */
Nicolas Fella's avatar
Nicolas Fella committed
108
    void prepareConversationsList();
109

Nicolas Fella's avatar
Nicolas Fella committed
110
    QStandardItem* conversationForThreadId(qint32 threadId);
111 112 113 114 115 116

    DeviceConversationsDbusInterface* m_conversationsInterface;
    QString m_deviceId;
};

#endif // CONVERSATIONLISTMODEL_H