File indexing completed on 2024-12-15 03:45:00

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "survey.h"
0008 
0009 #include <QDebug>
0010 #include <QJsonArray>
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QSharedData>
0014 #include <QString>
0015 #include <QUrl>
0016 #include <QUuid>
0017 
0018 using namespace KUserFeedback::Console;
0019 
0020 namespace KUserFeedback {
0021 namespace Console {
0022 
0023 class SurveyData : public QSharedData
0024 {
0025 public:
0026     QUuid uuid;
0027     QString name;
0028     QUrl url;
0029     QString target;
0030     bool active = false;
0031 };
0032 
0033 }
0034 }
0035 
0036 Survey::Survey() : d(new SurveyData) {}
0037 Survey::Survey(const Survey&) = default;
0038 Survey::~Survey() = default;
0039 Survey& Survey::operator=(const Survey&) = default;
0040 
0041 bool Survey::operator==(const Survey& other) const
0042 {
0043     return d->name == other.d->name
0044         && d->url == other.d->url
0045         && d->target == other.d->target
0046         && d->uuid == other.d->uuid
0047         && d->active == other.d->active;
0048 }
0049 
0050 bool Survey::operator!=(const Survey& other) const
0051 {
0052     return !(*this == other);
0053 }
0054 
0055 QUuid Survey::uuid() const
0056 {
0057     return d->uuid;
0058 }
0059 
0060 void Survey::setUuid(const QUuid &id)
0061 {
0062     d->uuid = id;
0063 }
0064 
0065 QString Survey::name() const
0066 {
0067     return d->name;
0068 }
0069 
0070 void Survey::setName(const QString& name)
0071 {
0072     d->name = name;
0073 }
0074 
0075 QUrl Survey::url() const
0076 {
0077     return d->url;
0078 }
0079 
0080 void Survey::setUrl(const QUrl& url)
0081 {
0082     d->url = url;
0083 }
0084 
0085 bool Survey::isActive() const
0086 {
0087     return d->active;
0088 }
0089 
0090 void Survey::setActive(bool active)
0091 {
0092     d->active = active;
0093 }
0094 
0095 QString Survey::target() const
0096 {
0097     return d->target;
0098 }
0099 
0100 void Survey::setTarget(const QString& target)
0101 {
0102     d->target = target;
0103 }
0104 
0105 QByteArray Survey::toJson() const
0106 {
0107     QJsonObject obj;
0108     obj.insert(QStringLiteral("uuid"), uuid().toString());
0109     obj.insert(QStringLiteral("name"), name());
0110     obj.insert(QStringLiteral("url"), url().toString());
0111     obj.insert(QStringLiteral("active"), isActive());
0112     obj.insert(QStringLiteral("target"), target());
0113     return QJsonDocument(obj).toJson();
0114 }
0115 
0116 QVector<Survey> Survey::fromJson(const QByteArray &data)
0117 {
0118     QVector<Survey> surveys;
0119     foreach (const auto &v, QJsonDocument::fromJson(data).array()) {
0120         const auto obj = v.toObject();
0121         Survey survey;
0122         survey.setUuid(QUuid(obj.value(QLatin1String("uuid")).toString()));
0123         survey.setName(obj.value(QLatin1String("name")).toString());
0124         survey.setUrl(QUrl(obj.value(QLatin1String("url")).toString()));
0125         survey.setActive(obj.value(QLatin1String("active")).toBool());
0126         survey.setTarget(obj.value(QLatin1String("target")).toString());
0127         surveys.push_back(survey);
0128     }
0129     return surveys;
0130 }