1/*
2 Copyright (c) 2011 Volker Krause <vkrause@kde.org>
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.h>
27
28using namespace Akonadi;
29
30namespace Akonadi {
31
32class InvalidateCacheJobPrivate : JobPrivate
33{
34public:
35 InvalidateCacheJobPrivate(InvalidateCacheJob *qq)
36 : JobPrivate(qq)
37 {
38 }
39 Collection collection;
40
41 void collectionFetchResult(KJob *job);
42 void itemFetchResult(KJob *job);
43 void itemStoreResult(KJob *job);
44
45 Q_DECLARE_PUBLIC(InvalidateCacheJob)
46};
47
48}
49
50void InvalidateCacheJobPrivate::collectionFetchResult(KJob *job)
51{
52 Q_Q(InvalidateCacheJob);
53 if (job->error()) {
54 return; // handled by KCompositeJob
55 }
56
57 CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>(job);
58 Q_ASSERT(fetchJob);
59 if (fetchJob->collections().size() == 1) {
60 collection = fetchJob->collections().first();
61 }
62
63 if (!collection.isValid()) {
64 q->setError(Job::Unknown);
65 q->setErrorText(i18n("Invalid collection."));
66 q->emitResult();
67 return;
68 }
69
70 ItemFetchJob *itemFetch = new ItemFetchJob(collection, q);
71 QObject::connect(itemFetch, SIGNAL(result(KJob*)), q, SLOT(itemFetchResult(KJob*)));
72}
73
74void InvalidateCacheJobPrivate::itemFetchResult(KJob *job)
75{
76 Q_Q(InvalidateCacheJob);
77 if (job->error()) {
78 return;
79 }
80 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
81 Q_ASSERT(fetchJob);
82 if (fetchJob->items().size() == 0) {
83 q->emitResult();
84 return;
85 }
86
87 ItemModifyJob *modJob = 0;
88 foreach (Item item, fetchJob->items()) { //krazy:exclude=foreach, item cannot be const
89 item.clearPayload();
90 modJob = new ItemModifyJob(item, q);
91 }
92 QObject::connect(modJob, SIGNAL(result(KJob*)), q, SLOT(itemStoreResult(KJob*)));
93}
94
95void InvalidateCacheJobPrivate::itemStoreResult(KJob *job)
96{
97 Q_Q(InvalidateCacheJob);
98 if (job->error()) {
99 return;
100 }
101 q->emitResult();
102}
103
104InvalidateCacheJob::InvalidateCacheJob(const Collection &collection, QObject *parent)
105 : Job(new InvalidateCacheJobPrivate(this), parent)
106{
107 Q_D(InvalidateCacheJob);
108 d->collection = collection;
109}
110
111void InvalidateCacheJob::doStart()
112{
113 Q_D(InvalidateCacheJob);
114 // resolve RID-only collections
115 CollectionFetchJob *job = new CollectionFetchJob(d->collection, Akonadi::CollectionFetchJob::Base, this);
116 connect(job, SIGNAL(result(KJob*)), SLOT(collectionFetchResult(KJob*)));
117}
118
119#include "moc_invalidatecachejob_p.cpp"
120