diff --git a/src/ruqolacore/model/usersmodel.cpp b/src/ruqolacore/model/usersmodel.cpp index 2be8a4ce..a90b1a17 100644 --- a/src/ruqolacore/model/usersmodel.cpp +++ b/src/ruqolacore/model/usersmodel.cpp @@ -1,177 +1,185 @@ /* Copyright (c) 2017-2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "usersmodel.h" #include "ruqola_debug.h" #include UsersModel::UsersModel(QObject *parent) : QAbstractListModel(parent) { } UsersModel::~UsersModel() { } int UsersModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return mUsers.size(); } QVariant UsersModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= mUsers.count()) { return QVariant(); } const User user = mUsers.at(index.row()); switch (role) { case UserName: return user.name(); case UserId: return user.userId(); case UserStatus: return user.status(); case UserIcon: return user.iconFromStatus(); case UserStatusText: return user.statusText(); } return {}; } QString UsersModel::userStatusIconFileName(const QString &name) { const int userCount = mUsers.count(); for (int i = 0; i < userCount; ++i) { if (mUsers.at(i).userName() == name) { return mUsers.at(i).iconFromStatus(); } } //qCWarning(RUQOLA_LOG) << "User for name " << name << " not defined yet"; return QStringLiteral("user-offline"); } QString UsersModel::status(const QString &userId) const { const int userCount = mUsers.count(); for (int i = 0; i < userCount; ++i) { if (mUsers.at(i).userId() == userId) { return mUsers.at(i).status(); } } //Return offline as default; return QStringLiteral("offline"); } void UsersModel::removeUser(const QString &userId) { qCDebug(RUQOLA_LOG) << " User removed " << userId; const int userCount = mUsers.count(); for (int i = 0; i < userCount; ++i) { if (mUsers.at(i).userId() == userId) { qCDebug(RUQOLA_LOG) << " User removed " << mUsers.at(i).name(); //Send info as it's disconnected. But don't remove it from list User user = mUsers.at(i); user.setStatus(QStringLiteral("offline")); mUsers.replace(i, user); const QModelIndex idx = createIndex(i, 0); Q_EMIT userStatusChanged(user); Q_EMIT dataChanged(idx, idx); break; } } } void UsersModel::addUser(const User &newuser) { //It can be duplicate as we don't remove user from list when user is disconnected. Otherwise it will not sync with // user for room list qCDebug(RUQOLA_LOG) << " User added " << newuser; const int userCount = mUsers.count(); bool found = false; for (int i = 0; i < userCount; ++i) { if (mUsers.at(i).userId() == newuser.userId()) { found = true; User user = mUsers.at(i); user.setStatus(newuser.status()); mUsers.replace(i, user); const QModelIndex idx = createIndex(i, 0); Q_EMIT userStatusChanged(user); Q_EMIT dataChanged(idx, idx); break; } } if (!found) { const int pos = mUsers.size(); beginInsertRows(QModelIndex(), pos, pos); mUsers.append(newuser); endInsertRows(); } } void UsersModel::updateUser(const QJsonObject &array) { const QString id = array.value(QLatin1String("id")).toString(); const int userCount = mUsers.count(); for (int i = 0; i < userCount; ++i) { if (mUsers.at(i).userId() == id) { User user = mUsers.at(i); const QJsonObject fields = array.value(QLatin1String("fields")).toObject(); const QString newStatus = fields.value(QLatin1String("status")).toString(); bool userDataChanged = false; if (!newStatus.isEmpty()) { user.setStatus(newStatus); mUsers.replace(i, user); const QModelIndex idx = createIndex(i, 0); Q_EMIT dataChanged(idx, idx); Q_EMIT userStatusChanged(user); userDataChanged = true; } const QString newName = fields.value(QLatin1String("name")).toString(); if (!newName.isEmpty()) { user.setName(newName); mUsers.replace(i, user); const QModelIndex idx = createIndex(i, 0); Q_EMIT dataChanged(idx, idx); Q_EMIT userNameChanged(user); userDataChanged = true; } const QString newuserName = fields.value(QLatin1String("username")).toString(); if (!newuserName.isEmpty()) { user.setUserName(newuserName); mUsers.replace(i, user); const QModelIndex idx = createIndex(i, 0); Q_EMIT dataChanged(idx, idx); Q_EMIT nameChanged(user); userDataChanged = true; } - //StatusText ? + const QString statusMessage = fields.value(QLatin1String("statusText")).toString(); + if (!statusMessage.isEmpty()) { + user.setStatusText(statusMessage); + mUsers.replace(i, user); + const QModelIndex idx = createIndex(i, 0); + Q_EMIT dataChanged(idx, idx); + Q_EMIT statusMessageChanged(user); + userDataChanged = true; + } if (!userDataChanged) { qCWarning(RUQOLA_LOG) << " Unsupported yet user data modification " << array; } break; } } } diff --git a/src/ruqolacore/model/usersmodel.h b/src/ruqolacore/model/usersmodel.h index 3dfe2290..e653b456 100644 --- a/src/ruqolacore/model/usersmodel.h +++ b/src/ruqolacore/model/usersmodel.h @@ -1,65 +1,66 @@ /* Copyright (c) 2017-2019 Montel Laurent This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef USERMODEL_H #define USERMODEL_H #include "libruqola_private_export.h" #include #include "user.h" class LIBRUQOLACORE_TESTS_EXPORT UsersModel : public QAbstractListModel { Q_OBJECT public: enum UserRoles { UserName = Qt::UserRole + 1, UserId, UserStatus, UserIcon, UserStatusText }; Q_ENUM(UserRoles) explicit UsersModel(QObject *parent = nullptr); ~UsersModel() override; Q_REQUIRED_RESULT int rowCount(const QModelIndex &parent = QModelIndex()) const override; Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; void addUser(const User &userFromUserId); void removeUser(const QString &userId); void updateUser(const QJsonObject &array); Q_REQUIRED_RESULT QString userStatusIconFileName(const QString &name); Q_REQUIRED_RESULT QString status(const QString &userId) const; Q_SIGNALS: void userStatusChanged(const User &user); void userNameChanged(const User &user); void nameChanged(const User &user); + void statusMessageChanged(const User &user); private: Q_DISABLE_COPY(UsersModel) QVector mUsers; }; #endif // USERMODEL_H