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

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 "source.h"
0011 
0012 #include "peopleservice.h"
0013 #include "profilemetadata.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 Source::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 etag == other.etag && profileMetadata == other.profileMetadata && id == other.id && updateTime == other.updateTime && type == other.type;
0037     }
0038 
0039     bool operator!=(const Private &other) const
0040     {
0041         return !(*this == other);
0042     }
0043 
0044     QString etag{};
0045     ProfileMetadata profileMetadata{};
0046     QString id{};
0047     QString updateTime{};
0048     Source::Type type{};
0049 };
0050 
0051 Source::Source()
0052     : d(new Private)
0053 {
0054 }
0055 
0056 Source::Source(const Source &) = default;
0057 Source::Source(Source &&) noexcept = default;
0058 Source &Source::operator=(const Source &) = default;
0059 Source &Source::operator=(Source &&) noexcept = default;
0060 Source::~Source() = default;
0061 
0062 bool Source::operator==(const Source &other) const
0063 {
0064     return *d == *other.d;
0065 }
0066 
0067 bool Source::operator!=(const Source &other) const
0068 {
0069     return !(*this == other);
0070 }
0071 
0072 QString Source::etag() const
0073 {
0074     return d->etag;
0075 }
0076 
0077 void Source::setEtag(const QString &value)
0078 {
0079     d->etag = value;
0080 }
0081 ProfileMetadata Source::profileMetadata() const
0082 {
0083     return d->profileMetadata;
0084 }
0085 QString Source::id() const
0086 {
0087     return d->id;
0088 }
0089 
0090 void Source::setId(const QString &value)
0091 {
0092     d->id = value;
0093 }
0094 QString Source::updateTime() const
0095 {
0096     return d->updateTime;
0097 }
0098 Source::Source::Type Source::type() const
0099 {
0100     return d->type;
0101 }
0102 
0103 void Source::setType(Source::Type value)
0104 {
0105     d->type = value;
0106 }
0107 
0108 Source Source::fromJSON(const QJsonObject &obj)
0109 {
0110     Source source;
0111 
0112     if(!obj.isEmpty()) {
0113         const auto typeEnumString = obj.value(QStringLiteral("type"));
0114 
0115         if (typeEnumString == QLatin1StringView("ACCOUNT")) {
0116             source.d->type = Type::ACCOUNT;
0117         } else if (typeEnumString == QLatin1StringView("PROFILE")) {
0118             source.d->type = Type::PROFILE;
0119         } else if (typeEnumString == QLatin1StringView("DOMAIN_PROFILE")) {
0120             source.d->type = Type::DOMAIN_PROFILE;
0121         } else if (typeEnumString == QLatin1StringView("CONTACT")) {
0122             source.d->type = Type::CONTACT;
0123         } else if (typeEnumString == QLatin1StringView("OTHER_CONTACT")) {
0124             source.d->type = Type::OTHER_CONTACT;
0125         } else if (typeEnumString == QLatin1StringView("DOMAIN_CONTACT")) {
0126             source.d->type = Type::DOMAIN_CONTACT;
0127         } else {
0128             source.d->type = Type::SOURCE_TYPE_UNSPECIFIED;
0129         }
0130 
0131         source.d->id = obj.value(QStringLiteral("id")).toString();
0132         source.d->etag = obj.value(QStringLiteral("etag")).toString();
0133         source.d->updateTime = obj.value(QStringLiteral("id")).toString();
0134         source.d->profileMetadata = ProfileMetadata::fromJSON(obj.value(QStringLiteral("profileMetadata")).toObject());
0135     }
0136 
0137     return source;
0138 }
0139 
0140 QJsonValue Source::toJSON() const
0141 {
0142     QJsonObject obj;
0143 
0144     PeopleUtils::addValueToJsonObjectIfValid(obj, "etag", d->etag);
0145     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "profileMetadata", d->profileMetadata.toJSON());
0146     PeopleUtils::addValueToJsonObjectIfValid(obj, "id", d->id);
0147     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "updateTime", d->updateTime);
0148     switch (d->type) {
0149     case Type::SOURCE_TYPE_UNSPECIFIED:
0150         PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("SOURCE_TYPE_UNSPECIFIED"));
0151         break;
0152     case Type::ACCOUNT:
0153         PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("ACCOUNT"));
0154         break;
0155     case Type::PROFILE:
0156         PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("PROFILE"));
0157         break;
0158     case Type::DOMAIN_PROFILE:
0159         PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("DOMAIN_PROFILE"));
0160         break;
0161     case Type::CONTACT:
0162         PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("CONTACT"));
0163         break;
0164     case Type::OTHER_CONTACT:
0165         PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OTHER_CONTACT"));
0166         break;
0167     case Type::DOMAIN_CONTACT:
0168         PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("DOMAIN_CONTACT"));
0169         break;
0170     }
0171     return obj;
0172 }
0173 
0174 } // namespace KGAPI2::People