Attica

contentparser.cpp
1 /*
2  This file is part of KDE.
3 
4  SPDX-FileCopyrightText: 2008 Cornelius Schumacher <[email protected]>
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 
14 using namespace Attica;
15 
16 Content 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 
90 QStringList Content::Parser::xmlElement() const
91 {
92  return QStringList(QStringLiteral("content"));
93 }
void append(const T &value)
void setDownloads(int downloads)
Sets the number of downloads for the Content.
Definition: content.cpp:89
void setRating(int rating)
Sets the rating of the Content.
Definition: content.cpp:79
QStringRef value(const QString &namespaceUri, const QString &name) const const
void setVideos(QList< QUrl > videos)
Set list of videos.
Definition: content.cpp:321
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
void setId(const QString &id)
Sets the id of the Content.
Definition: content.cpp:59
void setNumberOfComments(int numComments)
Sets the number of comments for the Content.
Definition: content.cpp:99
bool isNull() const const
void setUpdated(const QDateTime &updated)
Sets the time the Content has been last updated.
Definition: content.cpp:119
QList< QUrl > videos()
Get all videos for this content.
Definition: content.cpp:316
bool isEndElement() const const
QStringRef name() const const
QXmlStreamReader::TokenType readNext()
int toInt(bool *ok, int base) const const
QList< Icon > icons()
Get all icons for this content.
Definition: content.cpp:301
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:129
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QXmlStreamAttributes attributes() const const
QDateTime fromString(const QString &string, Qt::DateFormat format)
QDateTime created() const
Gets the date and time the Content has been created.
Definition: content.cpp:114
QString toString() const const
QString left(int n) const const
void setName(const QString &name)
Sets the name of the Content.
Definition: content.cpp:69
void setCreated(const QDateTime &created)
Sets the date and time the Content has been created.
Definition: content.cpp:109
The Attica namespace,.
bool isStartElement() const const
bool atEnd() const const
void setIcons(QList< Icon > icons)
Set list of icons.
Definition: content.cpp:311
QDateTime updated() const
Gets the date and time the Content has been last updated.
Definition: content.cpp:124
void setTags(const QStringList &tags)
Set the list of tags.
Definition: content.cpp:331
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Dec 11 2023 04:05:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.