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 "itematomimpl.h" |
9 | #include "categoryatomimpl.h" |
10 | #include "enclosureatomimpl.h" |
11 | |
12 | #include <atom/category.h> |
13 | #include <atom/content.h> |
14 | #include <atom/link.h> |
15 | #include <atom/person.h> |
16 | #include <category.h> |
17 | #include <constants.h> |
18 | #include <enclosure.h> |
19 | #include <personimpl.h> |
20 | #include <tools.h> |
21 | |
22 | #include <QDomElement> |
23 | #include <QList> |
24 | #include <QMultiMap> |
25 | #include <QString> |
26 | |
27 | using Syndication::Atom::Content; |
28 | using Syndication::Atom::Link; |
29 | using Syndication::Atom::Person; |
30 | |
31 | namespace Syndication |
32 | { |
33 | ItemAtomImpl::ItemAtomImpl(const Syndication::Atom::Entry &entry) |
34 | : m_entry(entry) |
35 | { |
36 | } |
37 | |
38 | QString ItemAtomImpl::title() const |
39 | { |
40 | return m_entry.title(); |
41 | } |
42 | |
43 | QString ItemAtomImpl::link() const |
44 | { |
45 | const QList<Syndication::Atom::Link> links = m_entry.links(); |
46 | |
47 | // return first link where rel="alternate" |
48 | auto it = std::find_if(first: links.cbegin(), last: links.cend(), pred: [](const Syndication::Atom::Link &link) { |
49 | return link.rel() == QLatin1String("alternate" ); |
50 | }); |
51 | return it != links.cend() ? it->href() : QString{}; |
52 | } |
53 | |
54 | QString ItemAtomImpl::description() const |
55 | { |
56 | return m_entry.summary(); |
57 | } |
58 | |
59 | QString ItemAtomImpl::content() const |
60 | { |
61 | Content content = m_entry.content(); |
62 | if (content.isNull()) { |
63 | return QString(); |
64 | } |
65 | |
66 | return content.asString(); |
67 | } |
68 | |
69 | QList<PersonPtr> ItemAtomImpl::authors() const |
70 | { |
71 | const QList<Syndication::Atom::Person> people = m_entry.authors() + m_entry.contributors(); |
72 | |
73 | QList<PersonPtr> list; |
74 | list.reserve(asize: people.size()); |
75 | |
76 | std::transform(first: people.cbegin(), last: people.cend(), result: std::back_inserter(x&: list), unary_op: [](const Syndication::Atom::Person &person) { |
77 | return PersonImplPtr(new PersonImpl(person.name(), person.uri(), person.email())); |
78 | }); |
79 | |
80 | return list; |
81 | } |
82 | |
83 | time_t ItemAtomImpl::datePublished() const |
84 | { |
85 | time_t pub = m_entry.published(); |
86 | if (pub == 0) { |
87 | return m_entry.updated(); |
88 | } else { |
89 | return pub; |
90 | } |
91 | } |
92 | |
93 | time_t ItemAtomImpl::dateUpdated() const |
94 | { |
95 | time_t upd = m_entry.updated(); |
96 | if (upd == 0) { |
97 | return m_entry.published(); |
98 | } else { |
99 | return upd; |
100 | } |
101 | } |
102 | |
103 | QString ItemAtomImpl::language() const |
104 | { |
105 | return m_entry.xmlLang(); |
106 | } |
107 | |
108 | QString ItemAtomImpl::id() const |
109 | { |
110 | const QString id = m_entry.id(); |
111 | if (!id.isEmpty()) { |
112 | return id; |
113 | } |
114 | |
115 | return QStringLiteral("hash:%1" ).arg(a: Syndication::calcMD5Sum(str: title() + description() + link() + content())); |
116 | } |
117 | |
118 | QList<Syndication::EnclosurePtr> ItemAtomImpl::enclosures() const |
119 | { |
120 | QList<Syndication::EnclosurePtr> list; |
121 | |
122 | const QList<Syndication::Atom::Link> links = m_entry.links(); |
123 | |
124 | for (const auto &link : links) { |
125 | if (link.rel() == QLatin1String("enclosure" )) { |
126 | list.append(t: EnclosureAtomImplPtr(new EnclosureAtomImpl(link))); |
127 | } |
128 | } |
129 | |
130 | return list; |
131 | } |
132 | |
133 | QList<Syndication::CategoryPtr> ItemAtomImpl::categories() const |
134 | { |
135 | const QList<Syndication::Atom::Category> cats = m_entry.categories(); |
136 | |
137 | QList<Syndication::CategoryPtr> list; |
138 | list.reserve(asize: cats.count()); |
139 | |
140 | std::transform(first: cats.cbegin(), last: cats.cend(), result: std::back_inserter(x&: list), unary_op: [](const Syndication::Atom::Category &c) { |
141 | return CategoryAtomImplPtr(new CategoryAtomImpl(c)); |
142 | }); |
143 | |
144 | return list; |
145 | } |
146 | |
147 | int ItemAtomImpl::() const |
148 | { |
149 | QString cstr = m_entry.extractElementTextNS(namespaceURI: slashNamespace(), QStringLiteral("comments" )); |
150 | bool ok = false; |
151 | int = cstr.toInt(ok: &ok); |
152 | return ok ? comments : -1; |
153 | } |
154 | |
155 | QString ItemAtomImpl::() const |
156 | { |
157 | return QString(); |
158 | } |
159 | |
160 | QString ItemAtomImpl::() const |
161 | { |
162 | return m_entry.extractElementTextNS(namespaceURI: commentApiNamespace(), QStringLiteral("commentRss" )); |
163 | } |
164 | |
165 | QString ItemAtomImpl::() const |
166 | { |
167 | return m_entry.extractElementTextNS(namespaceURI: commentApiNamespace(), QStringLiteral("comment" )); |
168 | } |
169 | |
170 | Syndication::SpecificItemPtr ItemAtomImpl::specificItem() const |
171 | { |
172 | return Syndication::SpecificItemPtr(new Syndication::Atom::Entry(m_entry)); |
173 | } |
174 | |
175 | QMultiMap<QString, QDomElement> ItemAtomImpl::additionalProperties() const |
176 | { |
177 | QMultiMap<QString, QDomElement> ret; |
178 | const auto unhandledElements = m_entry.unhandledElements(); |
179 | for (const QDomElement &i : unhandledElements) { |
180 | ret.insert(key: i.namespaceURI() + i.localName(), value: i); |
181 | } |
182 | |
183 | return ret; |
184 | } |
185 | |
186 | } // namespace Syndication |
187 | |