File indexing completed on 2024-12-22 04:56:56

0001 /*
0002     SPDX-FileCopyrightText: 2009 Grégory Oestreicher <greg@kamago.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <KDAV/Enums>
0010 #include <KDAV/EtagCache>
0011 
0012 #include <memory>
0013 
0014 #include <Akonadi/FreeBusyProviderBase>
0015 #include <Akonadi/ResourceBase>
0016 
0017 class DavFreeBusyHandler;
0018 
0019 #include <QSet>
0020 
0021 namespace KDAV
0022 {
0023 class DavItem;
0024 }
0025 
0026 class DavGroupwareResource : public Akonadi::ResourceBase, public Akonadi::AgentBase::Observer, public Akonadi::FreeBusyProviderBase
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     explicit DavGroupwareResource(const QString &id);
0032     ~DavGroupwareResource() override;
0033 
0034     void collectionRemoved(const Akonadi::Collection &collection) override;
0035     void cleanup() override;
0036 
0037     QDateTime lastCacheUpdate() const override;
0038     void canHandleFreeBusy(const QString &email) const override;
0039     void retrieveFreeBusy(const QString &email, const QDateTime &start, const QDateTime &end) override;
0040 
0041 public Q_SLOTS:
0042     void configure(WId windowId) override;
0043 
0044 private Q_SLOTS:
0045     void createInitialCache();
0046     void initialRetrieveCollections();
0047     void onCreateInitialCacheReady(KJob *);
0048 
0049 protected:
0050     using ResourceBase::retrieveItems; // Suppress -Woverload-virtual
0051 
0052 protected Q_SLOTS:
0053     void retrieveCollections() override;
0054     void retrieveItems(const Akonadi::Collection &collection) override;
0055     bool retrieveItem(const Akonadi::Item &item, const QSet<QByteArray> &parts) override;
0056 
0057 protected:
0058     void itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) override;
0059     void itemChanged(const Akonadi::Item &item, const QSet<QByteArray> &parts) override;
0060     void itemRemoved(const Akonadi::Item &item) override;
0061     void collectionChanged(const Akonadi::Collection &collection) override;
0062     void doSetOnline(bool online) override;
0063 
0064 private:
0065     enum ItemFetchUpdateType {
0066         ItemUpdateNone,
0067         ItemUpdateAdd,
0068         ItemUpdateChange,
0069     };
0070 
0071     KJob *createRetrieveCollectionsJob();
0072     void onReloadConfig();
0073     void onCollectionRemovedFinished(KJob *);
0074     void onCollectionChangedFinished(KJob *job, const Akonadi::Collection &collection);
0075 
0076     void onHandlesFreeBusy(const QString &email, bool handles);
0077     void onFreeBusyRetrieved(const QString &email, const QString &freeBusy, bool success, const QString &errorText);
0078 
0079     void onRetrieveCollectionsFinished(KJob *);
0080     void onRetrieveItemsFinished(KJob *);
0081     void onMultigetFinished(KJob *);
0082     void onRetrieveItemFinished(KJob *);
0083     /**
0084      * Called when a new item has been fetched from the backend.
0085      *
0086      * @param job The job that fetched the item
0087      * @param updateType The type of update that triggered this call. The task notification sent
0088      *        sent to Akonadi will depend on this flag.
0089      */
0090     void onItemFetched(KJob *job, ItemFetchUpdateType updateType);
0091     void onItemRefreshed(KJob *job);
0092 
0093     void onItemAddedFinished(KJob *);
0094     void onItemChangePrepared(KJob *);
0095     void onItemChangedFinished(KJob *);
0096     void onItemRemovalPrepared(KJob *);
0097     void onItemRemovedFinished(KJob *);
0098 
0099     void onCollectionDiscovered(KDAV::Protocol protocol, const QString &collectionUrl, const QString &configuredUrl);
0100     void onConflictModifyJobFinished(KJob *job);
0101     void onDeletedItemRecreated(KJob *job);
0102 
0103     void doItemChange(const Akonadi::Item &item, const Akonadi::Item::List &dependentItems = Akonadi::Item::List());
0104     void doItemRemoval(const Akonadi::Item &item);
0105     void handleConflict(const Akonadi::Item &localItem,
0106                         const Akonadi::Item::List &localDependentItems,
0107                         const KDAV::DavItem &remoteItem,
0108                         bool isLocalRemoval,
0109                         int responseCode);
0110 
0111     bool configurationIsValid();
0112     void retryAfterFailure(const QString &errorMessage);
0113 
0114     /**
0115      * Collections which only support one mime type have an icon indicating what they support.
0116      */
0117     static void setCollectionIcon(Akonadi::Collection &collection);
0118 
0119     Akonadi::Collection mDavCollectionRoot;
0120     QMap<QString, std::shared_ptr<KDAV::EtagCache>> mEtagCaches;
0121     QMap<QString, QString> mCTagCache;
0122     DavFreeBusyHandler *const mFreeBusyHandler;
0123     bool mSyncErrorNotified = false;
0124 };