1 | /* |
---|---|
2 | SPDX-FileCopyrightText: 2023 Alexander Lohnau <alexander.lohnau@gmx.de> |
3 | SPDX-License-Identifier: LGPL-2.0-or-later |
4 | */ |
5 | |
6 | #include "kquickconfigmoduleloader.h" |
7 | |
8 | #include "kcmutils_debug.h" |
9 | |
10 | #include <KPluginFactory> |
11 | #include <QJsonArray> |
12 | #include <QQmlEngine> |
13 | |
14 | #include "kquickconfigmodule.h" |
15 | |
16 | std::weak_ptr<QQmlEngine> s_kcmutilsCreatedEngine; |
17 | |
18 | KPluginFactory::Result<KQuickConfigModule> |
19 | KQuickConfigModuleLoader::loadModule(const KPluginMetaData &metaData, QObject *parent, const QVariantList &args, const std::shared_ptr<QQmlEngine> &engineArg) |
20 | { |
21 | const auto factoryResult = KPluginFactory::loadFactory(data: metaData); |
22 | KPluginFactory::Result<KQuickConfigModule> result; |
23 | if (!factoryResult) { |
24 | result.errorReason = factoryResult.errorReason; |
25 | result.errorString = factoryResult.errorString; |
26 | result.errorText = factoryResult.errorText; |
27 | return result; |
28 | } |
29 | KPluginFactory *factory = factoryResult.plugin; |
30 | |
31 | factory->setMetaData(KPluginMetaData(metaData)); |
32 | |
33 | const QVariantList pluginArgs = QVariantList(args) << metaData.rawData().value(key: QLatin1String("X-KDE-KCM-Args")).toArray().toVariantList(); |
34 | if (const auto kcm = factory->create<KQuickConfigModule>(parent, args: pluginArgs)) { |
35 | const std::shared_ptr<QQmlEngine> engine = |
36 | engineArg ? engineArg : (s_kcmutilsCreatedEngine.expired() ? std::make_shared<QQmlEngine>() : s_kcmutilsCreatedEngine.lock()); |
37 | |
38 | if (!engineArg && s_kcmutilsCreatedEngine.expired()) { |
39 | s_kcmutilsCreatedEngine = engine; |
40 | } |
41 | kcm->setInternalEngine(engine); |
42 | |
43 | result.plugin = kcm; |
44 | qCDebug(KCMUTILS_LOG) << "loaded QML KCM"<< metaData.fileName(); |
45 | } else { |
46 | result.errorReason = KPluginFactory::INVALID_KPLUGINFACTORY_INSTANTIATION; |
47 | } |
48 | |
49 | return result; |
50 | } |
51 |