KParts

partloader.h
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 2020 David Faure <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef KPARTS_PARTLOADER_H
9 #define KPARTS_PARTLOADER_H
10 
11 #include <KLocalizedString>
12 #include <KPluginFactory>
13 #include <KPluginMetaData>
14 #include <QObject>
15 #include <QVector>
16 #include <kparts/kparts_export.h>
17 
18 namespace KParts
19 {
20 /**
21  * Helper methods for locating and loading parts.
22  * This is based upon KPluginLoader and KPluginFactory, but it takes
23  * care of querying by mimetype, sorting the available parts by builtin
24  * preference and by user preference.
25  * @since 5.69
26  */
27 namespace PartLoader
28 {
29 #if KPARTS_ENABLE_DEPRECATED_SINCE(5, 88)
30 namespace Private
31 {
32 /**
33  * Helper for PartLoader::createPartInstanceForMimeType<T>
34  * @internal
35  */
36 KPARTS_EXPORT QObject *createPartInstanceForMimeTypeHelper(const char *iface, const QString &mimeType, QWidget *parentWidget, QObject *parent, QString *error);
37 }
38 #endif
39 
40 /**
41  * Locate all available KParts for a mimetype.
42  * @return a list of plugin metadata, sorted by preference.
43  * This takes care both of the builtin preference (set by developers)
44  * and of user preference (stored in mimeapps.list).
45  *
46  * This uses KPluginMetaData::findPlugins, i.e. it requires the parts to
47  * provide the metadata as JSON embedded into the plugin.
48  * Until KF6, however, it also supports .desktop files as a fallback solution.
49  *
50  * To load a part from one of the KPluginMetaData instances returned here,
51  * simply do
52  * @code
53  * KPluginLoader loader(metaData.fileName());
54  * m_part = loader.factory()->create<KParts::ReadOnlyPart>(parentWidget, parent);
55  * @endcode
56  *
57  * @since 5.69
58  */
59 KPARTS_EXPORT QVector<KPluginMetaData> partsForMimeType(const QString &mimeType);
60 
61 /**
62  * Use this method to create a KParts part. It will try to create an object which inherits
63  * \p T.
64  *
65  * Example:
66  * \code
67  * QString errorString;
68  * m_part = KParts::PartLoader::createPartInstanceForMimeType<KParts::ReadOnlyPart>(
69  * mimeType, this, this, &errorString);
70  * if (m_part) {
71  * layout->addWidget(m_part->widget()); // Integrate the widget
72  * createGUI(m_part); // Integrate the actions
73  * m_part->openUrl(url);
74  * } else {
75  * qWarning() << errorString;
76  * }
77  * \endcode
78  *
79  * \tparam T The interface for which an object should be created. The object will inherit \p T.
80  * \param mimeType The mimetype for which we need a KParts.
81  * \param parentWidget The parent widget for the part's widget.
82  * \param parent The parent of the part.
83  * \param error Optional output parameter, it will be set to the error string, if any.
84  * \returns A pointer to the created object is returned, or @c nullptr if an error occurred.
85  * @since 5.69
86  */
87 template<class T>
88 static T *createPartInstanceForMimeType(const QString &mimeType, QWidget *parentWidget = nullptr, QObject *parent = nullptr, QString *error = nullptr)
89 {
91  for (const auto &plugin : plugins) {
92  auto factory = KPluginFactory::loadFactory(plugin);
93  if (factory) {
94  if (T *part = factory.plugin->create<T>(parentWidget, parent, QVariantList())) {
95  return part;
96  } else if (error) {
97  *error = i18n("The plugin '%1' does not provide an interface '%2'", //
98  plugin.fileName(),
99  QLatin1String(T::staticMetaObject.className()));
100  }
101  } else if (error) {
102  *error = factory.errorString;
103  }
104  }
105  if (error && error->isEmpty()) {
106  *error = i18n("No part was found for mimeType %1", mimeType);
107  }
108  return nullptr;
109 }
110 
111 } // namespace
112 } // namespace
113 
114 #endif
KPARTS_EXPORT QVector< KPluginMetaData > partsForMimeType(const QString &mimeType)
Locate all available KParts for a mimetype.
Definition: partloader.cpp:77
static T * createPartInstanceForMimeType(const QString &mimeType, QWidget *parentWidget=nullptr, QObject *parent=nullptr, QString *error=nullptr)
Use this method to create a KParts part.
Definition: partloader.h:88
QString i18n(const char *text, const TYPE &arg...)
static Result< KPluginFactory > loadFactory(const KPluginMetaData &data)
The KParts namespace,.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 9 2022 03:51:33 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.