File indexing completed on 2024-11-10 04:40:31

0001 /*
0002     SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadicore_export.h"
0010 #include "item.h"
0011 
0012 #include <KJob>
0013 
0014 #include <memory>
0015 
0016 namespace Akonadi
0017 {
0018 class Collection;
0019 class ItemFetchScope;
0020 class RecursiveItemFetchJobPrivate;
0021 
0022 /**
0023  * @short Job that fetches all items of a collection recursive.
0024  *
0025  * This job takes a collection as argument and returns a list of
0026  * all items that are part of the passed collection and its child
0027  * collections. The items to fetch can be filtered by mime types and
0028  * the parts of the items that shall be fetched can
0029  * be specified via an ItemFetchScope.
0030  *
0031  * Example:
0032  *
0033  * @code
0034  *
0035  * // Assume the following Akonadi storage tree structure:
0036  * //
0037  * // Root Collection
0038  * // |
0039  * // +- Contacts
0040  * // |  |
0041  * // |  +- Private
0042  * // |  |
0043  * // |  `- Business
0044  * // |
0045  * // `- Events
0046  * //
0047  * // Collection 'Contacts' has the ID 15, then the following code
0048  * // returns all contact items from 'Contacts', 'Private' and 'Business'.
0049  *
0050  * const Akonadi::Collection contactsCollection( 15 );
0051  * const QStringList mimeTypes = QStringList() << KContacts::Addressee::mimeType();
0052  *
0053  * Akonadi::RecursiveItemFetchJob *job = new Akonadi::RecursiveItemFetchJob( contactsCollection, mimeTypes );
0054  * job->fetchScope().fetchFullPayload();
0055  * connect( job, SIGNAL(result(KJob*)), this, SLOT(fetchResult(KJob*)) );
0056  *
0057  * job->start();
0058  *
0059  * ...
0060  *
0061  * MyClass::fetchResult( KJob *job )
0062  * {
0063  *   Akonadi::RecursiveItemFetchJob *fetchJob = qobject_cast<Akonadi::RecursiveItemFetchJob*>( job );
0064  *   const Akonadi::Item::List items = fetchJob->items();
0065  *   // do something with the items
0066  * }
0067  *
0068  * @endcode
0069  *
0070  * @author Tobias Koenig <tokoe@kde.org>
0071  * @since 4.6
0072  */
0073 class AKONADICORE_EXPORT RecursiveItemFetchJob : public KJob
0074 {
0075     Q_OBJECT
0076 
0077 public:
0078     /**
0079      * Creates a new recursive item fetch job.
0080      *
0081      * @param collection The collection that shall be fetched recursive.
0082      * @param mimeTypes The list of mime types that will be used for filtering.
0083      * @param parent The parent object.
0084      */
0085     explicit RecursiveItemFetchJob(const Akonadi::Collection &collection, const QStringList &mimeTypes, QObject *parent = nullptr);
0086 
0087     /**
0088      * Destroys the recursive item fetch job.
0089      */
0090     ~RecursiveItemFetchJob() override;
0091 
0092     /**
0093      * Sets the item fetch scope.
0094      *
0095      * The ItemFetchScope controls how much of an item's data is fetched
0096      * from the server, e.g. whether to fetch the full item payload or
0097      * only meta data.
0098      *
0099      * @param fetchScope The new scope for item fetch operations.
0100      *
0101      * @see fetchScope()
0102      */
0103     void setFetchScope(const Akonadi::ItemFetchScope &fetchScope);
0104 
0105     /**
0106      * Returns the item fetch scope.
0107      *
0108      * Since this returns a reference it can be used to conveniently modify the
0109      * current scope in-place, i.e. by calling a method on the returned reference
0110      * without storing it in a local variable. See the ItemFetchScope documentation
0111      * for an example.
0112      *
0113      * @return a reference to the current item fetch scope
0114      *
0115      * @see setFetchScope() for replacing the current item fetch scope
0116      */
0117     Akonadi::ItemFetchScope &fetchScope();
0118 
0119     /**
0120      * Returns the list of fetched items.
0121      */
0122     [[nodiscard]] Akonadi::Item::List items() const;
0123 
0124     /**
0125      * Starts the recursive item fetch job.
0126      */
0127     void start() override;
0128 
0129 private:
0130     /// @cond PRIVATE
0131     friend class RecursiveItemFetchJobPrivate;
0132     std::unique_ptr<RecursiveItemFetchJobPrivate> const d;
0133     /// @endcond
0134 };
0135 
0136 }