File indexing completed on 2024-12-22 04:57:14

0001 /*
0002     SPDX-FileCopyrightText: 2006 Till Adam <adam@kde.org>
0003     SPDX-FileCopyrightText: 2009 David Jarvie <djarvie@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "settings.h"
0011 #include "singlefileresource.h"
0012 
0013 #include <KCalendarCore/FileStorage>
0014 #include <KCalendarCore/MemoryCalendar>
0015 
0016 class ICalResourceBase : public Akonadi::SingleFileResource<SETTINGS_NAMESPACE::Settings>
0017 {
0018     Q_OBJECT
0019 
0020 public:
0021     explicit ICalResourceBase(const QString &id);
0022     ~ICalResourceBase() override;
0023 
0024 protected:
0025     using ResourceBase::retrieveItems; // Suppress -Woverload-virtual
0026 
0027 protected Q_SLOTS:
0028     bool retrieveItem(const Akonadi::Item &item, const QSet<QByteArray> &parts) override;
0029     bool retrieveItems(const Akonadi::Item::List &items, const QSet<QByteArray> &parts) override;
0030     void retrieveItems(const Akonadi::Collection &col) override;
0031 
0032 protected:
0033     enum CheckType {
0034         CheckForAdded,
0035         CheckForChanged,
0036     };
0037 
0038     void initialise(const QStringList &mimeTypes, const QString &icon);
0039     bool readFromFile(const QString &fileName) override;
0040     bool writeToFile(const QString &fileName) override;
0041 
0042     void aboutToQuit() override;
0043 
0044     /**
0045      * Add the requested payload parts and call itemsRetrieved() when done.
0046      * It is guaranteed that all items in the list belong to the same Collection.
0047      * Retrieve an incidence from the calendar, and set it into a new item's payload.
0048      * Retrieval of items should be signalled by calling @p itemsRetrieved().
0049      * @param items the incidence ID to retrieve is provided by @c item.remoteId() for each item
0050      * @return true if all items are retrieved, false if not.
0051      */
0052     virtual bool doRetrieveItems(const Akonadi::Item::List &items, const QSet<QByteArray> &parts) = 0;
0053 
0054     /**
0055      * Retrieve all incidences from the calendar, and set each into a new item's payload.
0056      * Retrieval of the items should be signalled by calling @p itemsRetrieved().
0057      */
0058     virtual void doRetrieveItems(const Akonadi::Collection &col) = 0;
0059 
0060     /**
0061      * To be called at the start of derived class implementations of itemAdded()
0062      * or itemChanged() to verify that required conditions are true.
0063      * @param type the type of change to perform the checks for.
0064      * @return true if all checks are successful, and processing can continue;
0065      *         false if a check failed, in which case itemAdded() or itemChanged()
0066      *               should stop processing.
0067      */
0068     template<typename PayloadPtr>
0069     bool checkItemAddedChanged(const Akonadi::Item &item, CheckType type);
0070 
0071     void itemRemoved(const Akonadi::Item &item) override;
0072 
0073     /** Return the local calendar. */
0074     KCalendarCore::MemoryCalendar::Ptr calendar() const;
0075 
0076     /** Return the calendar file storage. */
0077     KCalendarCore::FileStorage::Ptr fileStorage() const;
0078 
0079 private:
0080     KCalendarCore::MemoryCalendar::Ptr mCalendar;
0081     KCalendarCore::FileStorage::Ptr mFileStorage;
0082 };
0083 
0084 template<typename PayloadPtr>
0085 bool ICalResourceBase::checkItemAddedChanged(const Akonadi::Item &item, CheckType type)
0086 {
0087     if (!mCalendar) {
0088         cancelTask(i18n("Calendar not loaded."));
0089         return false;
0090     }
0091     if (!item.hasPayload<PayloadPtr>()) {
0092         const QString msg =
0093             (type == CheckForAdded) ? i18n("Unable to retrieve added item %1.", item.id()) : i18n("Unable to retrieve modified item %1.", item.id());
0094         cancelTask(msg);
0095         return false;
0096     }
0097     return true;
0098 }