Syndication

entry.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 "entry.h"
9#include "atomtools.h"
10#include "category.h"
11#include "constants.h"
12#include "content.h"
13#include "link.h"
14#include "person.h"
15#include "source.h"
16
17#include <specificitemvisitor.h>
18#include <tools.h>
19
20#include <QDomElement>
21#include <QString>
22
23#include <vector>
24
25namespace Syndication
26{
27namespace Atom
28{
33
35 : ElementWrapper(element)
36{
37}
38
39void Entry::setFeedAuthors(const QList<Person> &feedAuthors)
40{
41 m_feedAuthors = feedAuthors;
42}
43
45{
46 const QList<QDomElement> a = elementsByTagNameNS(atom1Namespace(), QStringLiteral("author"));
47 QList<Person> list;
48
49 if (!a.isEmpty()) {
50 list.reserve(a.count());
51
52 std::transform(a.cbegin(), a.cend(), std::back_inserter(list), [](const QDomElement &element) {
53 return Person(element);
54 });
55 } else {
56 list = source().authors();
57 }
58
59 return !list.isEmpty() ? list : m_feedAuthors;
60}
61
63{
64 const QList<QDomElement> a = elementsByTagNameNS(atom1Namespace(), QStringLiteral("contributor"));
65 QList<Person> list;
66 list.reserve(a.count());
67
68 std::transform(a.cbegin(), a.cend(), std::back_inserter(list), [](const QDomElement &element) {
69 return Person(element);
70 });
71
72 return list;
73}
74
76{
77 const QList<QDomElement> a = elementsByTagNameNS(atom1Namespace(), QStringLiteral("category"));
78 QList<Category> list;
79 list.reserve(a.count());
80
81 std::transform(a.cbegin(), a.cend(), std::back_inserter(list), [](const QDomElement &element) {
82 return Category(element);
83 });
84
85 return list;
86}
87
89{
90 return extractElementTextNS(atom1Namespace(), QStringLiteral("id"));
91}
92
94{
95 const QList<QDomElement> a = elementsByTagNameNS(atom1Namespace(), QStringLiteral("link"));
96 QList<Link> list;
97 list.reserve(a.count());
98
99 std::transform(a.cbegin(), a.cend(), std::back_inserter(list), [](const QDomElement &element) {
100 return Link(element);
101 });
102
103 return list;
104}
105
107{
108 return extractAtomText(*this, QStringLiteral("rights"));
109}
110
112{
113 return Source(firstElementByTagNameNS(atom1Namespace(), QStringLiteral("source")));
114}
115
116time_t Entry::published() const
117{
118 QString pub = extractElementTextNS(atom1Namespace(), QStringLiteral("published"));
119 return parseDate(pub, ISODate);
120}
121
122time_t Entry::updated() const
123{
124 QString upd = extractElementTextNS(atom1Namespace(), QStringLiteral("updated"));
125 return parseDate(upd, ISODate);
126}
127
129{
130 return extractAtomText(*this, QStringLiteral("summary"));
131}
132
134{
135 return extractAtomText(*this, QStringLiteral("title"));
136}
137
139{
140 return Content(firstElementByTagNameNS(atom1Namespace(), QStringLiteral("content")));
141}
142
144{
145 // TODO: do not hardcode this list here
146 static std::vector<ElementType> handled; // QVector would require a default ctor, and ElementType is too big for QList
147 if (handled.empty()) {
148 handled.reserve(12);
149 handled.push_back(ElementType(QStringLiteral("author"), atom1Namespace()));
150 handled.push_back(ElementType(QStringLiteral("contributor"), atom1Namespace()));
151 handled.push_back(ElementType(QStringLiteral("category"), atom1Namespace()));
152 handled.push_back(ElementType(QStringLiteral("id"), atom1Namespace()));
153 handled.push_back(ElementType(QStringLiteral("link"), atom1Namespace()));
154 handled.push_back(ElementType(QStringLiteral("rights"), atom1Namespace()));
155 handled.push_back(ElementType(QStringLiteral("source"), atom1Namespace()));
156 handled.push_back(ElementType(QStringLiteral("published"), atom1Namespace()));
157 handled.push_back(ElementType(QStringLiteral("updated"), atom1Namespace()));
158 handled.push_back(ElementType(QStringLiteral("summary"), atom1Namespace()));
159 handled.push_back(ElementType(QStringLiteral("title"), atom1Namespace()));
160 handled.push_back(ElementType(QStringLiteral("content"), atom1Namespace()));
161 }
162
163 QList<QDomElement> notHandled;
164
165 QDomNodeList children = element().childNodes();
166 const int numChildren = children.size();
167 for (int i = 0; i < numChildren; ++i) {
168 QDomElement el = children.at(i).toElement();
169 if (!el.isNull() //
170 && std::find(handled.cbegin(), handled.cend(), ElementType(el.localName(), el.namespaceURI())) == handled.cend()) {
171 notHandled.append(el);
172 }
173 }
174
175 return notHandled;
176}
177
179{
180 QString info;
181 info += QLatin1String("### Entry: ###################\n");
182 if (!title().isEmpty()) {
183 info += QLatin1String("title: #") + title() + QLatin1String("#\n");
184 }
185 if (!summary().isEmpty()) {
186 info += QLatin1String("summary: #") + summary() + QLatin1String("#\n");
187 }
188 if (!id().isEmpty()) {
189 info += QLatin1String("id: #") + id() + QLatin1String("#\n");
190 }
191 if (!content().isNull()) {
192 info += content().debugInfo();
193 }
194
195 if (!rights().isEmpty()) {
196 info += QLatin1String("rights: #") + rights() + QLatin1String("#\n");
197 }
198
199 QString dupdated = dateTimeToString(updated());
200 if (!dupdated.isNull()) {
201 info += QLatin1String("updated: #") + dupdated + QLatin1String("#\n");
202 }
203
204 QString dpublished = dateTimeToString(published());
205 if (!dpublished.isNull()) {
206 info += QLatin1String("published: #") + dpublished + QLatin1String("#\n");
207 }
208
209 const QList<Link> dlinks = links();
210 for (const auto &link : dlinks) {
211 info += link.debugInfo();
212 }
213
214 const QList<Category> dcats = categories();
215 for (const auto &cat : dcats) {
216 info += cat.debugInfo();
217 }
218
219 info += QLatin1String("### Authors: ###################\n");
220
221 const QList<Person> dauthors = authors();
222 for (const auto &author : dauthors) {
223 info += author.debugInfo();
224 }
225
226 info += QLatin1String("### Contributors: ###################\n");
227
228 const QList<Person> dcontri = contributors();
229 for (const auto &person : dcontri) {
230 info += person.debugInfo();
231 }
232
233 if (!source().isNull()) {
234 info += source().debugInfo();
235 }
236
237 info += QLatin1String("### Entry end ################\n");
238
239 return info;
240}
241
243{
244 return visitor->visitAtomEntry(this);
245}
246
247} // namespace Atom
248} // namespace Syndication
The content element either contains or links the content of an entry.
Definition content.h:31
QString debugInfo() const
returns a description of the content object for debugging purposes
Definition content.cpp:173
QList< Person > contributors() const
list of persons contributing to this entry (optional)
Definition entry.cpp:62
bool accept(SpecificItemVisitor *visitor) override
Used by visitors for double dispatch.
Definition entry.cpp:242
QString rights() const
copyright information (optional)
Definition entry.cpp:106
time_t published() const
The datetime of the publication of this entry (optional).
Definition entry.cpp:116
QString title() const
title of the entry (required).
Definition entry.cpp:133
void setFeedAuthors(const QList< Person > &feedAuthors)
Sets the list of the containing feed's authors, which will be used as a fallback in authors() in case...
Definition entry.cpp:39
Content content() const
content of the entry (optional) See Content for details
Definition entry.cpp:138
Entry()
creates a null entry object
Definition entry.cpp:29
QList< QDomElement > unhandledElements() const
returns all child elements of this entry not covered by this class.
Definition entry.cpp:143
Source source() const
source description of the content (optional)
Definition entry.cpp:111
QString debugInfo() const
returns a description of this entry for debugging purposes
Definition entry.cpp:178
QList< Person > authors() const
list of persons who are authors of this entry.
Definition entry.cpp:44
time_t updated() const
The datetime of the last modification of this entry (required).
Definition entry.cpp:122
QString summary() const
a short summary, abstract or excerpt of an entry.
Definition entry.cpp:128
QString id() const
ID of the article.
Definition entry.cpp:88
QList< Category > categories() const
a list of categories this entry is filed to (optional)
Definition entry.cpp:75
QList< Link > links() const
links pointing to associated web sites and other resources.
Definition entry.cpp:93
If an entry was copied from another feed, this class contains a description of the source feed.
Definition atom/source.h:37
QList< Person > authors() const
authors of the original content (optional)
QString debugInfo() const
description of this source object for debugging purposes
A wrapper for XML elements.
const QDomElement & element() const
returns the wrapped resource.
QDomElement firstElementByTagNameNS(const QString &nsURI, const QString &tagName) const
searches the direct children of the wrapped element for an element with a given namespace and tag nam...
bool isNull() const
returns whether the wrapped element is a null element
QList< QDomElement > elementsByTagNameNS(const QString &nsURI, const QString &tagName) const
returns all child elements with tag name tagname and namespace URI nsURI.
QString extractElementTextNS(const QString &namespaceURI, const QString &localName) const
extracts the text from a child element, respecting namespaces.
Visitor interface, following the Visitor design pattern.
virtual bool visitAtomEntry(Syndication::Atom::Entry *item)
reimplement this method to handle Atom entries.
QString atom1Namespace()
namespace used by Atom 1.0 elements
QString extractAtomText(const Syndication::ElementWrapper &parent, const QString &tagname)
extracts the content of an atomTextConstruct.
Definition atomtools.cpp:21
QDomNodeList childNodes() const const
bool isNull() const const
QString localName() const const
QString namespaceURI() const const
QDomElement toElement() const const
QDomNode at(int index) const const
int size() const const
void append(QList< T > &&value)
const_iterator cbegin() const const
const_iterator cend() const const
qsizetype count() const const
bool isEmpty() const const
void reserve(qsizetype size)
bool isNull() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:02:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.