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 | |
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(n: 19); |
37 | content.setCreated(QDateTime::fromString(string: dateString, format: 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(n: 19); |
41 | content.setUpdated(QDateTime::fromString(string: dateString, format: 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(qualifiedName: QLatin1String("width" )); |
49 | if (!width.isEmpty()) { |
50 | icon.setWidth(width.toInt()); |
51 | } |
52 | |
53 | const auto height = attributes.value(qualifiedName: 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(t: 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(t: video); |
69 | content.setVideos(videos); |
70 | } else if (xml.name() == QLatin1String("tags" )) { |
71 | content.setTags(xml.readElementText().split(sep: QLatin1Char(','))); |
72 | } else { |
73 | content.addAttribute(key: xml.name().toString(), value: 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 | } |
94 | |