Branch data Line data Source code
1 : : #include "MailtoUrl.h"
2 : :
3 : : #include <QUrl>
4 : : #include <QUrlQuery>
5 : :
6 : 9 : static QString sanitizedHeaderValue(QString value) {
7 : 9 : value.removeIf([](QChar ch) {
8 [ + + - + ]: 171 : return ch.unicode() < 0x20 || ch.unicode() == 0x7f;
9 : : });
10 : 9 : return value.trimmed();
11 : : }
12 : :
13 : 9 : std::optional<MailtoRequest> MailtoRequest::parse(const QString &value) {
14 [ + - + - ]: 9 : const QUrl url = QUrl::fromEncoded(value.toUtf8(), QUrl::StrictMode);
15 [ + + ]: 16 : if (!url.isValid() ||
16 [ + - + + : 25 : url.scheme().compare(QStringLiteral("mailto"),
+ + + + -
- - - -
- ]
17 : 12 : Qt::CaseInsensitive) != 0 ||
18 [ + - + + : 22 : !url.host().isEmpty() || !url.userInfo().isEmpty()) {
+ - + + +
- - + + +
+ + + + -
- - - ]
19 : 4 : return std::nullopt;
20 : : }
21 : :
22 : 5 : MailtoRequest request;
23 [ + - + - ]: 5 : request.to = sanitizedHeaderValue(url.path(QUrl::FullyDecoded));
24 : :
25 : 5 : bool hasSubject = false;
26 : 5 : bool hasBody = false;
27 [ + - ]: 5 : const QUrlQuery query(url);
28 [ + - + - : 17 : for (const auto &[key, value] : query.queryItems(QUrl::FullyDecoded)) {
+ - + + ]
29 [ + + ]: 12 : if (key.compare(QStringLiteral("to"), Qt::CaseInsensitive) == 0) {
30 [ + - ]: 1 : const QString recipient = sanitizedHeaderValue(value);
31 [ + - ]: 1 : if (!recipient.isEmpty()) {
32 [ + - ]: 1 : if (!request.to.isEmpty())
33 [ + - ]: 1 : request.to += QStringLiteral(", ");
34 [ + - ]: 1 : request.to += recipient;
35 : : }
36 [ + + + + ]: 20 : } else if (!hasSubject &&
37 [ + + + + : 19 : key.compare(QStringLiteral("subject"),
+ + ]
38 : : Qt::CaseInsensitive) == 0) {
39 [ + - ]: 3 : request.subject = sanitizedHeaderValue(value);
40 : 3 : hasSubject = true;
41 [ + + + + ]: 15 : } else if (!hasBody &&
42 [ + + + + : 15 : key.compare(QStringLiteral("body"),
+ + ]
43 : : Qt::CaseInsensitive) == 0) {
44 : 2 : request.body = value;
45 : 2 : hasBody = true;
46 : : }
47 : 5 : }
48 : :
49 : 5 : return request;
50 : 9 : }
|