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

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 "fieldmetadata.h"
0011 #include "peopleservice.h"
0012 
0013 #include <QJsonArray>
0014 #include <QJsonObject>
0015 #include <QJsonValue>
0016 #include <QSharedData>
0017 
0018 #include <algorithm>
0019 
0020 namespace KGAPI2::People
0021 {
0022 class FieldMetadata::Private : public QSharedData
0023 {
0024 public:
0025     explicit Private() = default;
0026     Private(const Private &) = default;
0027     Private(Private &&) noexcept = delete;
0028     Private &operator=(const Private &) = delete;
0029     Private &operator=(Private &&) noexcept = delete;
0030     ~Private() = default;
0031 
0032     bool operator==(const Private &other) const
0033     {
0034         return source == other.source && sourcePrimary == other.sourcePrimary && primary == other.primary && verified == other.verified;
0035     }
0036 
0037     bool operator!=(const Private &other) const
0038     {
0039         return !(*this == other);
0040     }
0041 
0042     Source source{};
0043     bool sourcePrimary{};
0044     bool primary{};
0045     bool verified{};
0046 };
0047 
0048 FieldMetadata::FieldMetadata()
0049     : d(new Private)
0050 {
0051 }
0052 
0053 FieldMetadata::FieldMetadata(const FieldMetadata &) = default;
0054 FieldMetadata::FieldMetadata(FieldMetadata &&) noexcept = default;
0055 FieldMetadata &FieldMetadata::operator=(const FieldMetadata &) = default;
0056 FieldMetadata &FieldMetadata::operator=(FieldMetadata &&) noexcept = default;
0057 FieldMetadata::~FieldMetadata() = default;
0058 
0059 bool FieldMetadata::operator==(const FieldMetadata &other) const
0060 {
0061     return *d == *other.d;
0062 }
0063 
0064 bool FieldMetadata::operator!=(const FieldMetadata &other) const
0065 {
0066     return !(*this == other);
0067 }
0068 
0069 Source FieldMetadata::source() const
0070 {
0071     return d->source;
0072 }
0073 
0074 void FieldMetadata::setSource(const Source &value)
0075 {
0076     d->source = value;
0077 }
0078 bool FieldMetadata::sourcePrimary() const
0079 {
0080     return d->sourcePrimary;
0081 }
0082 
0083 void FieldMetadata::setSourcePrimary(bool value)
0084 {
0085     d->sourcePrimary = value;
0086 }
0087 bool FieldMetadata::primary() const
0088 {
0089     return d->primary;
0090 }
0091 bool FieldMetadata::verified() const
0092 {
0093     return d->verified;
0094 }
0095 
0096 FieldMetadata FieldMetadata::fromJSON(const QJsonObject &obj)
0097 {
0098     FieldMetadata fieldMetadata;
0099 
0100     if(!obj.isEmpty()) {
0101         fieldMetadata.d->primary = obj.value(QStringLiteral("primary")).toBool();
0102         fieldMetadata.d->sourcePrimary = obj.value(QStringLiteral("sourcePrimary")).toBool();
0103         fieldMetadata.d->verified = obj.value(QStringLiteral("verified")).toBool();
0104         fieldMetadata.d->source = Source::fromJSON(obj.value(QStringLiteral("source")).toObject());
0105     }
0106 
0107     return fieldMetadata;
0108 }
0109 
0110 QJsonValue FieldMetadata::toJSON() const
0111 {
0112     QJsonObject obj;
0113 
0114     PeopleUtils::addValueToJsonObjectIfValid(obj, "source", d->source.toJSON());
0115     PeopleUtils::addValueToJsonObjectIfValid(obj, "sourcePrimary", d->sourcePrimary);
0116     PeopleUtils::addValueToJsonObjectIfValid(obj, "primary", d->primary);
0117     PeopleUtils::addValueToJsonObjectIfValid(obj, "verified", d->verified);
0118     return obj;
0119 }
0120 
0121 } // namespace KGAPI2::People