syndication/rss2
item.cpp
00001 /* 00002 * This file is part of the syndication library 00003 * 00004 * Copyright (C) 2005 Frank Osterfeld <osterfeld@kde.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 */ 00022 00023 #include <rss2/item.h> 00024 #include <rss2/category.h> 00025 #include <rss2/enclosure.h> 00026 #include <rss2/source.h> 00027 #include <rss2/tools_p.h> 00028 #include <constants.h> 00029 #include <specificitem.h> 00030 #include <specificitemvisitor.h> 00031 #include <tools.h> 00032 00033 #include <QtXml/QDomElement> 00034 #include <QtCore/QString> 00035 #include <QtCore/QList> 00036 00037 namespace Syndication { 00038 namespace RSS2 { 00039 00040 class Item::ItemPrivate 00041 { 00042 public: 00043 00044 boost::shared_ptr<Document> doc; 00045 }; 00046 00047 Item::Item(boost::shared_ptr<Document> doc) : ElementWrapper(), d(new ItemPrivate) 00048 { 00049 d->doc = doc; 00050 } 00051 00052 Item::Item(const QDomElement& element, boost::shared_ptr<Document> doc) : ElementWrapper(element), d(new ItemPrivate) 00053 { 00054 d->doc = doc; 00055 } 00056 00057 Item::~Item() 00058 { 00059 } 00060 00061 Item::Item(const Item& other) : ElementWrapper(other), SpecificItem(other) 00062 { 00063 d = other.d; 00064 } 00065 00066 Item& Item::operator=(const Item& other) 00067 { 00068 ElementWrapper::operator=(other); 00069 SpecificItem::operator=(other); 00070 d = other.d; 00071 return *this; 00072 } 00073 00074 QString Item::title() const 00075 { 00076 if (!d->doc) 00077 return originalTitle(); 00078 00079 bool isCDATA = false; 00080 bool containsMarkup = false; 00081 d->doc->getItemTitleFormatInfo(&isCDATA, &containsMarkup); 00082 00083 return normalize(originalTitle(), isCDATA, containsMarkup); 00084 } 00085 00086 00087 QString Item::originalDescription() const 00088 { 00089 return extractElementTextNS(QString(), QLatin1String("description")); 00090 } 00091 00092 QString Item::originalTitle() const 00093 { 00094 return extractElementTextNS(QString(), QLatin1String("title")); 00095 } 00096 00097 QString Item::link() const 00098 { 00099 return extractElementTextNS(QString(), QLatin1String("link") ); 00100 } 00101 00102 QString Item::description() const 00103 { 00104 if (!d->doc) 00105 return originalDescription(); 00106 00107 bool isCDATA = false; 00108 bool containsMarkup = false; 00109 d->doc->getItemDescriptionFormatInfo(&isCDATA, &containsMarkup); 00110 00111 return normalize(originalDescription(), isCDATA, containsMarkup); 00112 } 00113 00114 QString Item::content() const 00115 { 00116 // parse encoded stuff from content:encoded, xhtml:body and friends into content 00117 return extractContent(*this); 00118 } 00119 00120 QList<Category> Item::categories() const 00121 { 00122 QList<QDomElement> cats = elementsByTagNameNS(QString(), 00123 QLatin1String("category")); 00124 00125 QList<Category> categories; 00126 00127 QList<QDomElement>::ConstIterator it = cats.constBegin(); 00128 for ( ; it != cats.constEnd(); ++it) 00129 { 00130 categories.append(Category(*it)); 00131 } 00132 return categories; 00133 } 00134 00135 QString Item::comments() const 00136 { 00137 return extractElementTextNS(QString(), QLatin1String("comments") ); 00138 } 00139 00140 QString Item::author() const 00141 { 00142 QString a = extractElementTextNS(QString(), QLatin1String("author") ); 00143 if (!a.isNull()) 00144 { 00145 return a; 00146 } 00147 else 00148 { 00149 // if author is not available, fall back to dc:creator 00150 return extractElementTextNS(dublinCoreNamespace(), 00151 QLatin1String("creator") ); 00152 } 00153 00154 } 00155 00156 QList<Enclosure> Item::enclosures() const 00157 { 00158 QList<QDomElement> encs = elementsByTagNameNS(QString(), 00159 QLatin1String("enclosure")); 00160 00161 QList<Enclosure> enclosures; 00162 00163 QList<QDomElement>::ConstIterator it = encs.constBegin(); 00164 for ( ; it != encs.constEnd(); ++it) 00165 { 00166 enclosures.append(Enclosure(*it)); 00167 } 00168 return enclosures; 00169 } 00170 00171 QString Item::guid() const 00172 { 00173 return extractElementTextNS(QString(), QLatin1String("guid") ); 00174 } 00175 00176 bool Item::guidIsPermaLink() const 00177 { 00178 bool guidIsPermaLink = true; // true is default 00179 00180 QDomElement guidNode = firstElementByTagNameNS(QString(), 00181 QLatin1String("guid")); 00182 if (!guidNode.isNull()) 00183 { 00184 if (guidNode.attribute(QLatin1String("isPermaLink")) 00185 == QLatin1String("false")) 00186 { 00187 guidIsPermaLink = false; 00188 } 00189 } 00190 00191 return guidIsPermaLink; 00192 } 00193 00194 time_t Item::pubDate() const 00195 { 00196 QString str = extractElementTextNS(QString(), QLatin1String("pubDate")); 00197 00198 if (!str.isNull()) 00199 { 00200 return parseDate(str, RFCDate); 00201 } 00202 00203 // if there is no pubDate, check for dc:date 00204 str = extractElementTextNS(dublinCoreNamespace(), QLatin1String("date")); 00205 return parseDate(str, ISODate); 00206 } 00207 00208 time_t Item::expirationDate() const 00209 { 00210 QString str = extractElementTextNS(QString(), QLatin1String("expirationDate")); 00211 return parseDate(str, RFCDate); 00212 } 00213 00214 Source Item::source() const 00215 { 00216 return Source(firstElementByTagNameNS(QString(), QLatin1String("source"))); 00217 } 00218 00219 QString Item::rating() const 00220 { 00221 return extractElementTextNS(QString(), QLatin1String("rating") ); 00222 } 00223 00224 QString Item::debugInfo() const 00225 { 00226 QString info; 00227 info += QLatin1String("### Item: ###################\n"); 00228 if (!title().isNull()) 00229 info += QLatin1String("title: #") + title() + QLatin1String("#\n"); 00230 if (!link().isNull()) 00231 info += QLatin1String("link: #") + link() + QLatin1String("#\n"); 00232 if (!description().isNull()) 00233 info += QLatin1String("description: #") + description() + QLatin1String("#\n"); 00234 if (!content().isNull()) 00235 info += QLatin1String("content: #") + content() + QLatin1String("#\n"); 00236 if (!author().isNull()) 00237 info += QLatin1String("author: #") + author() + QLatin1String("#\n"); 00238 if (!comments().isNull()) 00239 info += QLatin1String("comments: #") + comments() + QLatin1String("#\n"); 00240 QString dpubdate = dateTimeToString(pubDate()); 00241 if (!dpubdate.isNull()) 00242 info += QLatin1String("pubDate: #") + dpubdate + QLatin1String("#\n"); 00243 if (!guid().isNull()) 00244 info += QLatin1String("guid: #") + guid() + QLatin1String("#\n"); 00245 if (guidIsPermaLink()) 00246 info += QLatin1String("guid is PL: #true#\n"); 00247 if (!source().isNull()) 00248 info += source().debugInfo(); 00249 00250 QList<Category> cats = categories(); 00251 for (QList<Category>::ConstIterator it = cats.constBegin(); it != cats.constEnd(); ++it) 00252 info += (*it).debugInfo(); 00253 QList<Enclosure> encs = enclosures(); 00254 for (QList<Enclosure>::ConstIterator it = encs.constBegin(); it != encs.constEnd(); ++it) 00255 info += (*it).debugInfo(); 00256 00257 info += QLatin1String("### Item end ################\n"); 00258 return info; 00259 } 00260 00261 QList<QDomElement> Item::unhandledElements() const 00262 { 00263 // TODO: do not hardcode this list here 00264 QList<ElementType> handled; 00265 handled.append(ElementType(QLatin1String("title"))); 00266 handled.append(ElementType(QLatin1String("link"))); 00267 handled.append(ElementType(QLatin1String("description"))); 00268 handled.append(ElementType(QLatin1String("pubDate"))); 00269 handled.append(ElementType(QLatin1String("expirationDate"))); 00270 handled.append(ElementType(QLatin1String("rating"))); 00271 handled.append(ElementType(QLatin1String("source"))); 00272 handled.append(ElementType(QLatin1String("guid"))); 00273 handled.append(ElementType(QLatin1String("comments"))); 00274 handled.append(ElementType(QLatin1String("author"))); 00275 handled.append(ElementType(QLatin1String("date"), dublinCoreNamespace())); 00276 00277 QList<QDomElement> notHandled; 00278 00279 QDomNodeList children = element().childNodes(); 00280 for (int i = 0; i < children.size(); ++i) 00281 { 00282 QDomElement el = children.at(i).toElement(); 00283 if (!el.isNull() 00284 && !handled.contains(ElementType(el.localName(), el.namespaceURI()))) 00285 { 00286 notHandled.append(el); 00287 } 00288 } 00289 00290 return notHandled; 00291 } 00292 00293 bool Item::accept(SpecificItemVisitor* visitor) 00294 { 00295 return visitor->visitRSS2Item(this); 00296 } 00297 00298 } // namespace RSS2 00299 } // namespace Syndication
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:58:29 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:58:29 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.