File indexing completed on 2024-12-22 05:01:11
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 0006 */ 0007 0008 #include "clearcachejobinfolderandsubfolderjob.h" 0009 #include "kmail_debug.h" 0010 #include <Akonadi/ClearCacheFoldersJob> 0011 #include <Akonadi/CollectionFetchJob> 0012 #include <Akonadi/CollectionFetchScope> 0013 #include <KLocalizedString> 0014 #include <KMessageBox> 0015 #include <Libkdepim/ProgressManager> 0016 0017 ClearCacheJobInFolderAndSubFolderJob::ClearCacheJobInFolderAndSubFolderJob(QObject *parent, QWidget *parentWidget) 0018 : QObject(parent) 0019 , mParentWidget(parentWidget) 0020 { 0021 } 0022 0023 ClearCacheJobInFolderAndSubFolderJob::~ClearCacheJobInFolderAndSubFolderJob() = default; 0024 0025 void ClearCacheJobInFolderAndSubFolderJob::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 ClearCacheJobInFolderAndSubFolderJob::setTopLevelCollection(const Akonadi::Collection &topLevelCollection) 0046 { 0047 mTopLevelCollection = topLevelCollection; 0048 } 0049 0050 void ClearCacheJobInFolderAndSubFolderJob::slotFetchCollectionFailed() 0051 { 0052 qCDebug(KMAIL_LOG()) << "Fetch toplevel collection failed"; 0053 deleteLater(); 0054 } 0055 0056 void ClearCacheJobInFolderAndSubFolderJob::slotFetchCollectionDone(const Akonadi::Collection::List &list) 0057 { 0058 Akonadi::Collection::List lst; 0059 for (const Akonadi::Collection &collection : list) { 0060 if (collection.isValid()) { 0061 if (collection.rights() & Akonadi::Collection::CanDeleteItem) { 0062 lst.append(collection); 0063 } 0064 } 0065 } 0066 if (lst.isEmpty()) { 0067 deleteLater(); 0068 } else { 0069 KPIM::ProgressItem *item = KPIM::ProgressManager::createProgressItem(i18n("Clear Akonadi Cache")); 0070 item->setUsesBusyIndicator(true); 0071 item->setCryptoStatus(KPIM::ProgressItem::Unknown); 0072 0073 auto job = new Akonadi::ClearCacheFoldersJob(lst, this); 0074 job->setProperty("ProgressItem", QVariant::fromValue(item)); 0075 item->setProperty("ClearCacheFoldersJob", QVariant::fromValue(qobject_cast<Akonadi::Job *>(job))); 0076 connect(job, &Akonadi::ClearCacheFoldersJob::clearCacheDone, this, &ClearCacheJobInFolderAndSubFolderJob::clearCacheDone); 0077 connect(job, &Akonadi::ClearCacheFoldersJob::finished, this, [this, job](bool success) { 0078 // TODO use it 0079 Q_UNUSED(success) 0080 0081 slotFinished(job); 0082 }); 0083 // connect(job, &Akonadi::ClearCacheFoldersJob::description, this, &ClearCacheJobInFolderAndSubFolderJob::slotClearAkonadiCacheUpdate); 0084 connect(item, &KPIM::ProgressItem::progressItemCanceled, this, &ClearCacheJobInFolderAndSubFolderJob::slotClearAkonadiCacheCanceled); 0085 job->start(); 0086 } 0087 } 0088 0089 void ClearCacheJobInFolderAndSubFolderJob::slotFinished(Akonadi::ClearCacheFoldersJob *job) 0090 { 0091 auto item = job->property("ProgressItem").value<KPIM::ProgressItem *>(); 0092 if (item) { 0093 item->setComplete(); 0094 item->setStatus(i18n("Done")); 0095 item = nullptr; 0096 } 0097 deleteLater(); 0098 } 0099 0100 void ClearCacheJobInFolderAndSubFolderJob::slotClearAkonadiCacheUpdate(Akonadi::ClearCacheFoldersJob *job, const QString &description) 0101 { 0102 auto item = job->property("ProgressItem").value<KPIM::ProgressItem *>(); 0103 if (item) { 0104 item->setStatus(description); 0105 } 0106 } 0107 0108 void ClearCacheJobInFolderAndSubFolderJob::slotClearAkonadiCacheCanceled(KPIM::ProgressItem *item) 0109 { 0110 auto job = item->property("ClearCacheFoldersJob").value<Akonadi::ClearCacheFoldersJob *>(); 0111 if (job) { 0112 job->setCanceled(true); 0113 } 0114 0115 item->setComplete(); 0116 item = nullptr; 0117 deleteLater(); 0118 } 0119 0120 #include "moc_clearcachejobinfolderandsubfolderjob.cpp"