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
qqmllsmoduleexplorer.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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:significant reason:default
4
6
7#include <QtCore/qdiriterator.h>
8#include <QtCore/qfile.h>
9#include <QtCore/qmutex.h>
10#include <QtCore/qthread.h>
11
13
14using namespace Qt::StringLiterals;
15
16/*!
17\internal
18\class QQmlLSModuleExplorer
19\brief Searches import paths for QML modules on a background thread.
20
21Import paths passed to explore() are scanned for qmldir files one after the other. Modules are
22published as soon as they are found, so modulesIn() always returns the latest partial or complete
23result without waiting for the search to finish. Import paths that were already explored or are
24queued are not scanned again. cancel() stops the running search and drops the queued import paths,
25keeping the modules found so far.
26
27All functions can be called from any thread.
28*/
29
30static std::optional<QString> moduleNameFromQmldir(const QString &qmldirPath)
31{
32 static constexpr QLatin1String moduleKey = "module"_L1;
33 QFile qmldir(qmldirPath);
34 if (!qmldir.open(QIODevice::ReadOnly | QIODevice::Text))
35 return std::nullopt;
36 // Qt 6.12.0 ships with ~125 QML modules it seems, so we end up parsing at least
37 // that many qmldirs here. Most of them will never be imported and their QQmldirParser never
38 // be re-used, so do a manual grep instead of parsing via QQmldirParser.
39 while (!qmldir.atEnd()) {
40 const QByteArray lineData = qmldir.readLine().trimmed();
41 if (lineData.startsWith(moduleKey))
42 return QString::fromUtf8(lineData.mid(moduleKey.size())).trimmed();
43 }
44 return std::nullopt;
45}
46
47QQmlLSModuleExplorer::QQmlLSModuleExplorer()
48{
49}
50
51QQmlLSModuleExplorer::~QQmlLSModuleExplorer()
52{
53 {
54 QMutexLocker guard(&m_mutex);
55 m_stopRequested = true;
56 }
57 m_hasWork.wakeAll();
58 if (m_thread)
59 m_thread->wait();
60}
61
62void QQmlLSModuleExplorer::setEnabled(bool enabled)
63{
64 QMutexLocker guard(&m_mutex);
65 m_enabled = enabled;
66}
67
68void QQmlLSModuleExplorer::explore(const QStringList &importPaths)
69{
70 bool startedExploring = false;
71 {
72 QMutexLocker guard(&m_mutex);
73 if (!m_enabled)
74 return;
75 bool queuedAny = false;
76 for (const QString &importPath : importPaths) {
77 if (m_modulesPerImportPath.contains(importPath))
78 continue;
79 m_modulesPerImportPath.insert(importPath, {});
80 m_pendingImportPaths.push_back(importPath);
81 queuedAny = true;
82 }
83 if (!queuedAny)
84 return;
85
86 startedExploring = !std::exchange(m_exploring, true);
87 if (!m_thread) {
88 m_thread.reset(QThread::create([this] { run(); }));
89 m_thread->setObjectName(u"QQmlLSModuleExplorer"_s);
90 m_thread->start();
91 }
92 }
93 if (startedExploring)
94 emit explorationStarted();
95 m_hasWork.wakeAll();
96}
97
98void QQmlLSModuleExplorer::cancel()
99{
100 {
101 QMutexLocker guard(&m_mutex);
102 if (!m_exploring)
103 return;
104 m_pendingImportPaths.clear();
105 m_cancelRequested = true;
106 }
107 m_hasWork.wakeAll();
108}
109
110QStringList QQmlLSModuleExplorer::modulesIn(const QStringList &importPaths) const
111{
112 QStringList modules;
113 {
114 QMutexLocker guard(&m_mutex);
115 for (const QString &importPath : importPaths)
116 modules.append(m_modulesPerImportPath.value(importPath));
117 }
118 modules.removeDuplicates();
119 return modules;
120}
121
122bool QQmlLSModuleExplorer::isExploring() const
123{
124 QMutexLocker guard(&m_mutex);
125 return m_exploring;
126}
127
128std::optional<QString> QQmlLSModuleExplorer::takeNextImportPath()
129{
130 while (true) {
131 QMutexLocker guard(&m_mutex);
132 while (m_pendingImportPaths.empty() && !m_stopRequested && !m_cancelRequested)
133 m_hasWork.wait(&m_mutex);
134 if (m_stopRequested)
135 return std::nullopt;
136 if (!m_pendingImportPaths.empty()) {
137 QString importPath = std::move(m_pendingImportPaths.front());
138 m_pendingImportPaths.pop_front();
139 return importPath;
140 }
141 m_exploring = false;
142 m_cancelRequested = false;
143 guard.unlock();
144 emit explorationFinished();
145 }
146}
147
148void QQmlLSModuleExplorer::addModule(const QString &importPath, const QString &module)
149{
150 QMutexLocker guard(&m_mutex);
151 m_modulesPerImportPath[importPath].append(module);
152}
153
154bool QQmlLSModuleExplorer::finishExploring()
155{
156 QMutexLocker guard(&m_mutex);
157 if (!m_pendingImportPaths.empty())
158 return false;
159 m_exploring = false;
160 m_cancelRequested = false;
161 return true;
162}
163
164void QQmlLSModuleExplorer::run()
165{
166 while (const std::optional<QString> importPath = takeNextImportPath()) {
167 QDirIterator it(*importPath, { u"qmldir"_s }, QDir::Files,
168 QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
169 while (it.hasNext()) {
170 {
171 QMutexLocker guard(&m_mutex);
172 if (m_stopRequested)
173 return;
174 if (m_cancelRequested)
175 break;
176 }
177 if (const auto module = moduleNameFromQmldir(it.next()))
178 addModule(*importPath, *module);
179 }
180 if (finishExploring())
181 emit explorationFinished();
182 }
183}
184
185QT_END_NAMESPACE
Combined button and popup list for selecting options.
static std::optional< QString > moduleNameFromQmldir(const QString &qmldirPath)