File indexing completed on 2024-05-26 05:14:12

0001 /*
0002     SPDX-FileCopyrightText: 2008 Kevin Krammer <kevin.krammer@gmx.at>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadicore_export.h"
0010 
0011 #include <QDateTime>
0012 #include <QMetaType>
0013 #include <QSet>
0014 #include <QSharedDataPointer>
0015 
0016 template<typename T>
0017 class QSet;
0018 
0019 namespace Akonadi
0020 {
0021 class ItemFetchScopePrivate;
0022 class TagFetchScope;
0023 
0024 /**
0025  * @short Specifies which parts of an item should be fetched from the Akonadi storage.
0026  *
0027  * When items are fetched from server either by using ItemFetchJob explicitly or
0028  * when it is being used internally by other classes, e.g. ItemModel, the scope
0029  * of the fetch operation can be tailored to the application's current needs.
0030  *
0031  * There are two supported ways of changing the currently active ItemFetchScope
0032  * of classes:
0033  * - in-place: modify the ItemFetchScope object the other class holds as a member
0034  * - replace: replace the other class' member with a new scope object
0035  *
0036  * Example: modifying an ItemFetchJob's scope @c in-place
0037  * @code
0038  * Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( collection );
0039  * job->fetchScope().fetchFullPayload();
0040  * job->fetchScope().fetchAttribute<MyAttribute>();
0041  * @endcode
0042  *
0043  * Example: @c replacing an ItemFetchJob's scope
0044  * @code
0045  * Akonadi::ItemFetchScope scope;
0046  * scope.fetchFullPayload();
0047  * scope.fetchAttribute<MyAttribute>();
0048  *
0049  * Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( collection );
0050  * job->setFetchScope( scope );
0051  * @endcode
0052  *
0053  * This class is implicitly shared.
0054  *
0055  * @author Kevin Krammer <kevin.krammer@gmx.at>
0056  */
0057 class AKONADICORE_EXPORT ItemFetchScope
0058 {
0059 public:
0060     /**
0061      * Describes the ancestor retrieval depth.
0062      * @since 4.4
0063      */
0064     enum AncestorRetrieval {
0065         None, ///< No ancestor retrieval at all (the default)
0066         Parent, ///< Only retrieve the immediate parent collection
0067         All ///< Retrieve all ancestors, up to Collection::root()
0068     };
0069 
0070     /**
0071      * Creates an empty item fetch scope.
0072      *
0073      * Using an empty scope will only fetch the very basic meta data of items,
0074      * e.g. local id, remote id and mime type
0075      */
0076     ItemFetchScope();
0077 
0078     /**
0079      * Creates a new item fetch scope from an @p other.
0080      */
0081     ItemFetchScope(const ItemFetchScope &other);
0082 
0083     /**
0084      * Destroys the item fetch scope.
0085      */
0086     ~ItemFetchScope();
0087 
0088     /**
0089      * Assigns the @p other to this scope and returns a reference to this scope.
0090      */
0091     ItemFetchScope &operator=(const ItemFetchScope &other);
0092 
0093     /**
0094      * Returns the payload parts that should be fetched.
0095      *
0096      * @see fetchPayloadPart()
0097      */
0098     [[nodiscard]] QSet<QByteArray> payloadParts() const;
0099 
0100     /**
0101      * Sets which payload parts shall be fetched.
0102      *
0103      * @param part The payload part identifier.
0104      *             Valid values depend on the item type.
0105      * @param fetch @c true to fetch this part, @c false otherwise.
0106      */
0107     void fetchPayloadPart(const QByteArray &part, bool fetch = true);
0108 
0109     /**
0110      * Returns whether the full payload should be fetched.
0111      *
0112      * @see fetchFullPayload()
0113      */
0114     [[nodiscard]] bool fullPayload() const;
0115 
0116     /**
0117      * Sets whether the full payload shall be fetched.
0118      * The default is @c false.
0119      *
0120      * @param fetch @c true if the full payload should be fetched, @c false otherwise.
0121      */
0122     void fetchFullPayload(bool fetch = true);
0123 
0124     /**
0125      * Returns all explicitly fetched attributes.
0126      *
0127      * Undefined if fetchAllAttributes() returns true.
0128      *
0129      * @see fetchAttribute()
0130      */
0131     [[nodiscard]] QSet<QByteArray> attributes() const;
0132 
0133     /**
0134      * Sets whether the attribute of the given @p type should be fetched.
0135      *
0136      * @param type The attribute type to fetch.
0137      * @param fetch @c true if the attribute should be fetched, @c false otherwise.
0138      */
0139     void fetchAttribute(const QByteArray &type, bool fetch = true);
0140 
0141     /**
0142      * Sets whether the attribute of the requested type should be fetched.
0143      *
0144      * @param fetch @c true if the attribute should be fetched, @c false otherwise.
0145      */
0146     template<typename T>
0147     inline void fetchAttribute(bool fetch = true)
0148     {
0149         T dummy;
0150         fetchAttribute(dummy.type(), fetch);
0151     }
0152 
0153     /**
0154      * Returns whether all available attributes should be fetched.
0155      *
0156      * @see fetchAllAttributes()
0157      */
0158     [[nodiscard]] bool allAttributes() const;
0159 
0160     /**
0161      * Sets whether all available attributes should be fetched.
0162      * The default is @c false.
0163      *
0164      * @param fetch @c true if all available attributes should be fetched, @c false otherwise.
0165      */
0166     void fetchAllAttributes(bool fetch = true);
0167 
0168     /**
0169      * Returns whether payload data should be requested from remote sources or just
0170      * from the local cache.
0171      *
0172      * @see setCacheOnly()
0173      */
0174     [[nodiscard]] bool cacheOnly() const;
0175 
0176     /**
0177      * Sets whether payload data should be requested from remote sources or just
0178      * from the local cache.
0179      *
0180      * @param cacheOnly @c true if no remote data should be requested,
0181      * @c false otherwise (the default).
0182      */
0183     void setCacheOnly(bool cacheOnly);
0184 
0185     /**
0186      * Sets whether payload will be fetched or there will be only a test performed if the
0187      * requested payload is in the cache. Calling it calls @see setCacheOnly with true automatically.
0188      * Default is fetching the data.
0189      *
0190      * @since 4.11
0191      */
0192     void setCheckForCachedPayloadPartsOnly(bool check = true);
0193 
0194     /**
0195      * Returns whether payload data should be fetched or only checked for presence in the cache.
0196      *
0197      * @see setCheckForCachedPayloadPartsOnly()
0198      *
0199      * @since 4.11
0200      */
0201     [[nodiscard]] bool checkForCachedPayloadPartsOnly() const;
0202 
0203     /**
0204      * Sets how many levels of ancestor collections should be included in the retrieval.
0205      * The default is AncestorRetrieval::None.
0206      *
0207      * @param ancestorDepth The desired ancestor retrieval depth.
0208      * @since 4.4
0209      */
0210     void setAncestorRetrieval(AncestorRetrieval ancestorDepth);
0211 
0212     /**
0213      * Returns the ancestor retrieval depth.
0214      *
0215      * @see setAncestorRetrieval()
0216      * @since 4.4
0217      */
0218     [[nodiscard]] AncestorRetrieval ancestorRetrieval() const;
0219 
0220     /**
0221      * Enables retrieval of the item modification time.
0222      * This is enabled by default for backward compatibility reasons.
0223      *
0224      * @param retrieveMtime @c true to retrieve the modification time, @c false otherwise
0225      * @since 4.6
0226      */
0227     void setFetchModificationTime(bool retrieveMtime);
0228 
0229     /**
0230      * Returns whether item modification time should be retrieved.
0231      *
0232      * @see setFetchModificationTime()
0233      * @since 4.6
0234      */
0235     [[nodiscard]] bool fetchModificationTime() const;
0236 
0237     /**
0238      * Enables retrieval of the item GID.
0239      * This is disabled by default.
0240      *
0241      * @param retrieveGID @c true to retrieve the GID, @c false otherwise
0242      * @since 4.12
0243      */
0244     void setFetchGid(bool retrieveGID);
0245 
0246     /**
0247      * Returns whether item GID should be retrieved.
0248      *
0249      * @see setFetchGid()
0250      * @since 4.12
0251      */
0252     [[nodiscard]] bool fetchGid() const;
0253 
0254     /**
0255      * Ignore retrieval errors while fetching items, and always deliver what is available.
0256      * If items have missing parts and the part can't be retrieved from the resource (i.e. because the system is offline),
0257      * the fetch job would normally just fail. By setting this flag, the errors are ignored,
0258      * and all items which could be fetched completely are returned.
0259      * Note that all items that are returned are completely fetched, and incomplete items are simply ignored.
0260      * This flag is useful for displaying everything that is available, where it is not crucial to have all items.
0261      * Never use this for things like data migration or alike.
0262      *
0263      * @since 4.10
0264      */
0265     void setIgnoreRetrievalErrors(bool enabled);
0266 
0267     /**
0268      * Returns whether retrieval errors should be ignored.
0269      *
0270      * @see setIgnoreRetrievalErrors()
0271      * @since 4.10
0272      */
0273     [[nodiscard]] bool ignoreRetrievalErrors() const;
0274 
0275     /**
0276      * Returns @c true if there is nothing to fetch.
0277      */
0278     [[nodiscard]] bool isEmpty() const;
0279 
0280     /**
0281      * Only fetch items that were added or modified after given timestamp
0282      *
0283      * When this property is set, all results are filtered, i.e. even when you
0284      * request an item with a specific ID, it will not be fetched unless it was
0285      * modified after @p changedSince timestamp.
0286      *
0287      * @param changedSince The timestamp of oldest modified item to fetch
0288      * @since 4.11
0289      */
0290     void setFetchChangedSince(const QDateTime &changedSince);
0291 
0292     /**
0293      * Returns timestamp of the oldest item to fetch.
0294      */
0295     [[nodiscard]] QDateTime fetchChangedSince() const;
0296 
0297     /**
0298      * Fetch remote identification for items.
0299      *
0300      * These include Akonadi::Item::remoteId() and Akonadi::Item::remoteRevision(). This should
0301      * be off for normal clients usually, to save memory (not to mention normal clients should
0302      * not be concerned with these information anyway). It is however crucial for resource agents.
0303      * For backward compatibility the default is @c true.
0304      *
0305      * @param retrieveRid whether or not to load remote identification.
0306      * @since 4.12
0307      */
0308     void setFetchRemoteIdentification(bool retrieveRid);
0309 
0310     /**
0311      * Returns whether item remote identification should be retrieved.
0312      *
0313      * @see setFetchRemoteIdentification()
0314      * @since 4.12
0315      */
0316     [[nodiscard]] bool fetchRemoteIdentification() const;
0317 
0318     /**
0319      * Fetch tags for items.
0320      *
0321      * The fetched tags have only the Tag::id() set and need to be fetched first to access further attributes.
0322      *
0323      * The default is @c false.
0324      *
0325      * @param fetchTags whether or not to load tags.
0326      * @since 4.13
0327      */
0328     void setFetchTags(bool fetchTags);
0329 
0330     /**
0331      * Returns whether tags should be retrieved.
0332      *
0333      * @see setFetchTags()
0334      * @since 4.13
0335      */
0336     [[nodiscard]] bool fetchTags() const;
0337 
0338     /**
0339      * Sets the tag fetch scope.
0340      *
0341      * The TagFetchScope controls how much of an tags's data is fetched
0342      * from the server.
0343      *
0344      * By default setFetchIdOnly is set to true on the tag fetch scope.
0345      *
0346      * @param fetchScope The new fetch scope for tag fetch operations.
0347      * @see fetchScope()
0348      * @since 4.15
0349      */
0350     void setTagFetchScope(const TagFetchScope &fetchScope);
0351 
0352     /**
0353      * Returns the tag fetch scope.
0354      *
0355      * Since this returns a reference it can be used to conveniently modify the
0356      * current scope in-place, i.e. by calling a method on the returned reference
0357      * without storing it in a local variable. See the TagFetchScope documentation
0358      * for an example.
0359      *
0360      * By default setFetchIdOnly is set to true on the tag fetch scope.
0361      *
0362      * @return a reference to the current tag fetch scope
0363      *
0364      * @see setFetchScope() for replacing the current tag fetch scope
0365      * @since 4.15
0366      */
0367     TagFetchScope &tagFetchScope();
0368 
0369     /**
0370      * Returns the tag fetch scope.
0371      *
0372      * By default setFetchIdOnly is set to true on the tag fetch scope.
0373      *
0374      * @return a reference to the current tag fetch scope
0375      *
0376      * @see setFetchScope() for replacing the current tag fetch scope
0377      * @since 4.15
0378      */
0379     [[nodiscard]] TagFetchScope tagFetchScope() const;
0380 
0381     /**
0382      * Returns whether to fetch list of virtual collections the item is linked to
0383      *
0384      * @param fetchVRefs whether or not to fetch virtualc references
0385      * @since 4.14
0386      */
0387     void setFetchVirtualReferences(bool fetchVRefs);
0388 
0389     /**
0390      * Returns whether virtual references should be retrieved.
0391      *
0392      * @see setFetchVirtualReferences()
0393      * @since 4.14
0394      */
0395     [[nodiscard]] bool fetchVirtualReferences() const;
0396 
0397     /**
0398      * Fetch relations for items.
0399      *
0400      * The default is @c false.
0401      *
0402      * @param fetchRelations whether or not to load relations.
0403      * @since 4.15
0404      */
0405     void setFetchRelations(bool fetchRelations);
0406 
0407     /**
0408      * Returns whether relations should be retrieved.
0409      *
0410      * @see setFetchRelations()
0411      * @since 4.15
0412      */
0413     [[nodiscard]] bool fetchRelations() const;
0414 
0415 private:
0416     /// @cond PRIVATE
0417     QSharedDataPointer<ItemFetchScopePrivate> d;
0418     /// @endcond
0419 };
0420 
0421 }
0422 
0423 Q_DECLARE_METATYPE(Akonadi::ItemFetchScope)