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 "source.h" |
9 | #include "atomtools.h" |
10 | #include "category.h" |
11 | #include "constants.h" |
12 | #include "generator.h" |
13 | #include "link.h" |
14 | #include "person.h" |
15 | |
16 | #include <tools.h> |
17 | |
18 | #include <QDomElement> |
19 | #include <QList> |
20 | #include <QString> |
21 | |
22 | namespace Syndication |
23 | { |
24 | namespace Atom |
25 | { |
26 | Source::Source() |
27 | : ElementWrapper() |
28 | { |
29 | } |
30 | |
31 | Source::Source(const QDomElement &element) |
32 | : ElementWrapper(element) |
33 | { |
34 | } |
35 | |
36 | QList<Person> Source::authors() const |
37 | { |
38 | const QList<QDomElement> a = elementsByTagNameNS(nsURI: atom1Namespace(), QStringLiteral("author" )); |
39 | |
40 | QList<Person> list; |
41 | list.reserve(asize: a.count()); |
42 | |
43 | std::transform(first: a.cbegin(), last: a.cend(), result: std::back_inserter(x&: list), unary_op: [](const QDomElement &element) { |
44 | return Person(element); |
45 | }); |
46 | |
47 | return list; |
48 | } |
49 | |
50 | QList<Person> Source::contributors() const |
51 | { |
52 | const QList<QDomElement> a = elementsByTagNameNS(nsURI: atom1Namespace(), QStringLiteral("contributor" )); |
53 | QList<Person> list; |
54 | list.reserve(asize: a.count()); |
55 | |
56 | std::transform(first: a.cbegin(), last: a.cend(), result: std::back_inserter(x&: list), unary_op: [](const QDomElement &element) { |
57 | return Person(element); |
58 | }); |
59 | |
60 | return list; |
61 | } |
62 | |
63 | QList<Category> Source::categories() const |
64 | { |
65 | const QList<QDomElement> a = elementsByTagNameNS(nsURI: atom1Namespace(), QStringLiteral("category" )); |
66 | QList<Category> list; |
67 | list.reserve(asize: a.count()); |
68 | |
69 | std::transform(first: a.cbegin(), last: a.cend(), result: std::back_inserter(x&: list), unary_op: [](const QDomElement &element) { |
70 | return Category(element); |
71 | }); |
72 | |
73 | return list; |
74 | } |
75 | |
76 | Generator Source::generator() const |
77 | { |
78 | return Generator(firstElementByTagNameNS(nsURI: atom1Namespace(), QStringLiteral("generator" ))); |
79 | } |
80 | |
81 | QString Source::icon() const |
82 | { |
83 | return extractElementTextNS(namespaceURI: atom1Namespace(), QStringLiteral("icon" )); |
84 | } |
85 | |
86 | QString Source::id() const |
87 | { |
88 | return extractElementTextNS(namespaceURI: atom1Namespace(), QStringLiteral("id" )); |
89 | } |
90 | |
91 | QList<Link> Source::links() const |
92 | { |
93 | const QList<QDomElement> a = elementsByTagNameNS(nsURI: atom1Namespace(), QStringLiteral("link" )); |
94 | QList<Link> list; |
95 | list.reserve(asize: a.count()); |
96 | |
97 | std::transform(first: a.cbegin(), last: a.cend(), result: std::back_inserter(x&: list), unary_op: [](const QDomElement &element) { |
98 | return Link(element); |
99 | }); |
100 | |
101 | return list; |
102 | } |
103 | |
104 | QString Source::logo() const |
105 | { |
106 | return extractElementTextNS(namespaceURI: atom1Namespace(), QStringLiteral("logo" )); |
107 | } |
108 | |
109 | QString Source::rights() const |
110 | { |
111 | return extractAtomText(parent: *this, QStringLiteral("rights" )); |
112 | } |
113 | |
114 | QString Source::subtitle() const |
115 | { |
116 | return extractAtomText(parent: *this, QStringLiteral("subtitle" )); |
117 | } |
118 | |
119 | QString Source::title() const |
120 | { |
121 | return extractAtomText(parent: *this, QStringLiteral("title" )); |
122 | } |
123 | |
124 | time_t Source::updated() const |
125 | { |
126 | const QString upd = extractElementTextNS(namespaceURI: atom1Namespace(), QStringLiteral("updated" )); |
127 | return parseDate(str: upd, hint: ISODate); |
128 | } |
129 | |
130 | QString Source::debugInfo() const |
131 | { |
132 | QString info = QLatin1String("### Source: ###################\n" ); |
133 | if (!title().isEmpty()) { |
134 | info += QLatin1String("title: #" ) + title() + QLatin1String("#\n" ); |
135 | } |
136 | if (!subtitle().isEmpty()) { |
137 | info += QLatin1String("subtitle: #" ) + subtitle() + QLatin1String("#\n" ); |
138 | } |
139 | if (!id().isEmpty()) { |
140 | info += QLatin1String("id: #" ) + id() + QLatin1String("#\n" ); |
141 | } |
142 | |
143 | if (!rights().isEmpty()) { |
144 | info += QLatin1String("rights: #" ) + rights() + QLatin1String("#\n" ); |
145 | } |
146 | if (!icon().isEmpty()) { |
147 | info += QLatin1String("icon: #" ) + icon() + QLatin1String("#\n" ); |
148 | } |
149 | if (!logo().isEmpty()) { |
150 | info += QLatin1String("logo: #" ) + logo() + QLatin1String("#\n" ); |
151 | } |
152 | if (!generator().isNull()) { |
153 | info += generator().debugInfo(); |
154 | } |
155 | |
156 | QString dupdated = dateTimeToString(date: updated()); |
157 | if (!dupdated.isNull()) { |
158 | info += QLatin1String("updated: #" ) + dupdated + QLatin1String("#\n" ); |
159 | } |
160 | |
161 | const QList<Link> dlinks = links(); |
162 | for (const auto &link : dlinks) { |
163 | info += link.debugInfo(); |
164 | } |
165 | |
166 | const QList<Category> dcats = categories(); |
167 | for (const auto &cat : dcats) { |
168 | info += cat.debugInfo(); |
169 | } |
170 | |
171 | info += QLatin1String("### Authors: ###################\n" ); |
172 | |
173 | const QList<Person> dauthors = authors(); |
174 | for (const auto &author : dauthors) { |
175 | info += author.debugInfo(); |
176 | } |
177 | |
178 | info += QLatin1String("### Contributors: ###################\n" ); |
179 | |
180 | const QList<Person> dcontri = contributors(); |
181 | for (const auto &person : dcontri) { |
182 | info += person.debugInfo(); |
183 | } |
184 | |
185 | info += QLatin1String("### Source end ################\n" ); |
186 | |
187 | return info; |
188 | } |
189 | |
190 | } // namespace Atom |
191 | } // namespace Syndication |
192 | |