kaddressbookimportexportpluginmanager.cpp 6.48 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
2
   SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4
   SPDX-License-Identifier: GPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
5 6 7
*/

#include "kaddressbookimportexportpluginmanager.h"
Laurent Montel's avatar
Laurent Montel committed
8 9
#include "kaddressbookimportexportplugin.h"
#include "libkaddressbookexportimport_debug.h"
Laurent Montel's avatar
Laurent Montel committed
10
#include <PimCommon/PluginUtil>
Laurent Montel's avatar
Laurent Montel committed
11 12 13 14

#include <QFileInfo>
#include <QSet>
#include <KPluginLoader>
15
#include <KPluginMetaData>
Laurent Montel's avatar
Laurent Montel committed
16
#include <KPluginFactory>
Laurent Montel's avatar
Laurent Montel committed
17 18 19

using namespace KAddressBookImportExport;

Laurent Montel's avatar
Laurent Montel committed
20 21 22 23 24 25 26 27 28 29 30 31
class KAddressBookImportExportPluginManagerPrivate
{
public:
    KAddressBookImportExportPluginManagerPrivate()
        : pluginManager(new KAddressBookImportExportPluginManager)
    {
    }

    ~KAddressBookImportExportPluginManagerPrivate()
    {
        delete pluginManager;
    }
Laurent Montel's avatar
Laurent Montel committed
32

Laurent Montel's avatar
Laurent Montel committed
33
    KAddressBookImportExportPluginManager *const pluginManager;
Laurent Montel's avatar
Laurent Montel committed
34 35 36 37 38 39 40 41
};

class KAddressBookImportExportInfo
{
public:
    QString metaDataFileNameBaseName;
    QString metaDataFileName;
    PimCommon::PluginUtilData pluginData;
Laurent Montel's avatar
Laurent Montel committed
42
    KAddressBookImportExportPlugin *plugin = nullptr;
Laurent Montel's avatar
Laurent Montel committed
43
    bool isEnabled = true;
Laurent Montel's avatar
Laurent Montel committed
44 45 46 47
};

Q_GLOBAL_STATIC(KAddressBookImportExportPluginManagerPrivate, sInstance)

Laurent Montel's avatar
Laurent Montel committed
48
namespace {
Laurent Montel's avatar
Laurent Montel committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62
QString pluginVersion()
{
    return QStringLiteral("1.0");
}
}

class KAddressBookImportExport::KAddressBookImportExportPrivate
{
public:
    KAddressBookImportExportPrivate(KAddressBookImportExportPluginManager *qq)
        : q(qq)
    {
        initializePlugins();
    }
Laurent Montel's avatar
Laurent Montel committed
63

Laurent Montel's avatar
Laurent Montel committed
64 65 66 67 68 69 70 71 72 73 74
    void loadPlugin(KAddressBookImportExportInfo *item);
    QVector<KAddressBookImportExportPlugin *> pluginsList() const;
    QVector<PimCommon::PluginUtilData> pluginDataList() const;
    bool initializePlugins();

    QString configGroupName() const;
    QString configPrefixSettingKey() const;
    KAddressBookImportExportPlugin *pluginFromIdentifier(const QString &id);
private:
    QVector<KAddressBookImportExportInfo> mPluginList;
    QVector<PimCommon::PluginUtilData> mPluginDataList;
Laurent Montel's avatar
Laurent Montel committed
75
    KAddressBookImportExportPluginManager *q = nullptr;
Laurent Montel's avatar
Laurent Montel committed
76 77 78
};

bool KAddressBookImportExportPrivate::initializePlugins()
Laurent Montel's avatar
Laurent Montel committed
79
{
Laurent Montel's avatar
Laurent Montel committed
80
    const QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("kaddressbook/importexportplugin"));
Laurent Montel's avatar
Laurent Montel committed
81 82 83 84 85 86 87 88 89

    const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());

    QVectorIterator<KPluginMetaData> i(plugins);
    i.toBack();
    QSet<QString> unique;
    while (i.hasPrevious()) {
        KAddressBookImportExportInfo info;
        const KPluginMetaData data = i.previous();
Laurent Montel's avatar
Laurent Montel committed
90

Laurent Montel's avatar
Laurent Montel committed
91 92 93 94 95 96 97 98 99 100 101 102
        //1) get plugin data => name/description etc.
        info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
        //2) look at if plugin is activated
        const bool isPluginActivated = PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
        info.isEnabled = isPluginActivated;
        info.metaDataFileNameBaseName = QFileInfo(data.fileName()).baseName();
        info.metaDataFileName = data.fileName();
        if (pluginVersion() == data.version()) {
            // only load plugins once, even if found multiple times!
            if (unique.contains(info.metaDataFileNameBaseName)) {
                continue;
            }
Laurent Montel's avatar
Laurent Montel committed
103
            info.plugin = nullptr;
Laurent Montel's avatar
Laurent Montel committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            mPluginList.push_back(info);
            unique.insert(info.metaDataFileNameBaseName);
        } else {
            qCWarning(LIBKADDRESSBOOKIMPORTEXPORT_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
        }
    }
    QVector<KAddressBookImportExportInfo>::iterator end(mPluginList.end());
    for (QVector<KAddressBookImportExportInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
        loadPlugin(&(*it));
    }
    return true;
}

void KAddressBookImportExportPrivate::loadPlugin(KAddressBookImportExportInfo *item)
{
    KPluginLoader pluginLoader(item->metaDataFileName);
    if (pluginLoader.factory()) {
        item->plugin = pluginLoader.factory()->create<KAddressBookImportExportPlugin>(q, QVariantList() << item->metaDataFileNameBaseName);
        item->plugin->setIsEnabled(item->isEnabled);
        item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
        mPluginDataList.append(item->pluginData);
    }
}

QVector<KAddressBookImportExportPlugin *> KAddressBookImportExportPrivate::pluginsList() const
{
    QVector<KAddressBookImportExportPlugin *> lst;
    QVector<KAddressBookImportExportInfo>::ConstIterator end(mPluginList.constEnd());
    for (QVector<KAddressBookImportExportInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
Laurent Montel's avatar
Laurent Montel committed
133 134
        if (auto plugin = (*it).plugin) {
            lst << plugin;
Laurent Montel's avatar
Laurent Montel committed
135 136 137 138 139 140 141 142 143 144 145 146
        }
    }
    return lst;
}

QVector<PimCommon::PluginUtilData> KAddressBookImportExportPrivate::pluginDataList() const
{
    return mPluginDataList;
}

QString KAddressBookImportExportPrivate::configGroupName() const
{
Laurent Montel's avatar
Laurent Montel committed
147
    return QStringLiteral("KAddressBookPluginImportExport");
Laurent Montel's avatar
Laurent Montel committed
148 149 150 151
}

QString KAddressBookImportExportPrivate::configPrefixSettingKey() const
{
Laurent Montel's avatar
Laurent Montel committed
152
    return QStringLiteral("KAddressBookImportExportPlugin");
Laurent Montel's avatar
Laurent Montel committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166
}

KAddressBookImportExportPlugin *KAddressBookImportExportPrivate::pluginFromIdentifier(const QString &id)
{
    QVector<KAddressBookImportExportInfo>::ConstIterator end(mPluginList.constEnd());
    for (QVector<KAddressBookImportExportInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
        if ((*it).pluginData.mIdentifier == id) {
            return (*it).plugin;
        }
    }
    return {};
}

KAddressBookImportExportPluginManager::KAddressBookImportExportPluginManager(QObject *parent)
Laurent Montel's avatar
Laurent Montel committed
167 168
    : QObject(parent)
    , d(new KAddressBookImportExportPrivate(this))
Laurent Montel's avatar
Laurent Montel committed
169
{
Laurent Montel's avatar
Laurent Montel committed
170 171 172 173
}

KAddressBookImportExportPluginManager::~KAddressBookImportExportPluginManager()
{
Laurent Montel's avatar
Laurent Montel committed
174 175
    delete d;
}
Laurent Montel's avatar
Laurent Montel committed
176

Laurent Montel's avatar
Laurent Montel committed
177 178 179
KAddressBookImportExportPluginManager *KAddressBookImportExportPluginManager::self()
{
    return sInstance->pluginManager;
Laurent Montel's avatar
Laurent Montel committed
180
}
Laurent Montel's avatar
Laurent Montel committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

QVector<KAddressBookImportExportPlugin *> KAddressBookImportExportPluginManager::pluginsList() const
{
    return d->pluginsList();
}

QVector<PimCommon::PluginUtilData> KAddressBookImportExportPluginManager::pluginsDataList() const
{
    return d->pluginDataList();
}

QString KAddressBookImportExportPluginManager::configGroupName() const
{
    return d->configGroupName();
}

QString KAddressBookImportExportPluginManager::configPrefixSettingKey() const
{
    return d->configPrefixSettingKey();
}

KAddressBookImportExportPlugin *KAddressBookImportExportPluginManager::pluginFromIdentifier(const QString &id)
{
    return d->pluginFromIdentifier(id);
}