kaddressbookimportexportpluginmanager.cpp 7.24 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2016-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "kaddressbookimportexportpluginmanager.h"
Laurent Montel's avatar
Laurent Montel committed
21
22
#include "kaddressbookimportexportplugin.h"
#include "libkaddressbookexportimport_debug.h"
Laurent Montel's avatar
Laurent Montel committed
23
#include <PimCommon/PluginUtil>
Laurent Montel's avatar
Laurent Montel committed
24
25
26
27

#include <QFileInfo>
#include <QSet>
#include <KPluginLoader>
28
#include <KPluginMetaData>
Laurent Montel's avatar
Laurent Montel committed
29
#include <KPluginFactory>
Laurent Montel's avatar
Laurent Montel committed
30
31
32

using namespace KAddressBookImportExport;

Laurent Montel's avatar
Laurent Montel committed
33
34
35
36
37
38
39
40
41
42
43
44
class KAddressBookImportExportPluginManagerPrivate
{
public:
    KAddressBookImportExportPluginManagerPrivate()
        : pluginManager(new KAddressBookImportExportPluginManager)
    {
    }

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

Laurent Montel's avatar
Laurent Montel committed
46
    KAddressBookImportExportPluginManager *pluginManager = nullptr;
Laurent Montel's avatar
Laurent Montel committed
47
48
49
50
51
52
};

class KAddressBookImportExportInfo
{
public:
    KAddressBookImportExportInfo()
Laurent Montel's avatar
Laurent Montel committed
53
54
        : plugin(nullptr)
        , isEnabled(true)
Laurent Montel's avatar
Laurent Montel committed
55
56
    {
    }
Laurent Montel's avatar
Laurent Montel committed
57

Laurent Montel's avatar
Laurent Montel committed
58
59
60
    QString metaDataFileNameBaseName;
    QString metaDataFileName;
    PimCommon::PluginUtilData pluginData;
Laurent Montel's avatar
Laurent Montel committed
61
    KAddressBookImportExportPlugin *plugin = nullptr;
Laurent Montel's avatar
Laurent Montel committed
62
63
64
65
66
    bool isEnabled;
};

Q_GLOBAL_STATIC(KAddressBookImportExportPluginManagerPrivate, sInstance)

Laurent Montel's avatar
Laurent Montel committed
67
namespace {
Laurent Montel's avatar
Laurent Montel committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
QString pluginVersion()
{
    return QStringLiteral("1.0");
}
}

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

Laurent Montel's avatar
Laurent Montel committed
83
84
85
86
87
88
89
90
91
92
93
    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
94
    KAddressBookImportExportPluginManager *q = nullptr;
Laurent Montel's avatar
Laurent Montel committed
95
96
97
};

bool KAddressBookImportExportPrivate::initializePlugins()
Laurent Montel's avatar
Laurent Montel committed
98
{
Laurent Montel's avatar
Laurent Montel committed
99
    const QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("kaddressbook/importexportplugin"));
Laurent Montel's avatar
Laurent Montel committed
100
101
102
103
104
105
106
107
108

    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
109

Laurent Montel's avatar
Laurent Montel committed
110
111
112
113
114
115
116
117
118
119
120
121
        //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
122
            info.plugin = nullptr;
Laurent Montel's avatar
Laurent Montel committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
            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
152
153
        if (auto plugin = (*it).plugin) {
            lst << plugin;
Laurent Montel's avatar
Laurent Montel committed
154
155
156
157
158
159
160
161
162
163
164
165
        }
    }
    return lst;
}

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

QString KAddressBookImportExportPrivate::configGroupName() const
{
Laurent Montel's avatar
Laurent Montel committed
166
    return QStringLiteral("KAddressBookPluginImportExport");
Laurent Montel's avatar
Laurent Montel committed
167
168
169
170
}

QString KAddressBookImportExportPrivate::configPrefixSettingKey() const
{
Laurent Montel's avatar
Laurent Montel committed
171
    return QStringLiteral("KAddressBookImportExportPlugin");
Laurent Montel's avatar
Laurent Montel committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
}

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
186
187
    : QObject(parent)
    , d(new KAddressBookImportExportPrivate(this))
Laurent Montel's avatar
Laurent Montel committed
188
{
Laurent Montel's avatar
Laurent Montel committed
189
190
191
192
}

KAddressBookImportExportPluginManager::~KAddressBookImportExportPluginManager()
{
Laurent Montel's avatar
Laurent Montel committed
193
194
    delete d;
}
Laurent Montel's avatar
Laurent Montel committed
195

Laurent Montel's avatar
Laurent Montel committed
196
197
198
KAddressBookImportExportPluginManager *KAddressBookImportExportPluginManager::self()
{
    return sInstance->pluginManager;
Laurent Montel's avatar
Laurent Montel committed
199
}
Laurent Montel's avatar
Laurent Montel committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

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);
}