Attica

contentparser.cpp
1/*
2 This file is part of KDE.
3
4 SPDX-FileCopyrightText: 2008 Cornelius Schumacher <schumacher@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7*/
8
9#include "contentparser.h"
10
11#include <QDateTime>
12#include <QDebug>
13
14using namespace Attica;
15
16Content Content::Parser::parseXml(QXmlStreamReader &xml)
17{
18 Content content;
19
20 while (!xml.atEnd()) {
21 xml.readNext();
22
23 if (xml.isStartElement()) {
24 if (xml.name() == QLatin1String("id")) {
25 content.setId(xml.readElementText());
26 } else if (xml.name() == QLatin1String("name")) {
27 content.setName(xml.readElementText());
28 } else if (xml.name() == QLatin1String("score")) {
29 content.setRating(xml.readElementText().toInt());
30 } else if (xml.name() == QLatin1String("downloads")) {
31 content.setDownloads(xml.readElementText().toInt());
32 } else if (xml.name() == QLatin1String("comments")) {
33 content.setNumberOfComments(xml.readElementText().toInt());
34 } else if (xml.name() == QLatin1String("created")) {
35 // Qt doesn't accept +-Timezone modifiers, truncate if the string contains them
36 QString dateString = xml.readElementText().left(19);
37 content.setCreated(QDateTime::fromString(dateString, Qt::ISODate));
38 } else if (xml.name() == QLatin1String("changed")) {
39 // Qt doesn't accept +-Timezone modifiers, truncate if the string contains them
40 QString dateString = xml.readElementText().left(19);
41 content.setUpdated(QDateTime::fromString(dateString, Qt::ISODate));
42 } else if (xml.name() == QLatin1String("icon")) {
43 Icon icon;
44 icon.setUrl(QUrl(xml.readElementText()));
45
46 const QXmlStreamAttributes attributes = xml.attributes();
47
48 const auto width = attributes.value(QLatin1String("width"));
49 if (!width.isEmpty()) {
50 icon.setWidth(width.toInt());
51 }
52
53 const auto height = attributes.value(QLatin1String("height"));
54 if (!height.isEmpty()) {
55 icon.setHeight(height.toInt());
56 }
57
58 // append the icon to the current list of icons
59 QList<Icon> icons;
60 icons = content.icons();
61 icons.append(icon);
62 content.setIcons(icons);
63 } else if (xml.name() == QLatin1String("video")) {
64 QUrl video(xml.readElementText());
65 // append the video to the current list of videos
66 QList<QUrl> videos;
67 videos = content.videos();
68 videos.append(video);
69 content.setVideos(videos);
70 } else if (xml.name() == QLatin1String("tags")) {
71 content.setTags(xml.readElementText().split(QLatin1Char(',')));
72 } else {
73 content.addAttribute(xml.name().toString(), xml.readElementText());
74 }
75 }
76
77 if (xml.isEndElement() && xml.name() == QLatin1String("content")) {
78 break;
79 }
80 }
81
82 // in case the server only sets creation date, use that as updated also
83 if (content.updated().isNull()) {
84 content.setUpdated(content.created());
85 }
86
87 return content;
88}
89
90QStringList Content::Parser::xmlElement() const
91{
92 return QStringList(QStringLiteral("content"));
93}
The Attica namespace,.
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
void append(QList< T > &&value)
void setHeight(qreal)
void setWidth(qreal)
QString left(qsizetype n) const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QString toString() const const
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const const
bool atEnd() const const
QXmlStreamAttributes attributes() const const
bool isEndElement() const const
bool isStartElement() const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
TokenType readNext()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:02:10 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.