File indexing completed on 2024-11-24 04:43:07

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 "confirmbeforedeletingwidget.h"
0008 #include "confirmbeforedeletingcreateruledialog.h"
0009 #include "confirmbeforedeletingmanager.h"
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 #include <QHeaderView>
0013 #include <QIcon>
0014 #include <QMenu>
0015 #include <QPointer>
0016 #include <QPushButton>
0017 #include <QTreeWidget>
0018 #include <QVBoxLayout>
0019 
0020 ConfirmBeforeDeletingWidget::ConfirmBeforeDeletingWidget(QWidget *parent)
0021     : QWidget(parent)
0022     , mTreeWidget(new QTreeWidget(this))
0023     , mAddRule(new QPushButton(i18nc("@action:button", "Add Rule..."), this))
0024     , mRemoveRule(new QPushButton(i18nc("@action:button", "Remove Rule"), this))
0025     , mModifyRule(new QPushButton(i18nc("@action:button", "Modify Rule..."), this))
0026 {
0027     auto mainLayout = new QVBoxLayout(this);
0028     mainLayout->setContentsMargins({});
0029     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0030 
0031     auto listLayout = new QHBoxLayout;
0032     mainLayout->addLayout(listLayout);
0033 
0034     mTreeWidget->setObjectName(QLatin1StringView("mTreeWidget"));
0035     listLayout->addWidget(mTreeWidget);
0036     mTreeWidget->setAlternatingRowColors(true);
0037     mTreeWidget->setRootIsDecorated(false);
0038     mTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
0039     mTreeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
0040     mTreeWidget->header()->setSortIndicatorShown(true);
0041     mTreeWidget->setSortingEnabled(true);
0042     const QStringList lst = {i18n("Type"), i18n("Pattern")};
0043     mTreeWidget->setHeaderLabels(lst);
0044     connect(mTreeWidget, &QTreeWidget::customContextMenuRequested, this, &ConfirmBeforeDeletingWidget::slotCustomContextMenuRequested);
0045     connect(mTreeWidget, &QTreeWidget::itemDoubleClicked, this, &ConfirmBeforeDeletingWidget::slotEditRule);
0046     connect(mTreeWidget, &QTreeWidget::itemSelectionChanged, this, &ConfirmBeforeDeletingWidget::updateButtons);
0047 
0048     auto buttonLayout = new QVBoxLayout;
0049     listLayout->addLayout(buttonLayout);
0050 
0051     connect(mAddRule, &QPushButton::clicked, this, &ConfirmBeforeDeletingWidget::slotAddRule);
0052     mAddRule->setObjectName(QLatin1StringView("mAddRule"));
0053     buttonLayout->addWidget(mAddRule);
0054 
0055     connect(mModifyRule, &QPushButton::clicked, this, &ConfirmBeforeDeletingWidget::slotEditRule);
0056     mModifyRule->setObjectName(QLatin1StringView("mModifyRule"));
0057     buttonLayout->addWidget(mModifyRule);
0058 
0059     connect(mRemoveRule, &QPushButton::clicked, this, &ConfirmBeforeDeletingWidget::slotRemoveRule);
0060     mRemoveRule->setObjectName(QLatin1StringView("mRemoveRule"));
0061     buttonLayout->addWidget(mRemoveRule);
0062     buttonLayout->addStretch(1);
0063     updateButtons();
0064 
0065     fillRules();
0066 }
0067 
0068 ConfirmBeforeDeletingWidget::~ConfirmBeforeDeletingWidget() = default;
0069 
0070 void ConfirmBeforeDeletingWidget::updateButtons()
0071 {
0072     QTreeWidgetItem *item = mTreeWidget->currentItem();
0073     if (item) {
0074         mRemoveRule->setEnabled(true);
0075         mModifyRule->setEnabled(true);
0076     } else {
0077         mRemoveRule->setEnabled(false);
0078         mModifyRule->setEnabled(false);
0079     }
0080 }
0081 
0082 void ConfirmBeforeDeletingWidget::fillRules()
0083 {
0084     const QList<ConfirmBeforeDeletingRule> rules = ConfirmBeforeDeletingManager::self()->rules();
0085     for (const ConfirmBeforeDeletingRule &r : rules) {
0086         auto item = new QTreeWidgetItem(mTreeWidget);
0087         const ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo info(r.pattern(), ConfirmBeforeDeletingRule::ruleTypeToString(r.ruleType()));
0088         initializeItem(item, info);
0089     }
0090 }
0091 
0092 void ConfirmBeforeDeletingWidget::slotEditRule()
0093 {
0094     QTreeWidgetItem *item = mTreeWidget->currentItem();
0095     if (item) {
0096         QPointer<ConfirmBeforeDeletingCreateRuleDialog> dlg = new ConfirmBeforeDeletingCreateRuleDialog(this);
0097         const ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo currentInfo(item->text(1), item->text(0));
0098         dlg->setInfo(currentInfo);
0099         if (dlg->exec()) {
0100             const ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo newInfo = dlg->info();
0101             if (currentInfo != newInfo) {
0102                 initializeItem(item, newInfo);
0103             }
0104         }
0105         delete dlg;
0106     }
0107 }
0108 
0109 void ConfirmBeforeDeletingWidget::initializeItem(QTreeWidgetItem *item, const ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo &info)
0110 {
0111     item->setText(1, info.pattern);
0112     item->setText(0, info.ruleType);
0113 }
0114 
0115 void ConfirmBeforeDeletingWidget::slotCustomContextMenuRequested(const QPoint &p)
0116 {
0117     QMenu menu(this);
0118     const auto selectedItemCount{mTreeWidget->selectedItems().count()};
0119     menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add Rule..."), this, &ConfirmBeforeDeletingWidget::slotAddRule);
0120     if (selectedItemCount == 1) {
0121         menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Edit Rule..."), this, &ConfirmBeforeDeletingWidget::slotEditRule);
0122     }
0123     if (selectedItemCount > 0) {
0124         menu.addSeparator();
0125         menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")),
0126                        i18np("Remove Rule", "Remove Rules", selectedItemCount),
0127                        this,
0128                        &ConfirmBeforeDeletingWidget::slotRemoveRule);
0129     }
0130     menu.exec(QCursor::pos());
0131 }
0132 
0133 void ConfirmBeforeDeletingWidget::slotRemoveRule()
0134 {
0135     auto items = mTreeWidget->selectedItems();
0136     if (!items.isEmpty()) {
0137         const int answer = KMessageBox::questionTwoActions(this,
0138                                                            i18np("Do you want to remove this rule?", "Do you want to remove these rules?", items.count()),
0139                                                            i18nc("@title", "Remove Rule"),
0140                                                            KStandardGuiItem::remove(),
0141                                                            KStandardGuiItem::cancel());
0142 
0143         if (answer == KMessageBox::ButtonCode::PrimaryAction) {
0144             while (!items.isEmpty()) {
0145                 delete items.takeFirst();
0146             }
0147         }
0148     }
0149 }
0150 
0151 void ConfirmBeforeDeletingWidget::slotAddRule()
0152 {
0153     QPointer<ConfirmBeforeDeletingCreateRuleDialog> dlg = new ConfirmBeforeDeletingCreateRuleDialog(this);
0154     if (dlg->exec()) {
0155         const ConfirmBeforeDeletingCreateRuleWidget::ConfirmBeforeDeletingInfo info = dlg->info();
0156         ConfirmBeforeDeletingRule r;
0157         r.setPattern(info.pattern);
0158         r.setRuleType(ConfirmBeforeDeletingRule::stringToRuleType(info.ruleType));
0159 
0160         bool ruleFound = false;
0161         for (int i = 0, total = mTreeWidget->topLevelItemCount(); i < total; ++i) {
0162             QTreeWidgetItem *item = mTreeWidget->topLevelItem(i);
0163             ConfirmBeforeDeletingRule currentRule;
0164             currentRule.setPattern(item->text(1));
0165             currentRule.setRuleType(ConfirmBeforeDeletingRule::stringToRuleType(item->text(0)));
0166             if (currentRule == r) {
0167                 KMessageBox::information(this, i18n("Same rule already exists."), i18n("Create Rule"));
0168                 ruleFound = true;
0169                 break;
0170             }
0171         }
0172         if (!ruleFound) {
0173             auto item = new QTreeWidgetItem(mTreeWidget);
0174             initializeItem(item, dlg->info());
0175         }
0176     }
0177     delete dlg;
0178 }
0179 
0180 void ConfirmBeforeDeletingWidget::save()
0181 {
0182     QList<ConfirmBeforeDeletingRule> rules;
0183     for (int i = 0, total = mTreeWidget->topLevelItemCount(); i < total; ++i) {
0184         QTreeWidgetItem *item = mTreeWidget->topLevelItem(i);
0185         ConfirmBeforeDeletingRule r;
0186         r.setPattern(item->text(1));
0187         r.setRuleType(ConfirmBeforeDeletingRule::stringToRuleType(item->text(0)));
0188         rules.append(r);
0189     }
0190     ConfirmBeforeDeletingManager::self()->setRules(rules);
0191     ConfirmBeforeDeletingManager::self()->saveRules();
0192 }
0193 
0194 #include "moc_confirmbeforedeletingwidget.cpp"