File indexing completed on 2024-11-10 04:40:29

0001 /*
0002     SPDX-FileCopyrightText: 2011 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "collectionfetchjob.h"
0008 #include "invalidatecachejob_p.h"
0009 #include "itemfetchjob.h"
0010 #include "itemmodifyjob.h"
0011 #include "job_p.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 using namespace Akonadi;
0016 
0017 namespace Akonadi
0018 {
0019 class InvalidateCacheJobPrivate : JobPrivate
0020 {
0021 public:
0022     explicit InvalidateCacheJobPrivate(InvalidateCacheJob *qq)
0023         : JobPrivate(qq)
0024     {
0025     }
0026     Collection collection;
0027 
0028     QString jobDebuggingString() const override;
0029     void collectionFetchResult(KJob *job);
0030     void itemFetchResult(KJob *job);
0031     void itemStoreResult(KJob *job);
0032 
0033     Q_DECLARE_PUBLIC(InvalidateCacheJob)
0034 };
0035 
0036 QString InvalidateCacheJobPrivate::jobDebuggingString() const
0037 {
0038     return QStringLiteral("Invalidate Cache from collection id: %1").arg(collection.id());
0039 }
0040 
0041 } // namespace Akonadi
0042 
0043 void InvalidateCacheJobPrivate::collectionFetchResult(KJob *job)
0044 {
0045     Q_Q(InvalidateCacheJob);
0046     if (job->error()) {
0047         return; // handled by KCompositeJob
0048     }
0049 
0050     auto fetchJob = qobject_cast<CollectionFetchJob *>(job);
0051     Q_ASSERT(fetchJob);
0052     if (fetchJob->collections().size() == 1) {
0053         collection = fetchJob->collections().at(0);
0054     }
0055 
0056     if (!collection.isValid()) {
0057         q->setError(Job::Unknown);
0058         q->setErrorText(i18n("Invalid collection."));
0059         q->emitResult();
0060         return;
0061     }
0062 
0063     auto itemFetch = new ItemFetchJob(collection, q);
0064     QObject::connect(itemFetch, &ItemFetchJob::result, q, [this](KJob *job) {
0065         itemFetchResult(job);
0066     });
0067 }
0068 
0069 void InvalidateCacheJobPrivate::itemFetchResult(KJob *job)
0070 {
0071     Q_Q(InvalidateCacheJob);
0072     if (job->error()) {
0073         return;
0074     }
0075     auto fetchJob = qobject_cast<ItemFetchJob *>(job);
0076     Q_ASSERT(fetchJob);
0077     if (fetchJob->items().isEmpty()) {
0078         q->emitResult();
0079         return;
0080     }
0081 
0082     ItemModifyJob *modJob = nullptr;
0083     const Akonadi::Item::List itemsLst = fetchJob->items();
0084     for (Item item : itemsLst) {
0085         item.clearPayload();
0086         modJob = new ItemModifyJob(item, q);
0087     }
0088     QObject::connect(modJob, &KJob::result, q, [this](KJob *job) {
0089         itemStoreResult(job);
0090     });
0091 }
0092 
0093 void InvalidateCacheJobPrivate::itemStoreResult(KJob *job)
0094 {
0095     Q_Q(InvalidateCacheJob);
0096     if (job->error()) {
0097         return;
0098     }
0099     q->emitResult();
0100 }
0101 
0102 InvalidateCacheJob::InvalidateCacheJob(const Collection &collection, QObject *parent)
0103     : Job(new InvalidateCacheJobPrivate(this), parent)
0104 {
0105     Q_D(InvalidateCacheJob);
0106     d->collection = collection;
0107 }
0108 
0109 void InvalidateCacheJob::doStart()
0110 {
0111     Q_D(InvalidateCacheJob);
0112     // resolve RID-only collections
0113     auto job = new CollectionFetchJob(d->collection, Akonadi::CollectionFetchJob::Base, this);
0114     connect(job, &KJob::result, this, [d](KJob *job) {
0115         d->collectionFetchResult(job);
0116     });
0117 }
0118 
0119 #include "moc_invalidatecachejob_p.cpp"