MailTransport

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

KDE's Doxygen guidelines are available online.