File indexing completed on 2024-05-05 16:07:09

0001 /*
0002     SPDX-FileCopyrightText: 2011 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kdeclarative.h"
0008 #include "private/kdeclarative_p.h"
0009 #include "private/kioaccessmanagerfactory_p.h"
0010 #include "qmlobject.h"
0011 
0012 #include <QCoreApplication>
0013 #include <QQmlContext>
0014 #include <QQmlDebuggingEnabler>
0015 
0016 #include <KConfigGroup>
0017 #include <KQuickIconProvider>
0018 #include <KSharedConfig>
0019 
0020 namespace KDeclarative
0021 {
0022 QStringList KDeclarativePrivate::s_runtimePlatform;
0023 
0024 KDeclarativePrivate::KDeclarativePrivate()
0025     : contextObj(nullptr)
0026 {
0027 }
0028 
0029 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 100)
0030 KDeclarative::KDeclarative()
0031     : d(new KDeclarativePrivate)
0032 {
0033 }
0034 #endif
0035 
0036 KDeclarative::~KDeclarative()
0037 {
0038     if (d->declarativeEngine) {
0039         delete d->declarativeEngine->networkAccessManagerFactory();
0040         d->declarativeEngine->setNetworkAccessManagerFactory(nullptr);
0041     }
0042     delete d;
0043 }
0044 
0045 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 100)
0046 void KDeclarative::setDeclarativeEngine(QQmlEngine *engine)
0047 {
0048     if (d->declarativeEngine.data() == engine) {
0049         return;
0050     }
0051     d->declarativeEngine = engine;
0052 }
0053 
0054 QQmlEngine *KDeclarative::declarativeEngine() const
0055 {
0056     return d->declarativeEngine.data();
0057 }
0058 #endif
0059 
0060 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 0)
0061 void KDeclarative::initialize()
0062 {
0063 }
0064 #endif
0065 
0066 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 45)
0067 void KDeclarative::setupBindings()
0068 {
0069     setupContext();
0070     setupEngine(d->declarativeEngine.data());
0071 }
0072 #endif
0073 
0074 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
0075 void KDeclarative::setupContext()
0076 {
0077     /*Create a context object for the root qml context.
0078       in this way we can register global functions, in this case the i18n() family*/
0079     if (!d->contextObj) {
0080         d->contextObj = new KLocalizedContext(d->declarativeEngine.data());
0081     }
0082 
0083     // If the engine is in a qmlObject take the qmlObject rootContext instead of the engine one.
0084     if (d->qmlObj) {
0085         d->qmlObj->rootContext()->setContextObject(d->contextObj);
0086     } else {
0087         d->declarativeEngine.data()->rootContext()->setContextObject(d->contextObj);
0088     }
0089 
0090     if (!d->translationDomain.isNull()) {
0091         d->contextObj->setTranslationDomain(d->translationDomain);
0092     }
0093 }
0094 #endif
0095 
0096 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 98)
0097 void KDeclarative::setupEngine(QQmlEngine *engine)
0098 {
0099 #ifndef Q_OS_ANDROID
0100     // get rid of stock network access manager factory
0101     QQmlNetworkAccessManagerFactory *factory = engine->networkAccessManagerFactory();
0102     engine->setNetworkAccessManagerFactory(nullptr);
0103     delete factory;
0104     engine->setNetworkAccessManagerFactory(new KIOAccessManagerFactory());
0105 #endif
0106 
0107     /* Tell the engine to search for platform-specific imports first
0108        (so it will "win" in import name resolution).
0109        addImportPath adds the path at the beginning, so to honour user's
0110        paths we need to traverse the list in reverse order */
0111     const QStringList pluginPathList = engine->importPathList();
0112 
0113     const QString target = componentsTarget();
0114     if (target != defaultComponentsTarget()) {
0115         const QStringList paths = pluginPathList;
0116         QStringListIterator it(paths);
0117         it.toBack();
0118         while (it.hasPrevious()) {
0119             QString path = it.previous();
0120             path = path.left(path.lastIndexOf(QLatin1Char('/')));
0121             engine->addImportPath(path + QStringLiteral("/platformqml/") + target);
0122         }
0123     }
0124 
0125     // setup ImageProvider for KDE icons
0126     engine->addImageProvider(QStringLiteral("icon"), new KQuickIconProvider);
0127 }
0128 #endif
0129 
0130 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
0131 void KDeclarative::setTranslationDomain(const QString &translationDomain)
0132 {
0133     d->translationDomain = translationDomain;
0134     if (d->contextObj) {
0135         d->contextObj->setTranslationDomain(d->translationDomain);
0136     }
0137 }
0138 #endif
0139 
0140 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
0141 QString KDeclarative::translationDomain() const
0142 {
0143     return d->translationDomain;
0144 }
0145 #endif
0146 
0147 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 98)
0148 void KDeclarative::setupQmlJsDebugger()
0149 {
0150 #if QT_CONFIG(qml_debug)
0151     if (QCoreApplication::arguments().contains(QLatin1String("-qmljsdebugger"))) {
0152         QQmlDebuggingEnabler enabler;
0153     }
0154 #endif
0155 }
0156 
0157 QString KDeclarative::defaultComponentsTarget()
0158 {
0159     return QLatin1String("desktop");
0160 }
0161 
0162 QString KDeclarative::componentsTarget()
0163 {
0164     const QStringList platform = runtimePlatform();
0165     if (platform.isEmpty()) {
0166         return defaultComponentsTarget();
0167     }
0168 
0169     return platform.last();
0170 }
0171 
0172 QStringList KDeclarative::runtimePlatform()
0173 {
0174     if (KDeclarativePrivate::s_runtimePlatform.isEmpty()) {
0175         const QString env = QString::fromLocal8Bit(getenv("PLASMA_PLATFORM"));
0176         KDeclarativePrivate::s_runtimePlatform = QStringList(env.split(QLatin1Char(':'), Qt::SkipEmptyParts));
0177         if (KDeclarativePrivate::s_runtimePlatform.isEmpty()) {
0178             KConfigGroup cg(KSharedConfig::openConfig(), "General");
0179             KDeclarativePrivate::s_runtimePlatform = cg.readEntry(QStringLiteral("runtimePlatform"), KDeclarativePrivate::s_runtimePlatform);
0180         }
0181     }
0182 
0183     return KDeclarativePrivate::s_runtimePlatform;
0184 }
0185 
0186 void KDeclarative::setRuntimePlatform(const QStringList &platform)
0187 {
0188     KDeclarativePrivate::s_runtimePlatform = platform;
0189 }
0190 #endif
0191 }