File indexing completed on 2024-05-05 05:01:26
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 attempted = false; 0030 users.clear(); 0031 0032 if (m_connection) { 0033 m_connection->disconnect(this); 0034 } 0035 0036 endResetModel(); 0037 0038 m_connection = conn; 0039 Q_EMIT connectionChanged(); 0040 0041 if (m_job) { 0042 m_job->abandon(); 0043 m_job = nullptr; 0044 Q_EMIT searchingChanged(); 0045 } 0046 } 0047 0048 QString UserDirectoryListModel::searchText() const 0049 { 0050 return m_searchText; 0051 } 0052 0053 void UserDirectoryListModel::setSearchText(const QString &value) 0054 { 0055 if (m_searchText == value) { 0056 return; 0057 } 0058 0059 m_searchText = value; 0060 Q_EMIT searchTextChanged(); 0061 0062 if (m_job) { 0063 m_job->abandon(); 0064 m_job = nullptr; 0065 Q_EMIT searchingChanged(); 0066 } 0067 0068 attempted = false; 0069 } 0070 0071 bool UserDirectoryListModel::searching() const 0072 { 0073 return m_job != nullptr; 0074 } 0075 0076 void UserDirectoryListModel::search(int limit) 0077 { 0078 if (limit < 1) { 0079 return; 0080 } 0081 0082 if (m_job) { 0083 qDebug() << "UserDirectoryListModel: Other jobs running, ignore"; 0084 0085 return; 0086 } 0087 0088 if (attempted) { 0089 return; 0090 } 0091 0092 m_job = m_connection->callApi<SearchUserDirectoryJob>(m_searchText, limit); 0093 Q_EMIT searchingChanged(); 0094 0095 connect(m_job, &BaseJob::finished, this, [this] { 0096 attempted = true; 0097 0098 if (m_job->status() == BaseJob::Success) { 0099 auto users = m_job->results(); 0100 0101 this->beginResetModel(); 0102 this->users = users; 0103 this->endResetModel(); 0104 } 0105 0106 this->m_job = nullptr; 0107 Q_EMIT searchingChanged(); 0108 }); 0109 } 0110 0111 QVariant UserDirectoryListModel::data(const QModelIndex &index, int role) const 0112 { 0113 if (!index.isValid()) { 0114 return QVariant(); 0115 } 0116 0117 if (index.row() >= users.count()) { 0118 qDebug() << "UserDirectoryListModel, something's wrong: index.row() >= " 0119 "users.count()"; 0120 return {}; 0121 } 0122 auto user = users.at(index.row()); 0123 if (role == DisplayNameRole) { 0124 auto displayName = user.displayName; 0125 if (!displayName.isEmpty()) { 0126 return displayName; 0127 } 0128 0129 displayName = user.userId; 0130 if (!displayName.isEmpty()) { 0131 return displayName; 0132 } 0133 0134 return QStringLiteral("Unknown User"); 0135 } 0136 if (role == AvatarRole) { 0137 auto avatarUrl = user.avatarUrl; 0138 if (avatarUrl.isEmpty() || !m_connection) { 0139 return QUrl(); 0140 } 0141 return m_connection->makeMediaUrl(avatarUrl); 0142 } 0143 if (role == UserIDRole) { 0144 return user.userId; 0145 } 0146 if (role == DirectChatExistsRole) { 0147 if (!m_connection) { 0148 return false; 0149 }; 0150 0151 auto userObj = m_connection->user(user.userId); 0152 auto directChats = m_connection->directChats(); 0153 0154 if (userObj && directChats.contains(userObj)) { 0155 auto directChatsForUser = directChats.values(userObj); 0156 if (!directChatsForUser.isEmpty()) { 0157 return true; 0158 } 0159 } 0160 0161 return false; 0162 } 0163 0164 return {}; 0165 } 0166 0167 QHash<int, QByteArray> UserDirectoryListModel::roleNames() const 0168 { 0169 QHash<int, QByteArray> roles; 0170 0171 roles[DisplayNameRole] = "displayName"; 0172 roles[AvatarRole] = "avatarUrl"; 0173 roles[UserIDRole] = "userId"; 0174 roles[DirectChatExistsRole] = "directChatExists"; 0175 0176 return roles; 0177 } 0178 0179 int UserDirectoryListModel::rowCount(const QModelIndex &parent) const 0180 { 0181 if (parent.isValid()) { 0182 return 0; 0183 } 0184 0185 return users.count(); 0186 } 0187 0188 #include "moc_userdirectorylistmodel.cpp"