2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2022-05-27 17:31:54 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
2023-06-19 02:38:40 +03:00
|
|
|
#include <QQmlEngine>
|
2022-05-27 17:31:54 +03:00
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
|
|
|
|
#include <mtx/events/power_levels.hpp>
|
|
|
|
|
|
|
|
#include "CacheStructs.h"
|
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class PowerlevelsTypeListModel final : public QAbstractListModel
|
2022-05-27 17:31:54 +03:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2022-06-26 01:09:00 +03:00
|
|
|
signals:
|
|
|
|
void adminLevelChanged();
|
|
|
|
void moderatorLevelChanged();
|
|
|
|
|
2022-05-27 17:31:54 +03:00
|
|
|
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);
|
2022-06-26 01:09:00 +03:00
|
|
|
void addRole(int64_t role);
|
2022-05-27 17:31:54 +03:00
|
|
|
|
|
|
|
bool moveRows(const QModelIndex &sourceParent,
|
|
|
|
int sourceRow,
|
|
|
|
int count,
|
|
|
|
const QModelIndex &destinationParent,
|
|
|
|
int destinationChild) override;
|
|
|
|
|
2022-09-28 03:09:04 +03:00
|
|
|
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;
|
2022-05-27 17:31:54 +03:00
|
|
|
|
|
|
|
struct Entry
|
|
|
|
{
|
2022-06-24 02:55:32 +03:00
|
|
|
~Entry() = default;
|
2022-06-23 22:48:44 +03:00
|
|
|
|
2022-05-27 17:31:54 +03:00
|
|
|
std::string type;
|
|
|
|
mtx::events::state::power_level_t pl;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string room_id;
|
|
|
|
QVector<Entry> types;
|
|
|
|
mtx::events::state::PowerLevels powerLevels_;
|
|
|
|
};
|
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class PowerlevelsUserListModel final : public QAbstractListModel
|
2022-05-27 17:31:54 +03:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2022-06-26 01:09:00 +03:00
|
|
|
signals:
|
|
|
|
void defaultUserLevelChanged();
|
|
|
|
|
2022-05-27 17:31:54 +03:00
|
|
|
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);
|
2022-06-26 01:09:00 +03:00
|
|
|
void addRole(int64_t role);
|
2022-05-27 17:31:54 +03:00
|
|
|
|
|
|
|
bool moveRows(const QModelIndex &sourceParent,
|
|
|
|
int sourceRow,
|
|
|
|
int count,
|
|
|
|
const QModelIndex &destinationParent,
|
|
|
|
int destinationChild) override;
|
|
|
|
|
2022-09-28 03:09:04 +03:00
|
|
|
std::map<std::string, mtx::events::state::power_level_t, std::less<>> toUsers() const;
|
|
|
|
mtx::events::state::power_level_t usersDefault() const;
|
2022-05-27 17:31:54 +03:00
|
|
|
|
|
|
|
struct Entry
|
|
|
|
{
|
2022-06-24 02:55:32 +03:00
|
|
|
~Entry() = default;
|
2022-06-23 22:48:44 +03:00
|
|
|
|
2022-05-27 17:31:54 +03:00
|
|
|
std::string mxid;
|
|
|
|
mtx::events::state::power_level_t pl;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string room_id;
|
|
|
|
QVector<Entry> users;
|
|
|
|
mtx::events::state::PowerLevels powerLevels_;
|
|
|
|
};
|
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class PowerlevelsSpacesListModel final : public QAbstractListModel
|
2022-09-28 03:09:04 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-10-10 15:38:29 +03:00
|
|
|
class PowerlevelEditingModels final : public QObject
|
2022-05-27 17:31:54 +03:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2023-06-19 02:38:40 +03:00
|
|
|
QML_ELEMENT
|
|
|
|
QML_UNCREATABLE("Please use editPowerlevels to create the models")
|
|
|
|
|
2022-05-27 17:31:54 +03:00
|
|
|
Q_PROPERTY(PowerlevelsUserListModel *users READ users CONSTANT)
|
|
|
|
Q_PROPERTY(PowerlevelsTypeListModel *types READ types CONSTANT)
|
2022-09-28 03:09:04 +03:00
|
|
|
Q_PROPERTY(PowerlevelsSpacesListModel *spaces READ spaces CONSTANT)
|
2022-06-26 01:09:00 +03:00
|
|
|
Q_PROPERTY(qlonglong adminLevel READ adminLevel NOTIFY adminLevelChanged)
|
|
|
|
Q_PROPERTY(qlonglong moderatorLevel READ moderatorLevel NOTIFY moderatorLevelChanged)
|
|
|
|
Q_PROPERTY(qlonglong defaultUserLevel READ defaultUserLevel NOTIFY defaultUserLevelChanged)
|
2022-09-28 03:09:04 +03:00
|
|
|
Q_PROPERTY(bool isSpace READ isSpace CONSTANT)
|
2022-06-26 01:09:00 +03:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void adminLevelChanged();
|
|
|
|
void moderatorLevelChanged();
|
|
|
|
void defaultUserLevelChanged();
|
2022-05-27 17:31:54 +03:00
|
|
|
|
2022-09-28 03:09:04 +03:00
|
|
|
private:
|
|
|
|
mtx::events::state::PowerLevels calculateNewPowerlevel() const;
|
|
|
|
|
2022-05-27 17:31:54 +03:00
|
|
|
public:
|
|
|
|
explicit PowerlevelEditingModels(QString room_id, QObject *parent = nullptr);
|
|
|
|
|
|
|
|
PowerlevelsUserListModel *users() { return &users_; }
|
|
|
|
PowerlevelsTypeListModel *types() { return &types_; }
|
2022-09-28 03:09:04 +03:00
|
|
|
PowerlevelsSpacesListModel *spaces() { return &spaces_; }
|
2022-05-27 17:31:54 +03:00
|
|
|
qlonglong adminLevel() const
|
|
|
|
{
|
|
|
|
return powerLevels_.state_level(to_string(mtx::events::EventType::RoomPowerLevels));
|
|
|
|
}
|
|
|
|
qlonglong moderatorLevel() const { return powerLevels_.redact; }
|
2022-06-26 01:09:00 +03:00
|
|
|
qlonglong defaultUserLevel() const { return powerLevels_.users_default; }
|
2022-09-28 03:09:04 +03:00
|
|
|
bool isSpace() const;
|
2022-05-27 17:31:54 +03:00
|
|
|
|
|
|
|
Q_INVOKABLE void commit();
|
2022-09-28 03:09:04 +03:00
|
|
|
Q_INVOKABLE void updateSpacesModel();
|
2022-06-26 01:09:00 +03:00
|
|
|
Q_INVOKABLE void addRole(int pl);
|
2022-05-27 17:31:54 +03:00
|
|
|
|
|
|
|
mtx::events::state::PowerLevels powerLevels_;
|
|
|
|
PowerlevelsTypeListModel types_;
|
|
|
|
PowerlevelsUserListModel users_;
|
2022-09-28 03:09:04 +03:00
|
|
|
PowerlevelsSpacesListModel spaces_;
|
2022-05-27 17:31:54 +03:00
|
|
|
std::string room_id_;
|
|
|
|
};
|