7#include "genericpluginmanager.h"
8#include "genericplugin.h"
9#include "pimcommon_debug.h"
11#include <KPluginFactory>
17class GenericPluginInfo
20 GenericPluginInfo() =
default;
23 QString metaDataFileNameBaseName;
24 QString metaDataFileName;
25 PimCommon::PluginUtilData pluginData;
26 PimCommon::GenericPlugin *plugin =
nullptr;
27 bool isEnabled =
true;
34 return QStringLiteral(
"1.0");
38class PimCommon::GenericPluginManagerPrivate
41 explicit GenericPluginManagerPrivate(GenericPluginManager *qq)
46 void loadPlugin(GenericPluginInfo *item);
47 [[nodiscard]] QList<GenericPlugin *> pluginsList()
const;
48 bool initializePlugins();
49 QString pluginDirectory;
51 QList<GenericPluginInfo> mPluginList;
53 [[nodiscard]] QList<PluginUtilData> pluginsDataList()
const;
54 [[nodiscard]] QString configGroupName()
const;
55 [[nodiscard]] QString configPrefixSettingKey()
const;
56 GenericPlugin *pluginFromIdentifier(
const QString &
id);
59 QList<PluginUtilData> mPluginDataList;
60 GenericPluginManager *
const q;
63QString GenericPluginManagerPrivate::configGroupName()
const
65 return QStringLiteral(
"GenericPlugin-%1").
arg(pluginName);
68QString GenericPluginManagerPrivate::configPrefixSettingKey()
const
70 return QStringLiteral(
"%1Plugin").
arg(pluginName);
73bool GenericPluginManagerPrivate::initializePlugins()
75 if (!mPluginList.isEmpty()) {
79 if (pluginDirectory.isEmpty() || pluginName.isEmpty()) {
84 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(configGroupName(), configPrefixSettingKey());
85 QListIterator<KPluginMetaData> i(plugins);
87 while (i.hasPrevious()) {
88 GenericPluginInfo info;
89 const KPluginMetaData data = i.previous();
92 info.pluginData = PimCommon::PluginUtil::createPluginMetaData(data);
94 const bool isPluginActivated =
95 PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, info.pluginData.mEnableByDefault, info.pluginData.mIdentifier);
96 info.isEnabled = isPluginActivated;
97 info.metaDataFileNameBaseName = QFileInfo(data.
fileName()).baseName();
98 info.metaDataFileName = data.
fileName();
101 if (pluginVersion() == data.
version()) {
102 info.plugin =
nullptr;
103 mPluginList.push_back(info);
105 qCWarning(PIMCOMMON_LOG) <<
"Plugin " << data.
name() <<
" doesn't have correction plugin version. It will not be loaded.";
108 QList<GenericPluginInfo>::iterator
end(mPluginList.end());
109 for (QList<GenericPluginInfo>::iterator it = mPluginList.begin(); it != end; ++it) {
115QList<PluginUtilData> GenericPluginManagerPrivate::pluginsDataList()
const
117 return mPluginDataList;
120QList<GenericPlugin *> GenericPluginManagerPrivate::pluginsList()
const
122 QList<PimCommon::GenericPlugin *> lst;
125 if (
auto plugin = (*it).plugin) {
132void GenericPluginManagerPrivate::loadPlugin(GenericPluginInfo *item)
134 if (
auto plugin = KPluginFactory::instantiatePlugin<PimCommon::GenericPlugin>(item->data, q, QVariantList() << item->metaDataFileNameBaseName).plugin) {
135 item->plugin = plugin;
136 item->plugin->setIsEnabled(item->isEnabled);
137 item->pluginData.mHasConfigureDialog = item->plugin->hasConfigureDialog();
138 mPluginDataList.append(item->pluginData);
142GenericPlugin *GenericPluginManagerPrivate::pluginFromIdentifier(
const QString &
id)
146 if ((*it).pluginData.mIdentifier ==
id) {
153GenericPluginManager::GenericPluginManager(QObject *parent)
155 , d(new GenericPluginManagerPrivate(this))
159GenericPluginManager::~GenericPluginManager() =
default;
161bool GenericPluginManager::initializePlugins()
163 return d->initializePlugins();
166void GenericPluginManager::setPluginDirectory(
const QString &directory)
168 d->pluginDirectory = directory;
171QString GenericPluginManager::pluginDirectory()
const
173 return d->pluginDirectory;
176void GenericPluginManager::setPluginName(
const QString &pluginName)
178 d->pluginName = pluginName;
181QString GenericPluginManager::pluginName()
const
183 return d->pluginName;
186QList<GenericPlugin *> GenericPluginManager::pluginsList()
const
188 return d->pluginsList();
191QList<PluginUtilData> GenericPluginManager::pluginsDataList()
const
193 return d->pluginsDataList();
196QString GenericPluginManager::configGroupName()
const
198 return d->configGroupName();
201QString GenericPluginManager::configPrefixSettingKey()
const
203 return d->configPrefixSettingKey();
206GenericPlugin *GenericPluginManager::pluginFromIdentifier(
const QString &
id)
208 return d->pluginFromIdentifier(
id);
211#include "moc_genericpluginmanager.cpp"
const QList< QKeySequence > & end()
QString arg(Args &&... args) const const