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 "groupclientdata.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 GroupClientData::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 key == other.key && value == other.value;
0035     }
0036 
0037     bool operator!=(const Private &other) const
0038     {
0039         return !(*this == other);
0040     }
0041 
0042     QString key{};
0043     QString value{};
0044 };
0045 
0046 GroupClientData::GroupClientData()
0047     : d(new Private)
0048 {
0049 }
0050 
0051 GroupClientData::GroupClientData(const GroupClientData &) = default;
0052 GroupClientData::GroupClientData(GroupClientData &&) noexcept = default;
0053 GroupClientData &GroupClientData::operator=(const GroupClientData &) = default;
0054 GroupClientData &GroupClientData::operator=(GroupClientData &&) noexcept = default;
0055 GroupClientData::~GroupClientData() = default;
0056 
0057 bool GroupClientData::operator==(const GroupClientData &other) const
0058 {
0059     return *d == *other.d;
0060 }
0061 
0062 bool GroupClientData::operator!=(const GroupClientData &other) const
0063 {
0064     return !(*this == other);
0065 }
0066 
0067 QString GroupClientData::key() const
0068 {
0069     return d->key;
0070 }
0071 
0072 void GroupClientData::setKey(const QString &value)
0073 {
0074     d->key = value;
0075 }
0076 QString GroupClientData::value() const
0077 {
0078     return d->value;
0079 }
0080 
0081 void GroupClientData::setValue(const QString &value)
0082 {
0083     d->value = value;
0084 }
0085 
0086 GroupClientData GroupClientData::fromJSON(const QJsonObject &obj)
0087 {
0088     GroupClientData groupClientData;
0089 
0090     if (!obj.isEmpty()) {
0091         groupClientData.setKey(obj.value(QStringLiteral("key")).toString());
0092         groupClientData.setValue(obj.value(QStringLiteral("value")).toString());
0093     }
0094 
0095     return groupClientData;
0096 }
0097 
0098 QList<GroupClientData> GroupClientData::fromJSONArray(const QJsonArray &data)
0099 {
0100     QList<GroupClientData> returnGroupClientData;
0101 
0102     for(const auto &groupClientData : data) {
0103         if(groupClientData.isObject()) {
0104             const auto objectifiedGroupClientData = groupClientData.toObject();
0105             returnGroupClientData.append(fromJSON(objectifiedGroupClientData));
0106         }
0107     }
0108 
0109     return returnGroupClientData;
0110 }
0111 
0112 QJsonValue GroupClientData::toJSON() const
0113 {
0114     QJsonObject obj;
0115 
0116     PeopleUtils::addValueToJsonObjectIfValid(obj, "key", d->key);
0117     PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
0118     return obj;
0119 }
0120 
0121 } // namespace KGAPI2::People