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

0001 /*
0002     This file is part of the syndication library
0003     SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "itematomimpl.h"
0009 #include "categoryatomimpl.h"
0010 #include "enclosureatomimpl.h"
0011 
0012 #include <atom/category.h>
0013 #include <atom/content.h>
0014 #include <atom/link.h>
0015 #include <atom/person.h>
0016 #include <category.h>
0017 #include <constants.h>
0018 #include <enclosure.h>
0019 #include <personimpl.h>
0020 #include <tools.h>
0021 
0022 #include <QDomElement>
0023 #include <QList>
0024 #include <QMultiMap>
0025 #include <QString>
0026 
0027 using Syndication::Atom::Content;
0028 using Syndication::Atom::Link;
0029 using Syndication::Atom::Person;
0030 
0031 namespace Syndication
0032 {
0033 ItemAtomImpl::ItemAtomImpl(const Syndication::Atom::Entry &entry)
0034     : m_entry(entry)
0035 {
0036 }
0037 
0038 QString ItemAtomImpl::title() const
0039 {
0040     return m_entry.title();
0041 }
0042 
0043 QString ItemAtomImpl::link() const
0044 {
0045     const QList<Syndication::Atom::Link> links = m_entry.links();
0046 
0047     // return first link where rel="alternate"
0048     auto it = std::find_if(links.cbegin(), links.cend(), [](const Syndication::Atom::Link &link) {
0049         return link.rel() == QLatin1String("alternate");
0050     });
0051     return it != links.cend() ? it->href() : QString{};
0052 }
0053 
0054 QString ItemAtomImpl::description() const
0055 {
0056     return m_entry.summary();
0057 }
0058 
0059 QString ItemAtomImpl::content() const
0060 {
0061     Content content = m_entry.content();
0062     if (content.isNull()) {
0063         return QString();
0064     }
0065 
0066     return content.asString();
0067 }
0068 
0069 QList<PersonPtr> ItemAtomImpl::authors() const
0070 {
0071     const QList<Syndication::Atom::Person> people = m_entry.authors() + m_entry.contributors();
0072 
0073     QList<PersonPtr> list;
0074     list.reserve(people.size());
0075 
0076     std::transform(people.cbegin(), people.cend(), std::back_inserter(list), [](const Syndication::Atom::Person &person) {
0077         return PersonImplPtr(new PersonImpl(person.name(), person.uri(), person.email()));
0078     });
0079 
0080     return list;
0081 }
0082 
0083 time_t ItemAtomImpl::datePublished() const
0084 {
0085     time_t pub = m_entry.published();
0086     if (pub == 0) {
0087         return m_entry.updated();
0088     } else {
0089         return pub;
0090     }
0091 }
0092 
0093 time_t ItemAtomImpl::dateUpdated() const
0094 {
0095     time_t upd = m_entry.updated();
0096     if (upd == 0) {
0097         return m_entry.published();
0098     } else {
0099         return upd;
0100     }
0101 }
0102 
0103 QString ItemAtomImpl::language() const
0104 {
0105     return m_entry.xmlLang();
0106 }
0107 
0108 QString ItemAtomImpl::id() const
0109 {
0110     const QString id = m_entry.id();
0111     if (!id.isEmpty()) {
0112         return id;
0113     }
0114 
0115     return QStringLiteral("hash:%1").arg(Syndication::calcMD5Sum(title() + description() + link() + content()));
0116 }
0117 
0118 QList<Syndication::EnclosurePtr> ItemAtomImpl::enclosures() const
0119 {
0120     QList<Syndication::EnclosurePtr> list;
0121 
0122     const QList<Syndication::Atom::Link> links = m_entry.links();
0123 
0124     for (const auto &link : links) {
0125         if (link.rel() == QLatin1String("enclosure")) {
0126             list.append(EnclosureAtomImplPtr(new EnclosureAtomImpl(link)));
0127         }
0128     }
0129 
0130     return list;
0131 }
0132 
0133 QList<Syndication::CategoryPtr> ItemAtomImpl::categories() const
0134 {
0135     const QList<Syndication::Atom::Category> cats = m_entry.categories();
0136 
0137     QList<Syndication::CategoryPtr> list;
0138     list.reserve(cats.count());
0139 
0140     std::transform(cats.cbegin(), cats.cend(), std::back_inserter(list), [](const Syndication::Atom::Category &c) {
0141         return CategoryAtomImplPtr(new CategoryAtomImpl(c));
0142     });
0143 
0144     return list;
0145 }
0146 
0147 int ItemAtomImpl::commentsCount() const
0148 {
0149     QString cstr = m_entry.extractElementTextNS(slashNamespace(), QStringLiteral("comments"));
0150     bool ok = false;
0151     int comments = cstr.toInt(&ok);
0152     return ok ? comments : -1;
0153 }
0154 
0155 QString ItemAtomImpl::commentsLink() const
0156 {
0157     return QString();
0158 }
0159 
0160 QString ItemAtomImpl::commentsFeed() const
0161 {
0162     return m_entry.extractElementTextNS(commentApiNamespace(), QStringLiteral("commentRss"));
0163 }
0164 
0165 QString ItemAtomImpl::commentPostUri() const
0166 {
0167     return m_entry.extractElementTextNS(commentApiNamespace(), QStringLiteral("comment"));
0168 }
0169 
0170 Syndication::SpecificItemPtr ItemAtomImpl::specificItem() const
0171 {
0172     return Syndication::SpecificItemPtr(new Syndication::Atom::Entry(m_entry));
0173 }
0174 
0175 QMultiMap<QString, QDomElement> ItemAtomImpl::additionalProperties() const
0176 {
0177     QMultiMap<QString, QDomElement> ret;
0178     const auto unhandledElements = m_entry.unhandledElements();
0179     for (const QDomElement &i : unhandledElements) {
0180         ret.insert(i.namespaceURI() + i.localName(), i);
0181     }
0182 
0183     return ret;
0184 }
0185 
0186 } // namespace Syndication