Make use of the new RoomMember objects in libquotient in UserListModel
This commit is contained in:
@@ -265,6 +265,7 @@ int main(int argc, char *argv[])
|
|||||||
qRegisterMetaType<User *>("User*");
|
qRegisterMetaType<User *>("User*");
|
||||||
qRegisterMetaType<User *>("const User*");
|
qRegisterMetaType<User *>("const User*");
|
||||||
qRegisterMetaType<User *>("const Quotient::User*");
|
qRegisterMetaType<User *>("const Quotient::User*");
|
||||||
|
qRegisterMetaType<RoomMember *>("RoomMember*");
|
||||||
qRegisterMetaType<Room *>("Room*");
|
qRegisterMetaType<Room *>("Room*");
|
||||||
qRegisterMetaType<Connection *>("Connection*");
|
qRegisterMetaType<Connection *>("Connection*");
|
||||||
qRegisterMetaType<MessageEventType>("MessageEventType");
|
qRegisterMetaType<MessageEventType>("MessageEventType");
|
||||||
|
|||||||
@@ -33,11 +33,9 @@ void UserListModel::setRoom(NeoChatRoom *room)
|
|||||||
m_currentRoom = room;
|
m_currentRoom = room;
|
||||||
|
|
||||||
if (m_currentRoom) {
|
if (m_currentRoom) {
|
||||||
connect(m_currentRoom, &Room::userAdded, this, &UserListModel::userAdded);
|
connect(m_currentRoom, &Room::memberAdded, this, &UserListModel::memberAdded);
|
||||||
connect(m_currentRoom, &Room::userRemoved, this, &UserListModel::userRemoved);
|
connect(m_currentRoom, &Room::memberRemoved, this, &UserListModel::memberRemoved);
|
||||||
connect(m_currentRoom, &Room::memberAboutToRename, this, &UserListModel::userRemoved);
|
connect(m_currentRoom, &Room::memberUpdated, this, &UserListModel::memberUpdated);
|
||||||
connect(m_currentRoom, &Room::memberRenamed, this, &UserListModel::userAdded);
|
|
||||||
connect(m_currentRoom, &Room::changed, this, &UserListModel::refreshAllUsers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshAllUsers();
|
refreshAllUsers();
|
||||||
@@ -49,37 +47,29 @@ NeoChatRoom *UserListModel::room() const
|
|||||||
return m_currentRoom;
|
return m_currentRoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
Quotient::User *UserListModel::userAt(QModelIndex index) const
|
|
||||||
{
|
|
||||||
if (index.row() < 0 || index.row() >= m_users.size()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return m_users.at(index.row());
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariant UserListModel::data(const QModelIndex &index, int role) const
|
QVariant UserListModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if (!index.isValid()) {
|
if (!index.isValid()) {
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index.row() >= m_users.count()) {
|
if (index.row() >= rowCount()) {
|
||||||
qDebug() << "UserListModel, something's wrong: index.row() >= "
|
qDebug() << "UserListModel, something's wrong: index.row() >= "
|
||||||
"users.count()";
|
"users.count()";
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto user = m_users.at(index.row());
|
auto user = m_members.at(index.row());
|
||||||
if (role == DisplayNameRole) {
|
if (role == DisplayNameRole) {
|
||||||
return user->displayname(m_currentRoom);
|
return user->displayName();
|
||||||
}
|
}
|
||||||
if (role == UserIdRole) {
|
if (role == UserIdRole) {
|
||||||
return user->id();
|
return user->id();
|
||||||
}
|
}
|
||||||
if (role == AvatarRole) {
|
if (role == AvatarRole) {
|
||||||
return m_currentRoom->avatarForMember(user);
|
return user->avatarUrl();
|
||||||
}
|
}
|
||||||
if (role == ObjectRole) {
|
if (role == ObjectRole) {
|
||||||
return QVariant::fromValue(user);
|
return QVariant::fromValue<RoomMember *>(user.get());
|
||||||
}
|
}
|
||||||
if (role == PowerLevelRole) {
|
if (role == PowerLevelRole) {
|
||||||
auto plEvent = m_currentRoom->currentState().get<RoomPowerLevelsEvent>();
|
auto plEvent = m_currentRoom->currentState().get<RoomPowerLevelsEvent>();
|
||||||
@@ -118,59 +108,47 @@ int UserListModel::rowCount(const QModelIndex &parent) const
|
|||||||
if (parent.isValid()) {
|
if (parent.isValid()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return m_users.count();
|
return m_members.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UserListModel::userAdded(Quotient::User *user)
|
void UserListModel::memberAdded(QString memberId)
|
||||||
{
|
{
|
||||||
auto pos = findUserPos(user);
|
if (m_currentRoom->memberIds().contains(memberId)) {
|
||||||
beginInsertRows(QModelIndex(), pos, pos);
|
return;
|
||||||
m_users.insert(pos, user);
|
}
|
||||||
|
beginInsertRows(QModelIndex(), m_currentRoom->memberIds().size(), m_currentRoom->memberIds().size());
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
connect(user, &User::defaultAvatarChanged, this, [this, user]() {
|
|
||||||
refreshUser(user, {AvatarRole});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UserListModel::userRemoved(Quotient::User *user)
|
void UserListModel::memberUpdated(QString memberId)
|
||||||
{
|
{
|
||||||
auto pos = findUserPos(user);
|
if (!m_currentRoom->memberIds().contains(memberId)) {
|
||||||
if (pos != m_users.size()) {
|
return;
|
||||||
beginRemoveRows(QModelIndex(), pos, pos);
|
}
|
||||||
m_users.removeAt(pos);
|
auto row = m_currentRoom->memberIds().indexOf(memberId);
|
||||||
endRemoveRows();
|
if (row >= 0 && row < m_currentRoom->memberIds().size()) {
|
||||||
user->disconnect(this);
|
Q_EMIT dataChanged(index(row), index(row));
|
||||||
} else {
|
|
||||||
qWarning() << "Trying to remove a room member not in the user list";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UserListModel::refreshUser(Quotient::User *user, const QVector<int> &roles)
|
void UserListModel::memberRemoved(QString memberId)
|
||||||
{
|
{
|
||||||
auto pos = findUserPos(user);
|
if (!m_currentRoom->memberIds().contains(memberId)) {
|
||||||
if (pos != m_users.size()) {
|
return;
|
||||||
Q_EMIT dataChanged(index(pos), index(pos), roles);
|
}
|
||||||
} else {
|
auto row = m_currentRoom->memberIds().indexOf(memberId);
|
||||||
qWarning() << "Trying to access a room member not in the user list";
|
if (row >= 0 && row < m_currentRoom->memberIds().size()) {
|
||||||
|
beginRemoveRows(QModelIndex(), row, row);
|
||||||
|
endRemoveRows();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UserListModel::refreshAllUsers()
|
void UserListModel::refreshAllUsers()
|
||||||
{
|
{
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
for (User *user : std::as_const(m_users)) {
|
m_members.clear();
|
||||||
user->disconnect(this);
|
m_members = m_currentRoom->members();
|
||||||
}
|
|
||||||
m_users.clear();
|
|
||||||
|
|
||||||
m_users = m_currentRoom->users();
|
|
||||||
std::sort(m_users.begin(), m_users.end(), m_currentRoom->memberSorter());
|
|
||||||
|
|
||||||
for (User *user : std::as_const(m_users)) {
|
|
||||||
connect(user, &User::defaultAvatarChanged, this, [this, user]() {
|
|
||||||
refreshUser(user, {AvatarRole});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
connect(m_currentRoom->connection(), &Connection::loggedOut, this, [this]() {
|
connect(m_currentRoom->connection(), &Connection::loggedOut, this, [this]() {
|
||||||
setRoom(nullptr);
|
setRoom(nullptr);
|
||||||
});
|
});
|
||||||
@@ -178,19 +156,6 @@ void UserListModel::refreshAllUsers()
|
|||||||
Q_EMIT usersRefreshed();
|
Q_EMIT usersRefreshed();
|
||||||
}
|
}
|
||||||
|
|
||||||
int UserListModel::findUserPos(Quotient::User *user) const
|
|
||||||
{
|
|
||||||
return findUserPos(m_currentRoom->safeMemberName(user->id()));
|
|
||||||
}
|
|
||||||
|
|
||||||
int UserListModel::findUserPos(const QString &username) const
|
|
||||||
{
|
|
||||||
if (!m_currentRoom) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return m_currentRoom->memberSorter().lowerBoundIndex(m_users, username);
|
|
||||||
}
|
|
||||||
|
|
||||||
QHash<int, QByteArray> UserListModel::roleNames() const
|
QHash<int, QByteArray> UserListModel::roleNames() const
|
||||||
{
|
{
|
||||||
QHash<int, QByteArray> roles;
|
QHash<int, QByteArray> roles;
|
||||||
|
|||||||
@@ -54,11 +54,6 @@ public:
|
|||||||
[[nodiscard]] NeoChatRoom *room() const;
|
[[nodiscard]] NeoChatRoom *room() const;
|
||||||
void setRoom(NeoChatRoom *room);
|
void setRoom(NeoChatRoom *room);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief The user at the given index of the model.
|
|
||||||
*/
|
|
||||||
[[nodiscard]] Quotient::User *userAt(QModelIndex index) const;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the given role value at the given index.
|
* @brief Get the given role value at the given index.
|
||||||
*
|
*
|
||||||
@@ -85,15 +80,12 @@ Q_SIGNALS:
|
|||||||
void usersRefreshed();
|
void usersRefreshed();
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void userAdded(Quotient::User *user);
|
void memberAdded(QString memberId);
|
||||||
void userRemoved(Quotient::User *user);
|
void memberUpdated(QString memberId);
|
||||||
void refreshUser(Quotient::User *user, const QVector<int> &roles = {});
|
void memberRemoved(QString memberId);
|
||||||
void refreshAllUsers();
|
void refreshAllUsers();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QPointer<NeoChatRoom> m_currentRoom;
|
QPointer<NeoChatRoom> m_currentRoom;
|
||||||
QList<Quotient::User *> m_users;
|
QList<QSharedPointer<Quotient::RoomMember>> m_members;
|
||||||
|
|
||||||
int findUserPos(Quotient::User *user) const;
|
|
||||||
[[nodiscard]] int findUserPos(const QString &username) const;
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user