File indexing completed on 2024-09-08 06:38:03
0001 /* 0002 This file is part of KDE. 0003 0004 SPDX-FileCopyrightText: 2011 Laszlo Papp <djszapi@archlinux.us> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "achievementparser.h" 0010 #include "atticautils.h" 0011 0012 using namespace Attica; 0013 0014 Achievement Achievement::Parser::parseXml(QXmlStreamReader &xml) 0015 { 0016 Achievement achievement; 0017 0018 while (!xml.atEnd()) { 0019 xml.readNext(); 0020 0021 if (xml.isStartElement()) { 0022 if (xml.name() == QLatin1String("id")) { 0023 achievement.setId(xml.readElementText()); 0024 } else if (xml.name() == QLatin1String("content_id")) { 0025 achievement.setContentId(xml.readElementText()); 0026 } else if (xml.name() == QLatin1String("name")) { 0027 achievement.setName(xml.readElementText()); 0028 } else if (xml.name() == QLatin1String("description")) { 0029 achievement.setDescription(xml.readElementText()); 0030 } else if (xml.name() == QLatin1String("explanation")) { 0031 achievement.setExplanation(xml.readElementText()); 0032 } else if (xml.name() == QLatin1String("points")) { 0033 achievement.setPoints(xml.readElementText().toInt()); 0034 } else if (xml.name() == QLatin1String("image")) { 0035 achievement.setImage(QUrl(xml.readElementText())); 0036 } else if (xml.name() == QLatin1String("dependencies")) { 0037 QStringList dependencies = parseXmlDependencies(xml); 0038 achievement.setDependencies(dependencies); 0039 } else if (xml.name() == QLatin1String("visibility")) { 0040 achievement.setVisibility(Achievement::stringToAchievementVisibility(xml.readElementText())); 0041 } else if (xml.name() == QLatin1String("type")) { 0042 achievement.setType(Achievement::stringToAchievementType(xml.readElementText())); 0043 } else if (xml.name() == QLatin1String("options")) { 0044 QStringList options = parseXmlOptions(xml); 0045 achievement.setOptions(options); 0046 } else if (xml.name() == QLatin1String("steps")) { 0047 achievement.setSteps(xml.readElementText().toInt()); 0048 } else if (xml.name() == QLatin1String("progress")) { 0049 switch (achievement.type()) { 0050 case Achievement::FlowingAchievement: 0051 achievement.setProgress(QVariant(xml.readElementText().toFloat())); 0052 break; 0053 case Achievement::SteppedAchievement: 0054 achievement.setProgress(QVariant(xml.readElementText().toInt())); 0055 break; 0056 case Achievement::NamedstepsAchievement: 0057 achievement.setProgress(QVariant(xml.readElementText())); 0058 break; 0059 case Achievement::SetAchievement: { 0060 QVariant progress = parseXmlProgress(xml); 0061 achievement.setProgress(progress); 0062 break; 0063 } 0064 default: 0065 break; 0066 } 0067 } 0068 } else if (xml.isEndElement() && xml.name() == QLatin1String("achievement")) { 0069 break; 0070 } 0071 } 0072 0073 return achievement; 0074 } 0075 0076 QStringList Achievement::Parser::parseXmlDependencies(QXmlStreamReader &xml) 0077 { 0078 QStringList dependencies; 0079 0080 while (!xml.atEnd()) { 0081 xml.readNext(); 0082 0083 if (xml.isStartElement()) { 0084 if (xml.name() == QLatin1String("achievement_id")) { 0085 dependencies.append(xml.readElementText()); 0086 } 0087 } else if (xml.isEndElement() && xml.name() == QLatin1String("dependencies")) { 0088 break; 0089 } 0090 } 0091 0092 return dependencies; 0093 } 0094 0095 QStringList Achievement::Parser::parseXmlOptions(QXmlStreamReader &xml) 0096 { 0097 QStringList options; 0098 0099 while (!xml.atEnd()) { 0100 xml.readNext(); 0101 0102 if (xml.isStartElement()) { 0103 if (xml.name() == QLatin1String("option")) { 0104 options.append(xml.readElementText()); 0105 } 0106 } else if (xml.isEndElement() && xml.name() == QLatin1String("options")) { 0107 break; 0108 } 0109 } 0110 0111 return options; 0112 } 0113 0114 QVariant Achievement::Parser::parseXmlProgress(QXmlStreamReader &xml) 0115 { 0116 QStringList progress; 0117 0118 while (!xml.atEnd()) { 0119 xml.readNext(); 0120 0121 if (xml.isStartElement()) { 0122 if (xml.name() == QLatin1String("reached")) { 0123 progress.append(xml.readElementText()); 0124 } 0125 } else if (xml.isEndElement() && xml.name() == QLatin1String("progress")) { 0126 break; 0127 } 0128 } 0129 0130 return progress; 0131 } 0132 0133 QStringList Achievement::Parser::xmlElement() const 0134 { 0135 return QStringList(QStringLiteral("achievement")); 0136 }