File indexing completed on 2024-04-21 03:52:57

0001 /*
0002     SPDX-FileCopyrightText: 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
0003     SPDX-FileCopyrightText: 2000 Matthias Elter <elter@kde.org>
0004     SPDX-FileCopyrightText: 2003, 2004, 2006 Matthias Kretz <kretz@kde.org>
0005     SPDX-FileCopyrightText: 2004 Frans Englich <frans.englich@telia.com>
0006     SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-only
0009 */
0010 
0011 #include "kcmoduleloader.h"
0012 #include "kcmoduledata.h"
0013 #include "kcmoduleqml_p.h"
0014 #include "kquickconfigmoduleloader.h"
0015 #include <kcmutils_debug.h>
0016 
0017 #include <QJsonArray>
0018 #include <QLabel>
0019 #include <QLibrary>
0020 #include <QVBoxLayout>
0021 
0022 #include <KAboutData>
0023 #include <KAuthorized>
0024 #include <KLocalizedString>
0025 #include <KMessageBox>
0026 #include <KPluginFactory>
0027 #include <memory>
0028 #include <qqmlengine.h>
0029 
0030 #include "qml/kquickconfigmodule.h"
0031 
0032 using namespace KCModuleLoader;
0033 
0034 /***************************************************************/
0035 /**
0036  * When something goes wrong in loading the module, this one
0037  * jumps in as a "dummy" module.
0038  */
0039 class KCMError : public KCModule
0040 {
0041     Q_OBJECT
0042 public:
0043     KCMError(const QString &msg, const QString &details, QWidget *parent)
0044         : KCModule(parent, KPluginMetaData())
0045     {
0046         QString realDetails = details.trimmed();
0047         if (realDetails.isNull()) {
0048             realDetails = i18n(
0049                 "<qt><p>Possible reasons:<ul><li>An error occurred during your last "
0050                 "system upgrade, leaving an orphaned control module behind</li><li>You have old third party "
0051                 "modules lying around.</li></ul></p><p>Check these points carefully and try to remove "
0052                 "the module mentioned in the error message. If this fails, consider contacting "
0053                 "your distributor or packager.</p></qt>");
0054         }
0055 
0056         QVBoxLayout *topLayout = new QVBoxLayout(widget());
0057         QLabel *lab = new QLabel(msg, widget());
0058         {
0059             // Similar to Kirigami.Heading: Primary, level 3
0060             QFont font = lab->font();
0061             font.setPointSizeF(font.pointSizeF() * 1.15);
0062             font.setBold(true);
0063             lab->setFont(font);
0064         }
0065         lab->setWordWrap(true);
0066         lab->setTextInteractionFlags(lab->textInteractionFlags() | Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
0067         topLayout->addWidget(lab);
0068 
0069         lab = new QLabel(realDetails, widget());
0070         lab->setWordWrap(true);
0071         lab->setTextInteractionFlags(lab->textInteractionFlags() | Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
0072         topLayout->addWidget(lab);
0073     }
0074 };
0075 
0076 KCModule *KCModuleLoader::loadModule(const KPluginMetaData &metaData, QWidget *parent, const QVariantList &args, const std::shared_ptr<QQmlEngine> &eng)
0077 {
0078     if (!KAuthorized::authorizeControlModule(metaData.pluginId())) {
0079         return new KCMError(i18n("The module %1 is disabled.", metaData.pluginId()), i18n("The module has been disabled by the system administrator."), parent);
0080     }
0081 
0082     const auto qmlKcm = KQuickConfigModuleLoader::loadModule(metaData, parent, args, eng).plugin;
0083     if (qmlKcm) {
0084         if (!qmlKcm->mainUi()) {
0085             return new KCMError(i18n("Error loading QML file."), qmlKcm->errorString(), parent);
0086         }
0087         qCDebug(KCMUTILS_LOG) << "loaded KCM" << metaData.fileName();
0088         return new KCModuleQml(qmlKcm, parent);
0089     }
0090 
0091     const QVariantList pluginArgs = QVariantList(args) << metaData.rawData().value(QLatin1String("X-KDE-KCM-Args")).toArray().toVariantList();
0092     const auto kcmoduleResult = KPluginFactory::instantiatePlugin<KCModule>(metaData, parent, pluginArgs);
0093 
0094     if (kcmoduleResult) {
0095         qCDebug(KCMUTILS_LOG) << "loaded KCM" << metaData.fileName();
0096         return kcmoduleResult.plugin;
0097     }
0098 
0099     return new KCMError(QString(), kcmoduleResult.errorString, parent);
0100 }
0101 
0102 #include "kcmoduleloader.moc"