File indexing completed on 2024-04-14 03:58:25

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 "feedatomimpl.h"
0009 #include "categoryatomimpl.h"
0010 #include "imageatomimpl.h"
0011 #include "itematomimpl.h"
0012 #include <atom/category.h>
0013 #include <atom/entry.h>
0014 #include <atom/link.h>
0015 #include <atom/person.h>
0016 #include <personimpl.h>
0017 
0018 #include <QDomElement>
0019 #include <QList>
0020 #include <QMultiMap>
0021 #include <QString>
0022 
0023 namespace Syndication
0024 {
0025 FeedAtomImpl::FeedAtomImpl(Syndication::Atom::FeedDocumentPtr doc)
0026     : m_doc(doc)
0027 {
0028 }
0029 
0030 Syndication::SpecificDocumentPtr FeedAtomImpl::specificDocument() const
0031 {
0032     return m_doc;
0033 }
0034 
0035 QList<Syndication::ItemPtr> FeedAtomImpl::items() const
0036 {
0037     const QList<Syndication::Atom::Entry> entries = m_doc->entries();
0038 
0039     QList<ItemPtr> items;
0040     items.reserve(entries.count());
0041 
0042     std::transform(entries.cbegin(), entries.cend(), std::back_inserter(items), [](const Syndication::Atom::Entry &entry) {
0043         return ItemAtomImplPtr(new ItemAtomImpl(entry));
0044     });
0045 
0046     return items;
0047 }
0048 
0049 QList<Syndication::CategoryPtr> FeedAtomImpl::categories() const
0050 {
0051     const QList<Syndication::Atom::Category> entries = m_doc->categories();
0052     QList<CategoryPtr> categories;
0053     categories.reserve(entries.count());
0054 
0055     std::transform(entries.cbegin(), entries.cend(), std::back_inserter(categories), [](const Syndication::Atom::Category &entry) {
0056         return CategoryAtomImplPtr(new CategoryAtomImpl(entry));
0057     });
0058 
0059     return categories;
0060 }
0061 
0062 QString FeedAtomImpl::title() const
0063 {
0064     return m_doc->title();
0065 }
0066 
0067 QString FeedAtomImpl::link() const
0068 {
0069     const QList<Syndication::Atom::Link> links = m_doc->links();
0070     // return first link where rel="alternate"
0071     // TODO: if there are multiple "alternate" links, find other criteria to choose one of them
0072     auto it = std::find_if(links.cbegin(), links.cend(), [](const Syndication::Atom::Link &link) {
0073         return link.rel() == QLatin1String("alternate");
0074     });
0075     return it != links.cend() ? it->href() : QString{};
0076 }
0077 
0078 QString FeedAtomImpl::description() const
0079 {
0080     return m_doc->subtitle();
0081 }
0082 
0083 QList<PersonPtr> FeedAtomImpl::authors() const
0084 {
0085     const QList<Syndication::Atom::Person> people = m_doc->authors() + m_doc->contributors();
0086 
0087     QList<PersonPtr> list;
0088     list.reserve(people.size());
0089 
0090     std::transform(people.cbegin(), people.cend(), std::back_inserter(list), [](const Syndication::Atom::Person &person) {
0091         return PersonImplPtr(new PersonImpl(person.name(), person.uri(), person.email()));
0092     });
0093 
0094     return list;
0095 }
0096 
0097 QString FeedAtomImpl::language() const
0098 {
0099     return m_doc->xmlLang();
0100 }
0101 
0102 QString FeedAtomImpl::copyright() const
0103 {
0104     return m_doc->rights();
0105 }
0106 
0107 ImagePtr FeedAtomImpl::image() const
0108 {
0109     return ImageAtomImplPtr(new ImageAtomImpl(m_doc->logo()));
0110 }
0111 
0112 ImagePtr FeedAtomImpl::icon() const
0113 {
0114     return ImageAtomImplPtr(new ImageAtomImpl(m_doc->icon()));
0115 }
0116 
0117 QMultiMap<QString, QDomElement> FeedAtomImpl::additionalProperties() const
0118 {
0119     QMultiMap<QString, QDomElement> ret;
0120     const auto unhandledElements = m_doc->unhandledElements();
0121     for (const QDomElement &i : unhandledElements) {
0122         ret.insert(i.namespaceURI() + i.localName(), i);
0123     }
0124 
0125     return ret;
0126 }
0127 
0128 } // namespace Syndication