diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 2688463..ea65f4c 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -1,369 +1,369 @@ # Configure checks for the caching subdir include(CheckIncludeFiles) check_include_files("sys/types.h;sys/mman.h" HAVE_SYS_MMAN_H) configure_file(caching/config-caching.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-caching.h) include(CheckSymbolExists) check_symbol_exists("getgrouplist" "grp.h" HAVE_GETGROUPLIST) configure_file(util/config-getgrouplist.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-getgrouplist.h) set (KDE4_DEFAULT_HOME ".kde${_KDE4_DEFAULT_HOME_POSTFIX}" CACHE STRING "The default KDE home directory" ) configure_file(util/config-kde4home.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kde4home.h) set (ACCOUNTS_SERVICE_ICON_DIR "/var/lib/AccountsService/icons" CACHE STRING "Accounts Services icon storage directory") configure_file(util/config-accountsservice.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-accountsservice.h) ecm_create_qm_loader(kcoreaddons_QM_LOADER kcoreaddons5_qt) set(kcoreaddons_OPTIONAL_SRCS ) set(kcoreaddons_OPTIONAL_LIBS ) if (FAM_FOUND) include_directories(${FAM_INCLUDE_DIR}) set(kcoreaddons_OPTIONAL_LIBS ${kcoreaddons_OPTIONAL_LIBS} ${FAM_LIBRARIES}) endif () if (Inotify_FOUND) include_directories(${Inotify_INCLUDE_DIRS}) set(kcoreaddons_OPTIONAL_LIBS ${kcoreaddons_OPTIONAL_LIBS} ${Inotify_LIBRARIES}) endif () if(NOT WIN32) set(kcoreaddons_OPTIONAL_SRCS caching/kshareddatacache.cpp) set(kcoreaddons_OPTIONAL_LIBS ${kcoreaddons_OPTIONAL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) set_source_files_properties(caching/kshareddatacache.cpp PROPERTIES COMPILE_FLAGS -fexceptions) else() set(kcoreaddons_OPTIONAL_SRCS caching/kshareddatacache_win.cpp ) endif() if (WIN32) set(kcoreaddons_OPTIONAL_SRCS ${kcoreaddons_OPTIONAL_SRCS} text/kmacroexpander_win.cpp util/klistopenfilesjob_win.cpp util/kprocesslist_win.cpp util/kshell_win.cpp util/kuser_win.cpp ) endif () if (UNIX) set(kcoreaddons_OPTIONAL_SRCS ${kcoreaddons_OPTIONAL_SRCS} text/kmacroexpander_unix.cpp util/klistopenfilesjob_unix.cpp util/kuser_unix.cpp util/kshell_unix.cpp ) if (HAVE_PROCSTAT) set(kcoreaddons_OPTIONAL_SRCS ${kcoreaddons_OPTIONAL_SRCS} util/kprocesslist_unix_procstat.cpp ) else () set(kcoreaddons_OPTIONAL_SRCS ${kcoreaddons_OPTIONAL_SRCS} util/kprocesslist_unix.cpp ) endif () endif () set(libkcoreaddons_SRCS kaboutdata.cpp kcoreaddons.cpp io/kautosavefile.cpp io/kdirwatch.cpp io/kfilesystemtype.cpp io/kmessage.cpp io/kprocess.cpp io/kbackup.cpp io/kurlmimedata.cpp io/kfileutils.cpp jobs/kcompositejob.cpp jobs/kjob.cpp jobs/kjobtrackerinterface.cpp jobs/kjobuidelegate.cpp plugin/kpluginfactory.cpp plugin/kpluginloader.cpp plugin/kpluginmetadata.cpp plugin/desktopfileparser.cpp randomness/krandom.cpp randomness/krandomsequence.cpp text/kmacroexpander.cpp text/kstringhandler.cpp text/ktexttohtml.cpp util/kdelibs4migration.cpp util/kdelibs4configmigrator.cpp util/kformat.cpp util/kformatprivate.cpp util/kosrelease.cpp util/kprocesslist.cpp util/kshell.cpp ${kcoreaddons_OPTIONAL_SRCS} ${kcoreaddons_QM_LOADER} ) set(kcoreaddons_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/../.. # for kcoreaddons_version.h ${CMAKE_CURRENT_SOURCE_DIR}/caching/ ${CMAKE_CURRENT_BINARY_DIR}/io/ ${CMAKE_CURRENT_SOURCE_DIR}/io/ ${CMAKE_CURRENT_SOURCE_DIR}/jobs/ ${CMAKE_CURRENT_SOURCE_DIR}/plugin/ ${CMAKE_CURRENT_SOURCE_DIR}/randomness/ ${CMAKE_CURRENT_SOURCE_DIR}/text/ ${CMAKE_CURRENT_SOURCE_DIR}/util/ ) if (HAVE_PROCSTAT) set(kcoreaddons_INCLUDE_DIRS ${kcoreaddons_INCLUDE_DIRS} ${PROCSTAT_INCLUDE_DIR} ) endif() ecm_qt_export_logging_category( IDENTIFIER KDIRWATCH CATEGORY_NAME kf5.kcoreaddons.kdirwatch DEFAULT_SEVERITY Warning DESCRIPTION "KDirWatch (KCoreAddons)" EXPORT KCOREADDONS ) ecm_qt_export_logging_category( IDENTIFIER KABOUTDATA CATEGORY_NAME kf5.kcoreaddons.kaboutdata DESCRIPTION "KAboutData (KCoreAddons)" EXPORT KCOREADDONS ) ecm_qt_export_logging_category( IDENTIFIER DESKTOPPARSER CATEGORY_NAME kf5.kcoreaddons.desktopparser DEFAULT_SEVERITY Warning DESCRIPTION "DesktopParser (KCoreAddons)" EXPORT KCOREADDONS ) ecm_qt_export_logging_category( IDENTIFIER MIGRATOR CATEGORY_NAME kf5.kcoreaddons.kdelibs4configmigrator DEFAULT_SEVERITY Warning DESCRIPTION "Kdelibs4ConfigMigrator (KCoreAddons)" EXPORT KCOREADDONS ) ecm_qt_declare_logging_category(libkcoreaddons_SRCS HEADER kcoreaddons_debug.h IDENTIFIER KCOREADDONS_DEBUG CATEGORY_NAME org.kde.kcoreaddons DESCRIPTION "kcoreaddons (kcoreaddons lib)" EXPORT KCOREADDONS ) add_library(KF5CoreAddons ${libkcoreaddons_SRCS}) add_library(KF5::CoreAddons ALIAS KF5CoreAddons) ecm_generate_export_header(KF5CoreAddons BASE_NAME KCoreAddons GROUP_BASE_NAME KF VERSION ${KF5_VERSION} DEPRECATED_BASE_VERSION 0 - DEPRECATION_VERSIONS 4.0 5.0 5.2 5.65 5.67 + DEPRECATION_VERSIONS 4.0 5.0 5.2 5.65 5.67 5.70 EXCLUDE_DEPRECATED_BEFORE_AND_AT ${EXCLUDE_DEPRECATED_BEFORE_AND_AT} ) target_include_directories(KF5CoreAddons PUBLIC "$") target_link_libraries(KF5CoreAddons PUBLIC Qt5::Core PRIVATE ${kcoreaddons_OPTIONAL_LIBS} ) target_link_libraries(KF5CoreAddons PRIVATE ${CMAKE_THREAD_LIBS_INIT}) if(WIN32) target_link_libraries(KF5CoreAddons PRIVATE netapi32 userenv) endif() if(HAVE_PROCSTAT) target_link_libraries(KF5CoreAddons PRIVATE ${PROCSTAT_LIBRARIES}) endif() target_include_directories(KF5CoreAddons INTERFACE "$" ) target_compile_definitions(KF5CoreAddons INTERFACE "$") set_target_properties(KF5CoreAddons PROPERTIES VERSION ${KCOREADDONS_VERSION_STRING} SOVERSION ${KCOREADDONS_SOVERSION} EXPORT_NAME CoreAddons ) ecm_generate_headers(KCoreAddons_HEADERS HEADER_NAMES KAboutData KCoreAddons REQUIRED_HEADERS KCoreAddons_HEADERS ) ecm_generate_headers(KCoreAddons_HEADERS HEADER_NAMES KSharedDataCache RELATIVE caching REQUIRED_HEADERS KCoreAddons_HEADERS ) ecm_generate_headers(KCoreAddons_HEADERS HEADER_NAMES KAutoSaveFile KDirWatch KMessage KProcess KBackup KUrlMimeData KFileSystemType KFileUtils RELATIVE io REQUIRED_HEADERS KCoreAddons_HEADERS ) ecm_generate_headers(KCoreAddons_HEADERS HEADER_NAMES KCompositeJob KJob KJobTrackerInterface KJobUiDelegate RELATIVE jobs REQUIRED_HEADERS KCoreAddons_HEADERS ) ecm_generate_headers(KCoreAddons_HEADERS HEADER_NAMES KExportPlugin KPluginFactory KPluginLoader KPluginMetaData RELATIVE plugin REQUIRED_HEADERS KCoreAddons_HEADERS ) ecm_generate_headers(KCoreAddons_HEADERS HEADER_NAMES KRandom KRandomSequence RELATIVE randomness REQUIRED_HEADERS KCoreAddons_HEADERS ) ecm_generate_headers(KCoreAddons_HEADERS HEADER_NAMES KMacroExpander KStringHandler KTextToHTML KTextToHTMLEmoticonsInterface RELATIVE text REQUIRED_HEADERS KCoreAddons_HEADERS ) ecm_generate_headers(KCoreAddons_HEADERS HEADER_NAMES KFormat KOSRelease KUser KShell KProcessList KListOpenFilesJob Kdelibs4Migration Kdelibs4ConfigMigrator RELATIVE util REQUIRED_HEADERS KCoreAddons_HEADERS ) find_package(PythonModuleGeneration) if (PythonModuleGeneration_FOUND) ecm_generate_python_binding( TARGET KF5::CoreAddons PYTHONNAMESPACE PyKF5 MODULENAME KCoreAddons RULES_FILE "${CMAKE_SOURCE_DIR}/cmake/rules_PyKF5.py" SIP_DEPENDS QtCore/QtCoremod.sip HEADERS kaboutdata.h kcoreaddons.h caching/kshareddatacache.h io/kautosavefile.h io/kdirwatch.h io/kmessage.h io/kprocess.h io/kbackup.h io/kurlmimedata.h io/kfilesystemtype.h jobs/kcompositejob.h jobs/kjob.h jobs/kjobtrackerinterface.h jobs/kjobuidelegate.h plugin/kexportplugin.h plugin/kpluginfactory.h plugin/kpluginloader.h plugin/kpluginmetadata.h randomness/krandom.h randomness/krandomsequence.h text/kmacroexpander.h text/kstringhandler.h text/ktexttohtml.h text/ktexttohtmlemoticonsinterface.h util/kformat.h util/klistopenfilesjob.h util/kosrelease.h util/kprocesslist.h util/kuser.h util/kshell.h util/kdelibs4migration.h util/kdelibs4configmigrator.h ) endif() install(TARGETS KF5CoreAddons EXPORT KF5CoreAddonsTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${KCoreAddons_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/kcoreaddons_export.h DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KCoreAddons COMPONENT Devel ) # Includes CMake code to install open-source license texts for KAboutData. add_subdirectory(licenses) if(BUILD_QCH) ecm_add_qch( KF5CoreAddons_QCH NAME KCoreAddons BASE_NAME KF5CoreAddons VERSION ${KF5_VERSION} ORG_DOMAIN org.kde SOURCES # using only public headers, to cover only public API ${KCoreAddons_HEADERS} MD_MAINPAGE "${CMAKE_SOURCE_DIR}/README.md" LINK_QCHS Qt5Core_QCH INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR} ${kcoreaddons_INCLUDE_DIRS} BLANK_MACROS KCOREADDONS_EXPORT KCOREADDONS_DEPRECATED KCOREADDONS_DEPRECATED_EXPORT "KCOREADDONS_DEPRECATED_VERSION(x, y, t)" TAGFILE_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} QCH_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} COMPONENT Devel ) endif() include(ECMGeneratePriFile) ecm_generate_pri_file(BASE_NAME KCoreAddons LIB_NAME KF5CoreAddons DEPS "core" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/KCoreAddons) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) ecm_qt_install_logging_categories( EXPORT KCOREADDONS FILE kcoreaddons.categories DESTINATION "${KDE_INSTALL_LOGGINGCATEGORIESDIR}" ) diff --git a/src/lib/plugin/kpluginfactory.h b/src/lib/plugin/kpluginfactory.h index 13c5c61..ac2295a 100644 --- a/src/lib/plugin/kpluginfactory.h +++ b/src/lib/plugin/kpluginfactory.h @@ -1,573 +1,580 @@ /* This file is part of the KDE project SPDX-FileCopyrightText: 2007 Matthias Kretz SPDX-FileCopyrightText: 2007 Bernhard Loos SPDX-License-Identifier: LGPL-2.0-or-later */ #ifndef KPLUGINFACTORY_H #define KPLUGINFACTORY_H #include "kcoreaddons_export.h" #include #include #include #include // for source compat class QWidget; class KPluginFactoryPrivate; namespace KParts { class Part; } #define KPluginFactory_iid "org.kde.KPluginFactory" #define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_SKEL(name, baseFactory, ...) \ class name : public KPluginFactory \ { \ Q_OBJECT \ Q_INTERFACES(KPluginFactory) \ __VA_ARGS__ \ public: \ explicit name(); \ ~name(); \ }; #define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_JSON(name, baseFactory, json) \ K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_SKEL(name, baseFactory, Q_PLUGIN_METADATA(IID KPluginFactory_iid FILE json)) #define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \ K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_SKEL(name, baseFactory, Q_PLUGIN_METADATA(IID KPluginFactory_iid)) #define K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) \ name::name() \ { \ pluginRegistrations \ } \ name::~name() {} #define K_PLUGIN_FACTORY_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) \ K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \ K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) #define K_PLUGIN_FACTORY_WITH_BASEFACTORY_JSON(name, baseFactory, jsonFile, pluginRegistrations) \ K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY_JSON(name, baseFactory, jsonFile) \ K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) /** * \relates KPluginFactory * * Create a KPluginFactory subclass and export it as the root plugin object. * * \param name The name of the KPluginFactory derived class. * * \param pluginRegistrations Code to be inserted into the constructor of the * class. Usually a series of registerPlugin() calls. * * @note K_PLUGIN_FACTORY declares the subclass including a Q_OBJECT macro. * So you need to make sure to have Qt's moc run also for the source file * where you use the macro. E.g. in projects using CMake and it's automoc feature, * as usual you need to have a line * @code * #include * @endcode * in the same source file when that one has the name "myplugin.cpp". * * Example: * \code * #include * #include * * class MyPlugin : public PluginInterface * { * public: * MyPlugin(QObject *parent, const QVariantList &args) * : PluginInterface(parent) * {} * }; * * K_PLUGIN_FACTORY(MyPluginFactory, * registerPlugin(); * ) * * #include * \endcode * * If you want to compile a .json file into the plugin, use K_PLUGIN_FACTORY_WITH_JSON. * * \see K_PLUGIN_FACTORY_WITH_JSON * \see K_PLUGIN_FACTORY_DECLARATION * \see K_PLUGIN_FACTORY_DEFINITION */ #define K_PLUGIN_FACTORY(name, pluginRegistrations) K_PLUGIN_FACTORY_WITH_BASEFACTORY(name, KPluginFactory, pluginRegistrations) /** * \relates KPluginFactory * * Create a KPluginFactory subclass and export it as the root plugin object with * JSON metadata. * * This macro does the same as K_PLUGIN_FACTORY, but adds a JSON file as plugin * metadata. See Q_PLUGIN_METADATA() for more information. * * \param name The name of the KPluginFactory derived class. * * \param pluginRegistrations Code to be inserted into the constructor of the * class. Usually a series of registerPlugin() calls. * * \param jsonFile Name of the json file to be compiled into the plugin as metadata * * @note K_PLUGIN_FACTORY_WITH_JSON declares the subclass including a Q_OBJECT macro. * So you need to make sure to have Qt's moc run also for the source file * where you use the macro. E.g. in projects using CMake and it's automoc feature, * as usual you need to have a line * @code * #include * @endcode * in the same source file when that one has the name "myplugin.cpp". * * Example: * \code * #include * #include * * class MyPlugin : public PluginInterface * { * public: * MyPlugin(QObject *parent, const QVariantList &args) * : PluginInterface(parent) * {} * }; * * K_PLUGIN_FACTORY_WITH_JSON(MyPluginFactory, * "metadata.json", * registerPlugin(); * ) * * #include * \endcode * * \see K_PLUGIN_FACTORY * \see K_PLUGIN_FACTORY_DECLARATION * \see K_PLUGIN_FACTORY_DEFINITION * * @since 5.0 */ #define K_PLUGIN_FACTORY_WITH_JSON(name, jsonFile, pluginRegistrations) K_PLUGIN_FACTORY_WITH_BASEFACTORY_JSON(name, KPluginFactory, jsonFile, pluginRegistrations) /** * \relates KPluginFactory * * Create a KPluginFactory subclass and export it as the root plugin object with * JSON metadata. * * This macro does the same as K_PLUGIN_FACTORY_WITH_JSON, but you only have to pass the class name and the json file. * The factory name and registerPlugin call are deduced from the class name. * * @code * #include * @endcode * in the same source file when that one has the name "myplugin.cpp". * * Example: * \code * #include * #include * * class MyPlugin : public PluginInterface * { * public: * MyPlugin(QObject *parent, const QVariantList &args) * : PluginInterface(parent) * {} * }; * * K_PLUGIN_CLASS_WITH_JSON(MyPlugin, "metadata.json") * * #include * \endcode * * \see K_PLUGIN_FACTORY_WITH_JSON * * @since 5.44 */ #define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile) K_PLUGIN_FACTORY_WITH_JSON(classname ## Factory, jsonFile, registerPlugin();) /** * \relates KPluginFactory * * K_PLUGIN_FACTORY_DECLARATION declares the KPluginFactory subclass. This macro * can be used in a header file. * * \param name The name of the KPluginFactory derived class. * * \see K_PLUGIN_FACTORY * \see K_PLUGIN_FACTORY_DEFINITION */ #define K_PLUGIN_FACTORY_DECLARATION(name) K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, KPluginFactory) /** * \relates KPluginFactory * K_PLUGIN_FACTORY_DEFINITION defines the KPluginFactory subclass. This macro * can not be used in a header file. * * \param name The name of the KPluginFactory derived class. * * \param pluginRegistrations Code to be inserted into the constructor of the * class. Usually a series of registerPlugin() calls. * * \see K_PLUGIN_FACTORY * \see K_PLUGIN_FACTORY_DECLARATION */ #define K_PLUGIN_FACTORY_DEFINITION(name, pluginRegistrations) K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, KPluginFactory, pluginRegistrations) /** * \class KPluginFactory kpluginfactory.h * * KPluginFactory provides a convenient way to provide factory-style plugins. * Qt plugins provide a singleton object, but a common pattern is for plugins * to generate as many objects of a particular type as the application requires. * By using KPluginFactory, you can avoid implementing the factory pattern * yourself. * * KPluginFactory also allows plugins to provide multiple different object * types, indexed by keywords. * * The objects created by KPluginFactory must inherit QObject, and must have a * standard constructor pattern: * \li if the object is a KPart::Part, it must be of the form * \code * T(QWidget *parentWidget, QObject *parent, const QVariantList &args) * \endcode * \li if it is a QWidget, it must be of the form * \code * T(QWidget *parent, const QVariantList &args) * \endcode * \li otherwise it must be of the form * \code * T(QObject *parent, const QVariantList &args) * \endcode * * You should typically use either K_PLUGIN_FACTORY() or * K_PLUGIN_FACTORY_WITH_JSON() in your plugin code to create the factory. The * typical pattern is * * \code * #include * #include * * class MyPlugin : public PluginInterface * { * public: * MyPlugin(QObject *parent, const QVariantList &args) * : PluginInterface(parent) * {} * }; * * K_PLUGIN_FACTORY(MyPluginFactory, * registerPlugin(); * ) * #include * \endcode * * If you want to write a custom KPluginFactory not using the standard macro(s) * you can reimplement the * create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword) * method. * * Example: * \code * class SomeScriptLanguageFactory : public KPluginFactory * { * Q_OBJECT * public: * SomeScriptLanguageFactory() * {} * * protected: * virtual QObject *create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword) * { * const QString identifier = QLatin1String(iface) + QLatin1Char('_') + keyword; * // load scripting language module from the information in identifier * // and return it: * return object; * } * }; * \endcode * * If you want to load a library use KPluginLoader. * The application that wants to instantiate plugin classes can do the following: * \code * KPluginFactory *factory = KPluginLoader("libraryname").factory(); * if (factory) { * PluginInterface *p1 = factory->create(parent); * OtherInterface *p2 = factory->create(parent); * NextInterface *p3 = factory->create("keyword1", parent); * NextInterface *p3 = factory->create("keyword2", parent); * } * \endcode * * \author Matthias Kretz * \author Bernhard Loos */ class KCOREADDONS_EXPORT KPluginFactory : public QObject { Q_OBJECT Q_DECLARE_PRIVATE(KPluginFactory) public: /** * This constructor creates a factory for a plugin. */ explicit KPluginFactory(); /** * This destroys the PluginFactory. */ ~KPluginFactory() override; /** * Use this method to create an object. It will try to create an object which inherits * \p T. If it has multiple choices it's not defined which object will be returned, so be careful * to request a unique interface or use keywords. * * \tparam T The interface for which an object should be created. The object will inherit \p T. * \param parent The parent of the object. If \p parent is a widget type, it will also passed * to the parentWidget argument of the CreateInstanceFunction for the object. * \param args Additional arguments which will be passed to the object. * \returns A pointer to the created object is returned, or @c nullptr if an error occurred. */ template T *create(QObject *parent = nullptr, const QVariantList &args = QVariantList()); /** * Use this method to create an object. It will try to create an object which inherits * \p T and was registered with \p keyword. * * \tparam T The interface for which an object should be created. The object will inherit \p T. * \param keyword The keyword of the object. * \param parent The parent of the object. If \p parent is a widget type, it will also passed * to the parentWidget argument of the CreateInstanceFunction for the object. * \param args Additional arguments which will be passed to the object. * \returns A pointer to the created object is returned, or @c nullptr if an error occurred. */ template T *create(const QString &keyword, QObject *parent = nullptr, const QVariantList &args = QVariantList()); /** * Use this method to create an object. It will try to create an object which inherits * \p T and was registered with \p keyword. * This overload has an additional \p parentWidget argument, which is used by some plugins (e.g. Parts). * \tparam T The interface for which an object should be created. The object will inherit \p T. * \param parentWidget An additional parent widget. * \param parent The parent of the object. If \p parent is a widget type, it will also passed * to the parentWidget argument of the CreateInstanceFunction for the object. * \param keyword The keyword of the object. * \param args Additional arguments which will be passed to the object. * \returns A pointer to the created object is returned, or @c nullptr if an error occurred. */ template T *create(QWidget *parentWidget, QObject *parent, const QString &keyword = QString(), const QVariantList &args = QVariantList()); #if KCOREADDONS_ENABLE_DEPRECATED_SINCE(4, 0) /** * @deprecated since 4.0 use create(QObject *parent, const QVariantList &args) */ template KCOREADDONS_DEPRECATED_VERSION(4, 0, "Use KPluginFactory::create(QObject *parent, const QVariantList &args)") T *create(QObject *parent, const QStringList &args) { return create(parent, stringListToVariantList(args)); } /** * @deprecated since 4.0 use create(QObject *parent, const QVariantList &args) */ KCOREADDONS_DEPRECATED_VERSION(4, 0, "Use KPluginFactory::create(QObject *parent, const QVariantList &args)") QObject *create(QObject *parent = nullptr, const char *classname = "QObject", const QStringList &args = QStringList()) { return create(classname, nullptr, parent, stringListToVariantList(args), QString()); } #endif /** * \internal * Converts a QStringList to a QVariantList */ static QVariantList stringListToVariantList(const QStringList &list); /** * \internal * Converts a QVariantList of strings to a QStringList */ static QStringList variantListToStringList(const QVariantList &list); Q_SIGNALS: void objectCreated(QObject *object); protected: /** * Function pointer type to a function that instantiates a plugin. */ typedef QObject *(*CreateInstanceFunction)(QWidget *, QObject *, const QVariantList &); /** * This is used to detect the arguments need for the constructor of plugin classes. * You can inherit it, if you want to add new classes and still keep support for the old ones. */ template struct InheritanceChecker { CreateInstanceFunction createInstanceFunction(KParts::Part *) { return &createPartInstance; } CreateInstanceFunction createInstanceFunction(QWidget *) { return &createInstance; } CreateInstanceFunction createInstanceFunction(...) { return &createInstance; } }; explicit KPluginFactory(KPluginFactoryPrivate &dd); /** * Registers a plugin with the factory. Call this function from the constructor of the * KPluginFactory subclass to make the create function able to instantiate the plugin when asked * for an interface the plugin implements. * * You can register as many plugin classes as you want as long as either the plugin interface or * the \p keyword makes it unique. E.g. it is possible to register a KCModule and a * KParts::Part without having to specify keywords since their interfaces differ. * * \tparam T the name of the plugin class * * \param keyword An optional keyword as unique identifier for the plugin. This allows you to * put more than one plugin with the same interface into the same library using the same * factory. X-KDE-PluginKeyword is a convenient way to specify the keyword in a desktop file. * * \param instanceFunction A function pointer to a function that creates an instance of the * plugin. The default function that will be used depends on the type of interface. If the * interface inherits from * \li \c KParts::Part the function will call * \code * new T(QWidget *parentWidget, QObject *parent, const QVariantList &args) * \endcode * \li \c QWidget the function will call * \code * new T(QWidget *parent, const QVariantList &args) * \endcode * \li else the function will call * \code * new T(QObject *parent, const QVariantList &args) * \endcode */ template void registerPlugin(const QString &keyword = QString(), CreateInstanceFunction instanceFunction = InheritanceChecker().createInstanceFunction(static_cast(nullptr))) { registerPlugin(keyword, &T::staticMetaObject, instanceFunction); } KPluginFactoryPrivate *const d_ptr; #if KCOREADDONS_BUILD_DEPRECATED_SINCE(4, 0) /** * @deprecated since 4.0 use create(QObject *parent, const QVariantList &args) */ KCOREADDONS_DEPRECATED_VERSION(4, 0, "Use KPluginFactory::create(QObject *parent, const QVariantList &args)") virtual QObject *createObject(QObject *parent, const char *className, const QStringList &args); /** * @deprecated since 4.0 use create(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args) */ KCOREADDONS_DEPRECATED_VERSION(4, 0, "Use KPluginFactory::create(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args)") virtual KParts::Part *createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args); #endif /** * This function is called when the factory asked to create an Object. * * You may reimplement it to provide a very flexible factory. This is especially useful to * provide generic factories for plugins implemented using a scripting language. * * \param iface The staticMetaObject::className() string identifying the plugin interface that * was requested. E.g. for KCModule plugins this string will be "KCModule". * \param parentWidget Only used if the requested plugin is a KPart. * \param parent The parent object for the plugin object. * \param args A plugin specific list of arbitrary arguments. * \param keyword A string that uniquely identifies the plugin. If a KService is used this * keyword is read from the X-KDE-PluginKeyword entry in the .desktop file. */ virtual QObject *create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword); template static QObject *createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args) { Q_UNUSED(parentWidget) ParentType *p = nullptr; if (parent) { p = qobject_cast(parent); Q_ASSERT(p); } return new impl(p, args); } template static QObject *createPartInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args) { return new impl(parentWidget, parent, args); } private: void registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction); }; +// Deprecation wrapper macro added only for 5.70, while backward typedef added in 4.0 +#if KCOREADDONS_ENABLE_DEPRECATED_SINCE(5, 70) +/** + * Backward compatibility typedef for KPluginFactory + * @deprecated since 4.0, use KPluginFactory + */ typedef KPluginFactory KLibFactory; +#endif template inline T *KPluginFactory::create(QObject *parent, const QVariantList &args) { QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast(parent) : nullptr, parent, args, QString()); T *t = qobject_cast(o); if (!t) { delete o; } return t; } template inline T *KPluginFactory::create(const QString &keyword, QObject *parent, const QVariantList &args) { QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast(parent) : nullptr, parent, args, keyword); T *t = qobject_cast(o); if (!t) { delete o; } return t; } template inline T *KPluginFactory::create(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args) { QObject *o = create(T::staticMetaObject.className(), parentWidget, parent, args, keyword); T *t = qobject_cast(o); if (!t) { delete o; } return t; } Q_DECLARE_INTERFACE(KPluginFactory, KPluginFactory_iid) #endif // KPLUGINFACTORY_H