summary refs log tree commit diff
path: root/src/AliasEditModel.h
blob: 04de50161e25e646b426409240b70c9e2093c61e (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
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QAbstractListModel>
#include <QQmlEngine>
#include <QVector>

#include <mtx/events/canonical_alias.hpp>

#include "CacheStructs.h"

class FetchPublishedAliasesJob final : public QObject
{
    Q_OBJECT

public:
    explicit FetchPublishedAliasesJob(QObject *p = nullptr)
      : QObject(p)
    {
    }

signals:
    void aliasFetched(std::string alias, std::string target);
    void advertizedAliasesFetched(std::vector<std::string> aliases);
};

class AliasEditingModel final : public QAbstractListModel
{
    Q_OBJECT
    QML_ELEMENT
    QML_UNCREATABLE("Please use editAliases to create the models")

    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;
};