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

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 "contactgroup.h"
0011 
0012 #include "contactgroupmetadata.h"
0013 #include "groupclientdata.h"
0014 #include "peopleservice.h"
0015 
0016 #include <QJsonArray>
0017 #include <QJsonObject>
0018 #include <QJsonValue>
0019 #include <QSharedData>
0020 
0021 #include <algorithm>
0022 
0023 namespace KGAPI2::People
0024 {
0025 class ContactGroup::Private
0026 {
0027 public:
0028     explicit Private() = default;
0029     Private(const Private &) = default;
0030     Private(Private &&) noexcept = delete;
0031     Private &operator=(const Private &) = delete;
0032     Private &operator=(Private &&) noexcept = delete;
0033     ~Private() = default;
0034 
0035     bool operator==(const Private &other) const
0036     {
0037         return formattedName == other.formattedName && memberCount == other.memberCount && etag == other.etag && groupType == other.groupType
0038             && clientData == other.clientData && name == other.name && metadata == other.metadata && resourceName == other.resourceName
0039             && memberResourceNames == other.memberResourceNames;
0040     }
0041 
0042     bool operator!=(const Private &other) const
0043     {
0044         return !(*this == other);
0045     }
0046 
0047     QString formattedName{};
0048     int memberCount{};
0049     QString etag{};
0050     ContactGroup::GroupType groupType{};
0051     QList<GroupClientData> clientData{};
0052     QString name{};
0053     ContactGroupMetadata metadata{};
0054     QString resourceName{};
0055     QList<QString> memberResourceNames{};
0056 };
0057 
0058 ContactGroup::ContactGroup()
0059     : d(new Private)
0060 {
0061 }
0062 
0063 ContactGroup::~ContactGroup() = default;
0064 
0065 bool ContactGroup::operator==(const ContactGroup &other) const
0066 {
0067     return *d == *other.d;
0068 }
0069 
0070 bool ContactGroup::operator!=(const ContactGroup &other) const
0071 {
0072     return !(*this == other);
0073 }
0074 
0075 QString ContactGroup::formattedName() const
0076 {
0077     return d->formattedName;
0078 }
0079 int ContactGroup::memberCount() const
0080 {
0081     return d->memberCount;
0082 }
0083 QString ContactGroup::etag() const
0084 {
0085     return d->etag;
0086 }
0087 
0088 void ContactGroup::setEtag(const QString &value)
0089 {
0090     d->etag = value;
0091 }
0092 ContactGroup::ContactGroup::GroupType ContactGroup::groupType() const
0093 {
0094     return d->groupType;
0095 }
0096 QList<GroupClientData> ContactGroup::clientData() const
0097 {
0098     return d->clientData;
0099 }
0100 
0101 void ContactGroup::setClientData(const QList<GroupClientData> &value)
0102 {
0103     d->clientData = value;
0104 }
0105 
0106 void ContactGroup::addGroupClientData(const GroupClientData &value)
0107 {
0108     d->clientData.push_back(value);
0109 }
0110 
0111 void ContactGroup::removeGroupClientData(const GroupClientData &value)
0112 {
0113     d->clientData.removeOne(value);
0114 }
0115 
0116 void ContactGroup::clearClientData()
0117 {
0118     d->clientData.clear();
0119 }
0120 
0121 QString ContactGroup::name() const
0122 {
0123     return d->name;
0124 }
0125 
0126 void ContactGroup::setName(const QString &value)
0127 {
0128     d->name = value;
0129 }
0130 ContactGroupMetadata ContactGroup::metadata() const
0131 {
0132     return d->metadata;
0133 }
0134 QString ContactGroup::resourceName() const
0135 {
0136     return d->resourceName;
0137 }
0138 
0139 void ContactGroup::setResourceName(const QString &value)
0140 {
0141     d->resourceName = value;
0142 }
0143 QList<QString> ContactGroup::memberResourceNames() const
0144 {
0145     return d->memberResourceNames;
0146 }
0147 
0148 ContactGroupPtr ContactGroup::fromJSON(const QJsonObject &obj)
0149 {
0150     auto contactGroup = new ContactGroup;
0151 
0152     if (!obj.isEmpty()) {
0153         contactGroup->d->resourceName = obj.value(QStringLiteral("resourceName")).toString();
0154         contactGroup->d->etag = obj.value(QStringLiteral("etag")).toString();
0155 
0156         const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
0157         contactGroup->d->metadata = ContactGroupMetadata::fromJSON(metadata);
0158 
0159         const auto groupType = obj.value(QStringLiteral("groupType"));
0160         contactGroup->d->groupType = ContactGroup::GroupType(groupType.toInt());
0161 
0162         contactGroup->d->name = obj.value(QStringLiteral("name")).toString();
0163         contactGroup->d->formattedName = obj.value(QStringLiteral("formattedName")).toString();
0164 
0165         const auto memberResourceNames = obj.value(QStringLiteral("memberResourceNames")).toArray();
0166         std::transform(memberResourceNames.cbegin(),
0167                        memberResourceNames.cend(),
0168                        std::back_inserter(contactGroup->d->memberResourceNames),
0169                        [](const QJsonValue &jsonMemberResourceName) {
0170                            return jsonMemberResourceName.toString();
0171                        });
0172 
0173         contactGroup->d->memberCount = obj.value(QStringLiteral("memberCount")).toInt();
0174 
0175         const auto clientData = obj.value(QStringLiteral("clientData")).toArray();
0176         contactGroup->d->clientData = GroupClientData::fromJSONArray(clientData);
0177     }
0178 
0179     return ContactGroupPtr(contactGroup);
0180 }
0181 
0182 QJsonValue ContactGroup::toJSON() const
0183 {
0184     QJsonObject obj;
0185 
0186     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedName", d->formattedName);
0187     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "memberCount", d->memberCount);
0188     PeopleUtils::addValueToJsonObjectIfValid(obj, "etag", d->etag);
0189     /* Output only
0190     switch (d->groupType) {
0191     case GroupType::GROUP_TYPE_UNSPECIFIED:
0192         PeopleUtils::addValueToJsonObjectIfValid(obj, "groupType", QStringLiteral("GROUP_TYPE_UNSPECIFIED"));
0193         break;
0194     case GroupType::USER_CONTACT_GROUP:
0195         PeopleUtils::addValueToJsonObjectIfValid(obj, "groupType", QStringLiteral("USER_CONTACT_GROUP"));
0196         break;
0197     case GroupType::SYSTEM_CONTACT_GROUP:
0198         PeopleUtils::addValueToJsonObjectIfValid(obj, "groupType", QStringLiteral("SYSTEM_CONTACT_GROUP"));
0199         break;
0200     }*/
0201     if (!d->clientData.isEmpty()) {
0202         QJsonArray arr;
0203         std::transform(d->clientData.cbegin(), d->clientData.cend(), std::back_inserter(arr), [](const auto &val) {
0204             return val.toJSON();
0205         });
0206         PeopleUtils::addValueToJsonObjectIfValid(obj, "clientData", std::move(arr));
0207     }
0208     PeopleUtils::addValueToJsonObjectIfValid(obj, "name", d->name);
0209     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
0210     PeopleUtils::addValueToJsonObjectIfValid(obj, "resourceName", d->resourceName);
0211     /* Output only
0212     if (!d->memberResourceNames.isEmpty()) {
0213         QJsonArray arr;
0214         std::transform(d->memberResourceNames.cbegin(), d->memberResourceNames.cend(), std::back_inserter(arr), [](const auto &val) {
0215             return val;
0216         });
0217         PeopleUtils::addValueToJsonObjectIfValid(obj, "memberResourceNames", std::move(arr));
0218     }*/
0219     return obj;
0220 }
0221 
0222 } // namespace KGAPI2::People