syndication/rss2
item.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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(), QString::fromUtf8("description"));
00090 }
00091
00092 QString Item::originalTitle() const
00093 {
00094 return extractElementTextNS(QString(), QString::fromUtf8("title"));
00095 }
00096
00097 QString Item::link() const
00098 {
00099 return extractElementTextNS(QString(), QString::fromUtf8("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
00117 return extractContent(*this);
00118 }
00119
00120 QList<Category> Item::categories() const
00121 {
00122 QList<QDomElement> cats = elementsByTagNameNS(QString(),
00123 QString::fromUtf8("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(), QString::fromUtf8("comments") );
00138 }
00139
00140 QString Item::author() const
00141 {
00142 QString a = extractElementTextNS(QString(), QString::fromUtf8("author") );
00143 if (!a.isNull())
00144 {
00145 return a;
00146 }
00147 else
00148 {
00149
00150 return extractElementTextNS(dublinCoreNamespace(),
00151 QString::fromUtf8("creator") );
00152 }
00153
00154 }
00155
00156 QList<Enclosure> Item::enclosures() const
00157 {
00158 QList<QDomElement> encs = elementsByTagNameNS(QString(),
00159 QString::fromUtf8("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(), QString::fromUtf8("guid") );
00174 }
00175
00176 bool Item::guidIsPermaLink() const
00177 {
00178 bool guidIsPermaLink = true;
00179
00180 QDomElement guidNode = firstElementByTagNameNS(QString(),
00181 QString::fromUtf8("guid"));
00182 if (!guidNode.isNull())
00183 {
00184 if (guidNode.attribute(QString::fromUtf8("isPermaLink"))
00185 == QString::fromUtf8("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(), QString::fromUtf8("pubDate"));
00197
00198 if (!str.isNull())
00199 {
00200 return parseDate(str, RFCDate);
00201 }
00202
00203
00204 str = extractElementTextNS(dublinCoreNamespace(), QString::fromUtf8("date"));
00205 return parseDate(str, ISODate);
00206 }
00207
00208 time_t Item::expirationDate() const
00209 {
00210 QString str = extractElementTextNS(QString(), QString::fromUtf8("expirationDate"));
00211 return parseDate(str, RFCDate);
00212 }
00213
00214 Source Item::source() const
00215 {
00216 return Source(firstElementByTagNameNS(QString(), QString::fromUtf8("source")));
00217 }
00218
00219 QString Item::rating() const
00220 {
00221 return extractElementTextNS(QString(), QString::fromUtf8("rating") );
00222 }
00223
00224 QString Item::debugInfo() const
00225 {
00226 QString info;
00227 info += "### Item: ###################\n";
00228 if (!title().isNull())
00229 info += "title: #" + title() + "#\n";
00230 if (!link().isNull())
00231 info += "link: #" + link() + "#\n";
00232 if (!description().isNull())
00233 info += "description: #" + description() + "#\n";
00234 if (!content().isNull())
00235 info += "content: #" + content() + "#\n";
00236 if (!author().isNull())
00237 info += "author: #" + author() + "#\n";
00238 if (!comments().isNull())
00239 info += "comments: #" + comments() + "#\n";
00240 QString dpubdate = dateTimeToString(pubDate());
00241 if (!dpubdate.isNull())
00242 info += "pubDate: #" + dpubdate + "#\n";
00243 if (!guid().isNull())
00244 info += "guid: #" + guid() + "#\n";
00245 if (guidIsPermaLink())
00246 info += "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 += "### Item end ################\n";
00258 return info;
00259 }
00260
00261 QList<QDomElement> Item::unhandledElements() const
00262 {
00263
00264 QList<ElementType> handled;
00265 handled.append(QString::fromUtf8("title"));
00266 handled.append(QString::fromUtf8("link"));
00267 handled.append(QString::fromUtf8("description"));
00268 handled.append(QString::fromUtf8("pubDate"));
00269 handled.append(QString::fromUtf8("expirationDate"));
00270 handled.append(QString::fromUtf8("rating"));
00271 handled.append(QString::fromUtf8("source"));
00272 handled.append(QString::fromUtf8("guid"));
00273 handled.append(QString::fromUtf8("comments"));
00274 handled.append(QString::fromUtf8("author"));
00275 handled.append(ElementType(QString::fromUtf8("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 }
00299 }