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")) {
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
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
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
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}
Represents a single content.
Definition content.h:33
void setCreated(const QDateTime &created)
Sets the date and time the Content has been created.
Definition content.cpp:110
QDateTime created() const
Gets the date and time the Content has been created.
Definition content.cpp:115
void setIcons(QList< Icon > icons)
Set list of icons.
Definition content.cpp:305
void addAttribute(const QString &key, const QString &value)
Add an attribute that is not included in the basis set of attributes exposed by the Content class.
Definition content.cpp:130
void setVideos(QList< QUrl > videos)
Set list of videos.
Definition content.cpp:315
QList< Icon > icons()
Get all icons for this content.
Definition content.cpp:295
QDateTime updated() const
Gets the date and time the Content has been last updated.
Definition content.cpp:125
QList< QUrl > videos()
Get all videos for this content.
Definition content.cpp:310
void setId(const QString &id)
Sets the id of the Content.
Definition content.cpp:60
void setDownloads(int downloads)
Sets the number of downloads for the Content.
Definition content.cpp:90
void setName(const QString &name)
Sets the name of the Content.
Definition content.cpp:70
void setRating(int rating)
Sets the rating of the Content.
Definition content.cpp:80
void setNumberOfComments(int numComments)
Sets the number of comments for the Content.
Definition content.cpp:100
void setTags(const QStringList &tags)
Set the list of tags.
Definition content.cpp:325
QMap< QString, QString > attributes() const
Get all attributes that are not included in the basis set of attributes exposed by the Content class.
Definition content.cpp:140
void setUpdated(const QDateTime &updated)
Sets the time the Content has been last updated.
Definition content.cpp:120
The Attica namespace,.
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
bool isNull() const const
void append(QList< T > &&value)
T value(const Key &key, const T &defaultValue) const const
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
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-2024 The KDE developers.
Generated on Fri Jul 12 2024 12:02:39 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.