1/*
2 This file is part of KDE.
3
4 SPDX-FileCopyrightText: 2011 Laszlo Papp <djszapi@archlinux.us>
5
6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7*/
8
9#include "achievementparser.h"
10#include "atticautils.h"
11
12using namespace Attica;
13
14Achievement Achievement::Parser::parseXml(QXmlStreamReader &xml)
15{
16 Achievement achievement;
17
18 while (!xml.atEnd()) {
19 xml.readNext();
20
21 if (xml.isStartElement()) {
22 if (xml.name() == QLatin1String("id")) {
23 achievement.setId(xml.readElementText());
24 } else if (xml.name() == QLatin1String("content_id")) {
25 achievement.setContentId(xml.readElementText());
26 } else if (xml.name() == QLatin1String("name")) {
27 achievement.setName(xml.readElementText());
28 } else if (xml.name() == QLatin1String("description")) {
29 achievement.setDescription(xml.readElementText());
30 } else if (xml.name() == QLatin1String("explanation")) {
31 achievement.setExplanation(xml.readElementText());
32 } else if (xml.name() == QLatin1String("points")) {
33 achievement.setPoints(xml.readElementText().toInt());
34 } else if (xml.name() == QLatin1String("image")) {
35 achievement.setImage(QUrl(xml.readElementText()));
36 } else if (xml.name() == QLatin1String("dependencies")) {
37 QStringList dependencies = parseXmlDependencies(xml);
38 achievement.setDependencies(dependencies);
39 } else if (xml.name() == QLatin1String("visibility")) {
40 achievement.setVisibility(Achievement::stringToAchievementVisibility(achievementVisibilityString: xml.readElementText()));
41 } else if (xml.name() == QLatin1String("type")) {
42 achievement.setType(Achievement::stringToAchievementType(achievementTypeString: xml.readElementText()));
43 } else if (xml.name() == QLatin1String("options")) {
44 QStringList options = parseXmlOptions(xml);
45 achievement.setOptions(options);
46 } else if (xml.name() == QLatin1String("steps")) {
47 achievement.setSteps(xml.readElementText().toInt());
48 } else if (xml.name() == QLatin1String("progress")) {
49 switch (achievement.type()) {
50 case Achievement::FlowingAchievement:
51 achievement.setProgress(QVariant(xml.readElementText().toFloat()));
52 break;
53 case Achievement::SteppedAchievement:
54 achievement.setProgress(QVariant(xml.readElementText().toInt()));
55 break;
56 case Achievement::NamedstepsAchievement:
57 achievement.setProgress(QVariant(xml.readElementText()));
58 break;
59 case Achievement::SetAchievement: {
60 QVariant progress = parseXmlProgress(xml);
61 achievement.setProgress(progress);
62 break;
63 }
64 default:
65 break;
66 }
67 }
68 } else if (xml.isEndElement() && xml.name() == QLatin1String("achievement")) {
69 break;
70 }
71 }
72
73 return achievement;
74}
75
76QStringList Achievement::Parser::parseXmlDependencies(QXmlStreamReader &xml)
77{
78 QStringList dependencies;
79
80 while (!xml.atEnd()) {
81 xml.readNext();
82
83 if (xml.isStartElement()) {
84 if (xml.name() == QLatin1String("achievement_id")) {
85 dependencies.append(t: xml.readElementText());
86 }
87 } else if (xml.isEndElement() && xml.name() == QLatin1String("dependencies")) {
88 break;
89 }
90 }
91
92 return dependencies;
93}
94
95QStringList Achievement::Parser::parseXmlOptions(QXmlStreamReader &xml)
96{
97 QStringList options;
98
99 while (!xml.atEnd()) {
100 xml.readNext();
101
102 if (xml.isStartElement()) {
103 if (xml.name() == QLatin1String("option")) {
104 options.append(t: xml.readElementText());
105 }
106 } else if (xml.isEndElement() && xml.name() == QLatin1String("options")) {
107 break;
108 }
109 }
110
111 return options;
112}
113
114QVariant Achievement::Parser::parseXmlProgress(QXmlStreamReader &xml)
115{
116 QStringList progress;
117
118 while (!xml.atEnd()) {
119 xml.readNext();
120
121 if (xml.isStartElement()) {
122 if (xml.name() == QLatin1String("reached")) {
123 progress.append(t: xml.readElementText());
124 }
125 } else if (xml.isEndElement() && xml.name() == QLatin1String("progress")) {
126 break;
127 }
128 }
129
130 return progress;
131}
132
133QStringList Achievement::Parser::xmlElement() const
134{
135 return QStringList(QStringLiteral("achievement"));
136}
137

source code of attica/src/achievementparser.cpp