Akonadi Mime

filteractionjob.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "filteractionjob_p.h"
8
9#include <Akonadi/Collection>
10#include <Akonadi/ItemFetchJob>
11#include <Akonadi/ItemFetchScope>
12
13#include "akonadi_mime_debug.h"
14
15using namespace Akonadi;
16
17class Akonadi::FilterActionJobPrivate
18{
19public:
20 explicit FilterActionJobPrivate(FilterActionJob *qq)
21 : q(qq)
22 {
23 }
24
25 ~FilterActionJobPrivate()
26 {
27 delete functor;
28 }
29
30 FilterActionJob *const q;
31 Collection collection;
32 Item::List items;
33 FilterAction *functor = nullptr;
34 ItemFetchScope fetchScope;
35
36 // Q_SLOTS:
37 void fetchResult(KJob *job);
38
39 void traverseItems();
40};
41
42void FilterActionJobPrivate::fetchResult(KJob *job)
43{
44 if (job->error()) {
45 // KCompositeJob takes care of errors.
46 return;
47 }
48
49 auto fjob = qobject_cast<ItemFetchJob *>(job);
50 Q_ASSERT(fjob);
51 Q_ASSERT(items.isEmpty());
52 items = fjob->items();
53 traverseItems();
54}
55
56void FilterActionJobPrivate::traverseItems()
57{
58 Q_ASSERT(functor);
59 qCDebug(AKONADIMIME_LOG) << "Traversing" << items.count() << "items.";
60 for (const Item &item : std::as_const(items)) {
61 if (functor->itemAccepted(item)) {
62 functor->itemAction(item, q);
63 qCDebug(AKONADIMIME_LOG) << "Added subjob for item" << item.id();
64 }
65 }
66 if (q->subjobs().isEmpty()) {
67 qCDebug(AKONADIMIME_LOG) << "No subjobs; I am done";
68 } else {
69 qCDebug(AKONADIMIME_LOG) << "Have subjobs; Done when last of them is";
70 }
71 q->commit();
72}
73
74FilterAction::~FilterAction() = default;
75
76FilterActionJob::FilterActionJob(const Item &item, FilterAction *functor, QObject *parent)
77 : TransactionSequence(parent)
78 , d(new FilterActionJobPrivate(this))
79{
80 d->functor = functor;
81 d->items << item;
82}
83
84FilterActionJob::FilterActionJob(const Item::List &items, FilterAction *functor, QObject *parent)
85 : TransactionSequence(parent)
86 , d(new FilterActionJobPrivate(this))
87{
88 d->functor = functor;
89 d->items = items;
90}
91
92FilterActionJob::FilterActionJob(const Collection &collection, FilterAction *functor, QObject *parent)
93 : TransactionSequence(parent)
94 , d(new FilterActionJobPrivate(this))
95{
96 d->functor = functor;
97 Q_ASSERT(collection.isValid());
98 d->collection = collection;
99}
100
101FilterActionJob::~FilterActionJob() = default;
102
103void FilterActionJob::doStart()
104{
105 if (d->collection.isValid()) {
106 qCDebug(AKONADIMIME_LOG) << "Fetching collection" << d->collection.id();
107 auto fjob = new ItemFetchJob(d->collection, this);
108 Q_ASSERT(d->functor);
109 d->fetchScope = d->functor->fetchScope();
110 fjob->setFetchScope(d->fetchScope);
111 connect(fjob, &ItemFetchJob::result, this, [this](KJob *job) {
112 d->fetchResult(job);
113 });
114 } else {
115 d->traverseItems();
116 }
117}
118
119#include "moc_filteractionjob_p.cpp"
bool isValid() const
int error() const
qsizetype count() const const
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:32:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.