nheko/src/InviteesModel.cpp

104 lines
2.6 KiB
C++
Raw Normal View History

2021-07-17 19:31:38 +02:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
2021-07-17 19:31:38 +02:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2021-06-11 02:13:12 +02:00
#include "InviteesModel.h"
#include "Cache.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "mtx/responses/profile.hpp"
InviteesModel::InviteesModel(QObject *parent)
: QAbstractListModel{parent}
2022-05-10 03:53:35 +02:00
{}
2021-06-11 02:13:12 +02:00
void
InviteesModel::addUser(QString mxid)
{
2021-12-28 22:30:12 +01:00
for (const auto &invitee : qAsConst(invitees_))
2021-11-13 21:05:37 +01:00
if (invitee->mxid_ == mxid)
return;
2021-09-18 00:22:33 +02:00
beginInsertRows(QModelIndex(), invitees_.count(), invitees_.count());
2021-06-11 02:13:12 +02:00
2021-09-18 00:22:33 +02:00
auto invitee = new Invitee{mxid, this};
auto indexOfInvitee = invitees_.count();
connect(invitee, &Invitee::userInfoLoaded, this, [this, indexOfInvitee]() {
emit dataChanged(index(indexOfInvitee), index(indexOfInvitee));
});
2021-06-11 02:13:12 +02:00
2021-09-18 00:22:33 +02:00
invitees_.push_back(invitee);
2021-09-18 00:22:33 +02:00
endInsertRows();
emit countChanged();
2021-06-11 02:13:12 +02:00
}
void
InviteesModel::removeUser(QString mxid)
{
for (int i = 0; i < invitees_.length(); ++i) {
if (invitees_[i]->mxid_ == mxid) {
beginRemoveRows(QModelIndex(), i, i);
invitees_.removeAt(i);
endRemoveRows();
emit countChanged();
break;
}
}
}
2021-06-11 02:13:12 +02:00
QHash<int, QByteArray>
InviteesModel::roleNames() const
{
2021-09-18 00:22:33 +02:00
return {{Mxid, "mxid"}, {DisplayName, "displayName"}, {AvatarUrl, "avatarUrl"}};
2021-06-11 02:13:12 +02:00
}
QVariant
InviteesModel::data(const QModelIndex &index, int role) const
{
2021-09-18 00:22:33 +02:00
if (!index.isValid() || index.row() >= (int)invitees_.size() || index.row() < 0)
return {};
2021-06-11 02:13:12 +02:00
2021-09-18 00:22:33 +02:00
switch (role) {
case Mxid:
return invitees_[index.row()]->mxid_;
case DisplayName:
return invitees_[index.row()]->displayName_;
case AvatarUrl:
return invitees_[index.row()]->avatarUrl_;
default:
return {};
}
2021-06-11 02:13:12 +02:00
}
QStringList
InviteesModel::mxids()
{
2021-09-18 00:22:33 +02:00
QStringList mxidList;
mxidList.reserve(invitees_.size());
for (auto &invitee : qAsConst(invitees_))
mxidList.push_back(invitee->mxid_);
2021-09-18 00:22:33 +02:00
return mxidList;
2021-06-11 02:13:12 +02:00
}
Invitee::Invitee(QString mxid, QObject *parent)
2021-06-11 02:13:12 +02:00
: QObject{parent}
, mxid_{std::move(mxid)}
2021-06-11 02:13:12 +02:00
{
2021-09-18 00:22:33 +02:00
http::client()->get_profile(
mxid_.toStdString(), [this](const mtx::responses::Profile &res, mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn("failed to retrieve profile info");
emit userInfoLoaded();
return;
}
2021-06-11 02:13:12 +02:00
2021-09-18 00:22:33 +02:00
displayName_ = QString::fromStdString(res.display_name);
avatarUrl_ = QString::fromStdString(res.avatar_url);
2021-06-11 02:13:12 +02:00
2021-09-18 00:22:33 +02:00
emit userInfoLoaded();
});
2021-06-11 02:13:12 +02:00
}