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

syndication/rss2

  • sources
  • kde-4.14
  • kdepimlibs
  • syndication
  • rss2
item.cpp
1 /*
2  * This file is part of the syndication library
3  *
4  * Copyright (C) 2005 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 <rss2/item.h>
24 #include <rss2/category.h>
25 #include <rss2/enclosure.h>
26 #include <rss2/source.h>
27 #include <rss2/tools_p.h>
28 #include <constants.h>
29 #include <specificitem.h>
30 #include <specificitemvisitor.h>
31 #include <tools.h>
32 
33 #include <QtXml/QDomElement>
34 #include <QtCore/QString>
35 #include <QtCore/QList>
36 
37 namespace Syndication {
38 namespace RSS2 {
39 
40 class Item::ItemPrivate
41 {
42  public:
43 
44  boost::shared_ptr<Document> doc;
45 };
46 
47 Item::Item(boost::shared_ptr<Document> doc) : ElementWrapper(), d(new ItemPrivate)
48 {
49  d->doc = doc;
50 }
51 
52 Item::Item(const QDomElement& element, boost::shared_ptr<Document> doc) : ElementWrapper(element), d(new ItemPrivate)
53 {
54  d->doc = doc;
55 }
56 
57 Item::~Item()
58 {
59 }
60 
61 Item::Item(const Item& other) : ElementWrapper(other), SpecificItem(other)
62 {
63  d = other.d;
64 }
65 
66 Item& Item::operator=(const Item& other)
67 {
68  ElementWrapper::operator=(other);
69  SpecificItem::operator=(other);
70  d = other.d;
71  return *this;
72 }
73 
74 QString Item::title() const
75 {
76  if (!d->doc)
77  return originalTitle();
78 
79  bool isCDATA = false;
80  bool containsMarkup = false;
81  d->doc->getItemTitleFormatInfo(&isCDATA, &containsMarkup);
82 
83  return normalize(originalTitle(), isCDATA, containsMarkup);
84 }
85 
86 
87 QString Item::originalDescription() const
88 {
89  return extractElementTextNS(QString(), QLatin1String("description"));
90 }
91 
92 QString Item::originalTitle() const
93 {
94  return extractElementTextNS(QString(), QLatin1String("title"));
95 }
96 
97 QString Item::link() const
98 {
99  QString url = extractElementTextNS(QString(), QLatin1String("link") );
100  if (url.startsWith(QLatin1String("http://")) || url.startsWith(QLatin1String("https://"))) {
101  return url;
102  }
103  if (url.isEmpty()) {
104  return QString();
105  }
106  if (d->doc->link().isEmpty()) {
107  return url;
108  }
109  // link does not look like a complete url, assume the feed author expects
110  // the doc link to provide the base of the url.
111  QString baseUrl = d->doc->link();
112  if (url.startsWith(QLatin1Char('/')) || baseUrl.endsWith(QLatin1Char('/'))) {
113  return baseUrl + url;
114  } else {
115  return baseUrl + QLatin1Char('/') + url;
116  }
117 }
118 
119 QString Item::description() const
120 {
121  if (!d->doc)
122  return originalDescription();
123 
124  bool isCDATA = false;
125  bool containsMarkup = false;
126  d->doc->getItemDescriptionFormatInfo(&isCDATA, &containsMarkup);
127 
128  return normalize(originalDescription(), isCDATA, containsMarkup);
129 }
130 
131 QString Item::content() const
132 {
133  // parse encoded stuff from content:encoded, xhtml:body and friends into content
134  return extractContent(*this);
135 }
136 
137 QList<Category> Item::categories() const
138 {
139  QList<QDomElement> cats = elementsByTagNameNS(QString(),
140  QLatin1String("category"));
141 
142  QList<Category> categories;
143 
144  QList<QDomElement>::ConstIterator it = cats.constBegin();
145  for ( ; it != cats.constEnd(); ++it)
146  {
147  categories.append(Category(*it));
148  }
149  return categories;
150 }
151 
152 QString Item::comments() const
153 {
154  return extractElementTextNS(QString(), QLatin1String("comments") );
155 }
156 
157 QString Item::author() const
158 {
159  QString a = extractElementTextNS(QString(), QLatin1String("author") );
160  if (!a.isNull())
161  {
162  return a;
163  }
164  else
165  {
166  // if author is not available, fall back to dc:creator
167  return extractElementTextNS(dublinCoreNamespace(),
168  QLatin1String("creator") );
169  }
170 
171 }
172 
173 QList<Enclosure> Item::enclosures() const
174 {
175  QList<QDomElement> encs = elementsByTagNameNS(QString(),
176  QLatin1String("enclosure"));
177 
178  QList<Enclosure> enclosures;
179 
180  QList<QDomElement>::ConstIterator it = encs.constBegin();
181  for ( ; it != encs.constEnd(); ++it)
182  {
183  enclosures.append(Enclosure(*it));
184  }
185  return enclosures;
186 }
187 
188 QString Item::guid() const
189 {
190  return extractElementTextNS(QString(), QLatin1String("guid") );
191 }
192 
193 bool Item::guidIsPermaLink() const
194 {
195  bool guidIsPermaLink = true; // true is default
196 
197  QDomElement guidNode = firstElementByTagNameNS(QString(),
198  QLatin1String("guid"));
199  if (!guidNode.isNull())
200  {
201  if (guidNode.attribute(QLatin1String("isPermaLink"))
202  == QLatin1String("false"))
203  {
204  guidIsPermaLink = false;
205  }
206  }
207 
208  return guidIsPermaLink;
209 }
210 
211 time_t Item::pubDate() const
212 {
213  QString str = extractElementTextNS(QString(), QLatin1String("pubDate"));
214 
215  if (!str.isNull())
216  {
217  return parseDate(str, RFCDate);
218  }
219 
220  // if there is no pubDate, check for dc:date
221  str = extractElementTextNS(dublinCoreNamespace(), QLatin1String("date"));
222  return parseDate(str, ISODate);
223 }
224 
225 time_t Item::expirationDate() const
226 {
227  QString str = extractElementTextNS(QString(), QLatin1String("expirationDate"));
228  return parseDate(str, RFCDate);
229 }
230 
231 Source Item::source() const
232 {
233  return Source(firstElementByTagNameNS(QString(), QLatin1String("source")));
234 }
235 
236 QString Item::rating() const
237 {
238  return extractElementTextNS(QString(), QLatin1String("rating") );
239 }
240 
241 QString Item::debugInfo() const
242 {
243  QString info;
244  info += QLatin1String("### Item: ###################\n");
245  if (!title().isNull())
246  info += QLatin1String("title: #") + title() + QLatin1String("#\n");
247  if (!link().isNull())
248  info += QLatin1String("link: #") + link() + QLatin1String("#\n");
249  if (!description().isNull())
250  info += QLatin1String("description: #") + description() + QLatin1String("#\n");
251  if (!content().isNull())
252  info += QLatin1String("content: #") + content() + QLatin1String("#\n");
253  if (!author().isNull())
254  info += QLatin1String("author: #") + author() + QLatin1String("#\n");
255  if (!comments().isNull())
256  info += QLatin1String("comments: #") + comments() + QLatin1String("#\n");
257  QString dpubdate = dateTimeToString(pubDate());
258  if (!dpubdate.isNull())
259  info += QLatin1String("pubDate: #") + dpubdate + QLatin1String("#\n");
260  if (!guid().isNull())
261  info += QLatin1String("guid: #") + guid() + QLatin1String("#\n");
262  if (guidIsPermaLink())
263  info += QLatin1String("guid is PL: #true#\n");
264  if (!source().isNull())
265  info += source().debugInfo();
266 
267  QList<Category> cats = categories();
268  for (QList<Category>::ConstIterator it = cats.constBegin(); it != cats.constEnd(); ++it)
269  info += (*it).debugInfo();
270  QList<Enclosure> encs = enclosures();
271  for (QList<Enclosure>::ConstIterator it = encs.constBegin(); it != encs.constEnd(); ++it)
272  info += (*it).debugInfo();
273 
274  info += QLatin1String("### Item end ################\n");
275  return info;
276 }
277 
278 QList<QDomElement> Item::unhandledElements() const
279 {
280  // TODO: do not hardcode this list here
281  QList<ElementType> handled;
282  handled.append(ElementType(QLatin1String("title")));
283  handled.append(ElementType(QLatin1String("link")));
284  handled.append(ElementType(QLatin1String("description")));
285  handled.append(ElementType(QLatin1String("pubDate")));
286  handled.append(ElementType(QLatin1String("expirationDate")));
287  handled.append(ElementType(QLatin1String("rating")));
288  handled.append(ElementType(QLatin1String("source")));
289  handled.append(ElementType(QLatin1String("guid")));
290  handled.append(ElementType(QLatin1String("comments")));
291  handled.append(ElementType(QLatin1String("author")));
292  handled.append(ElementType(QLatin1String("date"), dublinCoreNamespace()));
293 
294  QList<QDomElement> notHandled;
295 
296  QDomNodeList children = element().childNodes();
297  for (int i = 0; i < children.size(); ++i)
298  {
299  QDomElement el = children.at(i).toElement();
300  if (!el.isNull()
301  && !handled.contains(ElementType(el.localName(), el.namespaceURI())))
302  {
303  notHandled.append(el);
304  }
305  }
306 
307  return notHandled;
308 }
309 
310 bool Item::accept(SpecificItemVisitor* visitor)
311 {
312  return visitor->visitRSS2Item(this);
313 }
314 
315 } // namespace RSS2
316 } // namespace Syndication
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const
QDomNodeList
QDomNode::namespaceURI
QString namespaceURI() const
QString::isNull
bool isNull() const
QDomNode::toElement
QDomElement toElement() const
QList::append
void append(const T &value)
QDomNode::localName
QString localName() const
QString::isEmpty
bool isEmpty() const
Syndication::RSS2::extractContent
QString extractContent(const ElementWrapper &parent)
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QString
QList
Definition: document.h:35
QLatin1Char
QList::contains
bool contains(const T &value) const
QDomNode::isNull
bool isNull() const
QLatin1String
QDomNodeList::size
int size() const
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
QDomElement
QDomNodeList::at
QDomNode at(int index) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:37:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

syndication/rss2

Skip menu "syndication/rss2"
  • 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