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 "removeduplicatemailjob.h"
0008 
0009 #include <Akonadi/Collection>
0010 #include <Akonadi/EntityTreeModel>
0011 #include <Akonadi/RemoveDuplicatesJob>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <Libkdepim/ProgressManager>
0015 
0016 #include <QItemSelectionModel>
0017 Q_DECLARE_METATYPE(KPIM::ProgressItem *)
0018 Q_DECLARE_METATYPE(Akonadi::Job *)
0019 
0020 RemoveDuplicateMailJob::RemoveDuplicateMailJob(QItemSelectionModel *selectionModel, QWidget *widget, QObject *parent)
0021     : QObject(parent)
0022     , mParent(widget)
0023     , mSelectionModel(selectionModel)
0024 {
0025 }
0026 
0027 RemoveDuplicateMailJob::~RemoveDuplicateMailJob() = default;
0028 
0029 void RemoveDuplicateMailJob::start()
0030 {
0031     KPIM::ProgressItem *item = KPIM::ProgressManager::createProgressItem(i18n("Removing duplicates"));
0032     item->setUsesBusyIndicator(true);
0033     item->setCryptoStatus(KPIM::ProgressItem::Unknown);
0034 
0035     const QModelIndexList indexes = mSelectionModel->selectedIndexes();
0036     Akonadi::Collection::List collections;
0037 
0038     for (const QModelIndex &index : indexes) {
0039         const auto collection = index.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
0040         if (collection.isValid()) {
0041             collections << collection;
0042         }
0043     }
0044 
0045     auto job = new Akonadi::RemoveDuplicatesJob(collections, this);
0046     job->setProperty("ProgressItem", QVariant::fromValue(item));
0047     item->setProperty("RemoveDuplicatesJob", QVariant::fromValue(job));
0048     connect(job, &KJob::finished, this, &RemoveDuplicateMailJob::slotRemoveDuplicatesDone);
0049     connect(job, &KJob::description, this, &RemoveDuplicateMailJob::slotRemoveDuplicatesUpdate);
0050     connect(item, &KPIM::ProgressItem::progressItemCanceled, this, &RemoveDuplicateMailJob::slotRemoveDuplicatesCanceled);
0051 }
0052 
0053 void RemoveDuplicateMailJob::slotRemoveDuplicatesDone(KJob *job)
0054 {
0055     auto item = job->property("ProgressItem").value<KPIM::ProgressItem *>();
0056     if (item) {
0057         item->setComplete();
0058         item->setStatus(i18n("Done"));
0059         item = nullptr;
0060     }
0061     if (job->error()) {
0062         KMessageBox::error(mParent,
0063                            i18n("Error occurred during removing duplicate emails: \'%1\'", job->errorText()),
0064                            i18nc("@title:window", "Error while removing duplicates"));
0065     }
0066     deleteLater();
0067 }
0068 
0069 void RemoveDuplicateMailJob::slotRemoveDuplicatesCanceled(KPIM::ProgressItem *item)
0070 {
0071     auto job = item->property("RemoveDuplicatesJob").value<Akonadi::Job *>();
0072     if (job) {
0073         job->kill(KJob::Quietly);
0074     }
0075 
0076     item->setComplete();
0077     item = nullptr;
0078     deleteLater();
0079 }
0080 
0081 void RemoveDuplicateMailJob::slotRemoveDuplicatesUpdate(KJob *job, const QString &description)
0082 {
0083     auto item = job->property("ProgressItem").value<KPIM::ProgressItem *>();
0084     if (item) {
0085         item->setStatus(description);
0086     }
0087 }
0088 
0089 #include "moc_removeduplicatemailjob.cpp"