diff --git a/src/lib/plugin/kpluginfactory.cpp b/src/lib/plugin/kpluginfactory.cpp index f87e200..5c24a36 100644 --- a/src/lib/plugin/kpluginfactory.cpp +++ b/src/lib/plugin/kpluginfactory.cpp @@ -1,174 +1,174 @@ /* This file is part of the KDE project Copyright (C) 2007 Matthias Kretz Copyright (C) 2007 Bernhard Loos 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 "kpluginfactory.h" #include "kpluginfactory_p.h" #include #include "kcoreaddons_debug.h" Q_GLOBAL_STATIC(QObjectCleanupHandler, factorycleanup) extern int kLibraryDebugArea(); KPluginFactory::KPluginFactory() : d_ptr(new KPluginFactoryPrivate) { Q_D(KPluginFactory); d->q_ptr = this; factorycleanup()->add(this); } KPluginFactory::KPluginFactory(KPluginFactoryPrivate &d) : d_ptr(&d) { factorycleanup()->add(this); } KPluginFactory::~KPluginFactory() { delete d_ptr; } void KPluginFactory::registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction) { Q_D(KPluginFactory); Q_ASSERT(metaObject); // we allow different interfaces to be registered without keyword if (!keyword.isEmpty()) { if (d->createInstanceHash.contains(keyword)) { qCWarning(KCOREADDONS_DEBUG) << "A plugin with the keyword" << keyword << "was already registered. A keyword must be unique!"; } d->createInstanceHash.insert(keyword, KPluginFactoryPrivate::Plugin(metaObject, instanceFunction)); } else { const QList clashes(d->createInstanceHash.values(keyword)); const QMetaObject *superClass = metaObject->superClass(); if (superClass) { for (const KPluginFactoryPrivate::Plugin &plugin : clashes) { for (const QMetaObject *otherSuper = plugin.first->superClass(); otherSuper; otherSuper = otherSuper->superClass()) { if (superClass == otherSuper) { qCWarning(KCOREADDONS_DEBUG) << "Two plugins with the same interface(" << superClass->className() << ") were registered. Use keywords to identify the plugins."; } } } } for (const KPluginFactoryPrivate::Plugin &plugin : clashes) { superClass = plugin.first->superClass(); if (superClass) { for (const QMetaObject *otherSuper = metaObject->superClass(); otherSuper; otherSuper = otherSuper->superClass()) { if (superClass == otherSuper) { qCWarning(KCOREADDONS_DEBUG) << "Two plugins with the same interface(" << superClass->className() << ") were registered. Use keywords to identify the plugins."; } } } } - d->createInstanceHash.insertMulti(keyword, KPluginFactoryPrivate::Plugin(metaObject, instanceFunction)); + d->createInstanceHash.insert(keyword, KPluginFactoryPrivate::Plugin(metaObject, instanceFunction)); } } #if KCOREADDONS_BUILD_DEPRECATED_SINCE(4, 0) QObject *KPluginFactory::createObject(QObject *parent, const char *className, const QStringList &args) { Q_UNUSED(parent); Q_UNUSED(className); Q_UNUSED(args); return nullptr; } #endif #if KCOREADDONS_BUILD_DEPRECATED_SINCE(4, 0) KParts::Part *KPluginFactory::createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args) { Q_UNUSED(parent); Q_UNUSED(parentWidget); Q_UNUSED(classname); Q_UNUSED(args); return nullptr; } #endif QObject *KPluginFactory::create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword) { Q_D(KPluginFactory); QObject *obj = nullptr; #if KCOREADDONS_BUILD_DEPRECATED_SINCE(4, 0) if (keyword.isEmpty()) { const QStringList argsStringList = variantListToStringList(args); if ((obj = reinterpret_cast(createPartObject(parentWidget, parent, iface, argsStringList)))) { Q_EMIT objectCreated(obj); return obj; } if ((obj = createObject(parent, iface, argsStringList))) { Q_EMIT objectCreated(obj); return obj; } } #endif const QList candidates(d->createInstanceHash.values(keyword)); // for !keyword.isEmpty() candidates.count() is 0 or 1 for (const KPluginFactoryPrivate::Plugin &plugin : candidates) { for (const QMetaObject *current = plugin.first; current; current = current->superClass()) { if (0 == qstrcmp(iface, current->className())) { if (obj) { qCWarning(KCOREADDONS_DEBUG) << "ambiguous interface requested from a DSO containing more than one plugin"; } obj = plugin.second(parentWidget, parent, args); break; } } } if (obj) { emit objectCreated(obj); } return obj; } QStringList KPluginFactory::variantListToStringList(const QVariantList &list) { QStringList stringlist; for (const QVariant &var : list) { stringlist << var.toString(); } return stringlist; } QVariantList KPluginFactory::stringListToVariantList(const QStringList &list) { QVariantList variantlist; for (const QString &str : list) { variantlist << QVariant(str); } return variantlist; } diff --git a/src/lib/plugin/kpluginfactory_p.h b/src/lib/plugin/kpluginfactory_p.h index 4dff2e0..0f195e5 100644 --- a/src/lib/plugin/kpluginfactory_p.h +++ b/src/lib/plugin/kpluginfactory_p.h @@ -1,47 +1,47 @@ /* This file is part of the KDE project Copyright (C) 2007 Matthias Kretz Copyright (C) 2007 Bernhard Loos 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 KPLUGINFACTORY_P_H #define KPLUGINFACTORY_P_H #include "kpluginfactory.h" -#include +#include class KPluginFactoryPrivate { Q_DECLARE_PUBLIC(KPluginFactory) protected: typedef QPair Plugin; KPluginFactoryPrivate() : catalogInitialized(false) {} ~KPluginFactoryPrivate() { } - QHash createInstanceHash; + QMultiHash createInstanceHash; QString catalogName; bool catalogInitialized; KPluginFactory *q_ptr; }; #endif // KPLUGINFACTORY_P_H