Akonadi

invalidatecachejob.cpp
1 /*
2  SPDX-FileCopyrightText: 2011 Volker Krause <[email protected]>
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 
15 using namespace Akonadi;
16 
17 namespace Akonadi
18 {
19 class InvalidateCacheJobPrivate : JobPrivate
20 {
21 public:
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 
36 QString InvalidateCacheJobPrivate::jobDebuggingString() const
37 {
38  return QStringLiteral("Invalidate Cache from collection id: %1").arg(collection.id());
39 }
40 
41 } // namespace Akonadi
42 
43 void 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 
69 void 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 
93 void InvalidateCacheJobPrivate::itemStoreResult(KJob *job)
94 {
95  Q_Q(InvalidateCacheJob);
96  if (job->error()) {
97  return;
98  }
99  q->emitResult();
100 }
101 
102 InvalidateCacheJob::InvalidateCacheJob(const Collection &collection, QObject *parent)
103  : Job(new InvalidateCacheJobPrivate(this), parent)
104 {
105  Q_D(InvalidateCacheJob);
106  d->collection = collection;
107 }
108 
109 void 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"
@ Unknown
Unknown error.
Definition: job.h:102
void result(KJob *job)
Represents a collection of PIM items.
Definition: collection.h:61
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Job that fetches collections from the Akonadi storage.
Job that modifies an existing item in the Akonadi storage.
Definition: itemmodifyjob.h:81
QString i18n(const char *text, const TYPE &arg...)
@ Base
Only fetch the base collection.
Base class for all actions in the Akonadi storage.
Definition: job.h:80
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:69
int error() const
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
Q_D(Todo)
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.