File indexing completed on 2024-12-08 07:33:47
0001 // SPDX-FileCopyrightText: 2019 Black Hat <bhat@encom.eu.org> 0002 // SPDX-License-Identifier: GPL-3.0-only 0003 0004 #include "userdirectorylistmodel.h" 0005 0006 #include <Quotient/connection.h> 0007 #include <Quotient/room.h> 0008 0009 using namespace Quotient; 0010 0011 UserDirectoryListModel::UserDirectoryListModel(QObject *parent) 0012 : QAbstractListModel(parent) 0013 { 0014 } 0015 0016 Quotient::Connection *UserDirectoryListModel::connection() const 0017 { 0018 return m_connection; 0019 } 0020 0021 void UserDirectoryListModel::setConnection(Connection *conn) 0022 { 0023 if (m_connection == conn) { 0024 return; 0025 } 0026 0027 beginResetModel(); 0028 0029 m_limited = false; 0030 attempted = false; 0031 users.clear(); 0032 0033 if (m_connection) { 0034 m_connection->disconnect(this); 0035 } 0036 0037 endResetModel(); 0038 0039 m_connection = conn; 0040 0041 if (job) { 0042 job->abandon(); 0043 job = nullptr; 0044 } 0045 0046 Q_EMIT connectionChanged(); 0047 Q_EMIT limitedChanged(); 0048 } 0049 0050 QString UserDirectoryListModel::keyword() const 0051 { 0052 return m_keyword; 0053 } 0054 0055 void UserDirectoryListModel::setKeyword(const QString &value) 0056 { 0057 if (m_keyword == value) { 0058 return; 0059 } 0060 0061 m_keyword = value; 0062 0063 m_limited = false; 0064 attempted = false; 0065 0066 if (job) { 0067 job->abandon(); 0068 job = nullptr; 0069 } 0070 0071 Q_EMIT keywordChanged(); 0072 Q_EMIT limitedChanged(); 0073 } 0074 0075 bool UserDirectoryListModel::limited() const 0076 { 0077 return m_limited; 0078 } 0079 0080 void UserDirectoryListModel::search(int count) 0081 { 0082 if (count < 1) { 0083 return; 0084 } 0085 0086 if (job) { 0087 qDebug() << "UserDirectoryListModel: Other jobs running, ignore"; 0088 0089 return; 0090 } 0091 0092 if (attempted) { 0093 return; 0094 } 0095 0096 job = m_connection->callApi<SearchUserDirectoryJob>(m_keyword, count); 0097 0098 connect(job, &BaseJob::finished, this, [this] { 0099 attempted = true; 0100 0101 if (job->status() == BaseJob::Success) { 0102 auto users = job->results(); 0103 0104 this->beginResetModel(); 0105 0106 this->users = users; 0107 this->m_limited = job->limited(); 0108 0109 this->endResetModel(); 0110 } 0111 0112 this->job = nullptr; 0113 0114 Q_EMIT limitedChanged(); 0115 }); 0116 } 0117 0118 QVariant UserDirectoryListModel::data(const QModelIndex &index, int role) const 0119 { 0120 if (!index.isValid()) { 0121 return QVariant(); 0122 } 0123 0124 if (index.row() >= users.count()) { 0125 qDebug() << "UserDirectoryListModel, something's wrong: index.row() >= " 0126 "users.count()"; 0127 return {}; 0128 } 0129 auto user = users.at(index.row()); 0130 if (role == NameRole) { 0131 auto displayName = user.displayName; 0132 if (!displayName.isEmpty()) { 0133 return displayName; 0134 } 0135 0136 displayName = user.userId; 0137 if (!displayName.isEmpty()) { 0138 return displayName; 0139 } 0140 0141 return QStringLiteral("Unknown User"); 0142 } 0143 if (role == AvatarRole) { 0144 auto avatarUrl = user.avatarUrl; 0145 0146 if (avatarUrl.isEmpty()) { 0147 return QString(); 0148 } 0149 return avatarUrl.url().remove(0, 6); 0150 } 0151 if (role == UserIDRole) { 0152 return user.userId; 0153 } 0154 if (role == DirectChatsRole) { 0155 if (!m_connection) { 0156 return QStringList(); 0157 }; 0158 0159 auto userObj = m_connection->user(user.userId); 0160 auto directChats = m_connection->directChats(); 0161 0162 if (userObj && directChats.contains(userObj)) { 0163 auto directChatsForUser = directChats.values(userObj); 0164 if (!directChatsForUser.isEmpty()) { 0165 return QVariant::fromValue(directChatsForUser); 0166 } 0167 } 0168 0169 return QStringList(); 0170 } 0171 0172 return {}; 0173 } 0174 0175 QHash<int, QByteArray> UserDirectoryListModel::roleNames() const 0176 { 0177 QHash<int, QByteArray> roles; 0178 0179 roles[NameRole] = "name"; 0180 roles[AvatarRole] = "avatar"; 0181 roles[UserIDRole] = "userID"; 0182 roles[DirectChatsRole] = "directChats"; 0183 0184 return roles; 0185 } 0186 0187 int UserDirectoryListModel::rowCount(const QModelIndex &parent) const 0188 { 0189 if (parent.isValid()) { 0190 return 0; 0191 } 0192 0193 return users.count(); 0194 } 0195 0196 #include "moc_userdirectorylistmodel.cpp"