• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

syndication/atom

  • sources
  • kde-4.14
  • kdepimlibs
  • syndication
  • atom
content.cpp
1 /*
2  * This file is part of the syndication library
3  *
4  * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "content.h"
24 
25 #include <tools.h>
26 
27 #include <QtCore/QByteArray>
28 #include <QtXml/QDomElement>
29 #include <QtCore/QString>
30 #include <QtCore/QStringList>
31 
32 namespace Syndication {
33 namespace Atom {
34 
35 class Content::ContentPrivate
36 {
37  public:
38 
39  ContentPrivate() : formatIdentified(false)
40  {
41  }
42  mutable Format format;
43  mutable bool formatIdentified;
44 };
45 
46 Content::Content() : ElementWrapper(), d(new ContentPrivate)
47 {
48 }
49 
50 Content::Content(const QDomElement& element) : ElementWrapper(element), d(new ContentPrivate)
51 {
52 }
53 
54 Content::Content(const Content& other) : ElementWrapper(other), d(other.d)
55 {
56 }
57 
58 Content::~Content()
59 {
60 }
61 
62 Content& Content::operator=(const Content& other)
63 {
64  ElementWrapper::operator=(other);
65  d = other.d;
66  return *this;
67 }
68 
69 QString Content::type() const
70 {
71  return attribute(QLatin1String("type"));
72 }
73 
74 QString Content::src() const
75 {
76  return completeURI(attribute(QLatin1String("src")));
77 }
78 
79 QByteArray Content::asByteArray() const
80 {
81  if (!isBinary())
82  return QByteArray();
83  return QByteArray::fromBase64(text().trimmed().toLatin1());
84 }
85 
86 //@cond PRIVATE
87 static QStringList xmltypes;
88 //@endcond
89 
90 Content::Format Content::mapTypeToFormat(const QString& typep, const QString& src)
91 {
92  QString type = typep;
93  //"If neither the type attribute nor the src attribute is provided,
94  //Atom Processors MUST behave as though the type attribute were
95  //present with a value of "text""
96  if (type.isNull() && src.isEmpty())
97  type = QLatin1String("text");
98 
99  if (type == QLatin1String("html")
100  || type == QLatin1String("text/html"))
101  return EscapedHTML;
102 
103  if (type == QLatin1String("text")
104  || (type.startsWith(QLatin1String("text/"), Qt::CaseInsensitive)
105  && !type.startsWith(QLatin1String("text/xml"), Qt::CaseInsensitive))
106  )
107  return PlainText;
108 
109  if (xmltypes.isEmpty())
110  {
111  xmltypes.append(QLatin1String("xhtml"));
112  xmltypes.append(QLatin1String("application/xhtml+xml"));
113  // XML media types as defined in RFC3023:
114  xmltypes.append(QLatin1String("text/xml"));
115  xmltypes.append(QLatin1String("application/xml"));
116  xmltypes.append(QLatin1String("text/xml-external-parsed-entity"));
117  xmltypes.append(QLatin1String("application/xml-external-parsed-entity"));
118  xmltypes.append(QLatin1String("application/xml-dtd"));
119  xmltypes.append(QLatin1String("text/x-dtd")); // from shared-mime-info
120  }
121 
122  if (xmltypes.contains(type)
123  || type.endsWith(QLatin1String("+xml"), Qt::CaseInsensitive)
124  || type.endsWith(QLatin1String("/xml"), Qt::CaseInsensitive))
125  return XML;
126 
127  return Binary;
128 }
129 
130 Content::Format Content::format() const
131 {
132  if (d->formatIdentified == false)
133  {
134  d->format = mapTypeToFormat(type(), src());
135  d->formatIdentified = true;
136  }
137  return d->format;
138 }
139 
140 bool Content::isBinary() const
141 {
142  return format() == Binary;
143 }
144 
145 bool Content::isContained() const
146 {
147  return src().isEmpty();
148 }
149 
150 bool Content::isPlainText() const
151 {
152  return format() == PlainText;
153 }
154 
155 bool Content::isEscapedHTML() const
156 {
157  return format() == EscapedHTML;
158 }
159 
160 bool Content::isXML() const
161 {
162  return format() == XML;
163 }
164 
165 QString Content::asString() const
166 {
167  Format f = format();
168 
169  if (f == PlainText)
170  {
171  return plainTextToHtml(text()).trimmed();
172  }
173  else if (f == EscapedHTML)
174  {
175  return text().trimmed();
176  }
177  else if (f == XML)
178  {
179  return childNodesAsXML().trimmed();
180  }
181 
182  return QString();
183 }
184 
185 QString Content::debugInfo() const
186 {
187  QString info;
188  info += QLatin1String("### Content: ###################\n");
189  info += QLatin1String("type: #") + type() + QLatin1String("#\n");
190  if (!src().isNull())
191  info += QLatin1String("src: #") + src() + QLatin1String("#\n");
192  if (!isBinary())
193  info += QLatin1String("content: #") + asString() + QLatin1String("#\n");
194  else
195  {
196  info += QLatin1String("binary length: #") + QString::number(asByteArray().size()) + QLatin1String("#\n");
197  }
198  info += QLatin1String("### Content end ################\n");
199 
200  return info;
201 }
202 
203 } // namespace Atom
204 } //namespace Syndication
Syndication::Atom::Content::isXML
bool isXML() const
returns whether the content is embedded XML.
Definition: content.cpp:160
Syndication::Atom::Content
The content element either contains or links the content of an entry.
Definition: content.h:46
Syndication::Atom::Content::EscapedHTML
the content is escaped HTML, (i.e., "<", ">" etc.
Definition: content.h:58
QByteArray
Syndication::Atom::Content::format
Format format() const
returns the content format
Definition: content.cpp:130
Syndication::Atom::Content::PlainText
the content is plain text (i.e.
Definition: content.h:55
Syndication::Atom::Content::isContained
bool isContained() const
returns whether the content is contained in the feed source, or not.
Definition: content.cpp:145
Syndication::Atom::Content::asString
QString asString() const
returns the content as string.
Definition: content.cpp:165
Syndication::Atom::Content::isEscapedHTML
bool isEscapedHTML() const
returns whether the content is escaped HTML or not Use asString() to access it
Definition: content.cpp:155
QString::isNull
bool isNull() const
Syndication::Atom::Content::operator=
Content & operator=(const Content &other)
assigns another content objecct
Definition: content.cpp:62
Syndication::Atom::Content::Binary
the content is base64-encoded binary content
Definition: content.h:61
QString::number
QString number(int n, int base)
Syndication::Atom::Content::isPlainText
bool isPlainText() const
returns whether the content is plain text or not.
Definition: content.cpp:150
Syndication::Atom::Content::debugInfo
QString debugInfo() const
returns a description of the content object for debugging purposes
Definition: content.cpp:185
QString::isEmpty
bool isEmpty() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QString
Syndication::Atom::Content::asByteArray
QByteArray asByteArray() const
returns binary content as byte array.
Definition: content.cpp:79
QStringList
Syndication::Atom::Content::XML
the content is embedded XML
Definition: content.h:60
Syndication::Atom::Content::~Content
~Content()
destructor
Definition: content.cpp:58
Syndication::Atom::Content::mapTypeToFormat
static Format mapTypeToFormat(const QString &type, const QString &src=QString())
maps a mimetype to Format enum according to the Atom 1.0 specification
Definition: content.cpp:90
QLatin1String
QByteArray::fromBase64
QByteArray fromBase64(const QByteArray &base64)
Syndication::Atom::Content::Format
Format
format of the content.
Definition: content.h:53
Syndication::Atom::Content::isBinary
bool isBinary() const
returns whether the content is binary content or not.
Definition: content.cpp:140
Syndication::Atom::Content::Content
Content()
creates a null content object.
Definition: content.cpp:46
Syndication::Atom::Content::type
QString type() const
the type of the content.
Definition: content.cpp:69
QDomElement
Syndication::Atom::Content::src
QString src() const
If src() is set, the content of this entry is not contained in the feed source, but available on the ...
Definition: content.cpp:74
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:35 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

syndication/atom

Skip menu "syndication/atom"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Members
  • File List

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal