Akonadi

invalidatecachejob.cpp
1/*
2 SPDX-FileCopyrightText: 2011 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "collectionfetchjob.h"
8#include "invalidatecachejob_p.h"
9#include "itemfetchjob.h"
10#include "itemmodifyjob.h"
11#include "job_p.h"
12
13#include <KLocalizedString>
14
15using namespace Akonadi;
16
17namespace Akonadi
18{
19class InvalidateCacheJobPrivate : JobPrivate
20{
21public:
22 explicit InvalidateCacheJobPrivate(InvalidateCacheJob *qq)
23 : JobPrivate(qq)
24 {
25 }
26 Collection collection;
27
28 QString jobDebuggingString() const override;
29 void collectionFetchResult(KJob *job);
30 void itemFetchResult(KJob *job);
31 void itemStoreResult(KJob *job);
32
33 Q_DECLARE_PUBLIC(InvalidateCacheJob)
34};
35
36QString InvalidateCacheJobPrivate::jobDebuggingString() const
37{
38 return QStringLiteral("Invalidate Cache from collection id: %1").arg(collection.id());
39}
40
41} // namespace Akonadi
42
43void InvalidateCacheJobPrivate::collectionFetchResult(KJob *job)
44{
45 Q_Q(InvalidateCacheJob);
46 if (job->error()) {
47 return; // handled by KCompositeJob
48 }
49
50 auto fetchJob = qobject_cast<CollectionFetchJob *>(job);
51 Q_ASSERT(fetchJob);
52 if (fetchJob->collections().size() == 1) {
53 collection = fetchJob->collections().at(0);
54 }
55
56 if (!collection.isValid()) {
57 q->setError(Job::Unknown);
58 q->setErrorText(i18n("Invalid collection."));
59 q->emitResult();
60 return;
61 }
62
63 auto itemFetch = new ItemFetchJob(collection, q);
64 QObject::connect(itemFetch, &ItemFetchJob::result, q, [this](KJob *job) {
65 itemFetchResult(job);
66 });
67}
68
69void InvalidateCacheJobPrivate::itemFetchResult(KJob *job)
70{
71 Q_Q(InvalidateCacheJob);
72 if (job->error()) {
73 return;
74 }
75 auto fetchJob = qobject_cast<ItemFetchJob *>(job);
76 Q_ASSERT(fetchJob);
77 if (fetchJob->items().isEmpty()) {
78 q->emitResult();
79 return;
80 }
81
82 ItemModifyJob *modJob = nullptr;
83 const Akonadi::Item::List itemsLst = fetchJob->items();
84 for (Item item : itemsLst) {
85 item.clearPayload();
86 modJob = new ItemModifyJob(item, q);
87 }
88 QObject::connect(modJob, &KJob::result, q, [this](KJob *job) {
89 itemStoreResult(job);
90 });
91}
92
93void InvalidateCacheJobPrivate::itemStoreResult(KJob *job)
94{
95 Q_Q(InvalidateCacheJob);
96 if (job->error()) {
97 return;
98 }
99 q->emitResult();
100}
101
102InvalidateCacheJob::InvalidateCacheJob(const Collection &collection, QObject *parent)
103 : Job(new InvalidateCacheJobPrivate(this), parent)
104{
105 Q_D(InvalidateCacheJob);
106 d->collection = collection;
107}
108
109void InvalidateCacheJob::doStart()
110{
111 Q_D(InvalidateCacheJob);
112 // resolve RID-only collections
113 auto job = new CollectionFetchJob(d->collection, Akonadi::CollectionFetchJob::Base, this);
114 connect(job, &KJob::result, this, [d](KJob *job) {
115 d->collectionFetchResult(job);
116 });
117}
118
119#include "moc_invalidatecachejob_p.cpp"
Job that fetches collections from the Akonadi storage.
@ Base
Only fetch the base collection.
Represents a collection of PIM items.
Definition collection.h:62
Job that fetches items from the Akonadi storage.
Job that modifies an existing item in the Akonadi storage.
Base class for all actions in the Akonadi storage.
Definition job.h:81
@ Unknown
Unknown error.
Definition job.h:102
int error() const
void result(KJob *job)
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString arg(Args &&... args) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.