diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2024-03-27 14:27:11 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2024-04-03 20:37:13 +0200 |
commit | aa2e2333376cefd1c7b8c3931097eb6dd4b96dd9 (patch) | |
tree | 1a1729fe2d684b3ed5f1e74178e7ad1418a8d271 /src/qml/qml | |
parent | 92f588ba1e7c2f14b8d57f1da82c78d9c5b8cb98 (diff) |
QtQml: Resolve preferred paths before setQmldirContent
We want the content from the preferred qmldir to be used. This way we
can omit all the details from the original qmldir.
Task-number: QTBUG-111763
Change-Id: I3a0b3db0207e919c962c98d79f9f200aaf3cad96
Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/qml')
-rw-r--r-- | src/qml/qml/qqmlimport.cpp | 46 | ||||
-rw-r--r-- | src/qml/qml/qqmlimport_p.h | 2 | ||||
-rw-r--r-- | src/qml/qml/qqmltypeloaderqmldircontent_p.h | 7 |
3 files changed, 41 insertions, 14 deletions
diff --git a/src/qml/qml/qqmlimport.cpp b/src/qml/qml/qqmlimport.cpp index 99e31fcfe8..1e205be6cb 100644 --- a/src/qml/qml/qqmlimport.cpp +++ b/src/qml/qml/qqmlimport.cpp @@ -489,18 +489,8 @@ bool QQmlImportInstance::setQmldirContent(const QString &resolvedUrl, const QQmlTypeLoaderQmldirContent &qmldir, QQmlImportNamespace *nameSpace, QList<QQmlError> *errors) { - - const QString preferredPath = qmldir.preferredPath(); - if (preferredPath.isEmpty()) { - Q_ASSERT(resolvedUrl.endsWith(Slash)); - url = resolvedUrl; - } else { - Q_ASSERT(preferredPath.endsWith(Slash)); - if (preferredPath.startsWith(u':')) - url = QStringLiteral("qrc") + preferredPath; - else - url = QUrl::fromLocalFile(preferredPath).toString(); - } + Q_ASSERT(resolvedUrl.endsWith(Slash)); + url = resolvedUrl; qmlDirComponents = qmldir.components(); @@ -934,6 +924,23 @@ QTypeRevision QQmlImports::importExtension( return importer.importPlugins(); } +QString QQmlImports::redirectQmldirContent( + QQmlTypeLoader *typeLoader, QQmlTypeLoaderQmldirContent *qmldir) +{ + const QString preferredPath = qmldir->preferredPath(); + const QString url = preferredPath.startsWith(u':') + ? QStringLiteral("qrc") + preferredPath + : QUrl::fromLocalFile(preferredPath).toString(); + + QQmlTypeLoaderQmldirContent redirected + = typeLoader->qmldirContent(url + QLatin1String("qmldir")); + + // Ignore errors: If the qmldir doesn't exist, stick to the old one. + if (redirected.hasContent() && !redirected.hasError()) + *qmldir = std::move(redirected); + return url; +} + bool QQmlImports::getQmldirContent( QQmlTypeLoader *typeLoader, const QString &qmldirIdentifier, const QString &uri, QQmlTypeLoaderQmldirContent *qmldir, QList<QQmlError> *errors) @@ -1181,7 +1188,11 @@ QTypeRevision QQmlImports::addLibraryImport( if (!version.isValid()) return QTypeRevision(); - if (!inserted->setQmldirContent(qmldirUrl, qmldir, nameSpace, errors)) + const QString resolvedUrl = qmldir.hasRedirection() + ? redirectQmldirContent(typeLoader, &qmldir) + : qmldirUrl; + + if (!inserted->setQmldirContent(resolvedUrl, qmldir, nameSpace, errors)) return QTypeRevision(); } } @@ -1360,6 +1371,9 @@ QTypeRevision QQmlImports::addFileImport( if (!version.isValid()) return QTypeRevision(); + if (qmldir.hasRedirection()) + url = redirectQmldirContent(typeLoader, &qmldir); + if (!inserted->setQmldirContent(url, qmldir, nameSpace, errors)) return QTypeRevision(); @@ -1399,7 +1413,11 @@ QTypeRevision QQmlImports::updateQmldirContent( if (!version.isValid()) return QTypeRevision(); - if (import->setQmldirContent(qmldirUrl, qmldir, nameSpace, errors)) { + const QString resolvedUrl = qmldir.hasRedirection() + ? redirectQmldirContent(typeLoader, &qmldir) + : qmldirUrl; + + if (import->setQmldirContent(resolvedUrl, qmldir, nameSpace, errors)) { if (import->qmlDirComponents.isEmpty() && import->qmlDirScripts.isEmpty()) { // The implicit import qmldir can be empty, and plugins have no extra versions if (uri != QLatin1String(".") && !QQmlMetaType::matchingModuleVersion(uri, version).isValid()) { diff --git a/src/qml/qml/qqmlimport_p.h b/src/qml/qml/qqmlimport_p.h index 7738d20891..ef9b4b3422 100644 --- a/src/qml/qml/qqmlimport_p.h +++ b/src/qml/qml/qqmlimport_p.h @@ -235,6 +235,8 @@ private: QQmlTypeLoader *typeLoader, const QString &uri, QTypeRevision version, const QQmlTypeLoaderQmldirContent *qmldir, QList<QQmlError> *errors); + QString redirectQmldirContent(QQmlTypeLoader *typeLoader, QQmlTypeLoaderQmldirContent *qmldir); + bool getQmldirContent( QQmlTypeLoader *typeLoader, const QString &qmldirIdentifier, const QString &uri, QQmlTypeLoaderQmldirContent *qmldir, QList<QQmlError> *errors); diff --git a/src/qml/qml/qqmltypeloaderqmldircontent_p.h b/src/qml/qml/qqmltypeloaderqmldircontent_p.h index 5749e04eaf..4d1f1967a1 100644 --- a/src/qml/qml/qqmltypeloaderqmldircontent_p.h +++ b/src/qml/qml/qqmltypeloaderqmldircontent_p.h @@ -47,6 +47,13 @@ public: QString qmldirLocation() const { return m_location; } QString preferredPath() const { return m_parser.preferredPath(); } + bool hasRedirection() const + { + const QString preferred = preferredPath(); + return !preferred.isEmpty() + && preferred != QStringView(m_location).chopped(strlen("qmldir")); + } + bool designerSupported() const { return m_parser.designerSupported(); } bool hasTypeInfo() const { return !m_parser.typeInfos().isEmpty(); } |