File indexing completed on 2024-12-01 12:41:48

0001 /*
0002     This file is part of the syndication library
0003     SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "source.h"
0009 #include "atomtools.h"
0010 #include "category.h"
0011 #include "constants.h"
0012 #include "generator.h"
0013 #include "link.h"
0014 #include "person.h"
0015 
0016 #include <tools.h>
0017 
0018 #include <QDomElement>
0019 #include <QList>
0020 #include <QString>
0021 
0022 namespace Syndication
0023 {
0024 namespace Atom
0025 {
0026 Source::Source()
0027     : ElementWrapper()
0028 {
0029 }
0030 
0031 Source::Source(const QDomElement &element)
0032     : ElementWrapper(element)
0033 {
0034 }
0035 
0036 QList<Person> Source::authors() const
0037 {
0038     const QList<QDomElement> a = elementsByTagNameNS(atom1Namespace(), QStringLiteral("author"));
0039 
0040     QList<Person> list;
0041     list.reserve(a.count());
0042 
0043     std::transform(a.cbegin(), a.cend(), std::back_inserter(list), [](const QDomElement &element) {
0044         return Person(element);
0045     });
0046 
0047     return list;
0048 }
0049 
0050 QList<Person> Source::contributors() const
0051 {
0052     const QList<QDomElement> a = elementsByTagNameNS(atom1Namespace(), QStringLiteral("contributor"));
0053     QList<Person> list;
0054     list.reserve(a.count());
0055 
0056     std::transform(a.cbegin(), a.cend(), std::back_inserter(list), [](const QDomElement &element) {
0057         return Person(element);
0058     });
0059 
0060     return list;
0061 }
0062 
0063 QList<Category> Source::categories() const
0064 {
0065     const QList<QDomElement> a = elementsByTagNameNS(atom1Namespace(), QStringLiteral("category"));
0066     QList<Category> list;
0067     list.reserve(a.count());
0068 
0069     std::transform(a.cbegin(), a.cend(), std::back_inserter(list), [](const QDomElement &element) {
0070         return Category(element);
0071     });
0072 
0073     return list;
0074 }
0075 
0076 Generator Source::generator() const
0077 {
0078     return Generator(firstElementByTagNameNS(atom1Namespace(), QStringLiteral("generator")));
0079 }
0080 
0081 QString Source::icon() const
0082 {
0083     return extractElementTextNS(atom1Namespace(), QStringLiteral("icon"));
0084 }
0085 
0086 QString Source::id() const
0087 {
0088     return extractElementTextNS(atom1Namespace(), QStringLiteral("id"));
0089 }
0090 
0091 QList<Link> Source::links() const
0092 {
0093     const QList<QDomElement> a = elementsByTagNameNS(atom1Namespace(), QStringLiteral("link"));
0094     QList<Link> list;
0095     list.reserve(a.count());
0096 
0097     std::transform(a.cbegin(), a.cend(), std::back_inserter(list), [](const QDomElement &element) {
0098         return Link(element);
0099     });
0100 
0101     return list;
0102 }
0103 
0104 QString Source::logo() const
0105 {
0106     return extractElementTextNS(atom1Namespace(), QStringLiteral("logo"));
0107 }
0108 
0109 QString Source::rights() const
0110 {
0111     return extractAtomText(*this, QStringLiteral("rights"));
0112 }
0113 
0114 QString Source::subtitle() const
0115 {
0116     return extractAtomText(*this, QStringLiteral("subtitle"));
0117 }
0118 
0119 QString Source::title() const
0120 {
0121     return extractAtomText(*this, QStringLiteral("title"));
0122 }
0123 
0124 time_t Source::updated() const
0125 {
0126     const QString upd = extractElementTextNS(atom1Namespace(), QStringLiteral("updated"));
0127     return parseDate(upd, ISODate);
0128 }
0129 
0130 QString Source::debugInfo() const
0131 {
0132     QString info = QLatin1String("### Source: ###################\n");
0133     if (!title().isEmpty()) {
0134         info += QLatin1String("title: #") + title() + QLatin1String("#\n");
0135     }
0136     if (!subtitle().isEmpty()) {
0137         info += QLatin1String("subtitle: #") + subtitle() + QLatin1String("#\n");
0138     }
0139     if (!id().isEmpty()) {
0140         info += QLatin1String("id: #") + id() + QLatin1String("#\n");
0141     }
0142 
0143     if (!rights().isEmpty()) {
0144         info += QLatin1String("rights: #") + rights() + QLatin1String("#\n");
0145     }
0146     if (!icon().isEmpty()) {
0147         info += QLatin1String("icon: #") + icon() + QLatin1String("#\n");
0148     }
0149     if (!logo().isEmpty()) {
0150         info += QLatin1String("logo: #") + logo() + QLatin1String("#\n");
0151     }
0152     if (!generator().isNull()) {
0153         info += generator().debugInfo();
0154     }
0155 
0156     QString dupdated = dateTimeToString(updated());
0157     if (!dupdated.isNull()) {
0158         info += QLatin1String("updated: #") + dupdated + QLatin1String("#\n");
0159     }
0160 
0161     const QList<Link> dlinks = links();
0162     for (const auto &link : dlinks) {
0163         info += link.debugInfo();
0164     }
0165 
0166     const QList<Category> dcats = categories();
0167     for (const auto &cat : dcats) {
0168         info += cat.debugInfo();
0169     }
0170 
0171     info += QLatin1String("### Authors: ###################\n");
0172 
0173     const QList<Person> dauthors = authors();
0174     for (const auto &author : dauthors) {
0175         info += author.debugInfo();
0176     }
0177 
0178     info += QLatin1String("### Contributors: ###################\n");
0179 
0180     const QList<Person> dcontri = contributors();
0181     for (const auto &person : dcontri) {
0182         info += person.debugInfo();
0183     }
0184 
0185     info += QLatin1String("### Source end ################\n");
0186 
0187     return info;
0188 }
0189 
0190 } // namespace Atom
0191 } // namespace Syndication