File indexing completed on 2024-10-27 04:51:07
0001 /* 0002 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 0006 */ 0007 0008 #include "removeduplicatemessageinfolderandsubfolderjob.h" 0009 #include "kmail_debug.h" 0010 #include <Akonadi/CollectionFetchJob> 0011 #include <Akonadi/CollectionFetchScope> 0012 #include <Akonadi/RemoveDuplicatesJob> 0013 #include <KLocalizedString> 0014 #include <KMessageBox> 0015 #include <Libkdepim/ProgressManager> 0016 0017 RemoveDuplicateMessageInFolderAndSubFolderJob::RemoveDuplicateMessageInFolderAndSubFolderJob(QObject *parent, QWidget *parentWidget) 0018 : QObject(parent) 0019 , mParentWidget(parentWidget) 0020 { 0021 } 0022 0023 RemoveDuplicateMessageInFolderAndSubFolderJob::~RemoveDuplicateMessageInFolderAndSubFolderJob() = default; 0024 0025 void RemoveDuplicateMessageInFolderAndSubFolderJob::start() 0026 { 0027 if (mTopLevelCollection.isValid()) { 0028 auto fetchJob = new Akonadi::CollectionFetchJob(mTopLevelCollection, Akonadi::CollectionFetchJob::Recursive, this); 0029 fetchJob->fetchScope().setAncestorRetrieval(Akonadi::CollectionFetchScope::All); 0030 connect(fetchJob, &Akonadi::CollectionFetchJob::result, this, [this](KJob *job) { 0031 if (job->error()) { 0032 qCWarning(KMAIL_LOG) << job->errorString(); 0033 slotFetchCollectionFailed(); 0034 } else { 0035 auto fetch = static_cast<Akonadi::CollectionFetchJob *>(job); 0036 slotFetchCollectionDone(fetch->collections()); 0037 } 0038 }); 0039 } else { 0040 qCDebug(KMAIL_LOG()) << "Invalid toplevel collection"; 0041 deleteLater(); 0042 } 0043 } 0044 0045 void RemoveDuplicateMessageInFolderAndSubFolderJob::setTopLevelCollection(const Akonadi::Collection &topLevelCollection) 0046 { 0047 mTopLevelCollection = topLevelCollection; 0048 } 0049 0050 void RemoveDuplicateMessageInFolderAndSubFolderJob::slotFetchCollectionFailed() 0051 { 0052 qCDebug(KMAIL_LOG()) << "Fetch toplevel collection failed"; 0053 deleteLater(); 0054 } 0055 0056 void RemoveDuplicateMessageInFolderAndSubFolderJob::slotFetchCollectionDone(const Akonadi::Collection::List &list) 0057 { 0058 Akonadi::Collection::List lst; 0059 if (mTopLevelCollection.rights() & Akonadi::Collection::CanDeleteItem) { 0060 lst.append(mTopLevelCollection); 0061 } 0062 for (const Akonadi::Collection &collection : list) { 0063 if (collection.isValid()) { 0064 if (collection.rights() & Akonadi::Collection::CanDeleteItem) { 0065 lst.append(collection); 0066 } 0067 } 0068 } 0069 if (lst.isEmpty()) { 0070 deleteLater(); 0071 } else { 0072 KPIM::ProgressItem *item = KPIM::ProgressManager::createProgressItem(i18n("Removing duplicates")); 0073 item->setUsesBusyIndicator(true); 0074 item->setCryptoStatus(KPIM::ProgressItem::Unknown); 0075 0076 auto job = new Akonadi::RemoveDuplicatesJob(lst, this); 0077 job->setProperty("ProgressItem", QVariant::fromValue(item)); 0078 item->setProperty("RemoveDuplicatesJob", QVariant::fromValue(qobject_cast<Akonadi::Job *>(job))); 0079 connect(job, &Akonadi::RemoveDuplicatesJob::finished, this, &RemoveDuplicateMessageInFolderAndSubFolderJob::slotFinished); 0080 connect(job, &Akonadi::RemoveDuplicatesJob::description, this, &RemoveDuplicateMessageInFolderAndSubFolderJob::slotRemoveDuplicatesUpdate); 0081 connect(item, &KPIM::ProgressItem::progressItemCanceled, this, &RemoveDuplicateMessageInFolderAndSubFolderJob::slotRemoveDuplicatesCanceled); 0082 } 0083 } 0084 0085 void RemoveDuplicateMessageInFolderAndSubFolderJob::slotFinished(KJob *job) 0086 { 0087 auto item = job->property("ProgressItem").value<KPIM::ProgressItem *>(); 0088 if (item) { 0089 item->setComplete(); 0090 item->setStatus(i18n("Done")); 0091 item = nullptr; 0092 } 0093 if (job->error()) { 0094 qCDebug(KMAIL_LOG()) << " Error during remove duplicates " << job->errorString(); 0095 KMessageBox::error(mParentWidget, 0096 i18n("Error occurred during removing duplicate emails: \'%1\'", job->errorText()), 0097 i18nc("@title:window", "Error while removing duplicates")); 0098 } 0099 0100 deleteLater(); 0101 } 0102 0103 void RemoveDuplicateMessageInFolderAndSubFolderJob::slotRemoveDuplicatesUpdate(KJob *job, const QString &description) 0104 { 0105 auto item = job->property("ProgressItem").value<KPIM::ProgressItem *>(); 0106 if (item) { 0107 item->setStatus(description); 0108 } 0109 } 0110 0111 void RemoveDuplicateMessageInFolderAndSubFolderJob::slotRemoveDuplicatesCanceled(KPIM::ProgressItem *item) 0112 { 0113 auto job = item->property("RemoveDuplicatesJob").value<Akonadi::Job *>(); 0114 if (job) { 0115 job->kill(KJob::Quietly); 0116 } 0117 0118 item->setComplete(); 0119 item = nullptr; 0120 deleteLater(); 0121 } 0122 0123 #include "moc_removeduplicatemessageinfolderandsubfolderjob.cpp"