| 1 | /* |
| 2 | This file is part of the syndication library |
| 3 | SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org> |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "itemrss2impl.h" |
| 8 | #include "categoryrss2impl.h" |
| 9 | #include "enclosurerss2impl.h" |
| 10 | #include <atom/constants.h> |
| 11 | #include <constants.h> |
| 12 | #include <personimpl.h> |
| 13 | #include <rss2/category.h> |
| 14 | #include <rss2/enclosure.h> |
| 15 | #include <tools.h> |
| 16 | |
| 17 | #include <QDomElement> |
| 18 | #include <QList> |
| 19 | #include <QMultiMap> |
| 20 | #include <QString> |
| 21 | |
| 22 | namespace Syndication |
| 23 | { |
| 24 | ItemRSS2Impl::(const Syndication::RSS2::Item &item) |
| 25 | : m_item(item) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | QString ItemRSS2Impl::() const |
| 30 | { |
| 31 | return m_item.title(); |
| 32 | } |
| 33 | |
| 34 | QString ItemRSS2Impl::() const |
| 35 | { |
| 36 | const QString link = m_item.link(); |
| 37 | if (!link.isEmpty()) { |
| 38 | return link; |
| 39 | } |
| 40 | |
| 41 | const QString guid = m_item.guid(); |
| 42 | if (m_item.guidIsPermaLink()) { |
| 43 | return guid; |
| 44 | } |
| 45 | |
| 46 | return QString(); |
| 47 | } |
| 48 | |
| 49 | QString ItemRSS2Impl::() const |
| 50 | { |
| 51 | return m_item.description(); |
| 52 | } |
| 53 | |
| 54 | QString ItemRSS2Impl::() const |
| 55 | { |
| 56 | return m_item.content(); |
| 57 | } |
| 58 | |
| 59 | QList<PersonPtr> ItemRSS2Impl::() const |
| 60 | { |
| 61 | QList<PersonPtr> list; |
| 62 | |
| 63 | PersonPtr ptr = personFromString(str: m_item.author()); |
| 64 | |
| 65 | if (!ptr->isNull()) { |
| 66 | list.append(t: ptr); |
| 67 | } |
| 68 | |
| 69 | return list; |
| 70 | } |
| 71 | |
| 72 | QString ItemRSS2Impl::() const |
| 73 | { |
| 74 | return QString(); |
| 75 | } |
| 76 | |
| 77 | QString ItemRSS2Impl::() const |
| 78 | { |
| 79 | QString guid = m_item.guid(); |
| 80 | if (!guid.isEmpty()) { |
| 81 | return guid; |
| 82 | } |
| 83 | |
| 84 | return QStringLiteral("hash:%1" ).arg(a: calcMD5Sum(str: title() + description() + link() + content())); |
| 85 | } |
| 86 | |
| 87 | time_t ItemRSS2Impl::() const |
| 88 | { |
| 89 | return m_item.pubDate(); |
| 90 | } |
| 91 | |
| 92 | time_t ItemRSS2Impl::() const |
| 93 | { |
| 94 | // Some RSS feeds contain atom elements - return atom:dateUpdated if present |
| 95 | const QString updstr = m_item.extractElementTextNS(namespaceURI: Atom::atom1Namespace(), QStringLiteral("updated" )); |
| 96 | if (!updstr.isEmpty()) { |
| 97 | return parseDate(str: updstr, hint: ISODate); |
| 98 | } else { |
| 99 | return datePublished(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | QList<Syndication::EnclosurePtr> ItemRSS2Impl::() const |
| 104 | { |
| 105 | const QList<Syndication::RSS2::Enclosure> encs = m_item.enclosures(); |
| 106 | |
| 107 | QList<Syndication::EnclosurePtr> list; |
| 108 | list.reserve(asize: encs.size()); |
| 109 | |
| 110 | std::transform(first: encs.cbegin(), last: encs.cend(), result: std::back_inserter(x&: list), unary_op: [this](const Syndication::RSS2::Enclosure &e) { |
| 111 | return EnclosureRSS2ImplPtr(new EnclosureRSS2Impl(m_item, e)); |
| 112 | }); |
| 113 | |
| 114 | return list; |
| 115 | } |
| 116 | |
| 117 | QList<Syndication::CategoryPtr> ItemRSS2Impl::() const |
| 118 | { |
| 119 | const QList<Syndication::RSS2::Category> cats = m_item.categories(); |
| 120 | |
| 121 | QList<Syndication::CategoryPtr> list; |
| 122 | list.reserve(asize: cats.size()); |
| 123 | |
| 124 | std::transform(first: cats.cbegin(), last: cats.cend(), result: std::back_inserter(x&: list), unary_op: [](const Syndication::RSS2::Category &c) { |
| 125 | return CategoryRSS2ImplPtr(new CategoryRSS2Impl(c)); |
| 126 | }); |
| 127 | |
| 128 | return list; |
| 129 | } |
| 130 | |
| 131 | int ItemRSS2Impl::() const |
| 132 | { |
| 133 | const QString cstr = m_item.extractElementTextNS(namespaceURI: slashNamespace(), QStringLiteral("comments" )); |
| 134 | bool ok = false; |
| 135 | int = cstr.toInt(ok: &ok); |
| 136 | return ok ? comments : -1; |
| 137 | } |
| 138 | |
| 139 | QString ItemRSS2Impl::() const |
| 140 | { |
| 141 | return m_item.comments(); |
| 142 | } |
| 143 | |
| 144 | QString ItemRSS2Impl::() const |
| 145 | { |
| 146 | QString t = m_item.extractElementTextNS(namespaceURI: commentApiNamespace(), QStringLiteral("commentRss" )); |
| 147 | if (t.isNull()) { |
| 148 | t = m_item.extractElementTextNS(namespaceURI: commentApiNamespace(), QStringLiteral("commentRSS" )); |
| 149 | } |
| 150 | return t; |
| 151 | } |
| 152 | |
| 153 | QString ItemRSS2Impl::() const |
| 154 | { |
| 155 | return m_item.extractElementTextNS(namespaceURI: commentApiNamespace(), QStringLiteral("comment" )); |
| 156 | } |
| 157 | |
| 158 | Syndication::SpecificItemPtr ItemRSS2Impl::() const |
| 159 | { |
| 160 | return Syndication::SpecificItemPtr(new Syndication::RSS2::Item(m_item)); |
| 161 | } |
| 162 | |
| 163 | QMultiMap<QString, QDomElement> ItemRSS2Impl::() const |
| 164 | { |
| 165 | QMultiMap<QString, QDomElement> ret; |
| 166 | |
| 167 | const auto unhandledElements = m_item.unhandledElements(); |
| 168 | for (const QDomElement &i : unhandledElements) { |
| 169 | ret.insert(key: i.namespaceURI() + i.localName(), value: i); |
| 170 | } |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | } // namespace Syndication |
| 176 | |