1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <private/qqmlglobal_p.h>
41
42#include <QtQml/qqmlengine.h>
43#include <QtCore/qvariant.h>
44#include <QtCore/qstringlist.h>
45#include <QtCore/qdebug.h>
46#include <QtCore/QCoreApplication>
47
48QT_BEGIN_NAMESPACE
49
50QQmlValueTypeProvider::QQmlValueTypeProvider()
51 : next(nullptr)
52{
53}
54
55QQmlValueTypeProvider::~QQmlValueTypeProvider()
56{
57 QQml_removeValueTypeProvider(this);
58}
59
60const QMetaObject *QQmlValueTypeProvider::metaObjectForMetaType(int type)
61{
62 QQmlValueTypeProvider *p = this;
63 do {
64 if (const QMetaObject *mo = p->getMetaObjectForMetaType(type))
65 return mo;
66 } while ((p = p->next));
67
68 return nullptr;
69}
70
71bool QQmlValueTypeProvider::initValueType(int type, QVariant& dst)
72{
73 QQmlValueTypeProvider *p = this;
74 do {
75 if (p->init(type, dst))
76 return true;
77 } while ((p = p->next));
78
79 return false;
80}
81
82QVariant QQmlValueTypeProvider::createValueType(int type, int argc, const void *argv[])
83{
84 QVariant v;
85
86 QQmlValueTypeProvider *p = this;
87 do {
88 if (p->create(type, argc, argv, &v))
89 return v;
90 } while ((p = p->next));
91
92 return QVariant();
93}
94
95bool QQmlValueTypeProvider::createValueFromString(int type, const QString &s, void *data, size_t n)
96{
97 Q_ASSERT(data);
98
99 QQmlValueTypeProvider *p = this;
100 do {
101 if (p->createFromString(type, s, data, n))
102 return true;
103 } while ((p = p->next));
104
105 return false;
106}
107
108bool QQmlValueTypeProvider::createStringFromValue(int type, const void *data, QString *s)
109{
110 Q_ASSERT(data);
111 Q_ASSERT(s);
112
113 QQmlValueTypeProvider *p = this;
114 do {
115 if (p->createStringFrom(type, data, s))
116 return true;
117 } while ((p = p->next));
118
119 return false;
120}
121
122QVariant QQmlValueTypeProvider::createVariantFromString(const QString &s)
123{
124 QVariant v;
125
126 QQmlValueTypeProvider *p = this;
127 do {
128 if (p->variantFromString(s, &v))
129 return v;
130 } while ((p = p->next));
131
132 // Return a variant containing the string itself
133 return QVariant(s);
134}
135
136QVariant QQmlValueTypeProvider::createVariantFromString(int type, const QString &s, bool *ok)
137{
138 QVariant v;
139
140 QQmlValueTypeProvider *p = this;
141 do {
142 if (p->variantFromString(type, s, &v)) {
143 if (ok) *ok = true;
144 return v;
145 }
146 } while ((p = p->next));
147
148 if (ok) *ok = false;
149 return QVariant();
150}
151
152QVariant QQmlValueTypeProvider::createVariantFromJsObject(int type, const QV4::Value &obj,
153 QV4::ExecutionEngine *e, bool *ok)
154{
155 QVariant v;
156
157 QQmlValueTypeProvider *p = this;
158 do {
159 if (p->variantFromJsObject(type, obj, e, &v)) {
160 if (ok) *ok = true;
161 return v;
162 }
163 } while ((p = p->next));
164
165 if (ok) *ok = false;
166 return QVariant();
167}
168
169bool QQmlValueTypeProvider::equalValueType(int type, const void *lhs, const QVariant& rhs)
170{
171 Q_ASSERT(lhs);
172
173 QQmlValueTypeProvider *p = this;
174 do {
175 if (p->equal(type, lhs, rhs))
176 return true;
177 } while ((p = p->next));
178
179 return false;
180}
181
182bool QQmlValueTypeProvider::storeValueType(int type, const void *src, void *dst, size_t dstSize)
183{
184 Q_ASSERT(src);
185 Q_ASSERT(dst);
186
187 QQmlValueTypeProvider *p = this;
188 do {
189 if (p->store(type, src, dst, dstSize))
190 return true;
191 } while ((p = p->next));
192
193 return false;
194}
195
196bool QQmlValueTypeProvider::readValueType(const QVariant& src, void *dst, int dstType)
197{
198 Q_ASSERT(dst);
199
200 QQmlValueTypeProvider *p = this;
201 do {
202 if (p->read(src, dst, dstType))
203 return true;
204 } while ((p = p->next));
205
206 return false;
207}
208
209bool QQmlValueTypeProvider::writeValueType(int type, const void *src, QVariant& dst)
210{
211 Q_ASSERT(src);
212
213 QQmlValueTypeProvider *p = this;
214 do {
215 if (p->write(type, src, dst))
216 return true;
217 } while ((p = p->next));
218
219 return false;
220}
221
222const QMetaObject *QQmlValueTypeProvider::getMetaObjectForMetaType(int) { return nullptr; }
223bool QQmlValueTypeProvider::init(int, QVariant&) { return false; }
224bool QQmlValueTypeProvider::create(int, int, const void *[], QVariant *) { return false; }
225bool QQmlValueTypeProvider::createFromString(int, const QString &, void *, size_t) { return false; }
226bool QQmlValueTypeProvider::createStringFrom(int, const void *, QString *) { return false; }
227bool QQmlValueTypeProvider::variantFromString(const QString &, QVariant *) { return false; }
228bool QQmlValueTypeProvider::variantFromString(int, const QString &, QVariant *) { return false; }
229bool QQmlValueTypeProvider::variantFromJsObject(int, const QV4::Value &, QV4::ExecutionEngine *, QVariant *) { return false; }
230bool QQmlValueTypeProvider::equal(int, const void *, const QVariant&) { return false; }
231bool QQmlValueTypeProvider::store(int, const void *, void *, size_t) { return false; }
232bool QQmlValueTypeProvider::read(const QVariant&, void *, int) { return false; }
233bool QQmlValueTypeProvider::write(int, const void *, QVariant&) { return false; }
234
235struct ValueTypeProviderList {
236 QQmlValueTypeProvider nullProvider;
237 QQmlValueTypeProvider *head = &nullProvider;
238};
239
240Q_GLOBAL_STATIC(ValueTypeProviderList, valueTypeProviders)
241
242Q_QML_PRIVATE_EXPORT void QQml_addValueTypeProvider(QQmlValueTypeProvider *newProvider)
243{
244 if (ValueTypeProviderList *providers = valueTypeProviders()) {
245 newProvider->next = providers->head;
246 providers->head = newProvider;
247 }
248}
249
250Q_QML_PRIVATE_EXPORT void QQml_removeValueTypeProvider(QQmlValueTypeProvider *oldProvider)
251{
252 if (ValueTypeProviderList *providers = valueTypeProviders()) {
253 QQmlValueTypeProvider *prev = providers->head;
254 if (prev == oldProvider) {
255 providers->head = oldProvider->next;
256 return;
257 }
258
259 // singly-linked list removal
260 for (; prev; prev = prev->next) {
261 if (prev->next != oldProvider)
262 continue; // this is not the provider you're looking for
263 prev->next = oldProvider->next;
264 return;
265 }
266
267 qWarning(msg: "QQml_removeValueTypeProvider: was asked to remove provider %p but it was not found", oldProvider);
268 }
269}
270
271Q_AUTOTEST_EXPORT QQmlValueTypeProvider *QQml_valueTypeProvider()
272{
273 if (ValueTypeProviderList *providers = valueTypeProviders())
274 return providers->head;
275 return nullptr;
276}
277
278QQmlColorProvider::~QQmlColorProvider() {}
279QVariant QQmlColorProvider::colorFromString(const QString &, bool *ok) { if (ok) *ok = false; return QVariant(); }
280unsigned QQmlColorProvider::rgbaFromString(const QString &, bool *ok) { if (ok) *ok = false; return 0; }
281QVariant QQmlColorProvider::fromRgbF(double, double, double, double) { return QVariant(); }
282QVariant QQmlColorProvider::fromHslF(double, double, double, double) { return QVariant(); }
283QVariant QQmlColorProvider::fromHsvF(double, double, double, double) { return QVariant(); }
284QVariant QQmlColorProvider::lighter(const QVariant &, qreal) { return QVariant(); }
285QVariant QQmlColorProvider::darker(const QVariant &, qreal) { return QVariant(); }
286QVariant QQmlColorProvider::tint(const QVariant &, const QVariant &) { return QVariant(); }
287
288static QQmlColorProvider *colorProvider = nullptr;
289
290Q_QML_PRIVATE_EXPORT QQmlColorProvider *QQml_setColorProvider(QQmlColorProvider *newProvider)
291{
292 QQmlColorProvider *old = colorProvider;
293 colorProvider = newProvider;
294 return old;
295}
296
297static QQmlColorProvider **getColorProvider(void)
298{
299 if (colorProvider == nullptr) {
300 qWarning() << "Warning: QQml_colorProvider: no color provider has been set!";
301 static QQmlColorProvider nullColorProvider;
302 colorProvider = &nullColorProvider;
303 }
304
305 return &colorProvider;
306}
307
308Q_AUTOTEST_EXPORT QQmlColorProvider *QQml_colorProvider(void)
309{
310 static QQmlColorProvider **providerPtr = getColorProvider();
311 return *providerPtr;
312}
313
314
315QQmlGuiProvider::~QQmlGuiProvider() {}
316QObject *QQmlGuiProvider::application(QObject *) { return new QQmlApplication(); }
317QStringList QQmlGuiProvider::fontFamilies() { return QStringList(); }
318bool QQmlGuiProvider::openUrlExternally(QUrl &) { return false; }
319
320QObject *QQmlGuiProvider::inputMethod()
321{
322 // We don't have any input method code by default
323 QObject *o = new QObject();
324 o->setObjectName(QStringLiteral("No inputMethod available"));
325 QQmlEngine::setObjectOwnership(o, QQmlEngine::JavaScriptOwnership);
326 return o;
327}
328
329QObject *QQmlGuiProvider::styleHints()
330{
331 QObject *o = new QObject();
332 o->setObjectName(QStringLiteral("No styleHints available"));
333 QQmlEngine::setObjectOwnership(o, QQmlEngine::JavaScriptOwnership);
334 return o;
335}
336
337QString QQmlGuiProvider::pluginName() const { return QString(); }
338
339static QQmlGuiProvider *guiProvider = nullptr;
340
341Q_QML_PRIVATE_EXPORT QQmlGuiProvider *QQml_setGuiProvider(QQmlGuiProvider *newProvider)
342{
343 QQmlGuiProvider *old = guiProvider;
344 guiProvider = newProvider;
345 return old;
346}
347
348static QQmlGuiProvider **getGuiProvider(void)
349{
350 if (guiProvider == nullptr) {
351 static QQmlGuiProvider nullGuiProvider; //Still provides an application with no GUI support
352 guiProvider = &nullGuiProvider;
353 }
354
355 return &guiProvider;
356}
357
358Q_AUTOTEST_EXPORT QQmlGuiProvider *QQml_guiProvider(void)
359{
360 static QQmlGuiProvider **providerPtr = getGuiProvider();
361 return *providerPtr;
362}
363
364//Docs in qqmlengine.cpp
365QQmlApplication::QQmlApplication(QObject *parent)
366 : QObject(*(new QQmlApplicationPrivate),parent)
367{
368 connect(sender: QCoreApplication::instance(), SIGNAL(aboutToQuit()),
369 receiver: this, SIGNAL(aboutToQuit()));
370 connect(sender: QCoreApplication::instance(), SIGNAL(applicationNameChanged()),
371 receiver: this, SIGNAL(nameChanged()));
372 connect(sender: QCoreApplication::instance(), SIGNAL(applicationVersionChanged()),
373 receiver: this, SIGNAL(versionChanged()));
374 connect(sender: QCoreApplication::instance(), SIGNAL(organizationNameChanged()),
375 receiver: this, SIGNAL(organizationChanged()));
376 connect(sender: QCoreApplication::instance(), SIGNAL(organizationDomainChanged()),
377 receiver: this, SIGNAL(domainChanged()));
378}
379
380QQmlApplication::QQmlApplication(QQmlApplicationPrivate &dd, QObject *parent)
381 : QObject(dd, parent)
382{
383 connect(sender: QCoreApplication::instance(), SIGNAL(aboutToQuit()),
384 receiver: this, SIGNAL(aboutToQuit()));
385 connect(sender: QCoreApplication::instance(), SIGNAL(applicationNameChanged()),
386 receiver: this, SIGNAL(nameChanged()));
387 connect(sender: QCoreApplication::instance(), SIGNAL(applicationVersionChanged()),
388 receiver: this, SIGNAL(versionChanged()));
389 connect(sender: QCoreApplication::instance(), SIGNAL(organizationNameChanged()),
390 receiver: this, SIGNAL(organizationChanged()));
391 connect(sender: QCoreApplication::instance(), SIGNAL(organizationDomainChanged()),
392 receiver: this, SIGNAL(domainChanged()));
393}
394
395QStringList QQmlApplication::args()
396{
397 Q_D(QQmlApplication);
398 if (!d->argsInit) {
399 d->argsInit = true;
400 d->args = QCoreApplication::arguments();
401 }
402 return d->args;
403}
404
405QString QQmlApplication::name() const
406{
407 return QCoreApplication::instance()->applicationName();
408}
409
410QString QQmlApplication::version() const
411{
412 return QCoreApplication::instance()->applicationVersion();
413}
414
415QString QQmlApplication::organization() const
416{
417 return QCoreApplication::instance()->organizationName();
418}
419
420QString QQmlApplication::domain() const
421{
422 return QCoreApplication::instance()->organizationDomain();
423}
424
425void QQmlApplication::setName(const QString &arg)
426{
427 QCoreApplication::instance()->setApplicationName(arg);
428}
429
430void QQmlApplication::setVersion(const QString &arg)
431{
432 QCoreApplication::instance()->setApplicationVersion(arg);
433}
434
435void QQmlApplication::setOrganization(const QString &arg)
436{
437 QCoreApplication::instance()->setOrganizationName(arg);
438}
439
440void QQmlApplication::setDomain(const QString &arg)
441{
442 QCoreApplication::instance()->setOrganizationDomain(arg);
443}
444
445QT_END_NAMESPACE
446
447#include "moc_qqmlglobal_p.cpp"
448

source code of qtdeclarative/src/qml/qml/qqmlglobal.cpp