KCMUtils

kquickconfigmoduleloader.cpp
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
16std::weak_ptr<QQmlEngine> s_kcmutilsCreatedEngine;
17
19KQuickConfigModuleLoader::loadModule(const KPluginMetaData &metaData, QObject *parent, const QVariantList &args, const std::shared_ptr<QQmlEngine> &engineArg)
20{
21 const auto factoryResult = KPluginFactory::loadFactory(metaData);
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(QLatin1String("X-KDE-KCM-Args")).toArray().toVariantList();
34 if (const auto kcm = factory->create<KQuickConfigModule>(parent, 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}
static Result< KPluginFactory > loadFactory(const KPluginMetaData &data)
T * create(QObject *parent=nullptr, const QVariantList &args={})
void setMetaData(const KPluginMetaData &metaData)
QJsonObject rawData() const
QString fileName() const
The base class for QtQuick configuration modules.
QVariantList toVariantList() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.