File indexing completed on 2024-04-21 04:01:01

0001 /*
0002     This file is part of the syndication library
0003     SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "itemrss2impl.h"
0008 #include "categoryrss2impl.h"
0009 #include "enclosurerss2impl.h"
0010 #include <atom/constants.h>
0011 #include <constants.h>
0012 #include <personimpl.h>
0013 #include <rss2/category.h>
0014 #include <rss2/enclosure.h>
0015 #include <tools.h>
0016 
0017 #include <QDomElement>
0018 #include <QList>
0019 #include <QMultiMap>
0020 #include <QString>
0021 
0022 namespace Syndication
0023 {
0024 ItemRSS2Impl::ItemRSS2Impl(const Syndication::RSS2::Item &item)
0025     : m_item(item)
0026 {
0027 }
0028 
0029 QString ItemRSS2Impl::title() const
0030 {
0031     return m_item.title();
0032 }
0033 
0034 QString ItemRSS2Impl::link() const
0035 {
0036     const QString link = m_item.link();
0037     if (!link.isEmpty()) {
0038         return link;
0039     }
0040 
0041     const QString guid = m_item.guid();
0042     if (m_item.guidIsPermaLink()) {
0043         return guid;
0044     }
0045 
0046     return QString();
0047 }
0048 
0049 QString ItemRSS2Impl::description() const
0050 {
0051     return m_item.description();
0052 }
0053 
0054 QString ItemRSS2Impl::content() const
0055 {
0056     return m_item.content();
0057 }
0058 
0059 QList<PersonPtr> ItemRSS2Impl::authors() const
0060 {
0061     QList<PersonPtr> list;
0062 
0063     PersonPtr ptr = personFromString(m_item.author());
0064 
0065     if (!ptr->isNull()) {
0066         list.append(ptr);
0067     }
0068 
0069     return list;
0070 }
0071 
0072 QString ItemRSS2Impl::language() const
0073 {
0074     return QString();
0075 }
0076 
0077 QString ItemRSS2Impl::id() const
0078 {
0079     QString guid = m_item.guid();
0080     if (!guid.isEmpty()) {
0081         return guid;
0082     }
0083 
0084     return QStringLiteral("hash:%1").arg(calcMD5Sum(title() + description() + link() + content()));
0085 }
0086 
0087 time_t ItemRSS2Impl::datePublished() const
0088 {
0089     return m_item.pubDate();
0090 }
0091 
0092 time_t ItemRSS2Impl::dateUpdated() const
0093 {
0094     // Some RSS feeds contain atom elements - return atom:dateUpdated if present
0095     const QString updstr = m_item.extractElementTextNS(Atom::atom1Namespace(), QStringLiteral("updated"));
0096     if (!updstr.isEmpty()) {
0097         return parseDate(updstr, ISODate);
0098     } else {
0099         return datePublished();
0100     }
0101 }
0102 
0103 QList<Syndication::EnclosurePtr> ItemRSS2Impl::enclosures() const
0104 {
0105     const QList<Syndication::RSS2::Enclosure> encs = m_item.enclosures();
0106 
0107     QList<Syndication::EnclosurePtr> list;
0108     list.reserve(encs.size());
0109 
0110     std::transform(encs.cbegin(), encs.cend(), std::back_inserter(list), [this](const Syndication::RSS2::Enclosure &e) {
0111         return EnclosureRSS2ImplPtr(new EnclosureRSS2Impl(m_item, e));
0112     });
0113 
0114     return list;
0115 }
0116 
0117 QList<Syndication::CategoryPtr> ItemRSS2Impl::categories() const
0118 {
0119     const QList<Syndication::RSS2::Category> cats = m_item.categories();
0120 
0121     QList<Syndication::CategoryPtr> list;
0122     list.reserve(cats.size());
0123 
0124     std::transform(cats.cbegin(), cats.cend(), std::back_inserter(list), [](const Syndication::RSS2::Category &c) {
0125         return CategoryRSS2ImplPtr(new CategoryRSS2Impl(c));
0126     });
0127 
0128     return list;
0129 }
0130 
0131 int ItemRSS2Impl::commentsCount() const
0132 {
0133     const QString cstr = m_item.extractElementTextNS(slashNamespace(), QStringLiteral("comments"));
0134     bool ok = false;
0135     int comments = cstr.toInt(&ok);
0136     return ok ? comments : -1;
0137 }
0138 
0139 QString ItemRSS2Impl::commentsLink() const
0140 {
0141     return m_item.comments();
0142 }
0143 
0144 QString ItemRSS2Impl::commentsFeed() const
0145 {
0146     QString t = m_item.extractElementTextNS(commentApiNamespace(), QStringLiteral("commentRss"));
0147     if (t.isNull()) {
0148         t = m_item.extractElementTextNS(commentApiNamespace(), QStringLiteral("commentRSS"));
0149     }
0150     return t;
0151 }
0152 
0153 QString ItemRSS2Impl::commentPostUri() const
0154 {
0155     return m_item.extractElementTextNS(commentApiNamespace(), QStringLiteral("comment"));
0156 }
0157 
0158 Syndication::SpecificItemPtr ItemRSS2Impl::specificItem() const
0159 {
0160     return Syndication::SpecificItemPtr(new Syndication::RSS2::Item(m_item));
0161 }
0162 
0163 QMultiMap<QString, QDomElement> ItemRSS2Impl::additionalProperties() const
0164 {
0165     QMultiMap<QString, QDomElement> ret;
0166 
0167     const auto unhandledElements = m_item.unhandledElements();
0168     for (const QDomElement &i : unhandledElements) {
0169         ret.insert(i.namespaceURI() + i.localName(), i);
0170     }
0171 
0172     return ret;
0173 }
0174 
0175 } // namespace Syndication