File indexing completed on 2024-10-27 04:51:07

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "removecollectionjob.h"
0008 #include "kmkernel.h"
0009 #include "kmmainwidget.h"
0010 #include <Akonadi/CollectionDeleteJob>
0011 #include <Akonadi/CollectionFetchJob>
0012 #include <KGuiItem>
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 #include <MailCommon/MailKernel>
0016 #include <MailCommon/MailUtil>
0017 
0018 RemoveCollectionJob::RemoveCollectionJob(QObject *parent)
0019     : QObject(parent)
0020 {
0021 }
0022 
0023 RemoveCollectionJob::~RemoveCollectionJob() = default;
0024 
0025 void RemoveCollectionJob::setMainWidget(KMMainWidget *mainWidget)
0026 {
0027     mMainWidget = mainWidget;
0028 }
0029 
0030 void RemoveCollectionJob::setCurrentFolder(const Akonadi::Collection &currentFolder)
0031 {
0032     mCurrentCollection = currentFolder;
0033 }
0034 
0035 void RemoveCollectionJob::start()
0036 {
0037     auto job = new Akonadi::CollectionFetchJob(mCurrentCollection, Akonadi::CollectionFetchJob::FirstLevel, this);
0038     job->fetchScope().setContentMimeTypes(QStringList() << KMime::Message::mimeType());
0039     job->setProperty("collectionId", mCurrentCollection.id());
0040     connect(job, &KJob::result, this, &RemoveCollectionJob::slotDelayedRemoveFolder);
0041 }
0042 
0043 void RemoveCollectionJob::slotDelayedRemoveFolder(KJob *job)
0044 {
0045     const Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob *>(job);
0046     Akonadi::Collection::List listOfCollection = fetchJob->collections();
0047     const bool hasNotSubDirectory = listOfCollection.isEmpty();
0048 
0049     const Akonadi::Collection::Id id = fetchJob->property("collectionId").toLongLong();
0050     const auto col = CommonKernel->collectionFromId(id);
0051     QString str;
0052     QString title;
0053     QString buttonLabel;
0054     const QString colNameHtmlEscaped{col.name().toHtmlEscaped()};
0055     if (col.resource() == QLatin1StringView("akonadi_search_resource")) {
0056         title = i18n("Delete Search");
0057         str = i18n(
0058             "<qt>Are you sure you want to delete the search <b>%1</b>?<br />"
0059             "Any messages it shows will still be available in their original folder.</qt>",
0060             colNameHtmlEscaped);
0061         buttonLabel = i18nc("@action:button Delete search", "&Delete");
0062     } else {
0063         title = i18n("Delete Folder");
0064 
0065         if (col.statistics().count() == 0) {
0066             if (hasNotSubDirectory) {
0067                 str = i18n(
0068                     "<qt>Are you sure you want to delete the empty folder "
0069                     "<b>%1</b>?</qt>",
0070                     colNameHtmlEscaped);
0071             } else {
0072                 str = i18n(
0073                     "<qt>Are you sure you want to delete the empty folder "
0074                     "<resource>%1</resource> and all its subfolders? Those subfolders might "
0075                     "not be empty and their contents will be discarded as well. "
0076                     "<p><b>Beware</b> that discarded messages are not saved "
0077                     "into your Trash folder and are permanently deleted.</p></qt>",
0078                     colNameHtmlEscaped);
0079             }
0080         } else {
0081             if (hasNotSubDirectory) {
0082                 str = i18n(
0083                     "<qt>Are you sure you want to delete the folder "
0084                     "<resource>%1</resource>, discarding its contents? "
0085                     "<p><b>Beware</b> that discarded messages are not saved "
0086                     "into your Trash folder and are permanently deleted.</p></qt>",
0087                     colNameHtmlEscaped);
0088             } else {
0089                 str = i18n(
0090                     "<qt>Are you sure you want to delete the folder <resource>%1</resource> "
0091                     "and all its subfolders, discarding their contents? "
0092                     "<p><b>Beware</b> that discarded messages are not saved "
0093                     "into your Trash folder and are permanently deleted.</p></qt>",
0094                     colNameHtmlEscaped);
0095             }
0096         }
0097         buttonLabel = i18nc("@action:button Delete folder", "&Delete");
0098     }
0099 
0100     if (KMessageBox::warningContinueCancel(mMainWidget,
0101                                            str,
0102                                            title,
0103                                            KGuiItem(buttonLabel, QStringLiteral("edit-delete")),
0104                                            KStandardGuiItem::cancel(),
0105                                            QString(),
0106                                            KMessageBox::Notify | KMessageBox::Dangerous)
0107         == KMessageBox::Continue) {
0108         kmkernel->checkFolderFromResources(listOfCollection << col);
0109 
0110         if (col.id() == mMainWidget->currentCollection().id()) {
0111             Q_EMIT clearCurrentFolder();
0112         }
0113 
0114         auto job = new Akonadi::CollectionDeleteJob(col);
0115         connect(job, &KJob::result, this, &RemoveCollectionJob::slotDeletionCollectionResult);
0116     } else {
0117         deleteLater();
0118     }
0119 }
0120 
0121 void RemoveCollectionJob::slotDeletionCollectionResult(KJob *job)
0122 {
0123     if (job) {
0124         if (!MailCommon::Util::showJobErrorMessage(job)) {
0125             // TODO
0126         }
0127     }
0128     deleteLater();
0129 }
0130 
0131 #include "moc_removecollectionjob.cpp"