Syndication

itematomimpl.cpp
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
30
31namespace Syndication
32{
33ItemAtomImpl::ItemAtomImpl(const Syndication::Atom::Entry &entry)
34 : m_entry(entry)
35{
36}
37
38QString ItemAtomImpl::title() const
39{
40 return m_entry.title();
41}
42
43QString 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(links.cbegin(), links.cend(), [](const Syndication::Atom::Link &link) {
49 return link.rel() == QLatin1String("alternate");
50 });
51 return it != links.cend() ? it->href() : QString{};
52}
53
54QString ItemAtomImpl::description() const
55{
56 return m_entry.summary();
57}
58
59QString 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
69QList<PersonPtr> ItemAtomImpl::authors() const
70{
71 const QList<Syndication::Atom::Person> people = m_entry.authors() + m_entry.contributors();
72
74 list.reserve(people.size());
75
76 std::transform(people.cbegin(), people.cend(), std::back_inserter(list), [](const Syndication::Atom::Person &person) {
77 return PersonImplPtr(new PersonImpl(person.name(), person.uri(), person.email()));
78 });
79
80 return list;
81}
82
83time_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
93time_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
103QString ItemAtomImpl::language() const
104{
105 return m_entry.xmlLang();
106}
107
108QString 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(Syndication::calcMD5Sum(title() + description() + link() + content()));
116}
117
118QList<Syndication::EnclosurePtr> ItemAtomImpl::enclosures() const
119{
121
122 const QList<Syndication::Atom::Link> links = m_entry.links();
123
124 for (const auto &link : links) {
125 if (link.rel() == QLatin1String("enclosure")) {
127 }
128 }
129
130 return list;
131}
132
133QList<Syndication::CategoryPtr> ItemAtomImpl::categories() const
134{
135 const QList<Syndication::Atom::Category> cats = m_entry.categories();
136
138 list.reserve(cats.count());
139
140 std::transform(cats.cbegin(), cats.cend(), std::back_inserter(list), [](const Syndication::Atom::Category &c) {
141 return CategoryAtomImplPtr(new CategoryAtomImpl(c));
142 });
143
144 return list;
145}
146
147int ItemAtomImpl::commentsCount() const
148{
149 QString cstr = m_entry.extractElementTextNS(slashNamespace(), QStringLiteral("comments"));
150 bool ok = false;
151 int comments = cstr.toInt(&ok);
152 return ok ? comments : -1;
153}
154
155QString ItemAtomImpl::commentsLink() const
156{
157 return QString();
158}
159
160QString ItemAtomImpl::commentsFeed() const
161{
162 return m_entry.extractElementTextNS(commentApiNamespace(), QStringLiteral("commentRss"));
163}
164
165QString ItemAtomImpl::commentPostUri() const
166{
167 return m_entry.extractElementTextNS(commentApiNamespace(), QStringLiteral("comment"));
168}
169
170Syndication::SpecificItemPtr ItemAtomImpl::specificItem() const
171{
172 return Syndication::SpecificItemPtr(new Syndication::Atom::Entry(m_entry));
173}
174
175QMultiMap<QString, QDomElement> ItemAtomImpl::additionalProperties() const
176{
178 const auto unhandledElements = m_entry.unhandledElements();
179 for (const QDomElement &i : unhandledElements) {
180 ret.insert(i.namespaceURI() + i.localName(), i);
181 }
182
183 return ret;
184}
185
186} // namespace Syndication
A category for categorizing items or whole feeds.
The content element either contains or links the content of an entry.
Definition content.h:31
QString asString() const
returns the content as string.
Definition content.cpp:158
an Atom entry, equivalent to the "items" in the RSS world.
Definition entry.h:39
describes a person, with name and optional URI and e-mail address.
Definition atom/person.h:27
bool isNull() const
returns whether the wrapped element is a null element
QString slashNamespace()
"slash" namespace http://purl.org/rss/1.0/modules/slash/
Definition constants.cpp:44
QString commentApiNamespace()
wellformedweb.org's RSS namespace for comment functionality "http://wellformedweb....
Definition constants.cpp:39
void append(QList< T > &&value)
const_iterator cbegin() const const
const_iterator cend() const const
qsizetype count() const const
void reserve(qsizetype size)
qsizetype size() const const
iterator insert(const Key &key, const T &value)
QString arg(Args &&... args) const const
int toInt(bool *ok, int base) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.