summary refs log tree commit diff
path: root/src/PowerlevelsEditModels.h
blob: 515fdb5659084a27b5455da26c46bd845ec1f554 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// SPDX-FileCopyrightText: 2022 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QAbstractListModel>
#include <QSortFilterProxyModel>

#include <mtx/events/power_levels.hpp>

#include "CacheStructs.h"

class PowerlevelsTypeListModel : public QAbstractListModel
{
    Q_OBJECT

signals:
    void adminLevelChanged();
    void moderatorLevelChanged();

public:
    enum Roles
    {
        DisplayName,
        Powerlevel,
        IsType,
        Moveable,
        Removeable,
    };

    explicit PowerlevelsTypeListModel(const std::string &room_id_,
                                      const mtx::events::state::PowerLevels &pl,
                                      QObject *parent = nullptr);

    QHash<int, QByteArray> roleNames() const override;
    int rowCount(const QModelIndex &) const override { return static_cast<int>(types.size()); }
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    Q_INVOKABLE bool remove(int row);
    Q_INVOKABLE bool move(int from, int to);
    Q_INVOKABLE void add(int index, QString type);
    void addRole(int64_t role);

    bool moveRows(const QModelIndex &sourceParent,
                  int sourceRow,
                  int count,
                  const QModelIndex &destinationParent,
                  int destinationChild) override;

    std::map<std::string, mtx::events::state::power_level_t, std::less<>> toEvents() const;
    mtx::events::state::power_level_t kick() const;
    mtx::events::state::power_level_t invite() const;
    mtx::events::state::power_level_t ban() const;
    mtx::events::state::power_level_t eventsDefault() const;
    mtx::events::state::power_level_t stateDefault() const;

    struct Entry
    {
        ~Entry() = default;

        std::string type;
        mtx::events::state::power_level_t pl;
    };

    std::string room_id;
    QVector<Entry> types;
    mtx::events::state::PowerLevels powerLevels_;
};

class PowerlevelsUserListModel : public QAbstractListModel
{
    Q_OBJECT

signals:
    void defaultUserLevelChanged();

public:
    enum Roles
    {
        Mxid,
        DisplayName,
        AvatarUrl,
        Powerlevel,
        IsUser,
        Moveable,
        Removeable,
    };

    explicit PowerlevelsUserListModel(const std::string &room_id_,
                                      const mtx::events::state::PowerLevels &pl,
                                      QObject *parent = nullptr);

    QHash<int, QByteArray> roleNames() const override;
    int rowCount(const QModelIndex &) const override { return static_cast<int>(users.size()); }
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    Q_INVOKABLE bool remove(int row);
    Q_INVOKABLE bool move(int from, int to);
    Q_INVOKABLE void add(int index, QString user);
    void addRole(int64_t role);

    bool moveRows(const QModelIndex &sourceParent,
                  int sourceRow,
                  int count,
                  const QModelIndex &destinationParent,
                  int destinationChild) override;

    std::map<std::string, mtx::events::state::power_level_t, std::less<>> toUsers() const;
    mtx::events::state::power_level_t usersDefault() const;

    struct Entry
    {
        ~Entry() = default;

        std::string mxid;
        mtx::events::state::power_level_t pl;
    };

    std::string room_id;
    QVector<Entry> users;
    mtx::events::state::PowerLevels powerLevels_;
};

class PowerlevelsSpacesListModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(bool applyToChildren READ applyToChildren WRITE setApplyToChildren NOTIFY
                 applyToChildrenChanged)
    Q_PROPERTY(bool overwriteDiverged READ overwriteDiverged WRITE setOverwriteDiverged NOTIFY
                 overwriteDivergedChanged)

signals:
    void applyToChildrenChanged();
    void overwriteDivergedChanged();

public:
    enum Roles
    {
        DisplayName,
        AvatarUrl,
        IsSpace,
        IsEditable,
        IsDifferentFromBase,
        IsAlreadyUpToDate,
        ApplyPermissions,
    };

    explicit PowerlevelsSpacesListModel(const std::string &room_id_,
                                        const mtx::events::state::PowerLevels &pl,
                                        QObject *parent = nullptr);

    QHash<int, QByteArray> roleNames() const override;
    int rowCount(const QModelIndex &) const override { return static_cast<int>(spaces.size()); }
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    bool
    setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override;

    bool applyToChildren() const { return applyToChildren_; }
    bool overwriteDiverged() const { return overwriteDiverged_; }

    void setApplyToChildren(bool val)
    {
        applyToChildren_ = val;
        emit applyToChildrenChanged();
        updateToDefaults();
    }
    void setOverwriteDiverged(bool val)
    {
        overwriteDiverged_ = val;
        emit overwriteDivergedChanged();
        updateToDefaults();
    }

    void updateToDefaults();

    Q_INVOKABLE void commit();

    struct Entry
    {
        ~Entry() = default;

        std::string roomid;
        mtx::events::state::PowerLevels pl;
        bool apply = false;
    };

    std::string room_id;
    QVector<Entry> spaces;
    mtx::events::state::PowerLevels oldPowerLevels_, newPowerlevels_;

    bool applyToChildren_ = true, overwriteDiverged_ = false;
};

class PowerlevelEditingModels : public QObject
{
    Q_OBJECT

    Q_PROPERTY(PowerlevelsUserListModel *users READ users CONSTANT)
    Q_PROPERTY(PowerlevelsTypeListModel *types READ types CONSTANT)
    Q_PROPERTY(PowerlevelsSpacesListModel *spaces READ spaces CONSTANT)
    Q_PROPERTY(qlonglong adminLevel READ adminLevel NOTIFY adminLevelChanged)
    Q_PROPERTY(qlonglong moderatorLevel READ moderatorLevel NOTIFY moderatorLevelChanged)
    Q_PROPERTY(qlonglong defaultUserLevel READ defaultUserLevel NOTIFY defaultUserLevelChanged)
    Q_PROPERTY(bool isSpace READ isSpace CONSTANT)

signals:
    void adminLevelChanged();
    void moderatorLevelChanged();
    void defaultUserLevelChanged();

private:
    mtx::events::state::PowerLevels calculateNewPowerlevel() const;

public:
    explicit PowerlevelEditingModels(QString room_id, QObject *parent = nullptr);

    PowerlevelsUserListModel *users() { return &users_; }
    PowerlevelsTypeListModel *types() { return &types_; }
    PowerlevelsSpacesListModel *spaces() { return &spaces_; }
    qlonglong adminLevel() const
    {
        return powerLevels_.state_level(to_string(mtx::events::EventType::RoomPowerLevels));
    }
    qlonglong moderatorLevel() const { return powerLevels_.redact; }
    qlonglong defaultUserLevel() const { return powerLevels_.users_default; }
    bool isSpace() const;

    Q_INVOKABLE void commit();
    Q_INVOKABLE void updateSpacesModel();
    Q_INVOKABLE void addRole(int pl);

    mtx::events::state::PowerLevels powerLevels_;
    PowerlevelsTypeListModel types_;
    PowerlevelsUserListModel users_;
    PowerlevelsSpacesListModel spaces_;
    std::string room_id_;
};