File indexing completed on 2024-06-02 05:20:39

0001 /*
0002     SPDX-FileCopyrightText: 2020 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "migratorbase.h"
0010 #include <Akonadi/AgentManager>
0011 
0012 #include <Akonadi/AgentInstance>
0013 
0014 #include <QMap>
0015 
0016 class GoogleResourceMigrator : public MigratorBase
0017 {
0018     Q_OBJECT
0019 public:
0020     explicit GoogleResourceMigrator();
0021 
0022     [[nodiscard]] QString displayName() const override;
0023     [[nodiscard]] QString description() const override;
0024 
0025     [[nodiscard]] bool shouldAutostart() const override;
0026 
0027 protected:
0028     void startWork() override;
0029     void migrateNextAccount();
0030 
0031 private:
0032     struct Instances {
0033         Akonadi::AgentInstance calendarResource;
0034         Akonadi::AgentInstance contactResource;
0035         bool alreadyExists = false;
0036     };
0037 
0038     template<typename T>
0039     struct ResourceValues {
0040         explicit ResourceValues() = default;
0041         template<typename U, typename V>
0042         ResourceValues(U &&calendar, V &&contacts)
0043             : calendar(calendar)
0044             , contacts(contacts)
0045         {
0046         }
0047 
0048         T calendar{};
0049         T contacts{};
0050     };
0051 
0052     [[nodiscard]] bool migrateAccount(const QString &account, const Instances &oldInstances, const Akonadi::AgentInstance &newInstance);
0053     void removeLegacyInstances(const QString &account, const Instances &instances);
0054     [[nodiscard]] QString mergeAccountNames(const ResourceValues<QString> &accountName, const Instances &oldInstances) const;
0055     [[nodiscard]] int mergeAccountIds(ResourceValues<int> accountId, const Instances &oldInstances) const;
0056 
0057     QMap<QString, Instances> mMigrations;
0058     int mMigrationCount = 0;
0059     int mMigrationsDone = 0;
0060 };