Akonadi

invalidatecachejob.cpp
1 /*
2  Copyright (c) 2011 Volker Krause <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "invalidatecachejob_p.h"
21 #include "job_p.h"
22 #include "collectionfetchjob.h"
23 #include "itemfetchjob.h"
24 #include "itemmodifyjob.h"
25 
26 #include <KLocalizedString>
27 
28 using namespace Akonadi;
29 
30 namespace Akonadi
31 {
32 
33 class InvalidateCacheJobPrivate : JobPrivate
34 {
35 public:
36  InvalidateCacheJobPrivate(InvalidateCacheJob *qq)
37  : JobPrivate(qq)
38  {
39  }
40  Collection collection;
41 
42  QString jobDebuggingString() const override;
43  void collectionFetchResult(KJob *job);
44  void itemFetchResult(KJob *job);
45  void itemStoreResult(KJob *job);
46 
47  Q_DECLARE_PUBLIC(InvalidateCacheJob)
48 };
49 
50 QString InvalidateCacheJobPrivate::jobDebuggingString() const
51 {
52  return QStringLiteral("Invalidate Cache from collection id: %1").arg(collection.id());
53 }
54 
55 }
56 
57 void InvalidateCacheJobPrivate::collectionFetchResult(KJob *job)
58 {
59  Q_Q(InvalidateCacheJob);
60  if (job->error()) {
61  return; // handled by KCompositeJob
62  }
63 
64  CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>(job);
65  Q_ASSERT(fetchJob);
66  if (fetchJob->collections().size() == 1) {
67  collection = fetchJob->collections().at(0);
68  }
69 
70  if (!collection.isValid()) {
71  q->setError(Job::Unknown);
72  q->setErrorText(i18n("Invalid collection."));
73  q->emitResult();
74  return;
75  }
76 
77  ItemFetchJob *itemFetch = new ItemFetchJob(collection, q);
78  QObject::connect(itemFetch, &ItemFetchJob::result, q, [this](KJob* job) { itemFetchResult(job);} );
79 }
80 
81 void InvalidateCacheJobPrivate::itemFetchResult(KJob *job)
82 {
83  Q_Q(InvalidateCacheJob);
84  if (job->error()) {
85  return;
86  }
87  ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
88  Q_ASSERT(fetchJob);
89  if (fetchJob->items().isEmpty()) {
90  q->emitResult();
91  return;
92  }
93 
94  ItemModifyJob *modJob = nullptr;
95  const Akonadi::Item::List itemsLst = fetchJob->items();
96  for (Item item : itemsLst) {
97  item.clearPayload();
98  modJob = new ItemModifyJob(item, q);
99  }
100  QObject::connect(modJob, &KJob::result, q, [this](KJob *job) { itemStoreResult(job); });
101 }
102 
103 void InvalidateCacheJobPrivate::itemStoreResult(KJob *job)
104 {
105  Q_Q(InvalidateCacheJob);
106  if (job->error()) {
107  return;
108  }
109  q->emitResult();
110 }
111 
112 InvalidateCacheJob::InvalidateCacheJob(const Collection &collection, QObject *parent)
113  : Job(new InvalidateCacheJobPrivate(this), parent)
114 {
115  Q_D(InvalidateCacheJob);
116  d->collection = collection;
117 }
118 
119 void InvalidateCacheJob::doStart()
120 {
121  Q_D(InvalidateCacheJob);
122  // resolve RID-only collections
124  connect(job, &KJob::result, this, [d](KJob *job) { d->collectionFetchResult(job); });
125 }
126 
127 #include "moc_invalidatecachejob_p.cpp"
Collection::List collections() const
Returns the list of fetched collection.
Unknown error.
Definition: job.h:115
Represents a collection of PIM items.
Definition: collection.h:76
Job that fetches collections from the Akonadi storage.
Base class for all actions in the Akonadi storage.
Definition: job.h:93
Item::List items() const
Returns the fetched items.
Only fetch the base collection.
QString i18n(const char *text, const TYPE &arg...)
const T & at(int i) const const
Helper integration between Akonadi and Qt.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Job that modifies an existing item in the Akonadi storage.
Definition: itemmodifyjob.h:96
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:84
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
int size() const const
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.