Commit 8ecdbbb1 authored by Laurent Montel's avatar Laurent Montel 😁

Add manager

parent 615f39f0
/*
Copyright (C) 2017 Laurent Montel <montel@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "plugineditorinitmanager.h"
#include "plugineditorinit.h"
#include "messagecomposer_debug.h"
#include <QFileInfo>
#include <QSet>
#include <KPluginLoader>
#include <kpluginmetadata.h>
#include <KPluginFactory>
using namespace MessageComposer;
class PluginEditorInitManagerInstancePrivate
{
public:
PluginEditorInitManagerInstancePrivate()
: pluginManager(new PluginEditorInitManager)
{
}
~PluginEditorInitManagerInstancePrivate()
{
delete pluginManager;
}
PluginEditorInitManager *pluginManager;
};
class PluginEditorInitInfo
{
public:
PluginEditorInitInfo()
: plugin(nullptr),
isEnabled(true)
{
}
QString metaDataFileNameBaseName;
QString metaDataFileName;
PimCommon::PluginUtilData pluginData;
PluginEditorInit *plugin;
bool isEnabled;
};
Q_GLOBAL_STATIC(PluginEditorInitManagerInstancePrivate, sInstance)
namespace
{
QString pluginVersion()
{
return QStringLiteral("1.0");
}
}
class MessageComposer::PluginEditorInitManagerPrivate
{
public:
PluginEditorInitManagerPrivate(PluginEditorInitManager *qq)
: q(qq)
{
initializePlugins();
}
void loadPlugin(PluginEditorInitInfo *item);
QVector<PluginEditorInit *> pluginsList() const;
bool initializePlugins();
QVector<PluginEditorInitInfo> mPluginList;
QString configPrefixSettingKey() const;
QString configGroupName() const;
QVector<PimCommon::PluginUtilData> pluginsDataList() const;
PluginEditorInit *pluginFromIdentifier(const QString &id);
private:
QVector<PimCommon::PluginUtilData> mPluginDataList;
PluginEditorInitManager *q;
};
QString PluginEditorInitManagerPrivate::configGroupName() const
{
return QStringLiteral("KMailPluginEditorInit");
}
QString PluginEditorInitManagerPrivate::configPrefixSettingKey() const
{
return QStringLiteral("PluginEditorInit");
}
QVector<PimCommon::PluginUtilData> PluginEditorInitManagerPrivate::pluginsDataList() const
{
return mPluginDataList;
}
bool PluginEditorInitManagerPrivate::initializePlugins()
{
const QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("kmail"), [](const KPluginMetaData & md) {
return md.serviceTypes().contains(QStringLiteral("KMailEditor/PluginEditorInit"));
});
const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
QVectorIterator<KPluginMetaData> i(plugins);
i.toBack();
QSet<QString> unique;
while (i.hasPrevious()) {
PluginEditorInitInfo info;
const KPluginMetaData data = i.previous();
//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;
}
info.plugin = nullptr;
mPluginList.push_back(info);
unique.insert(info.metaDataFileNameBaseName);
} else {
qCWarning(MESSAGECOMPOSER_LOG) << "Plugin " << data.name() << " doesn't have correction plugin version. It will not be loaded.";
}
}
QVector<PluginEditorInitInfo>::iterator end(mPluginList.end());
for (QVector<PluginEditorInitInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
loadPlugin(&(*it));
}
return true;
}
void PluginEditorInitManagerPrivate::loadPlugin(PluginEditorInitInfo *item)
{
KPluginLoader pluginLoader(item->metaDataFileName);
if (pluginLoader.factory()) {
item->plugin = pluginLoader.factory()->create<PluginEditorInit>(q, QVariantList() << item->metaDataFileNameBaseName);
item->plugin->setIsEnabled(item->isEnabled);
item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
mPluginDataList.append(item->pluginData);
}
}
QVector<PluginEditorInit *> PluginEditorInitManagerPrivate::pluginsList() const
{
QVector<PluginEditorInit *> lst;
QVector<PluginEditorInitInfo>::ConstIterator end(mPluginList.constEnd());
for (QVector<PluginEditorInitInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
if ((*it).plugin) {
lst << (*it).plugin;
}
}
return lst;
}
PluginEditorInit *PluginEditorInitManagerPrivate::pluginFromIdentifier(const QString &id)
{
QVector<PluginEditorInitInfo>::ConstIterator end(mPluginList.constEnd());
for (QVector<PluginEditorInitInfo>::ConstIterator it = mPluginList.constBegin(); it != end; ++it) {
if ((*it).pluginData.mIdentifier == id) {
return (*it).plugin;
}
}
return {};
}
PluginEditorInitManager::PluginEditorInitManager(QObject *parent)
: QObject(parent),
d(new MessageComposer::PluginEditorInitManagerPrivate(this))
{
}
PluginEditorInitManager::~PluginEditorInitManager()
{
delete d;
}
PluginEditorInitManager *PluginEditorInitManager::self()
{
return sInstance->pluginManager;
}
QVector<PluginEditorInit *> PluginEditorInitManager::pluginsList() const
{
return d->pluginsList();
}
QString PluginEditorInitManager::configGroupName() const
{
return d->configGroupName();
}
QString PluginEditorInitManager::configPrefixSettingKey() const
{
return d->configPrefixSettingKey();
}
QVector<PimCommon::PluginUtilData> PluginEditorInitManager::pluginsDataList() const
{
return d->pluginsDataList();
}
PluginEditorInit *PluginEditorInitManager::pluginFromIdentifier(const QString &id)
{
return d->pluginFromIdentifier(id);
}
/*
Copyright (C) 2017 Laurent Montel <montel@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef PLUGINEDITORCHECKBEFORESENDMANAGER_H
#define PLUGINEDITORCHECKBEFORESENDMANAGER_H
#include <QObject>
#include "messagecomposer_export.h"
#include <PimCommon/PluginUtil>
namespace MessageComposer
{
class PluginEditorInitManagerPrivate;
class PluginEditorInit;
class MESSAGECOMPOSER_EXPORT PluginEditorInitManager : public QObject
{
Q_OBJECT
public:
explicit PluginEditorInitManager(QObject *parent = nullptr);
~PluginEditorInitManager();
static PluginEditorInitManager *self();
QVector<PluginEditorInit *> pluginsList() const;
QString configGroupName() const;
QString configPrefixSettingKey() const;
QVector<PimCommon::PluginUtilData> pluginsDataList() const;
PluginEditorInit *pluginFromIdentifier(const QString &id);
private:
PluginEditorInitManagerPrivate *const d;
};
}
#endif // PLUGINEDITORCHECKBEFORESENDMANAGER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment