Akonadi

recursiveitemfetchjob.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
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
15using namespace Akonadi;
16
17class Akonadi::RecursiveItemFetchJobPrivate
18{
19public:
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
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"
Job that fetches collections from the Akonadi storage.
@ Recursive
List all sub-collections.
Collection::List collections() const
Returns the list of fetched collection.
Represents a collection of PIM items.
Definition collection.h:62
Job that fetches items from the Akonadi storage.
Item::List items() const
Returns the fetched items.
Specifies which parts of an item should be fetched from the Akonadi storage.
Job that fetches all items of a collection recursive.
Akonadi::ItemFetchScope & fetchScope()
Returns the item fetch scope.
void setFetchScope(const Akonadi::ItemFetchScope &fetchScope)
Sets the item fetch scope.
~RecursiveItemFetchJob() override
Destroys the recursive item fetch job.
void start() override
Starts the recursive item fetch job.
Akonadi::Item::List items() const
Returns the list of fetched items.
RecursiveItemFetchJob(const Akonadi::Collection &collection, const QStringList &mimeTypes, QObject *parent=nullptr)
Creates a new recursive item fetch job.
void emitResult()
int error() const
void result(KJob *job)
Helper integration between Akonadi and Qt.
A glue between Qt and the standard library.
bool isEmpty() const const
void prepend(parameter_type value)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 11:56:44 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.