Syndication

content.cpp
1/*
2 This file is part of the syndication library
3 SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "content.h"
9
10#include <tools.h>
11
12#include <QByteArray>
13#include <QDomElement>
14#include <QStringList>
15
16namespace Syndication
17{
18namespace Atom
19{
20class SYNDICATION_NO_EXPORT Content::ContentPrivate
21{
22public:
23 ContentPrivate()
24 : format(Format::PlainText)
25 , formatIdentified(false)
26 {
27 }
28 mutable Format format;
29 mutable bool formatIdentified;
30};
31
32Content::Content()
33 : ElementWrapper()
34 , d(new ContentPrivate)
35{
36}
37
38Content::Content(const QDomElement &element)
39 : ElementWrapper(element)
40 , d(new ContentPrivate)
41{
42}
43
44Content::Content(const Content &other)
45 : ElementWrapper(other)
46 , d(other.d)
47{
48}
49
50Content::~Content()
51{
52}
53
54Content &Content::operator=(const Content &other)
55{
56 ElementWrapper::operator=(other);
57 d = other.d;
58 return *this;
59}
60
61QString Content::type() const
62{
63 return attribute(QStringLiteral("type"));
64}
65
66QString Content::src() const
67{
68 return completeURI(attribute(QStringLiteral("src")));
69}
70
71QByteArray Content::asByteArray() const
72{
73 if (!isBinary()) {
74 return QByteArray();
75 }
76 return QByteArray::fromBase64(text().trimmed().toLatin1());
77}
78
79Content::Format Content::mapTypeToFormat(const QString &typep, const QString &src)
80{
81 QString type = typep;
82 //"If neither the type attribute nor the src attribute is provided,
83 // Atom Processors MUST behave as though the type attribute were
84 // present with a value of "text""
85 if (type.isNull() && src.isEmpty()) {
86 type = QStringLiteral("text");
87 }
88
89 if (type == QLatin1String("html") || type == QLatin1String("text/html")) {
90 return EscapedHTML;
91 }
92
93 /* clang-format off */
94 if (type == QLatin1String("text")
95 || (type.startsWith(QLatin1String("text/"), Qt::CaseInsensitive)
96 && !type.startsWith(QLatin1String("text/xml"), Qt::CaseInsensitive))) { /* clang-format on */
97 return PlainText;
98 }
99
100 static QStringList xmltypes;
101 if (xmltypes.isEmpty()) {
102 xmltypes.reserve(8);
103 xmltypes.append(QStringLiteral("xhtml"));
104 xmltypes.append(QStringLiteral("application/xhtml+xml"));
105 // XML media types as defined in RFC3023:
106 xmltypes.append(QStringLiteral("text/xml"));
107 xmltypes.append(QStringLiteral("application/xml"));
108 xmltypes.append(QStringLiteral("text/xml-external-parsed-entity"));
109 xmltypes.append(QStringLiteral("application/xml-external-parsed-entity"));
110 xmltypes.append(QStringLiteral("application/xml-dtd"));
111 xmltypes.append(QStringLiteral("text/x-dtd")); // from shared-mime-info
112 }
113
114 /* clang-format off */
115 if (xmltypes.contains(type)
116 || type.endsWith(QLatin1String("+xml"), Qt::CaseInsensitive)
117 || type.endsWith(QLatin1String("/xml"), Qt::CaseInsensitive)) { /* clang-format on */
118 return XML;
119 }
120
121 return Binary;
122}
123
124Content::Format Content::format() const
125{
126 if (d->formatIdentified == false) {
127 d->format = mapTypeToFormat(type(), src());
128 d->formatIdentified = true;
129 }
130 return d->format;
131}
132
133bool Content::isBinary() const
134{
135 return format() == Binary;
136}
137
138bool Content::isContained() const
139{
140 return src().isEmpty();
141}
142
143bool Content::isPlainText() const
144{
145 return format() == PlainText;
146}
147
148bool Content::isEscapedHTML() const
149{
150 return format() == EscapedHTML;
151}
152
153bool Content::isXML() const
154{
155 return format() == XML;
156}
157
158QString Content::asString() const
159{
160 Format f = format();
161
162 if (f == PlainText) {
163 return plainTextToHtml(text()).trimmed();
164 } else if (f == EscapedHTML) {
165 return text().trimmed();
166 } else if (f == XML) {
167 return childNodesAsXML().trimmed();
168 }
169
170 return QString();
171}
172
173QString Content::debugInfo() const
174{
175 QString info = QLatin1String("### Content: ###################\n");
176 info += QLatin1String("type: #") + type() + QLatin1String("#\n");
177 if (!src().isNull()) {
178 info += QLatin1String("src: #") + src() + QLatin1String("#\n");
179 }
180 if (!isBinary()) {
181 info += QLatin1String("content: #") + asString() + QLatin1String("#\n");
182 } else {
183 info += QLatin1String("binary length: #") + QString::number(asByteArray().size()) + QLatin1String("#\n");
184 }
185 info += QLatin1String("### Content end ################\n");
186
187 return info;
188}
189
190} // namespace Atom
191} // namespace Syndication
Type type(const QSqlDatabase &db)
QByteArray fromBase64(const QByteArray &base64, Base64Options options)
void append(QList< T > &&value)
bool isEmpty() const const
void reserve(qsizetype size)
bool isEmpty() const const
QString number(double n, char format, int precision)
QString trimmed() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:15 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.