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

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 "personmetadata.h"
0011 
0012 #include "peopleservice.h"
0013 #include "source.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 PersonMetadata::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 linkedPeopleResourceNames == other.linkedPeopleResourceNames && objectType == other.objectType
0037             && previousResourceNames == other.previousResourceNames && deleted == other.deleted && sources == other.sources;
0038     }
0039 
0040     bool operator!=(const Private &other) const
0041     {
0042         return !(*this == other);
0043     }
0044 
0045     QList<QString> linkedPeopleResourceNames{};
0046     PersonMetadata::ObjectType objectType{};
0047     QList<QString> previousResourceNames{};
0048     bool deleted{};
0049     QList<Source> sources{};
0050 };
0051 
0052 PersonMetadata::PersonMetadata()
0053     : d(new Private)
0054 {
0055 }
0056 
0057 PersonMetadata::PersonMetadata(const PersonMetadata &) = default;
0058 PersonMetadata::PersonMetadata(PersonMetadata &&) noexcept = default;
0059 PersonMetadata &PersonMetadata::operator=(const PersonMetadata &) = default;
0060 PersonMetadata &PersonMetadata::operator=(PersonMetadata &&) noexcept = default;
0061 PersonMetadata::~PersonMetadata() = default;
0062 
0063 bool PersonMetadata::operator==(const PersonMetadata &other) const
0064 {
0065     return *d == *other.d;
0066 }
0067 
0068 bool PersonMetadata::operator!=(const PersonMetadata &other) const
0069 {
0070     return !(*this == other);
0071 }
0072 
0073 QList<QString> PersonMetadata::linkedPeopleResourceNames() const
0074 {
0075     return d->linkedPeopleResourceNames;
0076 }
0077 PersonMetadata::PersonMetadata::ObjectType PersonMetadata::objectType() const
0078 {
0079     return d->objectType;
0080 }
0081 QList<QString> PersonMetadata::previousResourceNames() const
0082 {
0083     return d->previousResourceNames;
0084 }
0085 bool PersonMetadata::deleted() const
0086 {
0087     return d->deleted;
0088 }
0089 QList<Source> PersonMetadata::sources() const
0090 {
0091     return d->sources;
0092 }
0093 
0094 void PersonMetadata::setSources(const QList<Source> &value)
0095 {
0096     d->sources = value;
0097 }
0098 
0099 void PersonMetadata::addSource(const Source &value)
0100 {
0101     d->sources.push_back(value);
0102 }
0103 
0104 void PersonMetadata::removeSource(const Source &value)
0105 {
0106     d->sources.removeOne(value);
0107 }
0108 
0109 void PersonMetadata::clearSources()
0110 {
0111     d->sources.clear();
0112 }
0113 
0114 PersonMetadata PersonMetadata::fromJSON(const QJsonObject &obj)
0115 {
0116     PersonMetadata personMetadata;
0117 
0118     if(!obj.isEmpty()) {
0119         for(const auto &jsonSource : obj.value(QStringLiteral("sources")).toArray()) {
0120             personMetadata.d->sources.append(Source::fromJSON(jsonSource.toObject()));
0121         }
0122 
0123         for(const auto &jsonPrevResName : obj.value(QStringLiteral("previousResourceNames")).toArray()) {
0124             personMetadata.d->previousResourceNames.append(jsonPrevResName.toString());
0125         }
0126 
0127         for(const auto &jsonLinkedPeopleResName : obj.value(QStringLiteral("linkedPeopleResourceNames")).toArray()) {
0128             personMetadata.d->linkedPeopleResourceNames.append(jsonLinkedPeopleResName.toString());
0129         }
0130 
0131         personMetadata.d->deleted = obj.value(QStringLiteral("deleted")).toBool();
0132 
0133         // Don't add objectType here, is deprecated
0134     }
0135 
0136     return personMetadata;
0137 }
0138 
0139 QJsonValue PersonMetadata::toJSON() const
0140 {
0141     QJsonObject obj;
0142 
0143     /* Output only
0144     {
0145         QJsonArray arr;
0146         std::transform(d->linkedPeopleResourceNames.cbegin(), d->linkedPeopleResourceNames.cend(), std::back_inserter(arr), [](const auto &val) {
0147             return val;
0148         });
0149         PeopleUtils::addValueToJsonObjectIfValid(obj, "linkedPeopleResourceNames", std::move(arr));
0150     }
0151     */
0152     /* Output only
0153     switch (d->objectType) {
0154     case ObjectType::OBJECT_TYPE_UNSPECIFIED:
0155         PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("OBJECT_TYPE_UNSPECIFIED"));
0156         break;
0157     case ObjectType::PERSON:
0158         PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("PERSON"));
0159         break;
0160     case ObjectType::PAGE:
0161         PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("PAGE"));
0162         break;
0163     }*/
0164     /* Output only
0165     {
0166         QJsonArray arr;
0167         std::transform(d->previousResourceNames.cbegin(), d->previousResourceNames.cend(), std::back_inserter(arr), [](const auto &val) {
0168             return val;
0169         });
0170         PeopleUtils::addValueToJsonObjectIfValid(obj, "previousResourceNames", std::move(arr));
0171     }
0172     */
0173     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "deleted", d->deleted);
0174     {
0175         QJsonArray arr;
0176         std::transform(d->sources.cbegin(), d->sources.cend(), std::back_inserter(arr), [](const auto &val) {
0177             return val.toJSON();
0178         });
0179         if (!arr.isEmpty()) {
0180             PeopleUtils::addValueToJsonObjectIfValid(obj, "sources", std::move(arr));
0181         }
0182     }
0183 
0184     return obj;
0185 }
0186 
0187 } // namespace KGAPI2::People