/* This file is part of the syndication library SPDX-FileCopyrightText: 2006 Frank Osterfeld SPDX-License-Identifier: LGPL-2.0-or-later */ #include "itematomimpl.h" #include "categoryatomimpl.h" #include "enclosureatomimpl.h" #include #include #include #include #include #include #include #include #include #include #include #include #include using Syndication::Atom::Content; using Syndication::Atom::Link; using Syndication::Atom::Person; namespace Syndication { ItemAtomImpl::ItemAtomImpl(const Syndication::Atom::Entry &entry) : m_entry(entry) { } QString ItemAtomImpl::title() const { return m_entry.title(); } QString ItemAtomImpl::link() const { const QList links = m_entry.links(); // return first link where rel="alternate" auto it = std::find_if(links.cbegin(), links.cend(), [](const Syndication::Atom::Link &link) { return link.rel() == QLatin1String("alternate"); }); return it != links.cend() ? it->href() : QString{}; } QString ItemAtomImpl::description() const { return m_entry.summary(); } QString ItemAtomImpl::content() const { Content content = m_entry.content(); if (content.isNull()) { return QString(); } return content.asString(); } QList ItemAtomImpl::authors() const { const QList people = m_entry.authors() + m_entry.contributors(); QList list; list.reserve(people.size()); std::transform(people.cbegin(), people.cend(), std::back_inserter(list), [](const Syndication::Atom::Person &person) { return PersonImplPtr(new PersonImpl(person.name(), person.uri(), person.email())); }); return list; } time_t ItemAtomImpl::datePublished() const { time_t pub = m_entry.published(); if (pub == 0) { return m_entry.updated(); } else { return pub; } } time_t ItemAtomImpl::dateUpdated() const { time_t upd = m_entry.updated(); if (upd == 0) { return m_entry.published(); } else { return upd; } } QString ItemAtomImpl::language() const { return m_entry.xmlLang(); } QString ItemAtomImpl::id() const { const QString id = m_entry.id(); if (!id.isEmpty()) { return id; } return QStringLiteral("hash:%1").arg(Syndication::calcMD5Sum(title() + description() + link() + content())); } QList ItemAtomImpl::enclosures() const { QList list; const QList links = m_entry.links(); for (const auto &link : links) { if (link.rel() == QLatin1String("enclosure")) { list.append(EnclosureAtomImplPtr(new EnclosureAtomImpl(link))); } } return list; } QList ItemAtomImpl::categories() const { const QList cats = m_entry.categories(); QList list; list.reserve(cats.count()); std::transform(cats.cbegin(), cats.cend(), std::back_inserter(list), [](const Syndication::Atom::Category &c) { return CategoryAtomImplPtr(new CategoryAtomImpl(c)); }); return list; } int ItemAtomImpl::commentsCount() const { QString cstr = m_entry.extractElementTextNS(slashNamespace(), QStringLiteral("comments")); bool ok = false; int comments = cstr.toInt(&ok); return ok ? comments : -1; } QString ItemAtomImpl::commentsLink() const { return QString(); } QString ItemAtomImpl::commentsFeed() const { return m_entry.extractElementTextNS(commentApiNamespace(), QStringLiteral("commentRss")); } QString ItemAtomImpl::commentPostUri() const { return m_entry.extractElementTextNS(commentApiNamespace(), QStringLiteral("comment")); } Syndication::SpecificItemPtr ItemAtomImpl::specificItem() const { return Syndication::SpecificItemPtr(new Syndication::Atom::Entry(m_entry)); } QMultiMap ItemAtomImpl::additionalProperties() const { QMultiMap ret; const auto unhandledElements = m_entry.unhandledElements(); for (const QDomElement &i : unhandledElements) { ret.insert(i.namespaceURI() + i.localName(), i); } return ret; } } // namespace Syndication