matrixion/src/InviteesModel.cpp

104 lines
2.6 KiB
C++
Raw Normal View History

2021-07-17 20:31:38 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
2021-07-17 20:31:38 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2021-06-11 03:13:12 +03:00
#include "InviteesModel.h"
#include "Cache.h"
#include "Logging.h"
#include "MatrixClient.h"
#include "mtx/responses/profile.hpp"
InviteesModel::InviteesModel(QObject *parent)
: QAbstractListModel{parent}
{}
void
InviteesModel::addUser(QString mxid)
{
2021-12-29 00:30:12 +03:00
for (const auto &invitee : qAsConst(invitees_))
2021-11-13 23:05:37 +03:00
if (invitee->mxid_ == mxid)
return;
2021-09-18 01:22:33 +03:00
beginInsertRows(QModelIndex(), invitees_.count(), invitees_.count());
2021-06-11 03:13:12 +03:00
2021-09-18 01:22:33 +03: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 03:13:12 +03:00
2021-09-18 01:22:33 +03:00
invitees_.push_back(invitee);
2021-09-18 01:22:33 +03:00
endInsertRows();
emit countChanged();
2021-06-11 03:13:12 +03: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 03:13:12 +03:00
QHash<int, QByteArray>
InviteesModel::roleNames() const
{
2021-09-18 01:22:33 +03:00
return {{Mxid, "mxid"}, {DisplayName, "displayName"}, {AvatarUrl, "avatarUrl"}};
2021-06-11 03:13:12 +03:00
}
QVariant
InviteesModel::data(const QModelIndex &index, int role) const
{
2021-09-18 01:22:33 +03:00
if (!index.isValid() || index.row() >= (int)invitees_.size() || index.row() < 0)
return {};
2021-06-11 03:13:12 +03:00
2021-09-18 01:22:33 +03: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 03:13:12 +03:00
}
QStringList
InviteesModel::mxids()
{
2021-09-18 01:22:33 +03:00
QStringList mxidList;
mxidList.reserve(invitees_.size());
for (auto &invitee : qAsConst(invitees_))
mxidList.push_back(invitee->mxid_);
2021-09-18 01:22:33 +03:00
return mxidList;
2021-06-11 03:13:12 +03:00
}
Invitee::Invitee(QString mxid, QObject *parent)
2021-06-11 03:13:12 +03:00
: QObject{parent}
, mxid_{std::move(mxid)}
2021-06-11 03:13:12 +03:00
{
2021-09-18 01:22:33 +03: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 03:13:12 +03:00
2021-09-18 01:22:33 +03:00
displayName_ = QString::fromStdString(res.display_name);
avatarUrl_ = QString::fromStdString(res.avatar_url);
2021-06-11 03:13:12 +03:00
2021-09-18 01:22:33 +03:00
emit userInfoLoaded();
});
2021-06-11 03:13:12 +03:00
}