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
qqmlpreviewfileloader.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 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
6
7#include <QtQml/qqmlfile.h>
8
9#include <QtCore/qlibraryinfo.h>
10#include <QtCore/qstandardpaths.h>
11
13
15{
16 // Exclude some resource paths used by Qt itself. There is no point in loading those from the
17 // client as the client will not have the files (or even worse, it may have different ones).
18 m_blacklist.blacklist(":/qt-project.org");
19 m_blacklist.blacklist(":/QtQuick/Controls/Styles");
20 m_blacklist.blacklist(":/ExtrasImports/QtQuick/Controls/Styles");
21
22 // Target specific configuration should not replaced with files from the host.
23 m_blacklist.blacklist("/etc");
24
25 for (int loc = QLibraryInfo::PrefixPath; loc < QLibraryInfo::TestsPath; ++loc) {
26 m_blacklist.blacklist(QLibraryInfo::path(
27 static_cast<QLibraryInfo::LibraryPath>(loc)));
28 }
30
31 static const QStandardPaths::StandardLocation blackListLocations[] = {
39 };
40
41 for (auto locationType : blackListLocations) {
43 for (const QString &location : locations)
44 m_blacklist.blacklist(location);
45 }
46
48
51 connect(service, &QQmlPreviewServiceImpl::directory, this, &QQmlPreviewFileLoader::directory);
52 connect(service, &QQmlPreviewServiceImpl::file, this, &QQmlPreviewFileLoader::file);
53 connect(service, &QQmlPreviewServiceImpl::error, this, &QQmlPreviewFileLoader::error);
54 connect(service, &QQmlPreviewServiceImpl::clearCache, this, &QQmlPreviewFileLoader::clearCache);
55 moveToThread(&m_thread);
56 m_thread.start();
57}
58
60 m_thread.quit();
61 m_thread.wait();
62}
63
65{
66 QMutexLocker locker(&m_contentMutex);
67 m_path = path;
68
69 auto fileIterator = m_fileCache.constFind(path);
70 if (fileIterator != m_fileCache.constEnd()) {
71 m_result = File;
72 m_contents = *fileIterator;
73 m_entries.clear();
74 return m_result;
75 }
76
77 auto dirIterator = m_directoryCache.constFind(path);
78 if (dirIterator != m_directoryCache.constEnd()) {
79 m_result = Directory;
80 m_contents.clear();
81 m_entries = *dirIterator;
82 return m_result;
83 }
84
85 m_result = Unknown;
86 m_entries.clear();
87 m_contents.clear();
89 m_waitCondition.wait(&m_contentMutex);
90 return m_result;
91}
92
94{
95 QMutexLocker locker(&m_contentMutex);
96 return m_contents;
97}
98
100{
101 QMutexLocker locker(&m_contentMutex);
102 return m_entries;
103}
104
106{
108 if (!path.isEmpty()) {
109 QMutexLocker locker(&m_contentMutex);
110 m_blacklist.whitelist(path);
111 }
112}
113
115{
116 QMutexLocker locker(&m_contentMutex);
117 return m_blacklist.isBlacklisted(path);
118}
119
120void QQmlPreviewFileLoader::file(const QString &path, const QByteArray &contents)
121{
122 QMutexLocker locker(&m_contentMutex);
123 m_blacklist.whitelist(path);
124 m_fileCache[path] = contents;
125 if (path == m_path) {
126 m_contents = contents;
127 m_result = File;
128 m_waitCondition.wakeOne();
129 }
130}
131
132void QQmlPreviewFileLoader::directory(const QString &path, const QStringList &entries)
133{
134 QMutexLocker locker(&m_contentMutex);
135 m_blacklist.whitelist(path);
136 m_directoryCache[path] = entries;
137 if (path == m_path) {
138 m_entries = entries;
139 m_result = Directory;
140 m_waitCondition.wakeOne();
141 }
142}
143
144void QQmlPreviewFileLoader::error(const QString &path)
145{
146 QMutexLocker locker(&m_contentMutex);
147 m_blacklist.blacklist(path);
148 if (path == m_path) {
149 m_result = Fallback;
150 m_waitCondition.wakeOne();
151 }
152}
153
154void QQmlPreviewFileLoader::clearCache()
155{
156 QMutexLocker locker(&m_contentMutex);
157 m_fileCache.clear();
158 m_directoryCache.clear();
159}
160
162
163#include "moc_qqmlpreviewfileloader.cpp"
\inmodule QtCore
Definition qbytearray.h:57
void clear()
Clears the contents of the byte array and makes it null.
const_iterator constFind(const Key &key) const noexcept
Definition qhash.h:1299
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1219
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:951
static QString path(LibraryPath p)
LibraryPath
\keyword library location
\inmodule QtCore
Definition qmutex.h:313
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
bool moveToThread(QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
Changes the thread affinity for this object and its children and returns true on success.
Definition qobject.cpp:1643
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to \l{QFile}.
Definition qqmlfile.cpp:742
void blacklist(const QString &path)
void whitelist(const QString &path)
bool isBlacklisted(const QString &path) const
QQmlPreviewFileLoader(QQmlPreviewServiceImpl *service)
Result load(const QString &file)
bool isBlacklisted(const QString &file)
void whitelist(const QUrl &url)
void request(const QString &file)
void forwardRequest(const QString &file)
void file(const QString &file, const QByteArray &contents)
void directory(const QString &file, const QStringList &entries)
void error(const QString &file)
static QStringList standardLocations(StandardLocation type)
StandardLocation
This enum describes the different locations that can be queried using methods such as QStandardPaths:...
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
void start(Priority=InheritPriority)
Definition qthread.cpp:996
bool wait(QDeadlineTimer deadline=QDeadlineTimer(QDeadlineTimer::Forever))
Definition qthread.cpp:1023
void quit()
Definition qthread.cpp:1008
\inmodule QtCore
Definition qurl.h:94
bool wait(QMutex *, QDeadlineTimer=QDeadlineTimer(QDeadlineTimer::Forever))
Combined button and popup list for selecting options.
@ DirectConnection
GLint location
GLsizei const GLchar *const * path
GLuint const GLint * locations
#define emit
QUrl url("example.com")
[constructor-url-reference]
QNetworkRequest request(url)