File indexing completed on 2024-12-22 04:45:38

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 "usersinrolewidget.h"
0008 #include "connection.h"
0009 #include "dialogs/addusersinroomdialog.h"
0010 #include "misc/searchwithdelaylineedit.h"
0011 #include "model/searchtreebasefilterproxymodel.h"
0012 #include "model/usersinrolemodel.h"
0013 #include "rocketchataccount.h"
0014 #include "role/addusertorolejob.h"
0015 #include "role/getusersinrolejob.h"
0016 #include "role/removeuserfromrolejob.h"
0017 #include "ruqolawidgets_debug.h"
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 #include <QLabel>
0021 #include <QLayout>
0022 #include <QMenu>
0023 #include <QPointer>
0024 #include <QTreeView>
0025 
0026 UsersInRoleWidget::UsersInRoleWidget(RocketChatAccount *account, QWidget *parent)
0027     : SearchTreeBaseWidget(account, parent)
0028 {
0029     // Remove duplicate margins
0030     layout()->setContentsMargins({});
0031     mModel = new UsersInRoleModel(this);
0032     mModel->setObjectName(QStringLiteral("mAdminUsersModel"));
0033 
0034     mProxyModelModel = new SearchTreeBaseFilterProxyModel(mModel, this);
0035     mProxyModelModel->setObjectName(QStringLiteral("mAdminUsersProxyModel"));
0036     mSearchLineEdit->setPlaceholderText(i18n("Search users..."));
0037     mTreeView->setModel(mProxyModelModel);
0038     hideColumns();
0039     connectModel();
0040 }
0041 
0042 UsersInRoleWidget::~UsersInRoleWidget() = default;
0043 
0044 void UsersInRoleWidget::slotTextChanged(const QString &str)
0045 {
0046     mProxyModelModel->setFilterString(str);
0047 }
0048 
0049 void UsersInRoleWidget::slotAddUser()
0050 {
0051     QPointer<AddUsersInRoomDialog> dlg = new AddUsersInRoomDialog(mRocketChatAccount, this);
0052     dlg->setWindowTitle(i18nc("@title:window", "Add Users in Role"));
0053     if (dlg->exec()) {
0054         const auto users = dlg->userNames();
0055         for (const auto &user : users) {
0056             auto job = new RocketChatRestApi::AddUserToRoleJob(this);
0057             job->setRoleName(mRoleId);
0058             job->setUsername(user);
0059             mRocketChatAccount->restApi()->initializeRestApiJob(job);
0060             connect(job, &RocketChatRestApi::AddUserToRoleJob::addUsersToRoleDone, this, &UsersInRoleWidget::slotAddUsersToRoleDone);
0061             if (!job->start()) {
0062                 qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start AddUserToRoleJob job";
0063             }
0064         }
0065     }
0066     delete dlg;
0067 }
0068 
0069 void UsersInRoleWidget::slotAddUsersToRoleDone(const QJsonObject &replyObject)
0070 {
0071     // qDebug() << " replyObject " << replyObject;
0072     initialize(); // Reload list
0073 }
0074 
0075 void UsersInRoleWidget::slotRemoveUser(const QModelIndex &index)
0076 {
0077     QModelIndex modelIndex = mTreeView->model()->index(index.row(), UsersInRoleModel::Name);
0078     if (KMessageBox::ButtonCode::PrimaryAction
0079         == KMessageBox::questionTwoActions(this,
0080                                            i18n("Do you want to remove this user \"%1\"?", modelIndex.data().toString()),
0081                                            i18nc("@title:window", "Remove User"),
0082                                            KStandardGuiItem::remove(),
0083                                            KStandardGuiItem::cancel())) {
0084         auto job = new RocketChatRestApi::RemoveUserFromRoleJob(this);
0085         job->setRoleName(mRoleName);
0086         modelIndex = mTreeView->model()->index(index.row(), UsersInRoleModel::UserName);
0087         job->setUsername(modelIndex.data().toString());
0088 
0089         mRocketChatAccount->restApi()->initializeRestApiJob(job);
0090         connect(job, &RocketChatRestApi::RemoveUserFromRoleJob::removeUsersFromRoleDone, this, &UsersInRoleWidget::slotRemoveUsersFromRoleDone);
0091         if (!job->start()) {
0092             qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start RemoveUserFromRoleJob job";
0093         }
0094     }
0095 }
0096 
0097 void UsersInRoleWidget::slotRemoveUsersFromRoleDone(const QJsonObject &replyObject)
0098 {
0099     initialize(); // Reload list
0100     // qDebug() << " replyObject " << replyObject;
0101 }
0102 
0103 void UsersInRoleWidget::slotCustomContextMenuRequested(const QPoint &pos)
0104 {
0105     QMenu menu(this);
0106     menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, &UsersInRoleWidget::slotAddUser);
0107     const QModelIndex index = mTreeView->indexAt(pos);
0108     if (index.isValid()) {
0109         menu.addSeparator();
0110         menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove"), this, [this, index]() {
0111             slotRemoveUser(index);
0112         });
0113     }
0114     menu.exec(mTreeView->viewport()->mapToGlobal(pos));
0115 }
0116 
0117 void UsersInRoleWidget::updateLabel()
0118 {
0119     mLabelResultSearch->setText(mModel->total() == 0 ? i18n("No user found") : displayShowMessageInRoom());
0120 }
0121 
0122 QString UsersInRoleWidget::displayShowMessageInRoom() const
0123 {
0124     QString displayMessageStr = i18np("%1 user (Total: %2)", "%1 users (Total: %2)", mModel->rowCount(), mModel->total());
0125     if (!mModel->hasFullList()) {
0126         displayMessageStr += clickableStr();
0127     }
0128     return displayMessageStr;
0129 }
0130 
0131 const QString &UsersInRoleWidget::roleName() const
0132 {
0133     return mRoleName;
0134 }
0135 
0136 void UsersInRoleWidget::setRoleName(const QString &newRoleName)
0137 {
0138     mRoleName = newRoleName;
0139 }
0140 
0141 const QString &UsersInRoleWidget::roleId() const
0142 {
0143     return mRoleId;
0144 }
0145 
0146 void UsersInRoleWidget::setRoleId(const QString &newRoleId)
0147 {
0148     if (newRoleId.isEmpty()) {
0149         qCWarning(RUQOLAWIDGETS_LOG) << " RoleId is empty !";
0150         return;
0151     }
0152     if (mRoleId != newRoleId) {
0153         mRoleId = newRoleId;
0154         initialize();
0155     }
0156 }
0157 
0158 void UsersInRoleWidget::slotLoadElements(int offset, int count, const QString &searchName)
0159 {
0160     auto job = new RocketChatRestApi::GetUsersInRoleJob(this);
0161     job->setRoleId(mRoleId);
0162     RocketChatRestApi::QueryParameters parameters;
0163     QMap<QString, RocketChatRestApi::QueryParameters::SortOrder> map;
0164     map.insert(QStringLiteral("name"), RocketChatRestApi::QueryParameters::SortOrder::Ascendant);
0165     parameters.setSorting(map);
0166     if (offset != -1) {
0167         parameters.setOffset(offset);
0168     }
0169     if (count != -1) {
0170         parameters.setCount(count);
0171     }
0172     parameters.setSearchString(searchName);
0173     job->setQueryParameters(parameters);
0174 
0175     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0176     if (offset != -1) {
0177         connect(job, &RocketChatRestApi::GetUsersInRoleJob::getUsersInRoleDone, this, &UsersInRoleWidget::slotLoadMoreElementDone);
0178     } else {
0179         connect(job, &RocketChatRestApi::GetUsersInRoleJob::getUsersInRoleDone, this, &UsersInRoleWidget::slotSearchDone);
0180     }
0181     if (!job->start()) {
0182         qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start GetUsersInRoleJob job";
0183     }
0184 }
0185 
0186 #include "moc_usersinrolewidget.cpp"