matrixion/src/AliasEditModel.h

83 lines
2 KiB
C
Raw Normal View History

// SPDX-FileCopyrightText: Nheko Contributors
2022-07-08 18:28:28 +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-07-08 18:28:28 +03:00
#include <QVector>
#include <mtx/events/canonical_alias.hpp>
2022-10-10 15:38:29 +03:00
class FetchPublishedAliasesJob final : public QObject
2022-07-08 18:28:28 +03:00
{
Q_OBJECT
public:
explicit FetchPublishedAliasesJob(QObject *p = nullptr)
: QObject(p)
2022-09-25 21:05:08 +03:00
{
}
2022-07-08 18:28:28 +03:00
signals:
void aliasFetched(std::string alias, std::string target);
void advertizedAliasesFetched(std::vector<std::string> aliases);
};
2022-10-10 15:38:29 +03:00
class AliasEditingModel final : public QAbstractListModel
2022-07-08 18:28:28 +03:00
{
Q_OBJECT
2023-06-19 02:38:40 +03:00
QML_ELEMENT
QML_UNCREATABLE("Please use editAliases to create the models")
2022-07-08 18:28:28 +03:00
Q_PROPERTY(bool canAdvertize READ canAdvertize CONSTANT)
public:
enum Roles
{
Name,
IsPublished,
IsCanonical,
IsAdvertized,
};
explicit AliasEditingModel(const std::string &room_id_, QObject *parent = nullptr);
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &) const override { return static_cast<int>(aliases.size()); }
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool canAdvertize() const { return canSendStateEvent; }
Q_INVOKABLE bool deleteAlias(int row);
Q_INVOKABLE void addAlias(QString newAlias);
Q_INVOKABLE void makeCanonical(int row);
Q_INVOKABLE void togglePublish(int row);
Q_INVOKABLE void toggleAdvertize(int row);
Q_INVOKABLE void commit();
private slots:
void updateAlias(std::string alias, std::string target);
void updatePublishedAliases(std::vector<std::string> aliases);
private:
void fetchAliasesStatus(const std::string &alias);
void fetchPublishedAliases();
struct Entry
{
~Entry() = default;
std::string alias;
bool canonical = false;
bool advertized = false;
bool published = false;
};
std::string room_id;
QVector<Entry> aliases;
mtx::events::state::CanonicalAlias aliasEvent;
bool canSendStateEvent = false;
};