aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmltooling/qmldbg_preview/qqmlpreviewfileloader.cpp
blob: 0267aef138384862cdcb9f9f72e3b1c90bf5c1a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqmlpreviewfileloader.h"
#include "qqmlpreviewservice.h"

#include <QtQml/qqmlfile.h>

#include <QtCore/qlibraryinfo.h>
#include <QtCore/qstandardpaths.h>

QT_BEGIN_NAMESPACE

QQmlPreviewFileLoader::QQmlPreviewFileLoader(QQmlPreviewServiceImpl *service) : m_service(service)
{
    // Exclude some resource paths used by Qt itself. There is no point in loading those from the
    // client as the client will not have the files (or even worse, it may have different ones).
    m_blacklist.blacklist(":/qt-project.org");
    m_blacklist.blacklist(":/QtQuick/Controls/Styles");
    m_blacklist.blacklist(":/ExtrasImports/QtQuick/Controls/Styles");

    // Target specific configuration should not replaced with files from the host.
    m_blacklist.blacklist("/etc");

    for (int loc = QLibraryInfo::PrefixPath; loc < QLibraryInfo::TestsPath; ++loc) {
        m_blacklist.blacklist(QLibraryInfo::path(
                                  static_cast<QLibraryInfo::LibraryPath>(loc)));
    }
    m_blacklist.blacklist(QLibraryInfo::path(QLibraryInfo::SettingsPath));

    static const QStandardPaths::StandardLocation blackListLocations[] = {
        QStandardPaths::CacheLocation,
        QStandardPaths::GenericDataLocation,
        QStandardPaths::ConfigLocation,
        QStandardPaths::GenericCacheLocation,
        QStandardPaths::GenericConfigLocation,
        QStandardPaths::AppDataLocation,
        QStandardPaths::AppConfigLocation
    };

    for (auto locationType : blackListLocations) {
        const QStringList locations = QStandardPaths::standardLocations(locationType);
        for (const QString &location : locations)
            m_blacklist.blacklist(location);
    }

    m_blacklist.whitelist(QLibraryInfo::path(QLibraryInfo::TestsPath));

    connect(this, &QQmlPreviewFileLoader::request, service, &QQmlPreviewServiceImpl::forwardRequest,
            Qt::DirectConnection);
    connect(service, &QQmlPreviewServiceImpl::directory, this, &QQmlPreviewFileLoader::directory);
    connect(service, &QQmlPreviewServiceImpl::file, this, &QQmlPreviewFileLoader::file);
    connect(service, &QQmlPreviewServiceImpl::error, this, &QQmlPreviewFileLoader::error);
    connect(service, &QQmlPreviewServiceImpl::clearCache, this, &QQmlPreviewFileLoader::clearCache);
    moveToThread(&m_thread);
    m_thread.start();
}

QQmlPreviewFileLoader::~QQmlPreviewFileLoader() {
    m_thread.quit();
    m_thread.wait();
}

QQmlPreviewFileLoader::Result QQmlPreviewFileLoader::load(const QString &path)
{
    QMutexLocker locker(&m_contentMutex);
    m_path = path;

    auto fileIterator = m_fileCache.constFind(path);
    if (fileIterator != m_fileCache.constEnd()) {
        m_result = File;
        m_contents = *fileIterator;
        m_entries.clear();
        return m_result;
    }

    auto dirIterator = m_directoryCache.constFind(path);
    if (dirIterator != m_directoryCache.constEnd()) {
        m_result = Directory;
        m_contents.clear();
        m_entries = *dirIterator;
        return m_result;
    }

    m_result = Unknown;
    m_entries.clear();
    m_contents.clear();
    emit request(path);
    m_waitCondition.wait(&m_contentMutex);
    return m_result;
}

QByteArray QQmlPreviewFileLoader::contents()
{
    QMutexLocker locker(&m_contentMutex);
    return m_contents;
}

QStringList QQmlPreviewFileLoader::entries()
{
    QMutexLocker locker(&m_contentMutex);
    return m_entries;
}

void QQmlPreviewFileLoader::whitelist(const QUrl &url)
{
    const QString path = QQmlFile::urlToLocalFileOrQrc(url);
    if (!path.isEmpty()) {
        QMutexLocker locker(&m_contentMutex);
        m_blacklist.whitelist(path);
    }
}

bool QQmlPreviewFileLoader::isBlacklisted(const QString &path)
{
    QMutexLocker locker(&m_contentMutex);
    return m_blacklist.isBlacklisted(path);
}

void QQmlPreviewFileLoader::file(const QString &path, const QByteArray &contents)
{
    QMutexLocker locker(&m_contentMutex);
    m_blacklist.whitelist(path);
    m_fileCache[path] = contents;
    if (path == m_path) {
        m_contents = contents;
        m_result = File;
        m_waitCondition.wakeOne();
    }
}

void QQmlPreviewFileLoader::directory(const QString &path, const QStringList &entries)
{
    QMutexLocker locker(&m_contentMutex);
    m_blacklist.whitelist(path);
    m_directoryCache[path] = entries;
    if (path == m_path) {
        m_entries = entries;
        m_result = Directory;
        m_waitCondition.wakeOne();
    }
}

void QQmlPreviewFileLoader::error(const QString &path)
{
    QMutexLocker locker(&m_contentMutex);
    m_blacklist.blacklist(path);
    if (path == m_path) {
        m_result = Fallback;
        m_waitCondition.wakeOne();
    }
}

void QQmlPreviewFileLoader::clearCache()
{
    QMutexLocker locker(&m_contentMutex);
    m_fileCache.clear();
    m_directoryCache.clear();
}

QT_END_NAMESPACE

#include "moc_qqmlpreviewfileloader.cpp"