| 1 | /* |
| 2 | This file is part of the syndication library |
| 3 | SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "feedatomimpl.h" |
| 9 | #include "categoryatomimpl.h" |
| 10 | #include "imageatomimpl.h" |
| 11 | #include "itematomimpl.h" |
| 12 | #include <atom/category.h> |
| 13 | #include <atom/entry.h> |
| 14 | #include <atom/link.h> |
| 15 | #include <atom/person.h> |
| 16 | #include <personimpl.h> |
| 17 | |
| 18 | #include <QDomElement> |
| 19 | #include <QList> |
| 20 | #include <QMultiMap> |
| 21 | #include <QString> |
| 22 | |
| 23 | namespace Syndication |
| 24 | { |
| 25 | FeedAtomImpl::FeedAtomImpl(Syndication::Atom::FeedDocumentPtr doc) |
| 26 | : m_doc(doc) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | Syndication::SpecificDocumentPtr FeedAtomImpl::specificDocument() const |
| 31 | { |
| 32 | return m_doc; |
| 33 | } |
| 34 | |
| 35 | QList<Syndication::ItemPtr> FeedAtomImpl::items() const |
| 36 | { |
| 37 | const QList<Syndication::Atom::Entry> entries = m_doc->entries(); |
| 38 | |
| 39 | QList<ItemPtr> items; |
| 40 | items.reserve(asize: entries.count()); |
| 41 | |
| 42 | std::transform(first: entries.cbegin(), last: entries.cend(), result: std::back_inserter(x&: items), unary_op: [](const Syndication::Atom::Entry &entry) { |
| 43 | return ItemAtomImplPtr(new ItemAtomImpl(entry)); |
| 44 | }); |
| 45 | |
| 46 | return items; |
| 47 | } |
| 48 | |
| 49 | QList<Syndication::CategoryPtr> FeedAtomImpl::categories() const |
| 50 | { |
| 51 | const QList<Syndication::Atom::Category> entries = m_doc->categories(); |
| 52 | QList<CategoryPtr> categories; |
| 53 | categories.reserve(asize: entries.count()); |
| 54 | |
| 55 | std::transform(first: entries.cbegin(), last: entries.cend(), result: std::back_inserter(x&: categories), unary_op: [](const Syndication::Atom::Category &entry) { |
| 56 | return CategoryAtomImplPtr(new CategoryAtomImpl(entry)); |
| 57 | }); |
| 58 | |
| 59 | return categories; |
| 60 | } |
| 61 | |
| 62 | QString FeedAtomImpl::title() const |
| 63 | { |
| 64 | return m_doc->title(); |
| 65 | } |
| 66 | |
| 67 | QString FeedAtomImpl::link() const |
| 68 | { |
| 69 | const QList<Syndication::Atom::Link> links = m_doc->links(); |
| 70 | // return first link where rel="alternate" |
| 71 | // TODO: if there are multiple "alternate" links, find other criteria to choose one of them |
| 72 | auto it = std::find_if(first: links.cbegin(), last: links.cend(), pred: [](const Syndication::Atom::Link &link) { |
| 73 | return link.rel() == QLatin1String("alternate" ); |
| 74 | }); |
| 75 | return it != links.cend() ? it->href() : QString{}; |
| 76 | } |
| 77 | |
| 78 | QString FeedAtomImpl::description() const |
| 79 | { |
| 80 | return m_doc->subtitle(); |
| 81 | } |
| 82 | |
| 83 | QList<PersonPtr> FeedAtomImpl::authors() const |
| 84 | { |
| 85 | const QList<Syndication::Atom::Person> people = m_doc->authors() + m_doc->contributors(); |
| 86 | |
| 87 | QList<PersonPtr> list; |
| 88 | list.reserve(asize: people.size()); |
| 89 | |
| 90 | std::transform(first: people.cbegin(), last: people.cend(), result: std::back_inserter(x&: list), unary_op: [](const Syndication::Atom::Person &person) { |
| 91 | return PersonImplPtr(new PersonImpl(person.name(), person.uri(), person.email())); |
| 92 | }); |
| 93 | |
| 94 | return list; |
| 95 | } |
| 96 | |
| 97 | QString FeedAtomImpl::language() const |
| 98 | { |
| 99 | return m_doc->xmlLang(); |
| 100 | } |
| 101 | |
| 102 | QString FeedAtomImpl::copyright() const |
| 103 | { |
| 104 | return m_doc->rights(); |
| 105 | } |
| 106 | |
| 107 | ImagePtr FeedAtomImpl::image() const |
| 108 | { |
| 109 | return ImageAtomImplPtr(new ImageAtomImpl(m_doc->logo())); |
| 110 | } |
| 111 | |
| 112 | ImagePtr FeedAtomImpl::icon() const |
| 113 | { |
| 114 | return ImageAtomImplPtr(new ImageAtomImpl(m_doc->icon())); |
| 115 | } |
| 116 | |
| 117 | QMultiMap<QString, QDomElement> FeedAtomImpl::additionalProperties() const |
| 118 | { |
| 119 | QMultiMap<QString, QDomElement> ret; |
| 120 | const auto unhandledElements = m_doc->unhandledElements(); |
| 121 | for (const QDomElement &i : unhandledElements) { |
| 122 | ret.insert(key: i.namespaceURI() + i.localName(), value: i); |
| 123 | } |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | } // namespace Syndication |
| 129 | |