KPluginMetaData Class

Header: #include <KPluginMetaData>
CMake: find_package(KF6 REQUIRED COMPONENTS CoreAddons)
target_link_libraries(mytarget PRIVATE KF6::CoreAddons)
Since: 5.1

Public Types

(since 5.91) enum KPluginMetaDataOption { AllowEmptyMetaData, CacheMetaData }
flags KPluginMetaDataOptions

Public Functions

KPluginMetaData()
(since 6.0) KPluginMetaData(const QJsonObject &metaData, const QString &fileName)
KPluginMetaData(const QPluginLoader &loader, KPluginMetaData::KPluginMetaDataOptions options = {})
KPluginMetaData(const QString &pluginFile, KPluginMetaData::KPluginMetaDataOptions options = {})
QList<KAboutPerson> authors() const
(since 5.99) QString bugReportUrl() const
QString category() const
(since 5.18) QString copyrightText() const
QString description() const
QString fileName() const
(since 5.12) QStringList formFactors() const
QString iconName() const
bool isEnabledByDefault() const
(since 5.8) bool isHidden() const
(since 5.89) bool isStaticPlugin() const
bool isValid() const
QString license() const
(since 5.73) QString licenseText() const
(since 5.16) QStringList mimeTypes() const
QString name() const
(since 5.18) QList<KAboutPerson> otherContributors() const
QString pluginId() const
QJsonObject rawData() const
(since 5.66) bool supportsMimeType(const QString &mimeType) const
(since 5.18) QList<KAboutPerson> translators() const
QString value(const QString &key, const QString &defaultValue = QString()) const
(since 5.88) bool value(const QString &key, bool defaultValue) const
(since 5.88) QStringList value(const QString &key, const QStringList &defaultValue) const
(since 5.88) int value(const QString &key, int defaultValue) const
QString version() const
QString website() const

Static Public Members

(since 5.84) KPluginMetaData findPluginById(const QString &directory, const QString &pluginId, KPluginMetaData::KPluginMetaDataOptions options = {})
(since 5.86) QList<KPluginMetaData> findPlugins(const QString &directory, std::function<bool (const KPluginMetaData &)> filter = {}, KPluginMetaData::KPluginMetaDataOptions options = {})
(since 5.91) KPluginMetaData fromJsonFile(const QString &jsonFile)

Detailed Description

This class allows easily accessing some standardized values from the JSON metadata that can be embedded into Qt plugins. Additional plugin-specific metadata can be retrieved by directly reading from the QJsonObject returned by KPluginMetaData::rawData.

For embedded metadata, you should not specify an id manually. Instead the id will be derived from the file basename.

The following keys will be read from an object "KPlugin" inside the metadata JSON:

KeyAccessor functionJSON Type
Namename()string
Descriptiondescription()string
IconiconName()string
Authorsauthors()object array (KAboutPerson)
Categorycategory()string
Licenselicense()string
CopyrightcopyrightText()string
IdpluginId()string
Versionversion()string
Websitewebsite()string
BugReportUrlbugReportUrl()string
EnabledByDefaultisEnabledByDefault()bool
MimeTypesmimeTypes()string array
FormFactorsformFactors()string array
Translatorstranslators()object array (KAboutPerson)
OtherContributorsotherContributors()object array (KAboutPerson)

The Authors, Translators and OtherContributors keys are expected to be list of objects that match the structure expected by KAboutPerson::fromJSON.

An example metadata json file could look like this:

{
  "KPlugin": {
     "Name": "Date and Time",
     "Description": "Date and time by timezone",
     "Icon": "preferences-system-time",
     "Authors": [ { "Name": "Aaron Seigo", "Email": "aseigo@kde.org" } ],
     "Category": "Date and Time",
     "EnabledByDefault": "true",
     "License": "LGPL",
     "Version": "1.0",
     "Website": "https://plasma.kde.org/"
  }
}

See also KAboutPerson::fromJSON().

Member Type Documentation

[since 5.91] enum KPluginMetaData::KPluginMetaDataOption
flags KPluginMetaData::KPluginMetaDataOptions

Options for creating a KPluginMetaData object.

ConstantValueDescription
KPluginMetaData::AllowEmptyMetaData1Plugins with empty metaData are considered valid
KPluginMetaData::CacheMetaData (since KCoreAddons 6.0)2If KCoreAddons should keep metadata in cache. This makes querying the namespace again faster. Consider using this if you need revalidation of plugins

This enum was introduced in 5.91.

The KPluginMetaDataOptions type is a typedef for QFlags<KPluginMetaDataOption>. It stores an OR combination of KPluginMetaDataOption values.

Member Function Documentation

KPluginMetaData::KPluginMetaData()

Creates an invalid KPluginMetaData instance

[since 6.0] KPluginMetaData::KPluginMetaData(const QJsonObject &metaData, const QString &fileName)

Creates a KPluginMetaData from a QJsonObject holding the metadata and a file name This can be used if the data is not retrieved from a Qt C++ plugin library but from some other source.

metaData the JSON metadata to use for this object

pluginFile the file that the plugin can be loaded from

This function was introduced in 6.0.

KPluginMetaData::KPluginMetaData(const QPluginLoader &loader, KPluginMetaData::KPluginMetaDataOptions options = {})

Reads the plugin metadata from a QPluginLoader instance. You must call QPluginLoader::setFileName() or use the appropriate constructor on loader before calling this.

option Added in 6.0, see enum docs

KPluginMetaData::KPluginMetaData(const QString &pluginFile, KPluginMetaData::KPluginMetaDataOptions options = {})

Reads the plugin metadata from a plugin which can be loaded from file.

Platform-specific library suffixes may be omitted since file will be resolved using the same logic as QPluginLoader.

See also QPluginLoader::setFileName().

QList<KAboutPerson> KPluginMetaData::authors() const

Returns the author(s) of this plugin.

Note: Getter function for property authors.

[since 5.99] QString KPluginMetaData::bugReportUrl() const

Returns the website where people can report a bug found in this plugin

Note: Getter function for property bugReportUrl.

This function was introduced in 5.99.

QString KPluginMetaData::category() const

Returns the categories of this plugin (e.g. "playlist/skin").

Note: Getter function for property category.

[since 5.18] QString KPluginMetaData::copyrightText() const

Returns a short copyright statement

Note: Getter function for property copyrightText.

This function was introduced in 5.18.

QString KPluginMetaData::description() const

Returns a short description of the plugin.

Note: Getter function for property description.

QString KPluginMetaData::fileName() const

Returns the path to the plugin. When the KPluginMetaData(QJsonObject, QString) constructor is used, the string is not modified. Otherwise, the path is resolved using QPluginLoader. For static plugins the fileName is the namespace and pluginId concatenated

Note: It is not guaranteed that this is a valid path to a shared library (i.e. loadable by QPluginLoader) since the metadata could also refer to a non-C++ plugin.

Note: Getter function for property fileName.

[static, since 5.84] KPluginMetaData KPluginMetaData::findPluginById(const QString &directory, const QString &pluginId, KPluginMetaData::KPluginMetaDataOptions options = {})

directory The directory to search for plugins. If a relative path is given for directory, all entries of QCoreApplication::libraryPaths() will be checked with directory appended as a subdirectory. If an absolute path is given only that directory will be searched. @note Check if the returned KPluginMetaData is valid before continuing to use it.

pluginId The Id of the plugin. The id should be the same as the filename, see KPluginMetaData::pluginId()

option Added in 6.0, see enum docs

This function was introduced in 5.84.

[static, since 5.86] QList<KPluginMetaData> KPluginMetaData::findPlugins(const QString &directory, std::function<bool (const KPluginMetaData &)> filter = {}, KPluginMetaData::KPluginMetaDataOptions options = {})

Find all plugins inside directory. Only plugins which have JSON metadata will be considered.

directory The directory to search for plugins. If a relative path is given for directory, all entries of QCoreApplication::libraryPaths() will be checked with directory appended as a subdirectory. If an absolute path is given only that directory will be searched.

filter a callback function that returns true if the found plugin should be loaded and false if it should be skipped. If this argument is omitted all plugins will be loaded

option Weather or not allow plugins with empty metadata to be considered valid

Returns all plugins found in directory that fulfil the constraints of filter

This function was introduced in 5.86.

[since 5.12] QStringList KPluginMetaData::formFactors() const

Returns A string list of formfactors this plugin is useful for, e.g. desktop, handset or mediacenter. The keys for this are not formally defined, though the above-mentioned values should be used when applicable.

Note: Getter function for property formFactors.

This function was introduced in 5.12.

[static, since 5.91] KPluginMetaData KPluginMetaData::fromJsonFile(const QString &jsonFile)

Load a KPluginMetaData instance from a .json file. Unlike the constructor with a single file argument, this ensure that only JSON format plugins are loaded and any other type is rejected.

jsonFile the .json file to load

This function was introduced in 5.91.

QString KPluginMetaData::iconName() const

Returns the icon name for this plugin

Note: Getter function for property iconName.

See also QIcon::fromTheme().

bool KPluginMetaData::isEnabledByDefault() const

Returns whether the plugin should be enabled by default. This is only a recommendation, applications can ignore this value if they want to.

Note: Getter function for property isEnabledByDefault.

[since 5.8] bool KPluginMetaData::isHidden() const

Returns whether this object should be hidden

Note: Getter function for property isHidden.

This function was introduced in 5.8.

[since 5.89] bool KPluginMetaData::isStaticPlugin() const

Returns true if the instance represents a static plugin

Note: for loading plugin the plugin independently of it being static/dynamic use KPluginFactory::loadFactory or KPluginFactory::instantiatePlugin.

This function was introduced in 5.89.

bool KPluginMetaData::isValid() const

Returns whether this object holds valid information about a plugin. If this is true pluginId() will return a non-empty string.

Note: Getter function for property isValid.

QString KPluginMetaData::license() const

Returns the short license identifier (e.g. LGPL). See KAboutLicense::byKeyword() for retrieving the full license information

Note: Getter function for property license.

[since 5.73] QString KPluginMetaData::licenseText() const

Returns the text of the license, equivalent to KAboutLicense::byKeyword(license()).text()

Note: Getter function for property licenseText.

This function was introduced in 5.73.

[since 5.16] QStringList KPluginMetaData::mimeTypes() const

Returns a list of MIME types this plugin can handle (e.g. "application/pdf", "image/png", etc.)

Note: Getter function for property mimeTypes.

This function was introduced in 5.16.

QString KPluginMetaData::name() const

Returns the user visible name of the plugin.

Note: Getter function for property name.

[since 5.18] QList<KAboutPerson> KPluginMetaData::otherContributors() const

Returns a list of people that contributed to this plugin (other than the authors and translators).

Note: Getter function for property otherContributors.

This function was introduced in 5.18.

QString KPluginMetaData::pluginId() const

Returns the unique identifier within the namespace of the plugin

For C++ plugins, this ID is derived from the filename. It should not be set in the metadata explicitly.

When using KPluginMetaData::fromJsonFile or KPluginMetaData(QJsonObject, QString), the "Id" of the "KPlugin" object will be used. If unset, it will be derived from the filename.

Note: Getter function for property pluginId.

QJsonObject KPluginMetaData::rawData() const

Returns the full metadata stored inside the plugin file.

Note: Getter function for property rawData.

[since 5.66] bool KPluginMetaData::supportsMimeType(const QString &mimeType) const

Returns true if this plugin can handle the given mimetype This is more accurate than mimeTypes().contains(mimeType) because it also takes MIME type inheritance into account.

This function was introduced in 5.66.

[since 5.18] QList<KAboutPerson> KPluginMetaData::translators() const

Returns the translator(s) of this plugin.

Note: Getter function for property translators.

This function was introduced in 5.18.

QString KPluginMetaData::value(const QString &key, const QString &defaultValue = QString()) const

Returns the string value for key from the metadata or defaultValue if the key does not exist

if QString is not the correct type for key you should use the other overloads or KPluginMetaData::rawData

[since 5.88] bool KPluginMetaData::value(const QString &key, bool defaultValue) const

This is an overloaded function.

This function was introduced in 5.88.

[since 5.88] QStringList KPluginMetaData::value(const QString &key, const QStringList &defaultValue) const

Returns the value for key from the metadata or defaultValue if the key does not exist. If the type of key is string, a list containing just that string will be returned. If the type is array, the list will contain one entry for each array member.

This is an overloaded function.

This function was introduced in 5.88.

[since 5.88] int KPluginMetaData::value(const QString &key, int defaultValue) const

This is an overloaded function.

This function was introduced in 5.88.

QString KPluginMetaData::version() const

Returns the version of the plugin.

Note: Getter function for property version.

QString KPluginMetaData::website() const

Returns the website of the plugin.

Note: Getter function for property website.