File indexing completed on 2024-04-21 03:51:28

0001 /*
0002     This file is part of KDE.
0003 
0004     SPDX-FileCopyrightText: 2008 Cornelius Schumacher <schumacher@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "contentparser.h"
0010 
0011 #include <QDateTime>
0012 #include <QDebug>
0013 
0014 using namespace Attica;
0015 
0016 Content Content::Parser::parseXml(QXmlStreamReader &xml)
0017 {
0018     Content content;
0019 
0020     while (!xml.atEnd()) {
0021         xml.readNext();
0022 
0023         if (xml.isStartElement()) {
0024             if (xml.name() == QLatin1String("id")) {
0025                 content.setId(xml.readElementText());
0026             } else if (xml.name() == QLatin1String("name")) {
0027                 content.setName(xml.readElementText());
0028             } else if (xml.name() == QLatin1String("score")) {
0029                 content.setRating(xml.readElementText().toInt());
0030             } else if (xml.name() == QLatin1String("downloads")) {
0031                 content.setDownloads(xml.readElementText().toInt());
0032             } else if (xml.name() == QLatin1String("comments")) {
0033                 content.setNumberOfComments(xml.readElementText().toInt());
0034             } else if (xml.name() == QLatin1String("created")) {
0035                 // Qt doesn't accept +-Timezone modifiers, truncate if the string contains them
0036                 QString dateString = xml.readElementText().left(19);
0037                 content.setCreated(QDateTime::fromString(dateString, Qt::ISODate));
0038             } else if (xml.name() == QLatin1String("changed")) {
0039                 // Qt doesn't accept +-Timezone modifiers, truncate if the string contains them
0040                 QString dateString = xml.readElementText().left(19);
0041                 content.setUpdated(QDateTime::fromString(dateString, Qt::ISODate));
0042             } else if (xml.name() == QLatin1String("icon")) {
0043                 Icon icon;
0044                 icon.setUrl(QUrl(xml.readElementText()));
0045 
0046                 const QXmlStreamAttributes attributes = xml.attributes();
0047 
0048                 const auto width = attributes.value(QLatin1String("width"));
0049                 if (!width.isEmpty()) {
0050                     icon.setWidth(width.toInt());
0051                 }
0052 
0053                 const auto height = attributes.value(QLatin1String("height"));
0054                 if (!height.isEmpty()) {
0055                     icon.setHeight(height.toInt());
0056                 }
0057 
0058                 // append the icon to the current list of icons
0059                 QList<Icon> icons;
0060                 icons = content.icons();
0061                 icons.append(icon);
0062                 content.setIcons(icons);
0063             } else if (xml.name() == QLatin1String("video")) {
0064                 QUrl video(xml.readElementText());
0065                 // append the video to the current list of videos
0066                 QList<QUrl> videos;
0067                 videos = content.videos();
0068                 videos.append(video);
0069                 content.setVideos(videos);
0070             } else if (xml.name() == QLatin1String("tags")) {
0071                 content.setTags(xml.readElementText().split(QLatin1Char(',')));
0072             } else {
0073                 content.addAttribute(xml.name().toString(), xml.readElementText());
0074             }
0075         }
0076 
0077         if (xml.isEndElement() && xml.name() == QLatin1String("content")) {
0078             break;
0079         }
0080     }
0081 
0082     // in case the server only sets creation date, use that as updated also
0083     if (content.updated().isNull()) {
0084         content.setUpdated(content.created());
0085     }
0086 
0087     return content;
0088 }
0089 
0090 QStringList Content::Parser::xmlElement() const
0091 {
0092     return QStringList(QStringLiteral("content"));
0093 }