KDeclarative

kdeclarative.cpp
1 /*
2  SPDX-FileCopyrightText: 2011 Marco Martin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "kdeclarative.h"
8 #include "private/kdeclarative_p.h"
9 #include "private/kioaccessmanagerfactory_p.h"
10 #include "qmlobject.h"
11 
12 #include <QCoreApplication>
13 #include <QQmlContext>
14 #include <QQmlDebuggingEnabler>
15 
16 #include <KConfigGroup>
17 #include <KQuickIconProvider>
18 #include <KSharedConfig>
19 
20 namespace KDeclarative
21 {
22 QStringList KDeclarativePrivate::s_runtimePlatform;
23 
24 KDeclarativePrivate::KDeclarativePrivate()
25  : contextObj(nullptr)
26 {
27 }
28 
29 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 100)
31  : d(new KDeclarativePrivate)
32 {
33 }
34 #endif
35 
36 KDeclarative::~KDeclarative()
37 {
38  if (d->declarativeEngine) {
39  delete d->declarativeEngine->networkAccessManagerFactory();
40  d->declarativeEngine->setNetworkAccessManagerFactory(nullptr);
41  }
42  delete d;
43 }
44 
45 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 100)
46 void KDeclarative::setDeclarativeEngine(QQmlEngine *engine)
47 {
48  if (d->declarativeEngine.data() == engine) {
49  return;
50  }
51  d->declarativeEngine = engine;
52 }
53 
54 QQmlEngine *KDeclarative::declarativeEngine() const
55 {
56  return d->declarativeEngine.data();
57 }
58 #endif
59 
60 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 0)
61 void KDeclarative::initialize()
62 {
63 }
64 #endif
65 
66 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 45)
67 void KDeclarative::setupBindings()
68 {
69  setupContext();
70  setupEngine(d->declarativeEngine.data());
71 }
72 #endif
73 
74 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
75 void KDeclarative::setupContext()
76 {
77  /*Create a context object for the root qml context.
78  in this way we can register global functions, in this case the i18n() family*/
79  if (!d->contextObj) {
80  d->contextObj = new KLocalizedContext(d->declarativeEngine.data());
81  }
82 
83  // If the engine is in a qmlObject take the qmlObject rootContext instead of the engine one.
84  if (d->qmlObj) {
85  d->qmlObj->rootContext()->setContextObject(d->contextObj);
86  } else {
87  d->declarativeEngine.data()->rootContext()->setContextObject(d->contextObj);
88  }
89 
90  if (!d->translationDomain.isNull()) {
91  d->contextObj->setTranslationDomain(d->translationDomain);
92  }
93 }
94 #endif
95 
96 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 98)
97 void KDeclarative::setupEngine(QQmlEngine *engine)
98 {
99 #ifndef Q_OS_ANDROID
100  // get rid of stock network access manager factory
102  engine->setNetworkAccessManagerFactory(nullptr);
103  delete factory;
104  engine->setNetworkAccessManagerFactory(new KIOAccessManagerFactory());
105 #endif
106 
107  /* Tell the engine to search for platform-specific imports first
108  (so it will "win" in import name resolution).
109  addImportPath adds the path at the beginning, so to honour user's
110  paths we need to traverse the list in reverse order */
111  const QStringList pluginPathList = engine->importPathList();
112 
113  const QString target = componentsTarget();
114  if (target != defaultComponentsTarget()) {
115  const QStringList paths = pluginPathList;
116  QStringListIterator it(paths);
117  it.toBack();
118  while (it.hasPrevious()) {
119  QString path = it.previous();
120  path = path.left(path.lastIndexOf(QLatin1Char('/')));
121  engine->addImportPath(path + QStringLiteral("/platformqml/") + target);
122  }
123  }
124 
125  // setup ImageProvider for KDE icons
126  engine->addImageProvider(QStringLiteral("icon"), new KQuickIconProvider);
127 }
128 #endif
129 
130 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
131 void KDeclarative::setTranslationDomain(const QString &translationDomain)
132 {
133  d->translationDomain = translationDomain;
134  if (d->contextObj) {
135  d->contextObj->setTranslationDomain(d->translationDomain);
136  }
137 }
138 #endif
139 
140 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 75)
141 QString KDeclarative::translationDomain() const
142 {
143  return d->translationDomain;
144 }
145 #endif
146 
147 #if KDECLARATIVE_BUILD_DEPRECATED_SINCE(5, 98)
148 void KDeclarative::setupQmlJsDebugger()
149 {
150 #if QT_CONFIG(qml_debug)
151  if (QCoreApplication::arguments().contains(QLatin1String("-qmljsdebugger"))) {
152  QQmlDebuggingEnabler enabler;
153  }
154 #endif
155 }
156 
157 QString KDeclarative::defaultComponentsTarget()
158 {
159  return QLatin1String("desktop");
160 }
161 
162 QString KDeclarative::componentsTarget()
163 {
164  const QStringList platform = runtimePlatform();
165  if (platform.isEmpty()) {
166  return defaultComponentsTarget();
167  }
168 
169  return platform.last();
170 }
171 
172 QStringList KDeclarative::runtimePlatform()
173 {
174  if (KDeclarativePrivate::s_runtimePlatform.isEmpty()) {
175  const QString env = QString::fromLocal8Bit(getenv("PLASMA_PLATFORM"));
176  KDeclarativePrivate::s_runtimePlatform = QStringList(env.split(QLatin1Char(':'), Qt::SkipEmptyParts));
177  if (KDeclarativePrivate::s_runtimePlatform.isEmpty()) {
178  KConfigGroup cg(KSharedConfig::openConfig(), "General");
179  KDeclarativePrivate::s_runtimePlatform = cg.readEntry(QStringLiteral("runtimePlatform"), KDeclarativePrivate::s_runtimePlatform);
180  }
181  }
182 
183  return KDeclarativePrivate::s_runtimePlatform;
184 }
185 
186 void KDeclarative::setRuntimePlatform(const QStringList &platform)
187 {
188  KDeclarativePrivate::s_runtimePlatform = platform;
189 }
190 #endif
191 }
QString readEntry(const char *key, const char *aDefault=nullptr) const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QQmlNetworkAccessManagerFactory * networkAccessManagerFactory() const const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString fromLocal8Bit(const char *str, int size)
SkipEmptyParts
void addImageProvider(const QString &providerId, QQmlImageProviderBase *provider)
bool isEmpty() const const
T & last()
QStringList importPathList() const const
void setNetworkAccessManagerFactory(QQmlNetworkAccessManagerFactory *factory)
void addImportPath(const QString &path)
QString left(int n) const const
QStringList arguments()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Dec 6 2022 04:04:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.