21#include "KReportPluginManager.h"
22#include "KReportPluginManagerPrivate.h"
23#include "KReportPluginMetaData.h"
24#include "KReportUtils_p.h"
26#include "kreport_debug.h"
29#include <KPluginMetaData>
30#include <KPluginLoader>
33#include "../items/label/KReportLabelPlugin.h"
34#include "../items/check/KReportCheckBoxPlugin.h"
35#include "../items/field/KReportFieldPlugin.h"
36#include "../items/image/KReportImagePlugin.h"
37#include "../items/text/KReportTextPlugin.h"
40 : q(qq), m_parent(new
QObject), m_findPlugins(true)
48 : m_loader(nullptr), m_interface(nullptr), m_metaData(nullptr)
53 : m_loader(nullptr), m_interface(staticInterface), m_metaData(nullptr)
57KReportPluginEntry::~KReportPluginEntry()
69 kreportWarning() <<
"No such plugin";
72 if (!m_loader->load()) {
73 kreportWarning() <<
"Could not load plugin" << m_loader->fileName();
76 KPluginFactory *factory = qobject_cast<KPluginFactory*>(m_loader->instance());
78 kreportWarning() <<
"Could not create factory for plugin" << m_loader->fileName();
81 m_interface = factory->
create<KReportPluginInterface>();
83 kreportWarning() <<
"Could not create instance of plugin" << m_loader->fileName();
86 m_interface->setMetaData(m_metaData);
90void KReportPluginEntry::setBuiltIn(
bool set)
92 m_metaData->setBuiltIn(set);
95void KReportPluginEntry::setStatic(
bool set)
97 m_metaData->setStatic(set);
108 m_metaData = metaData;
110 m_interface->setMetaData(m_metaData);
114void KReportPluginEntry::setMetaData(
const QJsonObject &metaData)
116 setMetaData(
new KReportPluginMetaData(metaData));
122 setMetaData(
new KReportPluginMetaData(*m_loader));
127KReportPluginManager::Private::~Private()
132template<
class PluginClass>
138 entry->setMetaData(j);
139 entry->setBuiltIn(
true);
141 kreportWarning() <<
"Plugin" << entry->metaData()->
name() <<
"has no identifier so won't be added to manager";
145 entry->setStatic(
true);
146 m_plugins.insert(entry->metaData()->
id(), entry);
147 m_pluginsByLegacyName.insert(entry->metaData()->
value(
QLatin1String(
"X-KDE-PluginInfo-LegacyName"), entry->metaData()->
id()), entry);
150#define KREPORT_ADD_BUILTIN_PLUGIN(name) \
151 addBuiltInPlugin<name>(KREPORT_STATIC_PLUGIN_METADATA(name))
161QMap<QString, KReportPluginEntry*>* KReportPluginManager::Private::pluginsByLegacyName()
166 return &m_pluginsByLegacyName;
169void KReportPluginManager::Private::findPlugins()
171 KREPORT_ADD_BUILTIN_PLUGIN(KReportLabelPlugin);
172 KREPORT_ADD_BUILTIN_PLUGIN(KReportCheckBoxPlugin);
173 KREPORT_ADD_BUILTIN_PLUGIN(KReportFieldPlugin);
174 KREPORT_ADD_BUILTIN_PLUGIN(KReportImagePlugin);
175 KREPORT_ADD_BUILTIN_PLUGIN(KReportTextPlugin);
178 const QVector<KPluginMetaData> metaDataList = KPluginLoader::findPlugins(QStringLiteral(KREPORT_BASE_NAME_LOWER));
180 .
arg(KREPORT_STABLE_VERSION_MAJOR).
arg(KREPORT_STABLE_VERSION_MINOR);
181 for (
const KPluginMetaData &pluginMetaData : metaDataList) {
182 auto *loader =
new QPluginLoader(pluginMetaData.fileName());
183 QScopedPointer<KReportPluginEntry> entry(
new KReportPluginEntry);
184 entry->setMetaData(loader);
185 const KReportPluginMetaData *metaData = entry->metaData();
186 if (metaData->
version() != expectedVersion) {
187 kreportWarning() <<
"KReport element plugin with ID" << metaData->
id()
188 <<
"(" << metaData->
fileName() <<
")"
189 <<
"has version" << metaData->
version() <<
"but expected version is"
194 if (m_plugins.contains(metaData->
id())) {
195 kreportWarning() <<
"KReport element plugin with ID" << metaData->
id()
196 <<
"already found at"
197 << m_plugins.value(metaData->
id())->metaData()->fileName()
198 <<
"-- skipping another at" << metaData->
fileName();
201 if (!KReportPrivate::setupPrivateIconsResourceWithMessage(
202 QLatin1String(KREPORT_BASE_NAME_LOWER),
204 .arg(metaData->
id()).
arg(KReportPrivate::supportedIconTheme),
210 addEntry(entry.take());
212 m_findPlugins =
false;
215void KReportPluginManager::Private::addEntry(KReportPluginEntry *entry)
217 m_plugins.insert(entry->metaData()->
id(), entry);
218 const QString legacyName(entry->metaData()->
value(QLatin1String(
"X-KDE-PluginInfo-LegacyName"), entry->metaData()->
id()));
219 if (!legacyName.isEmpty() && entry->metaData()->
id().
startsWith(QLatin1String(
"org.kde.kreport"))) {
220 m_pluginsByLegacyName.insert(legacyName, entry);
227class KReportPluginManagerSingleton
230 KReportPluginManager object;
234KReportPluginManager::KReportPluginManager()
237 KReportPrivate::setupPrivateIconsResourceWithMessage(
238 QLatin1String(KREPORT_BASE_NAME_LOWER),
239 QString::fromLatin1(
"icons/kreport_%1.rcc").arg(KReportPrivate::supportedIconTheme), QtFatalMsg);
242KReportPluginManager::~KReportPluginManager()
247Q_GLOBAL_STATIC(KReportPluginManagerSingleton, s_self)
249KReportPluginManager* KReportPluginManager::self()
251 return &s_self->object;
254QStringList KReportPluginManager::pluginIds()
const
256 return d->plugins()->keys();
259const KReportPluginMetaData *KReportPluginManager::pluginMetaData(
const QString&
id)
const
261 KReportPluginEntry *entry = d->plugins()->value(
id);
265 return entry->metaData();
268KReportPluginInterface* KReportPluginManager::plugin(
const QString&
id)
const
270 KReportPluginEntry *entry;
272 entry = d->plugins()->value(
id);
275 entry = d->pluginsByLegacyName()->value(
id);
281 return entry->plugin();
284QList<QAction*> KReportPluginManager::createActions(
QObject *
parent)
287 const QMap<QString, KReportPluginEntry*> *plugins = d->plugins();
288 QList<QAction*> actList;
289 foreach(KReportPluginEntry* plugin, *plugins) {
290 const KReportPluginMetaData *metaData = plugin->metaData();
T * create(QObject *parent=nullptr, const QVariantList &args={})
KReportPluginEntry()
Used for dynamic plugins.
An interface for plugins delivering KReport elements.
void addBuiltInPlugin(const QJsonObject &json)
Add a built-in element plugins.
Manager class for finding and loading available plugins.
void setData(const QVariant &data)
QIcon fromTheme(const QString &name)
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QObject * parent() const const
void setObjectName(QAnyStringView name)
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const