mirror of https://github.com/Nheko-Reborn/nheko
parent
6c57fa6c5b
commit
e1acf5d324
@ -1,5 +0,0 @@ |
||||
import QtQuick 2.12 |
||||
|
||||
Item { |
||||
property string invitee |
||||
} |
@ -0,0 +1,77 @@ |
||||
#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) |
||||
{ |
||||
beginInsertRows(QModelIndex(), invitees_.count(), invitees_.count()); |
||||
|
||||
auto invitee = new Invitee{mxid, this}; |
||||
connect(invitee, &Invitee::userInfoLoaded, this, [this]() { |
||||
emit dataChanged(QModelIndex{}, QModelIndex{}); |
||||
}); |
||||
|
||||
invitees_.push_back(invitee); |
||||
|
||||
endInsertRows(); |
||||
} |
||||
|
||||
QHash<int, QByteArray> |
||||
InviteesModel::roleNames() const |
||||
{ |
||||
return {{Mxid, "mxid"}, {DisplayName, "displayName"}, {AvatarUrl, "avatarUrl"}}; |
||||
} |
||||
|
||||
QVariant |
||||
InviteesModel::data(const QModelIndex &index, int role) const |
||||
{ |
||||
if (!index.isValid() || index.row() >= (int)invitees_.size() || index.row() < 0) |
||||
return {}; |
||||
|
||||
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 {}; |
||||
} |
||||
} |
||||
|
||||
QStringList |
||||
InviteesModel::mxids() |
||||
{ |
||||
QStringList mxidList; |
||||
for (int i = 0; i < invitees_.length(); ++i) |
||||
mxidList.push_back(invitees_[i]->mxid_); |
||||
return mxidList; |
||||
} |
||||
|
||||
Invitee::Invitee(const QString &mxid, QObject *parent) |
||||
: QObject{parent} |
||||
, mxid_{mxid} |
||||
{ |
||||
http::client()->get_profile( |
||||
mxid_.toStdString(), |
||||
[this](const mtx::responses::Profile &res, mtx::http::RequestErr err) { |
||||
if (err) { |
||||
nhlog::net()->warn("failed to retrieve own profile info"); |
||||
return; |
||||
} |
||||
|
||||
displayName_ = QString::fromStdString(res.display_name); |
||||
avatarUrl_ = QString::fromStdString(res.avatar_url); |
||||
|
||||
emit userInfoLoaded(); |
||||
}); |
||||
} |
@ -0,0 +1,56 @@ |
||||
#ifndef INVITEESMODEL_H |
||||
#define INVITEESMODEL_H |
||||
|
||||
#include <QAbstractListModel> |
||||
#include <QVector> |
||||
|
||||
class Invitee : public QObject |
||||
{ |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
Invitee(const QString &mxid, QObject *parent = nullptr); |
||||
|
||||
signals: |
||||
void userInfoLoaded(); |
||||
|
||||
private: |
||||
const QString mxid_; |
||||
QString displayName_; |
||||
QString avatarUrl_; |
||||
|
||||
friend class InviteesModel; |
||||
}; |
||||
|
||||
class InviteesModel : public QAbstractListModel |
||||
{ |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
enum Roles |
||||
{ |
||||
Mxid, |
||||
DisplayName, |
||||
AvatarUrl, |
||||
}; |
||||
|
||||
InviteesModel(QObject *parent = nullptr); |
||||
|
||||
Q_INVOKABLE void addUser(QString mxid); |
||||
|
||||
QHash<int, QByteArray> roleNames() const override; |
||||
int rowCount(const QModelIndex & = QModelIndex()) const override |
||||
{ |
||||
return (int)invitees_.size(); |
||||
} |
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
||||
QStringList mxids(); |
||||
|
||||
signals: |
||||
void accept(); |
||||
|
||||
private: |
||||
QVector<Invitee *> invitees_; |
||||
}; |
||||
|
||||
#endif // INVITEESMODEL_H
|
Loading…
Reference in new issue