File indexing completed on 2025-01-05 04:49:32

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "akonadidatabasetoolsplugininterface.h"
0008 #include "akonadidatabasetoolsdialog.h"
0009 #include "akonadidatabasetoolsjob.h"
0010 #include "akonadidatasetools_debug.h"
0011 #include <KActionCollection>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QAction>
0015 
0016 AkonadiDatabaseToolsPluginInterface::AkonadiDatabaseToolsPluginInterface(QObject *parent)
0017     : PimCommon::GenericPluginInterface(parent)
0018 {
0019 }
0020 
0021 AkonadiDatabaseToolsPluginInterface::~AkonadiDatabaseToolsPluginInterface() = default;
0022 
0023 void AkonadiDatabaseToolsPluginInterface::createAction(KActionCollection *ac)
0024 {
0025     {
0026         auto action = new QAction(i18n("&Akonadi Vacuum..."), this);
0027         ac->addAction(QStringLiteral("akonadivacuum"), action);
0028         connect(action, &QAction::triggered, this, [this]() {
0029             if (KMessageBox::warningTwoActions(parentWidget(),
0030                                                i18n("Do you want to vacuum akonadi database?"),
0031                                                i18nc("@title:window", "Akonadi Vacuum"),
0032                                                KGuiItem(i18n("Vacuum")),
0033                                                KStandardGuiItem::cancel())
0034                 == KMessageBox::ButtonCode::PrimaryAction) {
0035                 mTool = AkonadiDatabaseToolsUtils::AkonadiDatabaseTool::Vacuum;
0036                 slotActivated();
0037             }
0038         });
0039 
0040         PimCommon::ActionType type(action, PimCommon::ActionType::Tools);
0041         addActionType(type);
0042     }
0043     {
0044         auto action = new QAction(i18n("&Akonadi Fsck..."), this);
0045         ac->addAction(QStringLiteral("akonadifsck"), action);
0046         connect(action, &QAction::triggered, this, [this]() {
0047             if (KMessageBox::warningTwoActions(
0048 
0049                     parentWidget(),
0050                     i18n("Do you want to fsck akonadi database?"),
0051                     i18nc("@title:window", "Akonadi Fsck"),
0052                     KGuiItem(i18n("Fsck")),
0053                     KStandardGuiItem::cancel())
0054                 == KMessageBox::ButtonCode::PrimaryAction) {
0055                 mTool = AkonadiDatabaseToolsUtils::AkonadiDatabaseTool::Fsck;
0056                 slotActivated();
0057             }
0058         });
0059 
0060         PimCommon::ActionType type(action, PimCommon::ActionType::Tools);
0061         addActionType(type);
0062     }
0063 }
0064 
0065 void AkonadiDatabaseToolsPluginInterface::slotActivated()
0066 {
0067     Q_EMIT emitPluginActivated(this);
0068 }
0069 
0070 void AkonadiDatabaseToolsPluginInterface::exec()
0071 {
0072     auto dlg = new AkonadiDatabaseToolsDialog(parentWidget());
0073     switch (mTool) {
0074     case AkonadiDatabaseToolsUtils::AkonadiDatabaseTool::Fsck:
0075         dlg->setWindowTitle(i18nc("@title:window", "Akonadi Fsck"));
0076         break;
0077     case AkonadiDatabaseToolsUtils::AkonadiDatabaseTool::Vacuum:
0078         dlg->setWindowTitle(i18nc("@title:window", "Akonadi Vacuum"));
0079         break;
0080     case AkonadiDatabaseToolsUtils::AkonadiDatabaseTool::Unknown:
0081         qCWarning(AKONADIDATABASETOOLS_LOG) << "Tool is unknown it's a bug";
0082         break;
0083     }
0084 
0085     dlg->show();
0086     auto job = new AkonadiDatabaseToolsJob(this);
0087     job->setTool(mTool);
0088     connect(job, &AkonadiDatabaseToolsJob::receivedStandardError, dlg, &AkonadiDatabaseToolsDialog::appendErrorText);
0089     connect(job, &AkonadiDatabaseToolsJob::receivedStandardOutput, dlg, &AkonadiDatabaseToolsDialog::appendText);
0090     connect(job, &AkonadiDatabaseToolsJob::finished, dlg, [dlg]() {
0091         dlg->appendText(QStringLiteral("---------"));
0092         dlg->appendText(i18n("Finished."));
0093     });
0094     dlg->appendText(i18n("Start..."));
0095     dlg->appendText(QStringLiteral("---------"));
0096     job->start();
0097 }