File indexing completed on 2024-05-12 05:22:27

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
0003  * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only
0006  * SPDX-License-Identifier: LGPL-3.0-only
0007  * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
0008  */
0009 
0010 #include "interest.h"
0011 
0012 #include "fieldmetadata.h"
0013 #include "peopleservice.h"
0014 
0015 #include <QJsonArray>
0016 #include <QJsonObject>
0017 #include <QJsonValue>
0018 #include <QSharedData>
0019 
0020 #include <algorithm>
0021 
0022 namespace KGAPI2::People
0023 {
0024 class Interest::Private : public QSharedData
0025 {
0026 public:
0027     explicit Private() = default;
0028     Private(const Private &) = default;
0029     Private(Private &&) noexcept = delete;
0030     Private &operator=(const Private &) = delete;
0031     Private &operator=(Private &&) noexcept = delete;
0032     ~Private() = default;
0033 
0034     bool operator==(const Private &other) const
0035     {
0036         return value == other.value && metadata == other.metadata;
0037     }
0038 
0039     bool operator!=(const Private &other) const
0040     {
0041         return !(*this == other);
0042     }
0043 
0044     QString value{};
0045     FieldMetadata metadata{};
0046 };
0047 
0048 Interest::Interest()
0049     : d(new Private)
0050 {
0051 }
0052 
0053 Interest::Interest(const Interest &) = default;
0054 Interest::Interest(Interest &&) noexcept = default;
0055 Interest &Interest::operator=(const Interest &) = default;
0056 Interest &Interest::operator=(Interest &&) noexcept = default;
0057 Interest::~Interest() = default;
0058 
0059 bool Interest::operator==(const Interest &other) const
0060 {
0061     return *d == *other.d;
0062 }
0063 
0064 bool Interest::operator!=(const Interest &other) const
0065 {
0066     return !(*this == other);
0067 }
0068 
0069 QString Interest::value() const
0070 {
0071     return d->value;
0072 }
0073 
0074 void Interest::setValue(const QString &value)
0075 {
0076     d->value = value;
0077 }
0078 FieldMetadata Interest::metadata() const
0079 {
0080     return d->metadata;
0081 }
0082 
0083 void Interest::setMetadata(const FieldMetadata &value)
0084 {
0085     d->metadata = value;
0086 }
0087 
0088 Interest Interest::fromJSON(const QJsonObject &obj)
0089 {
0090     Interest interest;
0091 
0092     if(!obj.isEmpty()) {
0093         const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
0094         interest.setMetadata(FieldMetadata::fromJSON(metadata));
0095         interest.setValue(obj.value(QStringLiteral("value")).toString());
0096     }
0097 
0098     return interest;
0099 }
0100 
0101 QList<Interest> Interest::fromJSONArray(const QJsonArray &data)
0102 {
0103     QList<Interest> interests;
0104 
0105     for(const auto &interest : data) {
0106         if(interest.isObject()) {
0107             const auto objectifiedInterest = interest.toObject();
0108             interests.append(fromJSON(objectifiedInterest));
0109         }
0110     }
0111 
0112     return interests;
0113 }
0114 
0115 QJsonValue Interest::toJSON() const
0116 {
0117     QJsonObject obj;
0118 
0119     PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
0120     // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
0121     return obj;
0122 }
0123 
0124 } // namespace KGAPI2::People