File indexing completed on 2024-12-22 04:55:36

0001 /*
0002  *  akonadiresourcemigrator.h  -  migrates KAlarm Akonadi resources
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2020-2022 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include "kalarmcalendar/kacalendar.h"
0012 
0013 #include <Akonadi/ServerManager>
0014 #include <Akonadi/Collection>
0015 
0016 class KJob;
0017 namespace Akonadi { class CollectionFetchJob; }
0018 using namespace KAlarmCal;
0019 
0020 /**
0021  * Class to migrate Akonadi or KResources alarm calendars from previous
0022  * versions of KAlarm, and to create default calendar resources if none exist.
0023  */
0024 class AkonadiResourceMigrator : public QObject
0025 {
0026     Q_OBJECT
0027 public:
0028     ~AkonadiResourceMigrator() override;
0029 
0030     /** Return the unique instance, creating it if necessary.
0031      *  Note that the instance will be destroyed once migration has completed.
0032      *  @return  Unique instance, or null if migration is not required or has
0033      *           already been done.
0034      */
0035     static AkonadiResourceMigrator* instance();
0036 
0037     /** Initiate Akonadi resource migration. */
0038     void initiateMigration();
0039 
0040     /** Delete a named Akonadi resource.
0041      *  This should be called after the resource has been migrated.
0042      */
0043     void deleteAkonadiResource(const QString& resourceName);
0044 
0045 Q_SIGNALS:
0046     /** Emitted when Akonadi resource migration has completed.
0047      *  @param migrated  true if Akonadi migration was performed.
0048      */
0049     void migrationComplete(bool migrated);
0050 
0051     /** Emitted when a file resource needs to be migrated. */
0052     void fileResource(const QString& resourceName, const QUrl& location, KAlarmCal::CalEvent::Types alarmTypes,
0053                       const QString& displayName, const QColor& backgroundColour,
0054                       KAlarmCal::CalEvent::Types enabledTypes, KAlarmCal::CalEvent::Types standardTypes,
0055                       bool readOnly);
0056 
0057     /** Emitted when a directory resource needs to be migrated. */
0058     void dirResource(const QString& resourceName, const QString& path, KAlarmCal::CalEvent::Types alarmTypes,
0059                       const QString& displayName, const QColor& backgroundColour,
0060                       KAlarmCal::CalEvent::Types enabledTypes, KAlarmCal::CalEvent::Types standardTypes,
0061                       bool readOnly);
0062 
0063 private Q_SLOTS:
0064     void checkServer(Akonadi::ServerManager::State);
0065     void collectionFetchResult(KJob*);
0066 
0067 private:
0068     explicit AkonadiResourceMigrator(QObject* parent = nullptr);
0069 
0070     void migrateResources();
0071     void doMigrateResources();
0072     void migrateCollection(const Akonadi::Collection&, bool dirType);
0073     void terminate(bool migrated);
0074 
0075     static AkonadiResourceMigrator* mInstance;
0076     struct AkResourceData
0077     {
0078         QString             resourceId;  // Akonadi resource identifier
0079         Akonadi::Collection collection;  // Akonadi collection
0080         bool                dirType;     // it's a directory resource
0081         AkResourceData() = default;
0082         AkResourceData(const QString& r, const Akonadi::Collection& c, bool dir)
0083             : resourceId(r), collection(c), dirType(dir) {}
0084     };
0085     QHash<QString, AkResourceData> mCollectionPaths;    // path, (Akonadi resource identifier, collection) pairs
0086     QHash<Akonadi::CollectionFetchJob*, bool> mFetchesPending;  // pending collection fetch jobs for existing resources, and whether directory resource
0087     bool            mAkonadiStarted {false};    // Akonadi was started by the migrator
0088     static bool     mCompleted;                 // migration has completed
0089 };
0090 
0091 // vim: et sw=4: