File indexing completed on 2025-01-05 04:58:18

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "collectionaclwidget.h"
0008 #include "acllistview.h"
0009 #include "aclmanager.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QAction>
0014 #include <QActionEvent>
0015 #include <QCheckBox>
0016 #include <QHBoxLayout>
0017 #include <QPushButton>
0018 #include <QVBoxLayout>
0019 
0020 using namespace PimCommon;
0021 /**
0022  * Unfortunately QPushButton doesn't support to plug in
0023  * a QAction like QToolButton does, so we have to reimplement it :(
0024  */
0025 class ActionButton : public QPushButton
0026 {
0027 public:
0028     ActionButton(QWidget *parent = nullptr)
0029         : QPushButton(parent)
0030     {
0031     }
0032 
0033     void setDefaultAction(QAction *action)
0034     {
0035         if (!actions().contains(action)) {
0036             addAction(action);
0037             connect(this, &QAbstractButton::clicked, action, &QAction::trigger);
0038         }
0039 
0040         setText(action->text());
0041         setEnabled(action->isEnabled());
0042 
0043         mDefaultAction = action;
0044     }
0045 
0046 protected:
0047     void actionEvent(QActionEvent *event) override
0048     {
0049         QAction *action = event->action();
0050         switch (event->type()) {
0051         case QEvent::ActionChanged:
0052             if (action == mDefaultAction) {
0053                 setDefaultAction(mDefaultAction);
0054             }
0055             return;
0056             break;
0057         default:
0058             break;
0059         }
0060 
0061         QPushButton::actionEvent(event);
0062     }
0063 
0064 private:
0065     QAction *mDefaultAction = nullptr;
0066 };
0067 
0068 CollectionAclWidget::CollectionAclWidget(QWidget *parent)
0069     : QWidget(parent)
0070     , mAclManager(new PimCommon::AclManager(this))
0071     , mRecursiveChk(new QCheckBox(i18n("Apply permissions on all &subfolders."), this))
0072 {
0073     auto layout = new QHBoxLayout(this);
0074     auto listViewLayout = new QVBoxLayout;
0075     layout->addLayout(listViewLayout);
0076 
0077     auto view = new AclListView;
0078     view->setObjectName(QLatin1StringView("list_view"));
0079     listViewLayout->addWidget(view);
0080     listViewLayout->addWidget(mRecursiveChk);
0081     connect(mRecursiveChk, &QCheckBox::clicked, this, &CollectionAclWidget::slotRecursivePermissionChanged);
0082 
0083     view->setAlternatingRowColors(true);
0084     view->setModel(mAclManager->model());
0085     view->setSelectionModel(mAclManager->selectionModel());
0086 
0087     auto buttonBox = new QWidget;
0088     auto buttonBoxVBoxLayout = new QVBoxLayout(buttonBox);
0089     buttonBoxVBoxLayout->setContentsMargins({});
0090     layout->addWidget(buttonBox);
0091 
0092     auto button = new ActionButton(buttonBox);
0093     buttonBoxVBoxLayout->addWidget(button);
0094     button->setObjectName(QLatin1StringView("add"));
0095     button->setDefaultAction(mAclManager->addAction());
0096 
0097     button = new ActionButton(buttonBox);
0098     buttonBoxVBoxLayout->addWidget(button);
0099     button->setObjectName(QLatin1StringView("edit"));
0100     button->setDefaultAction(mAclManager->editAction());
0101 
0102     button = new ActionButton(buttonBox);
0103     buttonBoxVBoxLayout->addWidget(button);
0104     button->setDefaultAction(mAclManager->deleteAction());
0105     button->setObjectName(QLatin1StringView("delete"));
0106 
0107     auto spacer = new QWidget(buttonBox);
0108     buttonBoxVBoxLayout->addWidget(spacer);
0109     spacer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
0110     connect(view, SIGNAL(doubleClicked(QModelIndex)), mAclManager->editAction(), SIGNAL(triggered()));
0111     connect(mAclManager, &AclManager::collectionCanBeAdministrated, this, &CollectionAclWidget::slotCollectionCanBeAdministrated);
0112     connect(mAclManager, &AclManager::collectionCanBeAdministrated, view, &AclListView::slotCollectionCanBeAdministrated);
0113 }
0114 
0115 CollectionAclWidget::~CollectionAclWidget() = default;
0116 
0117 void CollectionAclWidget::slotCollectionCanBeAdministrated(bool b)
0118 {
0119     if (!b) {
0120         mRecursiveChk->setChecked(false);
0121     }
0122     mRecursiveChk->setEnabled(b);
0123 }
0124 
0125 AclManager *CollectionAclWidget::aclManager() const
0126 {
0127     return mAclManager;
0128 }
0129 
0130 bool CollectionAclWidget::recursive() const
0131 {
0132     return mRecursiveChk->isChecked();
0133 }
0134 
0135 void CollectionAclWidget::setEnableRecursiveCheckBox(bool enable)
0136 {
0137     if (!enable) {
0138         mRecursiveChk->setChecked(false);
0139     }
0140     mRecursiveChk->setEnabled(enable);
0141 }
0142 
0143 void CollectionAclWidget::slotRecursivePermissionChanged()
0144 {
0145     mAclManager->setChanged(true);
0146 }
0147 
0148 #include "moc_collectionaclwidget.cpp"