Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlimportresolver.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 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// Qt-Security score:critical reason:data-parser
4
6
7#include <QtCore/qstringlist.h>
8#include <QtCore/qstringview.h>
9
11
13
14/*
15 Forms complete paths to a module, from a list of base paths,
16 a module URI and version specification.
17
18 For example, QtQml.Models 2.0:
19 - base/QtQml/Models.2.0
20 - base/QtQml.2.0/Models
21 - base/QtQml/Models.2
22 - base/QtQml.2/Models
23 - base/QtQml/Models
24*/
25QStringList qQmlResolveImportPaths(QStringView uri, const QStringList &basePaths,
26 QTypeRevision version)
27{
28 static const QLatin1Char Slash('/');
29 static const QLatin1Char Backslash('\\');
30
31 const QList<QStringView> parts = uri.split(u'.', Qt::SkipEmptyParts);
32
33 QStringList importPaths;
34 // fully & partially versioned parts + 1 unversioned for each base path
35 importPaths.reserve(2 * parts.size() + 1);
36
37 auto versionString = [](QTypeRevision version, ImportVersion mode)
38 {
39 if (mode == FullyVersioned) {
40 // extension with fully encoded version number (eg. MyModule.3.2)
41 return QString::fromLatin1(".%1.%2").arg(version.majorVersion())
42 .arg(version.minorVersion());
43 }
44 if (mode == PartiallyVersioned) {
45 // extension with encoded version major (eg. MyModule.3)
46 return QString::fromLatin1(".%1").arg(version.majorVersion());
47 }
48 // else extension without version number (eg. MyModule)
49 return QString();
50 };
51
52 auto joinStringRefs = [](const QList<QStringView> &refs, const QChar &sep) {
53 QString str;
54 for (auto it = refs.cbegin(); it != refs.cend(); ++it) {
55 if (it != refs.cbegin())
56 str += sep;
57 str += *it;
58 }
59 return str;
60 };
61
62 const ImportVersion initial = (version.hasMinorVersion())
64 : (version.hasMajorVersion() ? PartiallyVersioned : Unversioned);
65 for (int mode = initial; mode <= Unversioned; ++mode) {
66 const QString ver = versionString(version, ImportVersion(mode));
67
68 for (const QString &path : basePaths) {
69 QString dir = path;
70 if (!dir.endsWith(Slash) && !dir.endsWith(Backslash))
71 dir += Slash;
72
73 // append to the end
74 importPaths += dir + joinStringRefs(parts, Slash) + ver;
75
76 if (mode != Unversioned) {
77 // insert in the middle
78 for (int index = parts.size() - 2; index >= 0; --index) {
79 importPaths += dir + joinStringRefs(parts.mid(0, index + 1), Slash)
80 + ver + Slash
81 + joinStringRefs(parts.mid(index + 1), Slash);
82 }
83 }
84 }
85 }
86
87 return importPaths;
88}
89
90QT_END_NAMESPACE
Combined button and popup list for selecting options.
@ Unversioned
@ PartiallyVersioned
@ FullyVersioned
QStringList qQmlResolveImportPaths(QStringView uri, const QStringList &basePaths, QTypeRevision version)