Add visualisation of the account's third party IDs in the account editor.

A category won't be shown if there are no relevant IDs (will add the ability to add new ones later).

Part of network/neochat#565

![image](/uploads/7da00b0b4acf90d145c09969ac2a91e1/image.png)
This commit is contained in:
James Graham
2024-04-14 16:37:34 +00:00
parent 1e24bde9a9
commit b7ee83f6b6
8 changed files with 190 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2024 James Graham <james.h.graham@protonmail.com>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include "threepidmodel.h"
#include "neochatconnection.h"
ThreePIdModel::ThreePIdModel(NeoChatConnection *connection)
: QAbstractListModel(connection)
{
Q_ASSERT(connection);
connect(connection, &NeoChatConnection::stateChanged, this, [this]() {
const auto connection = dynamic_cast<NeoChatConnection *>(this->parent());
if (connection != nullptr && connection->isLoggedIn()) {
const auto threePIdJob = connection->callApi<Quotient::GetAccount3PIDsJob>();
connect(threePIdJob, &Quotient::BaseJob::success, this, [this, threePIdJob]() {
beginResetModel();
m_threePIds = threePIdJob->threepids();
endResetModel();
});
}
});
}
QVariant ThreePIdModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return {};
}
if (index.row() >= rowCount()) {
qDebug() << "ThreePIdModel, something's wrong: index.row() >= m_threePIds.count()";
return {};
}
if (role == AddressRole) {
return m_threePIds.at(index.row()).address;
}
if (role == MediumRole) {
return m_threePIds.at(index.row()).medium;
}
return {};
}
int ThreePIdModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_threePIds.count();
}
QHash<int, QByteArray> ThreePIdModel::roleNames() const
{
return {
{AddressRole, QByteArrayLiteral("address")},
{MediumRole, QByteArrayLiteral("medium")},
};
}