Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmlimport_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQMLIMPORT_P_H
5#define QQMLIMPORT_P_H
6
7#include <QtCore/qurl.h>
8#include <QtCore/qcoreapplication.h>
9#include <QtCore/qloggingcategory.h>
10#include <QtCore/qset.h>
11#include <QtCore/qstringlist.h>
12#include <QtQml/qqmlengine.h>
13#include <QtQml/qqmlerror.h>
14#include <QtQml/qqmlfile.h>
15#include <private/qqmldirparser_p.h>
16#include <private/qqmltype_p.h>
17#include <private/qstringhash_p.h>
18#include <private/qfieldlist_p.h>
19
20//
21// W A R N I N G
22// -------------
23//
24// This file is not part of the Qt API. It exists purely as an
25// implementation detail. This header file may change from version to
26// version without notice, or even be removed.
27//
28// We mean it.
29//
30
32
34class QQmlEngine;
35class QDir;
38class QQmlTypeLoader;
40class QTypeRevision;
41
43
47
49{
51 Lowest = std::numeric_limits<quint8>::max(),
54 };
55
56 QString uri; // e.g. QtQuick
57 QString url; // the base path of the import
58 QTypeRevision version; // the version imported
59
60 bool isLibrary; // true means that this is not a file import
61
62 // not covered by precedence. You can set a component as implicitly imported after the fact.
63 bool implicitlyImported = false;
64 bool isInlineComponent = false;
65
67
68 QQmlDirComponents qmlDirComponents; // a copy of the components listed in the qmldir
69 QQmlDirScripts qmlDirScripts; // a copy of the scripts in the qmldir
70
72 QQmlImportNamespace *nameSpace, QList<QQmlError> *errors);
73
74 static QQmlDirScripts getVersionedScripts(const QQmlDirScripts &qmldirscripts,
76
77 bool resolveType(QQmlTypeLoader *typeLoader, const QHashedStringRef &type,
78 QTypeRevision *version_return, QQmlType* type_return,
79 const QString *base = nullptr, bool *typeRecursionDetected = nullptr,
82 QList<QQmlError> *errors = nullptr) const;
83};
84
86{
87public:
90
91 QList<QQmlImportInstance *> imports;
92
93 QQmlImportInstance *findImport(const QString &uri) const;
94
95 bool resolveType(QQmlTypeLoader *typeLoader, const QHashedStringRef& type,
96 QTypeRevision *version_return, QQmlType* type_return,
97 const QString *base = nullptr, QList<QQmlError> *errors = nullptr,
99 bool *typeRecursionDeteced = nullptr);
100
101 // Prefix when used as a qualified import. Otherwise empty.
103
104 // Used by QQmlImports::m_qualifiedSets
105 // set to this in unqualifiedSet to indicate that the lists of imports needs
106 // to be sorted when an inline component import was added
107 // We can't use flag pointer, as that does not work with QFieldList
109 bool needsSorting() const { return nextNamespace == this; }
111 {
112 Q_ASSERT(nextNamespace == this || nextNamespace == nullptr);
113 nextNamespace = needsSorting ? this : nullptr;
114 }
115};
116
117class Q_QML_EXPORT QQmlImports final : public QQmlRefCounted<QQmlImports>
118{
119 Q_DISABLE_COPY_MOVE(QQmlImports)
120public:
122
124 ImportNoFlag = 0x0,
125 ImportIncomplete = 0x1,
126 };
127 Q_DECLARE_FLAGS(ImportFlags, ImportFlag)
128
129 QQmlImports() = default;
131 {
132 while (QQmlImportNamespace *ns = m_qualifiedSets.takeFirst())
133 delete ns;
134 }
135
136 void setBaseUrl(const QUrl &url, const QString &urlString = QString());
137 QUrl baseUrl() const { return m_baseUrl; }
138
139 bool resolveType(
140 QQmlTypeLoader *typeLoader, const QHashedStringRef &type, QQmlType *type_return,
141 QTypeRevision *version_return, QQmlImportNamespace **ns_return,
142 QList<QQmlError> *errors = nullptr,
144 bool *typeRecursionDetected = nullptr) const;
145
147 QQmlTypeLoader *typeLoader, QString *localQmldir, QList<QQmlError> *errors)
148 {
149 Q_ASSERT(errors);
150 qCDebug(lcQmlImport) << "addImplicitImport:" << qPrintable(baseUrl().toString());
151
152 const ImportFlags flags =
153 ImportFlags(!isLocal(baseUrl()) ? ImportIncomplete : ImportNoFlag);
154 return addFileImport(
155 typeLoader, QLatin1String("."), QString(), QTypeRevision(), flags,
156 QQmlImportInstance::Implicit, localQmldir, errors);
157 }
158
159 bool addInlineComponentImport(
160 QQmlImportInstance *const importInstance, const QString &name, const QUrl importUrl);
161
162 QTypeRevision addFileImport(
163 QQmlTypeLoader *typeLoader, const QString &uri, const QString &prefix,
164 QTypeRevision version, ImportFlags flags, quint16 precedence, QString *localQmldir,
165 QList<QQmlError> *errors);
166
167 QTypeRevision addLibraryImport(
168 QQmlTypeLoader *typeLoader, const QString &uri, const QString &prefix,
169 QTypeRevision version, const QString &qmldirIdentifier, const QString &qmldirUrl,
170 ImportFlags flags, quint16 precedence, QList<QQmlError> *errors);
171
172 QTypeRevision updateQmldirContent(
173 QQmlTypeLoader *typeLoader, const QString &uri, const QString &prefix,
174 const QString &qmldirIdentifier, const QString &qmldirUrl, QList<QQmlError> *errors);
175
176 void populateCache(QQmlTypeNameCache *cache) const;
177
184
185 QList<ScriptReference> resolvedScripts() const;
186
193
194 QList<CompositeSingletonReference> resolvedCompositeSingletons() const;
195
196 static QStringList completeQmldirPaths(
197 const QString &uri, const QStringList &basePaths, QTypeRevision version);
198
199 static QString versionString(QTypeRevision version, ImportVersion importVersion);
200
201 static bool isLocal(const QString &url)
202 {
203 return !QQmlFile::urlToLocalFileOrQrc(url).isEmpty();
204 }
205
206 static bool isLocal(const QUrl &url)
207 {
208 return !QQmlFile::urlToLocalFileOrQrc(url).isEmpty();
209 }
210
211 static QUrl urlFromLocalFileOrQrcOrUrl(const QString &);
212
213 static void setDesignerSupportRequired(bool b);
214
215 static QTypeRevision validVersion(QTypeRevision version = QTypeRevision());
216
217private:
218 friend class QQmlImportDatabase;
219
220 QQmlImportNamespace *importNamespace(const QString &prefix);
221
222 bool resolveType(
223 QQmlTypeLoader *typeLoader, const QHashedStringRef &type, QTypeRevision *version_return,
224 QQmlType *type_return, QList<QQmlError> *errors,
225 QQmlType::RegistrationType registrationType,
226 bool *typeRecursionDetected = nullptr) const;
227
228 QQmlImportNamespace *findQualifiedNamespace(const QHashedStringRef &) const;
229
230 static QTypeRevision matchingQmldirVersion(
231 const QQmlTypeLoaderQmldirContent &qmldir, const QString &uri,
232 QTypeRevision version, QList<QQmlError> *errors);
233
234 QTypeRevision importExtension(
235 QQmlTypeLoader *typeLoader, const QString &uri, QTypeRevision version,
236 const QQmlTypeLoaderQmldirContent *qmldir, QList<QQmlError> *errors);
237
238 QString redirectQmldirContent(QQmlTypeLoader *typeLoader, QQmlTypeLoaderQmldirContent *qmldir);
239
240 bool getQmldirContent(
241 QQmlTypeLoader *typeLoader, const QString &qmldirIdentifier, const QString &uri,
242 QQmlTypeLoaderQmldirContent *qmldir, QList<QQmlError> *errors);
243
244 QString resolvedUri(const QString &dir_arg, QQmlImportDatabase *database);
245
246 QUrl m_baseUrl;
247 QString m_base;
248
249 // storage of data related to imports without a namespace
250 // TODO: This needs to be mutable because QQmlImportNamespace likes to sort itself on
251 // resolveType(). Therefore, QQmlImportNamespace::resolveType() is not const.
252 // There should be a better way to do this.
253 mutable QQmlImportNamespace m_unqualifiedset;
254
255 // storage of data related to imports with a namespace
256 QFieldList<QQmlImportNamespace, &QQmlImportNamespace::nextNamespace> m_qualifiedSets;
257};
258
259Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlImports::ImportFlags)
260
261class Q_QML_EXPORT QQmlImportDatabase
262{
264public:
266
271
278
280 ~QQmlImportDatabase() { clearDirCache(); }
281
282 bool removeDynamicPlugin(const QString &pluginId);
284
287 void addImportPath(const QString& dir);
288
289 QStringList pluginPathList() const { return filePluginPath; }
291
292 void addPluginPath(const QString& path);
293
294 template<typename Callback>
297 const Callback &callback);
298
299 static QTypeRevision lockModule(const QString &uri, const QString &typeNamespace,
300 QTypeRevision version, QList<QQmlError> *errors);
301
302private:
303 friend class QQmlImports;
304 friend class QQmlPluginImporter;
305
306 QString absoluteFilePath(const QString &path) const;
307 void clearDirCache();
308
309 struct QmldirCache {
310 QTypeRevision version;
311 QString qmldirFilePath;
312 QString qmldirPathUrl;
313 QmldirCache *next;
314 };
315 // Maps from an import to a linked list of qmldir info.
316 // Used in QQmlImports::locateQmldir()
317 QStringHash<QmldirCache *> qmldirCache;
318
319 // XXX thread
320 QStringList filePluginPath;
321 QStringList fileImportPath;
322
323 QSet<QString> modulesForWhichPluginsHaveBeenLoaded;
324 QSet<QString> initializedPlugins;
326};
327
328template<typename Callback>
330 const QString &uri, QTypeRevision version,
332{
333 // Check cache first
334
336 QmldirCache *cacheTail = nullptr;
337
338 QmldirCache **cachePtr = qmldirCache.value(uri);
339 QmldirCache *cacheHead = cachePtr ? *cachePtr : nullptr;
340 if (cacheHead) {
341 cacheTail = cacheHead;
342 do {
343 if (cacheTail->version == version) {
344 if (cacheTail->qmldirFilePath.isEmpty()) {
345 return cacheTail->qmldirPathUrl.isEmpty()
348 }
349 if (callback(cacheTail->qmldirFilePath, cacheTail->qmldirPathUrl))
350 return QmldirFound;
352 }
353 } while (cacheTail->next && (cacheTail = cacheTail->next));
354 }
355
356
357 // Do not try to construct the cache if it already had any entries for the URI.
358 // Otherwise we might duplicate cache entries.
360 return result;
361
362 const bool hasInterceptors = !engine->urlInterceptors().isEmpty();
363
364 // Interceptor might redirect remote files to local ones.
365 QStringList localImportPaths = importPathList(hasInterceptors ? LocalOrRemote : Local);
366
367 // Search local import paths for a matching version
369 uri, localImportPaths, version);
370
371 QString qmldirAbsoluteFilePath;
372 for (QString qmldirPath : qmlDirPaths) {
373 if (hasInterceptors) {
374 const QUrl intercepted = engine->interceptUrl(
377 qmldirPath = QQmlFile::urlToLocalFileOrQrc(intercepted);
379 && qmldirPath.isEmpty()
380 && !QQmlFile::isLocalFile(intercepted)) {
382 }
383 }
384
385 qmldirAbsoluteFilePath = absoluteFilePath(qmldirPath);
386 if (!qmldirAbsoluteFilePath.isEmpty()) {
387 QString url;
388 const QString absolutePath = qmldirAbsoluteFilePath.left(
389 qmldirAbsoluteFilePath.lastIndexOf(u'/') + 1);
390 if (absolutePath.at(0) == u':') {
392 } else {
394 // This handles the UNC path case as when the path is retrieved from the QUrl it
395 // will convert the host name from upper case to lower case. So the absoluteFilePath
396 // is changed at this point to make sure it will match later on in that case.
397 if (qmldirAbsoluteFilePath.startsWith(QStringLiteral("//"))) {
398 qmldirAbsoluteFilePath = QUrl::fromLocalFile(qmldirAbsoluteFilePath)
399 .toString(QUrl::RemoveScheme);
400 }
401 }
402
403 QmldirCache *cache = new QmldirCache;
404 cache->version = version;
405 cache->qmldirFilePath = qmldirAbsoluteFilePath;
406 cache->qmldirPathUrl = url;
407 cache->next = nullptr;
408 if (cacheTail)
409 cacheTail->next = cache;
410 else
411 qmldirCache.insert(uri, cache);
412 cacheTail = cache;
413
414 if (result != QmldirFound)
415 result = callback(qmldirAbsoluteFilePath, url) ? QmldirFound : QmldirRejected;
416
417 // Do not return here. Rather, construct the complete cache for this URI.
418 }
419 }
420
421 // Nothing found? Add an empty cache entry to signal that for further requests.
423 QmldirCache *cache = new QmldirCache;
424 cache->version = version;
425 cache->next = cacheHead;
427 // The actual value doesn't matter as long as it's not empty.
428 // We only use it to discern QmldirInterceptedToRemote from QmldirNotFound above.
429 cache->qmldirPathUrl = QStringLiteral("intercepted");
430 }
431 qmldirCache.insert(uri, cache);
432
433 if (result == QmldirNotFound) {
435 << "locateLocalQmldir:" << qPrintable(uri) << "module's qmldir file not found";
436 }
437 } else {
439 << "locateLocalQmldir:" << qPrintable(uri) << "module's qmldir found at"
440 << qmldirAbsoluteFilePath;
441 }
442
443 return result;
444}
445
446void qmlClearEnginePlugins();// For internal use by qmlClearRegisteredProperties
447
449
450#endif // QQMLIMPORT_P_H
451
\inmodule QtCore
Definition qdir.h:20
bool isEmpty() const noexcept
Definition qlist.h:401
\inmodule QtCore
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
QList< QQmlAbstractUrlInterceptor * > urlInterceptors() const
Returns the list of currently active URL interceptors.
QUrl interceptUrl(const QUrl &url, QQmlAbstractUrlInterceptor::DataType type) const
Run the current URL interceptors on the given url of the given type and return the result.
static bool isLocalFile(const QString &url)
Returns true if url is a local file that can be opened with \l{QFile}.
Definition qqmlfile.cpp:644
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to \l{QFile}.
Definition qqmlfile.cpp:742
The QQmlImportDatabase class manages the QML imports for a QQmlEngine.
void addPluginPath(const QString &path)
QStringList dynamicPlugins() const
QStringList importPathList(PathType type=LocalOrRemote) const
bool removeDynamicPlugin(const QString &pluginId)
void setPluginPathList(const QStringList &paths)
void addImportPath(const QString &dir)
QStringList pluginPathList() const
static QTypeRevision lockModule(const QString &uri, const QString &typeNamespace, QTypeRevision version, QList< QQmlError > *errors)
QQmlImportDatabase(QQmlEngine *)
LocalQmldirResult locateLocalQmldir(const QString &uri, QTypeRevision version, LocalQmldirSearchLocation location, const Callback &callback)
void setImportPathList(const QStringList &paths)
bool needsSorting() const
QQmlImportNamespace * nextNamespace
QQmlImportInstance * findImport(const QString &uri) const
QHashedString prefix
bool resolveType(QQmlTypeLoader *typeLoader, const QHashedStringRef &type, QTypeRevision *version_return, QQmlType *type_return, const QString *base=nullptr, QList< QQmlError > *errors=nullptr, QQmlType::RegistrationType registrationType=QQmlType::AnyRegistrationType, bool *typeRecursionDeteced=nullptr)
QList< QQmlImportInstance * > imports
void setNeedsSorting(bool needsSorting)
The QQmlImports class encapsulates one QML document's import statements.
QUrl baseUrl() const
static QUrl urlFromLocalFileOrQrcOrUrl(const QString &)
QTypeRevision addImplicitImport(QQmlTypeLoader *typeLoader, QString *localQmldir, QList< QQmlError > *errors)
static bool isLocal(const QString &url)
static QStringList completeQmldirPaths(const QString &uri, const QStringList &basePaths, QTypeRevision version)
Forms complete paths to a qmldir file, from a base URL, a module URI and version specification.
static bool isLocal(const QUrl &url)
The QQmlTypeLoader class abstracts loading files and their dependencies over the network.
@ AnyRegistrationType
Definition qqmltype_p.h:167
void insert(const K &, const T &)
T * value(const K &) const
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
\inmodule QtCore
\inmodule QtCore
Definition qurl.h:94
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3368
@ RemoveScheme
Definition qurl.h:105
#define this
Definition dialogs.cpp:9
qDeleteAll(list.begin(), list.end())
QCache< int, Employee > cache
[0]
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
#define Q_DECLARE_TR_FUNCTIONS(context)
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
#define qCDebug(category,...)
GLint location
GLboolean GLboolean GLboolean b
GLenum type
GLsizei const GLuint * paths
GLbitfield flags
GLuint name
GLsizei const GLchar *const * path
GLuint64EXT * result
[6]
const QLoggingCategory & lcQmlImport()
void qmlClearEnginePlugins()
const QLoggingCategory & lcQmlImport()
@ Unversioned
@ PartiallyVersioned
static QString absolutePath(const QString &path)
static QUrl resolvedUrl(const QUrl &url, const QQmlRefPointer< QQmlContextData > &context)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define qPrintable(string)
Definition qstring.h:1531
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
static QString absoluteFilePath(const Options *options, const QString &relativeFileName)
Definition main.cpp:1902
unsigned short quint16
Definition qtypes.h:48
unsigned char quint8
Definition qtypes.h:46
static const uint base
Definition qurlidna.cpp:20
QUrl url("example.com")
[constructor-url-reference]
QUrl baseUrl
QObject::connect nullptr
QString dir
[11]
char * toString(const MyType &t)
[31]
QJSEngine engine
[0]
QQmlDirComponents qmlDirComponents
bool resolveType(QQmlTypeLoader *typeLoader, const QHashedStringRef &type, QTypeRevision *version_return, QQmlType *type_return, const QString *base=nullptr, bool *typeRecursionDetected=nullptr, QQmlType::RegistrationType=QQmlType::AnyRegistrationType, QQmlImport::RecursionRestriction recursionRestriction=QQmlImport::PreventRecursion, QList< QQmlError > *errors=nullptr) const
bool setQmldirContent(const QString &resolvedUrl, const QQmlTypeLoaderQmldirContent &qmldir, QQmlImportNamespace *nameSpace, QList< QQmlError > *errors)
QQmlDirScripts qmlDirScripts
QTypeRevision version
static QQmlDirScripts getVersionedScripts(const QQmlDirScripts &qmldirscripts, QTypeRevision version)