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

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@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 "collection.h"
0011 #include "job.h"
0012 
0013 namespace Akonadi
0014 {
0015 class CollectionFetchScope;
0016 class CollectionFetchJobPrivate;
0017 
0018 /**
0019  * @short Job that fetches collections from the Akonadi storage.
0020  *
0021  * This class can be used to retrieve the complete or partial collection tree
0022  * from the Akonadi storage. This fetches collection data, not item data.
0023  *
0024  * @code
0025  *
0026  * using namespace Akonadi;
0027  *
0028  * // fetching all collections containing emails recursively, starting at the root collection
0029  * CollectionFetchJob *job = new CollectionFetchJob(Collection::root(), CollectionFetchJob::Recursive, this);
0030  * job->fetchScope().setContentMimeTypes(QStringList() << "message/rfc822");
0031  * connect(job, SIGNAL(collectionsReceived(Akonadi::Collection::List)),
0032  *         this, SLOT(myCollectionsReceived(Akonadi::Collection::List)));
0033  * connect(job, SIGNAL(result(KJob*)), this, SLOT(collectionFetchResult(KJob*)));
0034  *
0035  * @endcode
0036  *
0037  * @author Volker Krause <vkrause@kde.org>
0038  */
0039 class AKONADICORE_EXPORT CollectionFetchJob : public Job
0040 {
0041     Q_OBJECT
0042 
0043 public:
0044     /**
0045      * Describes the type of fetch depth.
0046      */
0047     enum Type {
0048         Base, ///< Only fetch the base collection.
0049         FirstLevel, ///< Only list direct sub-collections of the base collection.
0050         Recursive, ///< List all sub-collections.
0051         NonOverlappingRoots ///< List the roots of a list of fetched collections. @since 4.7
0052     };
0053 
0054     /**
0055      * Creates a new collection fetch job. If the given base collection
0056      * has a unique identifier, this is used to identify the collection in the
0057      * Akonadi server. If only a remote identifier is available the collection
0058      * is identified using that, provided that a resource search context has
0059      * been specified by calling setResource().
0060      *
0061      * @internal
0062      * For internal use only, if a remote identifier is set, the resource
0063      * search context can be set globally using ResourceSelectJob.
0064      * @endinternal
0065      *
0066      * @param collection The base collection for the listing.
0067      * @param type The type of fetch depth.
0068      * @param parent The parent object.
0069      */
0070     explicit CollectionFetchJob(const Collection &collection, Type type = FirstLevel, QObject *parent = nullptr);
0071 
0072     /**
0073      * Creates a new collection fetch job to retrieve a list of collections.
0074      * If a given collection has a unique identifier, this is used to identify
0075      * the collection in the Akonadi server. If only a remote identifier is
0076      * available the collection is identified using that, provided that a
0077      * resource search context has been specified by calling setResource().
0078      *
0079      * @internal
0080      * For internal use only, if a remote identifier is set, the resource
0081      * search context can be set globally using ResourceSelectJob.
0082      * @endinternal
0083      *
0084      * @param collections A list of collections to fetch. Must not be empty.
0085      * @param parent The parent object.
0086      */
0087     explicit CollectionFetchJob(const Collection::List &collections, QObject *parent = nullptr);
0088 
0089     /**
0090      * Creates a new collection fetch job to retrieve a list of collections.
0091      * If a given collection has a unique identifier, this is used to identify
0092      * the collection in the Akonadi server. If only a remote identifier is
0093      * available the collection is identified using that, provided that a
0094      * resource search context has been specified by calling setResource().
0095      *
0096      * @internal
0097      * For internal use only, if a remote identifier is set, the resource
0098      * search context can be set globally using ResourceSelectJob.
0099      * @endinternal
0100      *
0101      * @param collections A list of collections to fetch. Must not be empty.
0102      * @param type The type of fetch depth.
0103      * @param parent The parent object.
0104      * @todo KDE5 merge with ctor above.
0105      * @since 4.7
0106      */
0107     CollectionFetchJob(const Collection::List &collections, Type type, QObject *parent = nullptr);
0108 
0109     /**
0110      * Convenience ctor equivalent to CollectionFetchJob(const Collection::List &collections, Type type, QObject *parent = nullptr)
0111      * @since 4.8
0112      * @param collections list of collection ids
0113      * @param type fetch job type
0114      * @param parent parent object
0115      */
0116     explicit CollectionFetchJob(const QList<Collection::Id> &collections, Type type = Base, QObject *parent = nullptr);
0117 
0118     /**
0119      * Destroys the collection fetch job.
0120      */
0121     ~CollectionFetchJob() override;
0122 
0123     /**
0124      * Returns the list of fetched collection.
0125      */
0126     [[nodiscard]] Collection::List collections() const;
0127 
0128     /**
0129      * Sets the collection fetch scope.
0130      *
0131      * The CollectionFetchScope controls how much of a collection's data is
0132      * fetched from the server as well as a filter to select which collections
0133      * to fetch.
0134      *
0135      * @param fetchScope The new scope for collection fetch operations.
0136      *
0137      * @see fetchScope()
0138      * @since 4.4
0139      */
0140     void setFetchScope(const CollectionFetchScope &fetchScope);
0141 
0142     /**
0143      * Returns the collection fetch scope.
0144      *
0145      * Since this returns a reference it can be used to conveniently modify the
0146      * current scope in-place, i.e. by calling a method on the returned reference
0147      * without storing it in a local variable. See the CollectionFetchScope documentation
0148      * for an example.
0149      *
0150      * @return a reference to the current collection fetch scope
0151      *
0152      * @see setFetchScope() for replacing the current collection fetch scope
0153      * @since 4.4
0154      */
0155     [[nodiscard]] CollectionFetchScope &fetchScope();
0156 
0157 Q_SIGNALS:
0158     /**
0159      * This signal is emitted whenever the job has received collections.
0160      *
0161      * @param collections The received collections.
0162      */
0163     void collectionsReceived(const Akonadi::Collection::List &collections);
0164 
0165 protected:
0166     void doStart() override;
0167     bool doHandleResponse(qint64 tag, const Protocol::CommandPtr &response) override;
0168 
0169 protected Q_SLOTS:
0170     /// @cond PRIVATE
0171     void slotResult(KJob *job) override;
0172     /// @endcond
0173 
0174 private:
0175     Q_DECLARE_PRIVATE(CollectionFetchJob)
0176 };
0177 
0178 }