Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qlocalfileapi.cpp
Go to the documentation of this file.
1
// Copyright (C) 2022 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
5
#
include
"qlocalfileapi_p.h"
6
#
include
<
private
/
qstdweb_p
.
h
>
7
#
include
<
QtCore
/
QRegularExpression
>
8
9
QT_BEGIN_NAMESPACE
10
namespace
LocalFileApi
{
11
namespace
{
12
std::string qtFilterListToFileInputAccept(
const
QStringList &filterList)
13
{
14
QStringList transformed;
15
for
(
const
auto
&filter : filterList) {
16
const
auto
type = Type::fromQt(filter);
17
if
(type && type->accept()) {
18
const
auto
&extensions = type->accept()->mimeType().extensions();
19
std::transform(extensions.begin(), extensions.end(), std::back_inserter(transformed),
20
[](
const
Type::Accept::MimeType::Extension &extension) {
21
return
extension.value().toString();
22
});
23
}
24
}
25
return
transformed.join(QStringLiteral(
","
)).toStdString();
26
}
27
28
std::optional<emscripten::val> qtFilterListToTypes(
const
QStringList &filterList)
29
{
30
using
namespace
qstdweb;
31
using
namespace
emscripten;
32
auto
types = emscripten::val::array();
33
34
for
(
const
auto
&fileFilter : filterList) {
35
auto
type = Type::fromQt(fileFilter);
36
if
(type) {
37
auto
jsType = emscripten::val::object();
38
jsType.set(
"description"
, type->description().toString().toStdString());
39
if
(type->accept()) {
40
jsType.set(
"accept"
, ([&mimeType = type->accept()->mimeType()]() {
41
val acceptDict = val::object();
42
43
QList<emscripten::val> extensions;
44
extensions.reserve(mimeType.extensions().size());
45
std::transform(
46
mimeType.extensions().begin(), mimeType.extensions().end(),
47
std::back_inserter(extensions),
48
[](
const
Type::Accept::MimeType::Extension &extension) {
49
return
val(extension.value().toString().toStdString());
50
});
51
acceptDict.set(
"application/octet-stream"
,
52
emscripten::val::array(extensions.begin(),
53
extensions.end()));
54
return
acceptDict;
55
})());
56
}
57
types.call<
void
>(
"push"
, std::move(jsType));
58
}
59
}
60
61
return
types[
"length"
].as<
int
>() == 0 ? std::optional<emscripten::val>() : types;
62
}
63
}
// namespace
64
65
Type
::
Type
(
QStringView
description
,
std
::
optional
<
Accept
>
accept
)
66
:
m_description
(
description
.
trimmed
()),
m_accept
(
std
::
move
(
accept
))
67
{
68
}
69
70
Type
::~
Type
() =
default
;
71
72
std
::
optional
<
Type
>
Type
::
fromQt
(
QStringView
type
)
73
{
74
using
namespace
emscripten
;
75
76
// Accepts either a string in format:
77
// GROUP3
78
// or in this format:
79
// GROUP1 (GROUP2)
80
// Group 1 is treated as the description, whereas group 2 or 3 are treated as the filter list.
81
static
QRegularExpression
regex
(
82
QString
(
QStringLiteral
(
"(?:(?:([^(]*)\\(([^()]+)\\)[^)]*)|([^()]+))"
)));
83
const
auto
match
=
regex
.
matchView
(
type
);
84
85
if
(!
match
.
hasMatch
())
86
return
std
::
nullopt
;
87
88
constexpr
size_t
DescriptionIndex
= 1;
89
constexpr
size_t
FilterListFromParensIndex
= 2;
90
constexpr
size_t
PlainFilterListIndex
= 3;
91
92
const
auto
description
=
match
.
hasCaptured
(
DescriptionIndex
)
93
?
match
.
capturedView
(
DescriptionIndex
)
94
:
QStringView
();
95
const
auto
filterList
=
match
.
capturedView
(
match
.
hasCaptured
(
FilterListFromParensIndex
)
96
?
FilterListFromParensIndex
97
:
PlainFilterListIndex
);
98
99
auto
accept
=
Type
::
Accept
::
fromQt
(
filterList
);
100
if
(!
accept
)
101
return
std
::
nullopt
;
102
103
return
Type
(
description
,
std
::
move
(*
accept
));
104
}
105
106
Type
::
Accept
::
Accept
() =
default
;
107
108
Type
::
Accept
::~
Accept
() =
default
;
109
110
std
::
optional
<
Type
::
Accept
>
Type
::
Accept
::
fromQt
(
QStringView
qtRepresentation
)
111
{
112
Accept
accept
;
113
114
// Used for accepting multiple extension specifications on a filter list.
115
// The next group of non-empty characters.
116
static
QRegularExpression
internalRegex
(
QString
(
QStringLiteral
(
"([^\\s]+)\\s*"
)));
117
int
offset
= 0;
118
auto
internalMatch
=
internalRegex
.
matchView
(
qtRepresentation
,
offset
);
119
MimeType
mimeType
;
120
121
while
(
internalMatch
.
hasMatch
()) {
122
auto
webExtension
=
MimeType
::
Extension
::
fromQt
(
internalMatch
.
capturedView
(1));
123
124
if
(!
webExtension
)
125
return
std
::
nullopt
;
126
127
mimeType
.
addExtension
(*
webExtension
);
128
129
internalMatch
=
internalRegex
.
matchView
(
qtRepresentation
,
internalMatch
.
capturedEnd
());
130
}
131
132
accept
.
setMimeType
(
mimeType
);
133
return
accept
;
134
}
135
136
void
Type
::
Accept
::
setMimeType
(
MimeType
mimeType
)
137
{
138
m_mimeType
=
std
::
move
(
mimeType
);
139
}
140
141
Type
::
Accept
::
MimeType
::
MimeType
() =
default
;
142
143
Type
::
Accept
::
MimeType
::~
MimeType
() =
default
;
144
145
void
Type
::
Accept
::
MimeType
::
addExtension
(
Extension
extension
)
146
{
147
m_extensions
.
push_back
(
std
::
move
(
extension
));
148
}
149
150
Type
::
Accept
::
MimeType
::
Extension
::
Extension
(
QStringView
extension
) :
m_value
(
extension
) { }
151
152
Type
::
Accept
::
MimeType
::
Extension
::~
Extension
() =
default
;
153
154
std
::
optional
<
Type
::
Accept
::
MimeType
::
Extension
>
155
Type
::
Accept
::
MimeType
::
Extension
::
fromQt
(
QStringView
qtRepresentation
)
156
{
157
// Checks for a filter that matches everything:
158
// Any number of asterisks or any number of asterisks with a '.' between them.
159
// The web filter does not support wildcards.
160
static
QRegularExpression
qtAcceptAllRegex
(
161
QRegularExpression
::
anchoredPattern
(
QString
(
QStringLiteral
(
"[*]+|[*]+\\.[*]+"
))));
162
if
(
qtAcceptAllRegex
.
matchView
(
qtRepresentation
).
hasMatch
())
163
return
std
::
nullopt
;
164
165
// Checks for correctness. The web filter only allows filename extensions and does not filter
166
// the actual filenames, therefore we check whether the filter provided only filters for the
167
// extension.
168
static
QRegularExpression
qtFilenameMatcherRegex
(
169
QRegularExpression
::
anchoredPattern
(
QString
(
QStringLiteral
(
"(\\*?)(\\.[^*]+)"
))));
170
171
auto
extensionMatch
=
qtFilenameMatcherRegex
.
matchView
(
qtRepresentation
);
172
if
(
extensionMatch
.
hasMatch
())
173
return
Extension
(
extensionMatch
.
capturedView
(2));
174
175
// Mapping impossible.
176
return
std
::
nullopt
;
177
}
178
179
emscripten
::
val
makeOpenFileOptions
(
const
QStringList &filterList,
bool
acceptMultiple)
180
{
181
auto
options = emscripten::val::object();
182
if
(
auto
typeList = qtFilterListToTypes(filterList); typeList) {
183
options.set(
"types"
,
std
::move(*typeList));
184
options.set(
"excludeAcceptAllOption"
,
true
);
185
}
186
187
options.set(
"multiple"
, acceptMultiple);
188
189
return
options;
190
}
191
192
emscripten
::
val
makeSaveFileOptions
(
const
QStringList &filterList,
const
std::string& suggestedName)
193
{
194
auto
options = emscripten::val::object();
195
196
if
(!suggestedName.empty())
197
options.set(
"suggestedName"
, emscripten::val(suggestedName));
198
199
if
(
auto
typeList = qtFilterListToTypes(filterList))
200
options.set(
"types"
, emscripten::val(std::move(*typeList)));
201
202
return
options;
203
}
204
205
std
::
string
makeFileInputAccept
(
const
QStringList &filterList)
206
{
207
return
qtFilterListToFileInputAccept(filterList);
208
}
209
210
}
// namespace LocalFileApi
211
212
QT_END_NAMESPACE
LocalFileApi
Definition
qlocalfileapi.cpp:10
LocalFileApi::makeFileInputAccept
std::string makeFileInputAccept(const QStringList &filterList)
Definition
qlocalfileapi.cpp:205
LocalFileApi::makeOpenFileOptions
emscripten::val makeOpenFileOptions(const QStringList &filterList, bool acceptMultiple)
Definition
qlocalfileapi.cpp:179
LocalFileApi::makeSaveFileOptions
emscripten::val makeSaveFileOptions(const QStringList &filterList, const std::string &suggestedName)
Definition
qlocalfileapi.cpp:192
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtbase
src
gui
platform
wasm
qlocalfileapi.cpp
Generated on
for Qt by
1.18.0