File indexing completed on 2024-04-28 03:53:56

0001 /*
0002     SPDX-FileCopyrightText: 2010 Grégory Oestreicher <greg@kamago.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDAV_ETAGCACHE_H
0008 #define KDAV_ETAGCACHE_H
0009 
0010 #include "kdav_export.h"
0011 
0012 #include <QObject>
0013 #include <QStringList>
0014 
0015 #include <memory>
0016 
0017 namespace KDAV
0018 {
0019 class EtagCachePrivate;
0020 
0021 /**
0022  * @class EtagCache etagcache.h <KDAV/EtagCache>
0023  *
0024  * @short A helper class to cache ETags.
0025  *
0026  * The EtagCache caches the remote ids and ETags of all items
0027  * in a given collection. This cache is needed to find
0028  * out which items have been changed in the backend and have to
0029  * be refetched on the next call of Akonadi::ResourceBase::retrieveItems()
0030  */
0031 class KDAV_EXPORT EtagCache : public QObject
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     /**
0037      * Creates a new ETag cache and populates it with the ETags
0038      * of items found in @p collection.
0039      */
0040     explicit EtagCache(QObject *parent = nullptr);
0041     ~EtagCache() override;
0042 
0043     /**
0044      * Sets the ETag for the remote ID. If the remote ID is marked as
0045      * changed (is contained in the return of changedRemoteIds), remove
0046      * it from the changed list.
0047      */
0048     void setEtag(const QString &remoteId, const QString &etag);
0049 
0050     /**
0051      * Checks if the given item is in the cache
0052      */
0053     Q_REQUIRED_RESULT bool contains(const QString &remoteId) const;
0054 
0055     /**
0056      * Check if the known ETag for the remote ID is equal to @p refEtag.
0057      */
0058     Q_REQUIRED_RESULT bool etagChanged(const QString &remoteId, const QString &refEtag) const;
0059 
0060     /**
0061      * Mark an item as changed in the backend.
0062      */
0063     void markAsChanged(const QString &remoteId);
0064 
0065     /**
0066      * Returns true if the remote ID is marked as changed (is contained in the
0067      * return of changedRemoteIds)
0068      */
0069     Q_REQUIRED_RESULT bool isOutOfDate(const QString &remoteId) const;
0070 
0071     /**
0072      * Removes the entry for item with remote ID @p remoteId.
0073      */
0074     void removeEtag(const QString &remoteId);
0075 
0076     /**
0077      * Returns the list of all items URLs.
0078      */
0079     Q_REQUIRED_RESULT QStringList urls() const;
0080 
0081     /**
0082      * Returns the list of remote ids of items that have been changed
0083      * in the backend.
0084      */
0085     Q_REQUIRED_RESULT QStringList changedRemoteIds() const;
0086 
0087 protected:
0088     /**
0089      * Sets the ETag for the remote ID.
0090      */
0091     void setEtagInternal(const QString &remoteId, const QString &etag);
0092 
0093 private:
0094     const std::unique_ptr<EtagCachePrivate> d;
0095 
0096     friend class DavItemsListJobPrivate;
0097     // @internal
0098     // Returns a map of remote Id and corresponding etag string key/value pairs.
0099     // Only used by DavItemsListJobPrivate
0100     Q_DECL_HIDDEN QMap<QString, QString> etagCacheMap() const;
0101 };
0102 }
0103 
0104 #endif