Akonadi

recursiveitemfetchjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Tobias Koenig <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "recursiveitemfetchjob.h"
8 #include "collectionfetchjob.h"
9 #include "collectionfetchscope.h"
10 #include "itemfetchjob.h"
11 #include "itemfetchscope.h"
12 
13 #include <QStringList>
14 
15 using namespace Akonadi;
16 
17 class Akonadi::RecursiveItemFetchJobPrivate
18 {
19 public:
20  RecursiveItemFetchJobPrivate(const Collection &collection, const QStringList &mimeTypes, RecursiveItemFetchJob *parent)
21  : mParent(parent)
22  , mCollection(collection)
23  , mMimeTypes(mimeTypes)
24  {
25  }
26 
27  void collectionFetchResult(KJob *job)
28  {
29  if (job->error()) {
30  mParent->emitResult();
31  return;
32  }
33 
34  const CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>(job);
35 
36  Collection::List collections = fetchJob->collections();
37  collections.prepend(mCollection);
38 
39  for (const Collection &collection : std::as_const(collections)) {
40  auto itemFetchJob = new ItemFetchJob(collection, mParent);
41  itemFetchJob->setFetchScope(mFetchScope);
42  mParent->connect(itemFetchJob, &KJob::result, mParent, [this](KJob *job) {
43  itemFetchResult(job);
44  });
45 
46  mFetchCount++;
47  }
48  }
49 
50  void itemFetchResult(KJob *job)
51  {
52  if (!job->error()) {
53  const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
54 
55  if (!mMimeTypes.isEmpty()) {
56  const Akonadi::Item::List lstItems = fetchJob->items();
57  for (const Item &item : lstItems) {
58  if (mMimeTypes.contains(item.mimeType())) {
59  mItems << item;
60  }
61  }
62  } else {
63  mItems << fetchJob->items();
64  }
65  }
66 
67  mFetchCount--;
68 
69  if (mFetchCount == 0) {
70  mParent->emitResult();
71  }
72  }
73 
74  RecursiveItemFetchJob *const mParent;
75  const Collection mCollection;
76  Item::List mItems;
77  ItemFetchScope mFetchScope;
78  const QStringList mMimeTypes;
79 
80  int mFetchCount = 0;
81 };
82 
83 RecursiveItemFetchJob::RecursiveItemFetchJob(const Collection &collection, const QStringList &mimeTypes, QObject *parent)
84  : KJob(parent)
85  , d(new RecursiveItemFetchJobPrivate(collection, mimeTypes, this))
86 {
87 }
88 
90 
92 {
93  d->mFetchScope = fetchScope;
94 }
95 
97 {
98  return d->mFetchScope;
99 }
100 
102 {
103  auto job = new CollectionFetchJob(d->mCollection, CollectionFetchJob::Recursive, this);
104 
105  if (!d->mMimeTypes.isEmpty()) {
106  job->fetchScope().setContentMimeTypes(d->mMimeTypes);
107  }
108 
109  connect(job, &CollectionFetchJob::result, this, [this](KJob *job) {
110  d->collectionFetchResult(job);
111  });
112 }
113 
115 {
116  return d->mItems;
117 }
118 
119 #include "moc_recursiveitemfetchjob.cpp"
void prepend(T &&value)
void result(KJob *job)
void start() override
Starts the recursive item fetch 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)
QVector< Item > List
Describes a list of items.
Definition: item.h:115
Job that fetches collections from the Akonadi storage.
~RecursiveItemFetchJob() override
Destroys the recursive item fetch job.
Collection::List collections() const
Returns the list of fetched collection.
Item::List items() const
Returns the fetched items.
void setFetchScope(const Akonadi::ItemFetchScope &fetchScope)
Sets the item fetch scope.
RecursiveItemFetchJob(const Akonadi::Collection &collection, const QStringList &mimeTypes, QObject *parent=nullptr)
Creates a new recursive item fetch job.
QStringList mimeTypes(Mode mode=Writing)
Job that fetches all items of a collection recursive.
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:69
@ Recursive
List all sub-collections.
int error() const
Akonadi::ItemFetchScope & fetchScope()
Returns the item fetch scope.
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
Akonadi::Item::List items() const
Returns the list of fetched items.
Specifies which parts of an item should be fetched from the Akonadi storage.
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:00:32 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.