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  const QXmlStreamAttributes attributes = xml.attributes();
46  const QStringRef width = attributes.value(QLatin1String("width"));
47  const QStringRef height = attributes.value(QLatin1String("height"));
48  if (!width.isEmpty()) {
49  icon.setWidth(width.toInt());
50  }
51  if (!height.isEmpty()) {
52  icon.setHeight(height.toInt());
53  }
54  // append the icon to the current list of icons
55  QList<Icon> icons;
56  icons = content.icons();
57  icons.append(icon);
58  content.setIcons(icons);
59  } else if (xml.name() == QLatin1String("video")) {
60  QUrl video(xml.readElementText());
61  // append the video to the current list of videos
62  QList<QUrl> videos;
63  videos = content.videos();
64  videos.append(video);
65  content.setVideos(videos);
66  } else if (xml.name() == QLatin1String("tags")) {
67  content.setTags(xml.readElementText().split(QLatin1Char(',')));
68  } else {
69  content.addAttribute(xml.name().toString(), xml.readElementText());
70  }
71  }
72 
73  if (xml.isEndElement() && xml.name() == QLatin1String("content")) {
74  break;
75  }
76  }
77 
78  // in case the server only sets creation date, use that as updated also
79  if (content.updated().isNull()) {
80  content.setUpdated(content.created());
81  }
82 
83  return content;
84 }
85 
86 QStringList Content::Parser::xmlElement() const
87 {
88  return QStringList(QStringLiteral("content"));
89 }
Represents a single content.
Definition: content.h:31
bool atEnd() const const
int toInt(bool *ok, int base) const const
void setUpdated(const QDateTime &updated)
Sets the time the Content has been last updated.
Definition: content.cpp:119
void setNumberOfComments(int numComments)
Sets the number of comments for the Content.
Definition: content.cpp:99
QString toString() const const
void setVideos(QList< QUrl > videos)
Set list of videos.
Definition: content.cpp:313
QStringRef value(const QString &namespaceUri, const QString &name) const const
void setId(const QString &id)
Sets the id of the Content.
Definition: content.cpp:59
void append(const T &value)
QList< QUrl > videos()
Get all videos for this content.
Definition: content.cpp:308
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
int toInt(bool *ok, int base) const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
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
QXmlStreamReader::TokenType readNext()
QDateTime updated() const
Gets the date and time the Content has been last updated.
Definition: content.cpp:124
QDateTime created() const
Gets the date and time the Content has been created.
Definition: content.cpp:114
void setCreated(const QDateTime &created)
Sets the date and time the Content has been created.
Definition: content.cpp:109
QDateTime fromString(const QString &string, Qt::DateFormat format)
The Icon class contains information about an icon.
Definition: icon.h:24
bool isStartElement() const const
QList< Icon > icons()
Get all icons for this content.
Definition: content.cpp:293
bool isNull() const const
void setIcons(QList< Icon > icons)
Set list of icons.
Definition: content.cpp:303
void setName(const QString &name)
Sets the name of the Content.
Definition: content.cpp:69
QString left(int n) const const
bool isEmpty() const const
QXmlStreamAttributes attributes() const const
void setTags(const QStringList &tags)
Set the list of tags.
Definition: content.cpp:323
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 name() const const
bool isEndElement() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 23:06:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.