File indexing completed on 2024-11-17 04:45:00

0001 /*
0002     SPDX-FileCopyrightText: 2020 Igor Poboiko <igor.poboiko@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <KGAPI/Types>
0010 
0011 #include <QObject>
0012 #include <QSharedPointer>
0013 
0014 #include <Akonadi/Collection>
0015 #include <Akonadi/Item>
0016 
0017 #include <KLocalizedString>
0018 
0019 #include "googleresourcestateinterface.h"
0020 
0021 #define ITEM_PROPERTY "_AkonadiItem"
0022 #define ITEMS_PROPERTY "_AkonadiItems"
0023 #define COLLECTION_PROPERTY "_AkonadiCollection"
0024 
0025 namespace KGAPI2
0026 {
0027 class Job;
0028 }
0029 
0030 class GoogleSettings;
0031 
0032 class GenericHandler : public QObject
0033 {
0034     Q_OBJECT
0035 public:
0036     using Ptr = std::unique_ptr<GenericHandler>;
0037 
0038     GenericHandler(GoogleResourceStateInterface *iface, GoogleSettings *settings);
0039     ~GenericHandler() override;
0040 
0041     virtual QString mimeType() = 0;
0042 
0043     virtual void retrieveCollections(const Akonadi::Collection &rootCollection) = 0;
0044     virtual void retrieveItems(const Akonadi::Collection &collection) = 0;
0045 
0046     virtual void itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) = 0;
0047     virtual void itemChanged(const Akonadi::Item &item, const QSet<QByteArray> &partIdentifiers) = 0;
0048     virtual void itemsRemoved(const Akonadi::Item::List &items) = 0;
0049     virtual void
0050     itemsMoved(const Akonadi::Item::List &items, const Akonadi::Collection &collectionSource, const Akonadi::Collection &collectionDestination) = 0;
0051     virtual void itemsLinked(const Akonadi::Item::List &items, const Akonadi::Collection &collection);
0052     virtual void itemsUnlinked(const Akonadi::Item::List &items, const Akonadi::Collection &collection);
0053 
0054     virtual void collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent) = 0;
0055     virtual void collectionChanged(const Akonadi::Collection &collection) = 0;
0056     virtual void collectionRemoved(const Akonadi::Collection &collection) = 0;
0057 
0058     /*
0059      * Helper function for various handlers
0060      */
0061     template<typename T>
0062     bool canPerformTask(const Akonadi::Item &item)
0063     {
0064         if (item.isValid() && (!item.hasPayload<T>() || item.mimeType() != mimeType())) {
0065             m_iface->cancelTask(i18n("Invalid item."));
0066             return false;
0067         }
0068         return m_iface->canPerformTask();
0069     }
0070 
0071     template<typename T>
0072     bool canPerformTask(const Akonadi::Item::List &items)
0073     {
0074         if (std::any_of(items.cbegin(), items.cend(), [this](const Akonadi::Item &item) {
0075                 return item.isValid() && (!item.hasPayload<T>() || item.mimeType() != mimeType());
0076             })) {
0077             m_iface->cancelTask(i18n("Invalid item."));
0078             return false;
0079         }
0080         return m_iface->canPerformTask();
0081     }
0082 
0083     virtual bool canPerformTask(const Akonadi::Item &item) = 0;
0084     virtual bool canPerformTask(const Akonadi::Item::List &items) = 0;
0085 protected Q_SLOTS:
0086     void slotGenericJobFinished(KGAPI2::Job *job);
0087 
0088 protected:
0089     void emitReadyStatus();
0090 
0091     GoogleResourceStateInterface *const m_iface;
0092     GoogleSettings *const m_settings;
0093 };