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 "permissionswidget.h" 0008 #include "connection.h" 0009 #include "model/permissionsmodel.h" 0010 #include "permissions/permissions.h" 0011 #include "permissions/permissionslistalljob.h" 0012 #include "permissions/permissionupdatejob.h" 0013 #include "permissionseditdialog.h" 0014 #include "permissionstreeview.h" 0015 #include "rocketchataccount.h" 0016 #include "ruqolawidgets_debug.h" 0017 #include <KLineEditEventHandler> 0018 #include <KLocalizedString> 0019 #include <QLineEdit> 0020 #include <QMenu> 0021 #include <QPointer> 0022 #include <QSortFilterProxyModel> 0023 #include <QVBoxLayout> 0024 0025 PermissionsWidget::PermissionsWidget(RocketChatAccount *account, QWidget *parent) 0026 : QWidget(parent) 0027 , mTreeView(new PermissionsTreeView(this)) 0028 , mSearchLineWidget(new QLineEdit(this)) 0029 , mAdminPermissionsModel(new PermissionsModel(this)) 0030 , mPermissionFilterProxyModel(new QSortFilterProxyModel(this)) 0031 , mRocketChatAccount(account) 0032 { 0033 mPermissionFilterProxyModel->setObjectName(QStringLiteral("permissionFilterProxyModel")); 0034 0035 auto mainLayout = new QVBoxLayout(this); 0036 mainLayout->setObjectName(QStringLiteral("mainLayout")); 0037 mainLayout->setContentsMargins({}); 0038 mainLayout->setSpacing(0); 0039 0040 mSearchLineWidget->setObjectName(QStringLiteral("mSearchLineWidget")); 0041 mSearchLineWidget->setPlaceholderText(i18n("Search permissions...")); 0042 KLineEditEventHandler::catchReturnKey(mSearchLineWidget); 0043 mSearchLineWidget->setClearButtonEnabled(true); 0044 mainLayout->addWidget(mSearchLineWidget); 0045 mTreeView->setObjectName(QStringLiteral("mTreeView")); 0046 mainLayout->addWidget(mTreeView); 0047 mPermissionFilterProxyModel->setSourceModel(mAdminPermissionsModel); 0048 mTreeView->setModel(mPermissionFilterProxyModel); 0049 mTreeView->setColumnHidden(PermissionsModel::RolesRole, true); 0050 connect(mTreeView, &QTreeView::customContextMenuRequested, this, &PermissionsWidget::slotCustomContextMenuRequested); 0051 connect(mSearchLineWidget, &QLineEdit::textChanged, this, &PermissionsWidget::slotFilterTextChanged); 0052 connect(mTreeView, &QTreeView::doubleClicked, this, &PermissionsWidget::slotModifyDoubleClickRoles); 0053 } 0054 0055 PermissionsWidget::~PermissionsWidget() = default; 0056 0057 void PermissionsWidget::slotFilterTextChanged(const QString &str) 0058 { 0059 mPermissionFilterProxyModel->setFilterFixedString(str); 0060 } 0061 0062 void PermissionsWidget::initialize() 0063 { 0064 // First load list of roles. 0065 mRoleInfo = mRocketChatAccount->roleInfo(); 0066 0067 auto permissionsListAllJob = new RocketChatRestApi::PermissionsListAllJob(this); 0068 mRocketChatAccount->restApi()->initializeRestApiJob(permissionsListAllJob); 0069 connect(permissionsListAllJob, &RocketChatRestApi::PermissionsListAllJob::permissionListAllDone, this, &PermissionsWidget::slotPermissionListAllDone); 0070 if (!permissionsListAllJob->start()) { 0071 qCDebug(RUQOLAWIDGETS_LOG) << "Impossible to start PermissionsListAllJob"; 0072 } 0073 } 0074 0075 void PermissionsWidget::slotPermissionListAllDone(const QJsonObject &obj) 0076 { 0077 Permissions p; 0078 p.parsePermissions(obj, {}, mRoleInfo); 0079 mAdminPermissionsModel->setPermissions(p); 0080 // qDebug() << "obj" << obj; 0081 for (int i : {PermissionsModel::IdentifierRole, PermissionsModel::RolesStrRole}) { 0082 mTreeView->resizeColumnToContents(i); 0083 } 0084 } 0085 0086 void PermissionsWidget::slotCustomContextMenuRequested(const QPoint &pos) 0087 { 0088 const QModelIndex index = mTreeView->indexAt(pos); 0089 if (mRocketChatAccount->hasPermission(QStringLiteral("access-permissions"))) { 0090 if (index.isValid()) { 0091 QMenu menu(this); 0092 menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Modify..."), this, [this, index]() { 0093 modifyRoles(index); 0094 }); 0095 menu.exec(mTreeView->viewport()->mapToGlobal(pos)); 0096 } 0097 } 0098 } 0099 0100 void PermissionsWidget::slotModifyDoubleClickRoles(const QModelIndex &index) 0101 { 0102 if (index.isValid()) { 0103 if (mRocketChatAccount->hasPermission(QStringLiteral("access-permissions"))) { 0104 modifyRoles(index); 0105 } 0106 } 0107 } 0108 0109 void PermissionsWidget::modifyRoles(const QModelIndex &index) 0110 { 0111 const QModelIndex modelIndex = mTreeView->model()->index(index.row(), PermissionsModel::RolesRole); 0112 const QString identifier = mTreeView->model()->index(index.row(), PermissionsModel::IdentifierRole).data().toString(); 0113 slotEditRoles(modelIndex.data().toStringList(), identifier); 0114 } 0115 0116 void PermissionsWidget::slotEditRoles(const QStringList &roles, const QString &identifier) 0117 { 0118 QPointer<PermissionsEditDialog> dialog = new PermissionsEditDialog(this, identifier); 0119 dialog->setRolesInfo(mRoleInfo); 0120 dialog->setRoles(roles); 0121 if (dialog->exec()) { 0122 const QStringList lst = dialog->roles(); 0123 auto permissionsUpdateJob = new RocketChatRestApi::PermissionUpdateJob(this); 0124 mRocketChatAccount->restApi()->initializeRestApiJob(permissionsUpdateJob); 0125 QMap<QString, QStringList> mapPermission; 0126 mapPermission.insert(identifier, lst); 0127 permissionsUpdateJob->setPermissions(mapPermission); 0128 connect(permissionsUpdateJob, &RocketChatRestApi::PermissionUpdateJob::permissionUpdateDone, this, &PermissionsWidget::slotPermissionUpdateDone); 0129 if (!permissionsUpdateJob->start()) { 0130 qCDebug(RUQOLAWIDGETS_LOG) << "Impossible to start PermissionUpdateJob"; 0131 } 0132 } 0133 delete dialog; 0134 } 0135 0136 void PermissionsWidget::slotPermissionUpdateDone(const QJsonObject &obj) 0137 { 0138 // qDebug() << " obj " << obj; 0139 Permissions p; 0140 p.parsePermissions(obj, QStringLiteral("permissions"), mRoleInfo); 0141 mAdminPermissionsModel->setPermissions(p); 0142 } 0143 0144 #include "moc_permissionswidget.cpp"