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

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "confirmbeforedeletinginterface.h"
0007 #include "confirmbeforedeletingmanager.h"
0008 #include "confirmbeforedeletingmessageboxdialog.h"
0009 #include "confirmbeforedeletingplugin_debug.h"
0010 #include <KLocalizedString>
0011 #include <QAction>
0012 #include <QDialogButtonBox>
0013 #include <QIcon>
0014 #include <QMenu>
0015 #include <QPointer>
0016 
0017 ConfirmBeforeDeletingInterface::ConfirmBeforeDeletingInterface(QObject *parent)
0018     : MessageViewer::MessageViewerCheckBeforeDeletingInterface(parent)
0019 {
0020 }
0021 
0022 ConfirmBeforeDeletingInterface::~ConfirmBeforeDeletingInterface() = default;
0023 
0024 QList<QAction *> ConfirmBeforeDeletingInterface::actions() const
0025 {
0026     return mAction;
0027 }
0028 
0029 void ConfirmBeforeDeletingInterface::createActions(KActionCollection *ac)
0030 {
0031     if (ac) {
0032         auto mainMenu = new QAction(i18n("Confirm Before Deleting"), this);
0033         auto menu = new QMenu;
0034         auto act = new QAction(QIcon::fromTheme(QStringLiteral("settings-configure")), i18n("Configure"), menu);
0035         connect(act, &QAction::triggered, this, &ConfirmBeforeDeletingInterface::slotConfigure);
0036         menu->addAction(act);
0037         mainMenu->setMenu(menu);
0038         mAction << mainMenu;
0039     }
0040 }
0041 
0042 void ConfirmBeforeDeletingInterface::slotConfigure()
0043 {
0044     Q_EMIT configure(parentWidget());
0045 }
0046 
0047 Akonadi::Item::List ConfirmBeforeDeletingInterface::exec(const Akonadi::Item::List &list)
0048 {
0049     Akonadi::Item::List lst;
0050     QString checkFoundStr;
0051 
0052     QList<ConfirmBeforeDeletingRule> ruleDelete;
0053     QList<ConfirmBeforeDeletingRule> ruleNotDelete;
0054     for (const auto &item : list) {
0055         ConfirmBeforeDeletingRule r;
0056         if (ConfirmBeforeDeletingManager::self()->deletingNeedToConfirm(item, checkFoundStr, r)) {
0057             if (ruleDelete.contains(r)) {
0058                 lst << item;
0059                 continue;
0060             }
0061             if (ruleNotDelete.contains(r)) {
0062                 continue;
0063             }
0064 
0065             QPointer<ConfirmBeforeDeletingMessageBoxDialog> dlg = new ConfirmBeforeDeletingMessageBoxDialog(parentWidget());
0066             if (checkFoundStr.length() > 200) {
0067                 checkFoundStr = checkFoundStr.left(200) + QStringLiteral("...");
0068             }
0069             dlg->setInfo(i18n("Do you want to delete this email?\n%1", checkFoundStr));
0070             const int result = dlg->exec();
0071             const auto button = static_cast<QDialogButtonBox::StandardButton>(result);
0072             if (button == QDialogButtonBox::StandardButton::Yes) {
0073                 lst << item;
0074                 if (dlg->useSameResult()) {
0075                     ruleDelete.append(r);
0076                 }
0077                 delete dlg;
0078             } else if (button == QDialogButtonBox::StandardButton::No) {
0079                 if (dlg->useSameResult()) {
0080                     ruleNotDelete.append(r);
0081                 }
0082                 delete dlg;
0083             } else if (button == QDialogButtonBox::StandardButton::Cancel) {
0084                 lst.clear();
0085                 delete dlg;
0086                 break;
0087             } else {
0088                 qCWarning(KMAIL_CONFIRMBEFOREDELETING_PLUGIN_LOG) << " StandardButton is invalid " << button << " .It's a bug!";
0089             }
0090         } else {
0091             lst << item;
0092         }
0093     }
0094     return lst;
0095 }
0096 
0097 #include "moc_confirmbeforedeletinginterface.cpp"