File indexing completed on 2024-02-25 16:44:34

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "adminusersmodel.h"
0008 #include "ruqola_debug.h"
0009 #include "utils.h"
0010 #include <KLocalizedString>
0011 
0012 AdminUsersModel::AdminUsersModel(QObject *parent)
0013     : DirectoryUsersModel(parent)
0014 {
0015 }
0016 
0017 AdminUsersModel::~AdminUsersModel() = default;
0018 
0019 Users::ParseType AdminUsersModel::parseType() const
0020 {
0021     return Users::ParseType::Administrator;
0022 }
0023 
0024 void AdminUsersModel::insertElement(const QJsonObject &obj)
0025 {
0026     User user;
0027     const QJsonObject userObj = obj[QLatin1String("user")].toObject();
0028     user.parseUserRestApi(userObj, {});
0029     if (user.isValid()) {
0030         const int numberOfElement = mUsers.count();
0031         mUsers.appendUser(user); // TODO useful ?
0032         beginInsertRows(QModelIndex(), numberOfElement, mUsers.count() - 1);
0033         endInsertRows();
0034         checkFullList(); // TODO verify it
0035     }
0036 }
0037 
0038 void AdminUsersModel::removeElement(const QString &identifier)
0039 {
0040     const int userCount = mUsers.count();
0041     for (int i = 0; i < userCount; ++i) {
0042         if (mUsers.at(i).userId() == identifier) {
0043             beginRemoveRows(QModelIndex(), i, i);
0044             mUsers.takeAt(i);
0045             mUsers.setTotal(mUsers.count()); // Update total
0046             endRemoveRows();
0047             Q_EMIT totalChanged();
0048             break;
0049         }
0050     }
0051 }
0052 
0053 void AdminUsersModel::updateElement(const QJsonObject &userObj)
0054 {
0055     const int roomCount = mUsers.count();
0056     const QJsonObject obj = userObj[QLatin1String("user")].toObject();
0057     const QString identifier{obj.value(QStringLiteral("_id")).toString()};
0058     for (int i = 0; i < roomCount; ++i) {
0059         if (mUsers.at(i).userId() == identifier) {
0060             beginRemoveRows(QModelIndex(), i, i);
0061             mUsers.takeAt(i);
0062             endRemoveRows();
0063             User newUser;
0064             newUser.parseUserRestApi(obj, {} /*, mRocketChatAccount->roleInfo()*/); // TODO necessary ?
0065             beginInsertRows(QModelIndex(), i, i);
0066             mUsers.insertUser(i, newUser);
0067             endInsertRows();
0068             break;
0069         }
0070     }
0071 }
0072 
0073 bool AdminUsersModel::setData(const QModelIndex &id, const QVariant &value, int role)
0074 {
0075     if (!id.isValid()) {
0076         qCWarning(RUQOLA_LOG) << "ERROR: invalid index";
0077         return false;
0078     }
0079     const int idx = id.row();
0080     User &user = mUsers[idx];
0081     switch (role) {
0082     case AdminUsersRoles::ActiveUser: {
0083         user.setActive(value.toBool());
0084         const QModelIndex newIndex = index(id.row(), AdminUsersModel::ActiveUserDisplay);
0085         Q_EMIT dataChanged(newIndex, newIndex);
0086         return true;
0087     }
0088     case AdminUsersRoles::Name:
0089     case AdminUsersRoles::UserName:
0090     case AdminUsersRoles::Email:
0091     case AdminUsersRoles::Roles:
0092     case AdminUsersRoles::Status:
0093     case AdminUsersRoles::UserId:
0094     case AdminUsersRoles::ActiveUserDisplay:
0095     case AdminUsersRoles::Administrator:
0096         return false;
0097     }
0098     return false;
0099 }
0100 
0101 QVariant AdminUsersModel::headerData(int section, Qt::Orientation orientation, int role) const
0102 {
0103     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0104         switch (static_cast<AdminUsersRoles>(section)) {
0105         case AdminUsersModel::Name:
0106             return i18n("Name");
0107         case AdminUsersModel::UserName:
0108             return i18n("UserName");
0109         case AdminUsersModel::Email:
0110             return i18n("Email");
0111         case AdminUsersModel::Roles:
0112             return i18n("Role");
0113         case AdminUsersModel::ActiveUserDisplay:
0114             return i18n("Disabled");
0115         case AdminUsersModel::Status:
0116             return i18n("Status");
0117         case AdminUsersModel::UserId:
0118         case AdminUsersModel::ActiveUser:
0119         case AdminUsersRoles::Administrator:
0120             return {};
0121         }
0122     }
0123     return {};
0124 }
0125 
0126 int AdminUsersModel::columnCount(const QModelIndex &parent) const
0127 {
0128     Q_UNUSED(parent)
0129     constexpr int val = static_cast<int>(AdminUsersModel::LastColumn) + 1;
0130     return val;
0131 }
0132 
0133 QList<int> AdminUsersModel::hideColumns() const
0134 {
0135     return {AdminUsersRoles::UserId, AdminUsersRoles::ActiveUser, AdminUsersRoles::Administrator};
0136 }
0137 
0138 QVariant AdminUsersModel::data(const QModelIndex &index, int role) const
0139 {
0140     if (index.row() < 0 || index.row() >= mUsers.count()) {
0141         return {};
0142     }
0143     if (role != Qt::DisplayRole) {
0144         return {};
0145     }
0146 
0147     const User &user = mUsers.at(index.row());
0148     const int col = index.column();
0149     switch (static_cast<AdminUsersRoles>(col)) {
0150     case AdminUsersRoles::Name:
0151         return user.name();
0152     case AdminUsersRoles::UserName:
0153         return user.userName();
0154     case AdminUsersRoles::Email:
0155         return user.userEmailsInfo().email;
0156     case AdminUsersRoles::Roles:
0157         return user.i18nRoles().join(QLatin1Char(','));
0158     case AdminUsersRoles::Status:
0159         return Utils::displaytextFromPresenceStatus(user.status());
0160     case AdminUsersRoles::ActiveUserDisplay:
0161         return user.active() ? i18n("Active") : i18n("Disabled");
0162     case AdminUsersRoles::ActiveUser:
0163         return user.active();
0164     case AdminUsersRoles::UserId:
0165         return user.userId();
0166     case AdminUsersRoles::Administrator:
0167         return user.roles().contains(QStringLiteral("admin"));
0168     }
0169     return {};
0170 }
0171 
0172 #include "moc_adminusersmodel.cpp"