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

syndication/atom

  • 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
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:26:22 by doxygen 1.8.3.1 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-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 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
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