Akonadi

recursiveitemfetchjob.cpp
1 /*
2  Copyright (c) 2009 Tobias Koenig <[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 "recursiveitemfetchjob.h"
21 #include "collectionfetchjob.h"
22 #include "collectionfetchscope.h"
23 #include "itemfetchjob.h"
24 #include "itemfetchscope.h"
25 
26 
27 #include <QStringList>
28 
29 using namespace Akonadi;
30 
31 class Q_DECL_HIDDEN RecursiveItemFetchJob::Private
32 {
33 public:
34  Private(const Collection &collection, const QStringList &mimeTypes, RecursiveItemFetchJob *parent)
35  : mParent(parent)
36  , mCollection(collection)
37  , mMimeTypes(mimeTypes)
38  , mFetchCount(0)
39  {
40  }
41 
42  void collectionFetchResult(KJob *job)
43  {
44  if (job->error()) {
45  mParent->emitResult();
46  return;
47  }
48 
49  const CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>(job);
50 
51  Collection::List collections = fetchJob->collections();
52  collections.prepend(mCollection);
53 
54  for (const Collection &collection : qAsConst(collections)) {
55  ItemFetchJob *itemFetchJob = new ItemFetchJob(collection, mParent);
56  itemFetchJob->setFetchScope(mFetchScope);
57  mParent->connect(itemFetchJob, SIGNAL(result(KJob*)),
58  mParent, SLOT(itemFetchResult(KJob*)));
59 
60  mFetchCount++;
61  }
62  }
63 
64  void itemFetchResult(KJob *job)
65  {
66  if (!job->error()) {
67  const ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
68 
69  if (!mMimeTypes.isEmpty()) {
70  const Akonadi::Item::List lstItems = fetchJob->items();
71  for (const Item &item : lstItems) {
72  if (mMimeTypes.contains(item.mimeType())) {
73  mItems << item;
74  }
75  }
76  } else {
77  mItems << fetchJob->items();
78  }
79  }
80 
81  mFetchCount--;
82 
83  if (mFetchCount == 0) {
84  mParent->emitResult();
85  }
86  }
87 
88  RecursiveItemFetchJob *mParent = nullptr;
89  Collection mCollection;
90  Item::List mItems;
91  ItemFetchScope mFetchScope;
92  QStringList mMimeTypes;
93 
94  int mFetchCount;
95 };
96 
97 RecursiveItemFetchJob::RecursiveItemFetchJob(const Collection &collection, const QStringList &mimeTypes, QObject *parent)
98  : KJob(parent)
99  , d(new Private(collection, mimeTypes, this))
100 {
101 }
102 
104 {
105  delete d;
106 }
107 
109 {
110  d->mFetchScope = fetchScope;
111 }
112 
114 {
115  return d->mFetchScope;
116 }
117 
119 {
120  CollectionFetchJob *job = new CollectionFetchJob(d->mCollection, CollectionFetchJob::Recursive, this);
121 
122  if (!d->mMimeTypes.isEmpty()) {
123  job->fetchScope().setContentMimeTypes(d->mMimeTypes);
124  }
125 
126  connect(job, &CollectionFetchJob::result, this, [this](KJob *job) { d->collectionFetchResult(job); });
127 }
128 
129 Akonadi::Item::List RecursiveItemFetchJob::items() const
130 {
131  return d->mItems;
132 }
133 
134 #include "moc_recursiveitemfetchjob.cpp"
Collection::List collections() const
Returns the list of fetched collection.
CollectionFetchScope & fetchScope()
Returns the collection fetch scope.
Represents a collection of PIM items.
Definition: collection.h:76
Job that fetches collections from the Akonadi storage.
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.
Item::List items() const
Returns the fetched items.
void setContentMimeTypes(const QStringList &mimeTypes)
Sets a content mimetypes filter, that is only collections that contain at least one of the given mime...
Akonadi::ItemFetchScope & fetchScope()
Returns the item fetch scope.
void setFetchScope(const ItemFetchScope &fetchScope)
Sets the item fetch scope.
Specifies which parts of an item should be fetched from the Akonadi storage.
void start() override
Starts the recursive item fetch job.
Helper integration between Akonadi and Qt.
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:84
void prepend(T &&value)
Job that fetches all items of a collection recursive.
~RecursiveItemFetchJob() override
Destroys the recursive item fetch job.
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Akonadi::Item::List items() const
Returns the list of fetched items.
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.