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

syndication/rss2

  • 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  return extractElementTextNS(QString(), QLatin1String("link") );
100 }
101 
102 QString Item::description() const
103 {
104  if (!d->doc)
105  return originalDescription();
106 
107  bool isCDATA = false;
108  bool containsMarkup = false;
109  d->doc->getItemDescriptionFormatInfo(&isCDATA, &containsMarkup);
110 
111  return normalize(originalDescription(), isCDATA, containsMarkup);
112 }
113 
114 QString Item::content() const
115 {
116  // parse encoded stuff from content:encoded, xhtml:body and friends into content
117  return extractContent(*this);
118 }
119 
120 QList<Category> Item::categories() const
121 {
122  QList<QDomElement> cats = elementsByTagNameNS(QString(),
123  QLatin1String("category"));
124 
125  QList<Category> categories;
126 
127  QList<QDomElement>::ConstIterator it = cats.constBegin();
128  for ( ; it != cats.constEnd(); ++it)
129  {
130  categories.append(Category(*it));
131  }
132  return categories;
133 }
134 
135 QString Item::comments() const
136 {
137  return extractElementTextNS(QString(), QLatin1String("comments") );
138 }
139 
140 QString Item::author() const
141 {
142  QString a = extractElementTextNS(QString(), QLatin1String("author") );
143  if (!a.isNull())
144  {
145  return a;
146  }
147  else
148  {
149  // if author is not available, fall back to dc:creator
150  return extractElementTextNS(dublinCoreNamespace(),
151  QLatin1String("creator") );
152  }
153 
154 }
155 
156 QList<Enclosure> Item::enclosures() const
157 {
158  QList<QDomElement> encs = elementsByTagNameNS(QString(),
159  QLatin1String("enclosure"));
160 
161  QList<Enclosure> enclosures;
162 
163  QList<QDomElement>::ConstIterator it = encs.constBegin();
164  for ( ; it != encs.constEnd(); ++it)
165  {
166  enclosures.append(Enclosure(*it));
167  }
168  return enclosures;
169 }
170 
171 QString Item::guid() const
172 {
173  return extractElementTextNS(QString(), QLatin1String("guid") );
174 }
175 
176 bool Item::guidIsPermaLink() const
177 {
178  bool guidIsPermaLink = true; // true is default
179 
180  QDomElement guidNode = firstElementByTagNameNS(QString(),
181  QLatin1String("guid"));
182  if (!guidNode.isNull())
183  {
184  if (guidNode.attribute(QLatin1String("isPermaLink"))
185  == QLatin1String("false"))
186  {
187  guidIsPermaLink = false;
188  }
189  }
190 
191  return guidIsPermaLink;
192 }
193 
194 time_t Item::pubDate() const
195 {
196  QString str = extractElementTextNS(QString(), QLatin1String("pubDate"));
197 
198  if (!str.isNull())
199  {
200  return parseDate(str, RFCDate);
201  }
202 
203  // if there is no pubDate, check for dc:date
204  str = extractElementTextNS(dublinCoreNamespace(), QLatin1String("date"));
205  return parseDate(str, ISODate);
206 }
207 
208 time_t Item::expirationDate() const
209 {
210  QString str = extractElementTextNS(QString(), QLatin1String("expirationDate"));
211  return parseDate(str, RFCDate);
212 }
213 
214 Source Item::source() const
215 {
216  return Source(firstElementByTagNameNS(QString(), QLatin1String("source")));
217 }
218 
219 QString Item::rating() const
220 {
221  return extractElementTextNS(QString(), QLatin1String("rating") );
222 }
223 
224 QString Item::debugInfo() const
225 {
226  QString info;
227  info += QLatin1String("### Item: ###################\n");
228  if (!title().isNull())
229  info += QLatin1String("title: #") + title() + QLatin1String("#\n");
230  if (!link().isNull())
231  info += QLatin1String("link: #") + link() + QLatin1String("#\n");
232  if (!description().isNull())
233  info += QLatin1String("description: #") + description() + QLatin1String("#\n");
234  if (!content().isNull())
235  info += QLatin1String("content: #") + content() + QLatin1String("#\n");
236  if (!author().isNull())
237  info += QLatin1String("author: #") + author() + QLatin1String("#\n");
238  if (!comments().isNull())
239  info += QLatin1String("comments: #") + comments() + QLatin1String("#\n");
240  QString dpubdate = dateTimeToString(pubDate());
241  if (!dpubdate.isNull())
242  info += QLatin1String("pubDate: #") + dpubdate + QLatin1String("#\n");
243  if (!guid().isNull())
244  info += QLatin1String("guid: #") + guid() + QLatin1String("#\n");
245  if (guidIsPermaLink())
246  info += QLatin1String("guid is PL: #true#\n");
247  if (!source().isNull())
248  info += source().debugInfo();
249 
250  QList<Category> cats = categories();
251  for (QList<Category>::ConstIterator it = cats.constBegin(); it != cats.constEnd(); ++it)
252  info += (*it).debugInfo();
253  QList<Enclosure> encs = enclosures();
254  for (QList<Enclosure>::ConstIterator it = encs.constBegin(); it != encs.constEnd(); ++it)
255  info += (*it).debugInfo();
256 
257  info += QLatin1String("### Item end ################\n");
258  return info;
259 }
260 
261 QList<QDomElement> Item::unhandledElements() const
262 {
263  // TODO: do not hardcode this list here
264  QList<ElementType> handled;
265  handled.append(ElementType(QLatin1String("title")));
266  handled.append(ElementType(QLatin1String("link")));
267  handled.append(ElementType(QLatin1String("description")));
268  handled.append(ElementType(QLatin1String("pubDate")));
269  handled.append(ElementType(QLatin1String("expirationDate")));
270  handled.append(ElementType(QLatin1String("rating")));
271  handled.append(ElementType(QLatin1String("source")));
272  handled.append(ElementType(QLatin1String("guid")));
273  handled.append(ElementType(QLatin1String("comments")));
274  handled.append(ElementType(QLatin1String("author")));
275  handled.append(ElementType(QLatin1String("date"), dublinCoreNamespace()));
276 
277  QList<QDomElement> notHandled;
278 
279  QDomNodeList children = element().childNodes();
280  for (int i = 0; i < children.size(); ++i)
281  {
282  QDomElement el = children.at(i).toElement();
283  if (!el.isNull()
284  && !handled.contains(ElementType(el.localName(), el.namespaceURI())))
285  {
286  notHandled.append(el);
287  }
288  }
289 
290  return notHandled;
291 }
292 
293 bool Item::accept(SpecificItemVisitor* visitor)
294 {
295  return visitor->visitRSS2Item(this);
296 }
297 
298 } // namespace RSS2
299 } // 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:36 by doxygen 1.8.3.1 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-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