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
qqmlpreviewblacklist.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// Qt-Security score:significant
4
6
8
9void QQmlPreviewBlacklist::blacklist(const QStringList &paths)
10{
11 for (const QString &path : paths)
12 blacklist(path);
13}
14
15void QQmlPreviewBlacklist::blacklist(const QString &path)
16{
17 if (!path.isEmpty())
18 m_root.insert(path, 0);
19}
20
21void QQmlPreviewBlacklist::whitelist(const QStringList &paths)
22{
23 for (const QString &path : paths)
24 whitelist(path);
25}
26
27void QQmlPreviewBlacklist::whitelist(const QString &path)
28{
29 if (!path.isEmpty())
30 m_root.remove(path, 0);
31}
32
33bool QQmlPreviewBlacklist::isBlacklisted(const QString &path) const
34{
35 return path.isEmpty() ? true : m_root.findPrefix(path, 0) == Node::MatchedLeaf;
36}
37
38void QQmlPreviewBlacklist::clear()
39{
40 m_root = Node();
41}
42
43QQmlPreviewBlacklist::Node::Node()
44{
45}
46
47QQmlPreviewBlacklist::Node::Node(const QQmlPreviewBlacklist::Node &other) :
48 m_mine(other.m_mine), m_isLeaf(other.m_isLeaf)
49{
50 for (auto it = other.m_next.begin(), end = other.m_next.end(); it != end; ++it)
51 m_next.insert(it.key(), new Node(**it));
52}
53
54QQmlPreviewBlacklist::Node::Node(QQmlPreviewBlacklist::Node &&other) noexcept
55{
56 m_mine.swap(other.m_mine);
57 m_next.swap(other.m_next);
58 m_isLeaf = other.m_isLeaf;
59}
60
61QQmlPreviewBlacklist::Node::~Node()
62{
63 qDeleteAll(m_next);
64}
65
66QQmlPreviewBlacklist::Node &QQmlPreviewBlacklist::Node::operator=(
67 const QQmlPreviewBlacklist::Node &other)
68{
69 if (&other != this) {
70 m_mine = other.m_mine;
71 for (auto it = other.m_next.begin(), end = other.m_next.end(); it != end; ++it)
72 m_next.insert(it.key(), new Node(**it));
73 m_isLeaf = other.m_isLeaf;
74 }
75 return *this;
76}
77
78QQmlPreviewBlacklist::Node &QQmlPreviewBlacklist::Node::operator=(
79 QQmlPreviewBlacklist::Node &&other) noexcept
80{
81 if (&other != this) {
82 m_mine.swap(other.m_mine);
83 m_next.swap(other.m_next);
84 m_isLeaf = other.m_isLeaf;
85 }
86 return *this;
87}
88
89void QQmlPreviewBlacklist::Node::split(QString::iterator it, QString::iterator end)
90{
91 QString existing;
92 existing.resize(end - it - 1);
93 std::copy(it + 1, end, existing.begin());
94
95 Node *node = new Node(existing, m_next, m_isLeaf);
96 m_next.clear();
97 m_next.insert(*it, node);
98 m_mine.resize(it - m_mine.begin());
99 m_isLeaf = false;
100}
101
102void QQmlPreviewBlacklist::Node::insert(const QString &path, int offset)
103{
104 for (auto it = m_mine.begin(), end = m_mine.end(); it != end; ++it) {
105 if (offset == path.size()) {
106 split(it, end);
107 m_isLeaf = true;
108 return;
109 }
110
111 if (path.at(offset) != *it) {
112 split(it, end);
113
114 QString inserted;
115 inserted.resize(path.size() - offset - 1);
116 std::copy(path.begin() + offset + 1, path.end(), inserted.begin());
117 m_next.insert(path.at(offset), new Node(inserted));
118 return;
119 }
120
121 ++offset;
122 }
123
124 if (offset == path.size()) {
125 m_isLeaf = true;
126 return;
127 }
128
129 Node *&node = m_next[path.at(offset++)];
130 if (node == nullptr) {
131 QString inserted;
132 inserted.resize(path.size() - offset);
133 std::copy(path.begin() + offset, path.end(), inserted.begin());
134 node = new Node(inserted);
135 } else {
136 node->insert(path, offset);
137 }
138}
139
140void QQmlPreviewBlacklist::Node::remove(const QString &path, int offset)
141{
142 for (auto it = m_mine.begin(), end = m_mine.end(); it != end; ++it) {
143 if (offset == path.size() || path.at(offset) != *it) {
144 split(it, end);
145 return;
146 }
147 ++offset;
148 }
149
150 m_isLeaf = false;
151 if (offset == path.size())
152 return;
153
154 Node *&node = m_next[path.at(offset++)];
155 if (node) {
156 node->remove(path, offset);
157 } else {
158 QString inserted;
159 inserted.resize(path.size() - offset);
160 std::copy(path.begin() + offset, path.end(), inserted.begin());
161 node = new Node(inserted, {}, false);
162 }
163}
164
165QQmlPreviewBlacklist::Node::PrefixResult QQmlPreviewBlacklist::Node::findPrefix(
166 const QString &path, int offset) const
167{
168 if (offset == path.size()) {
169 if (!m_mine.isEmpty())
170 return Unmatched;
171 return m_isLeaf ? MatchedLeaf : MatchedBranch;
172 }
173
174 for (auto it = m_mine.begin(), end = m_mine.end(); it != end; ++it) {
175 if (path.at(offset) != *it)
176 return Unmatched;
177
178 if (++offset == path.size()) {
179 if (++it != end)
180 return Unmatched;
181 return m_isLeaf ? MatchedLeaf : MatchedBranch;
182 }
183 }
184
185 const QChar c = path.at(offset);
186 const auto it = m_next.find(c);
187 if (it != m_next.end()) {
188 const PrefixResult result = (*it)->findPrefix(path, offset + 1);
189 if (result != Unmatched)
190 return result;
191 }
192
193 if (c == '/')
194 return m_isLeaf ? MatchedLeaf : MatchedBranch;
195
196 return Unmatched;
197}
198
199QQmlPreviewBlacklist::Node::Node(const QString &mine,
200 const QHash<QChar, QQmlPreviewBlacklist::Node *> &next,
201 bool isLeaf)
202 : m_mine(mine), m_next(next), m_isLeaf(isLeaf)
203{
204}
205
206QT_END_NAMESPACE
Combined button and popup list for selecting options.