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

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 "imclient.h"
0011 
0012 #include "fieldmetadata.h"
0013 #include "peopleservice.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 
0025 struct ImClientDefinition {
0026     FieldMetadata metadata;
0027     QString username;
0028     QString type;
0029     QString formattedType;
0030     QString protocol;
0031     QString formattedProtocol;
0032 };
0033 
0034 class ImClient::Private : public QSharedData
0035 {
0036 public:
0037     explicit Private() = default;
0038     Private(const Private &) = default;
0039     Private(Private &&) noexcept = delete;
0040     Private &operator=(const Private &) = delete;
0041     Private &operator=(Private &&) noexcept = delete;
0042     ~Private() = default;
0043 
0044     bool operator==(const Private &other) const
0045     {
0046         return protocol == other.protocol && username == other.username && type == other.type && metadata == other.metadata
0047             && formattedType == other.formattedType && formattedProtocol == other.formattedProtocol;
0048     }
0049 
0050     bool operator!=(const Private &other) const
0051     {
0052         return !(*this == other);
0053     }
0054 
0055     QString protocol{};
0056     QString username{};
0057     QString type{};
0058     FieldMetadata metadata{};
0059     QString formattedType{};
0060     QString formattedProtocol{};
0061 };
0062 
0063 ImClient::ImClient()
0064     : d(new Private)
0065 {
0066 }
0067 
0068 ImClient::ImClient(const ImClientDefinition &definition)
0069     : d(new Private)
0070 {
0071     d->metadata = definition.metadata;
0072     d->username = definition.username;
0073     d->type = definition.type;
0074     d->formattedType = definition.formattedType;
0075     d->protocol = definition.protocol;
0076     d->formattedProtocol = definition.formattedProtocol;
0077 }
0078 
0079 ImClient::ImClient(const ImClient &) = default;
0080 ImClient::ImClient(ImClient &&) noexcept = default;
0081 ImClient &ImClient::operator=(const ImClient &) = default;
0082 ImClient &ImClient::operator=(ImClient &&) noexcept = default;
0083 ImClient::~ImClient() = default;
0084 
0085 bool ImClient::operator==(const ImClient &other) const
0086 {
0087     return *d == *other.d;
0088 }
0089 
0090 bool ImClient::operator!=(const ImClient &other) const
0091 {
0092     return !(*this == other);
0093 }
0094 
0095 QString ImClient::protocol() const
0096 {
0097     return d->protocol;
0098 }
0099 
0100 void ImClient::setProtocol(const QString &value)
0101 {
0102     d->protocol = value;
0103 }
0104 QString ImClient::username() const
0105 {
0106     return d->username;
0107 }
0108 
0109 void ImClient::setUsername(const QString &value)
0110 {
0111     d->username = value;
0112 }
0113 QString ImClient::type() const
0114 {
0115     return d->type;
0116 }
0117 
0118 void ImClient::setType(const QString &value)
0119 {
0120     d->type = value;
0121 }
0122 FieldMetadata ImClient::metadata() const
0123 {
0124     return d->metadata;
0125 }
0126 
0127 void ImClient::setMetadata(const FieldMetadata &value)
0128 {
0129     d->metadata = value;
0130 }
0131 QString ImClient::formattedType() const
0132 {
0133     return d->formattedType;
0134 }
0135 QString ImClient::formattedProtocol() const
0136 {
0137     return d->formattedProtocol;
0138 }
0139 
0140 ImClient ImClient::fromJSON(const QJsonObject &obj)
0141 {
0142     if(obj.isEmpty()) {
0143         return ImClient();
0144     }
0145 
0146     ImClientDefinition definition;
0147 
0148     const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
0149     definition.metadata = FieldMetadata::fromJSON(metadata);
0150     definition.username = obj.value(QStringLiteral("username")).toString();
0151     definition.type = obj.value(QStringLiteral("type")).toString();
0152     definition.formattedType = obj.value(QStringLiteral("formattedType")).toString();
0153     definition.protocol = obj.value(QStringLiteral("protocol")).toString();
0154     definition.formattedProtocol = obj.value(QStringLiteral("formattedProtocol")).toString();
0155 
0156     return ImClient(definition);
0157 }
0158 
0159 QList<ImClient> ImClient::fromJSONArray(const QJsonArray &data)
0160 {
0161     QList<ImClient> imClients;
0162 
0163     for(const auto &imClient : data) {
0164         if(imClient.isObject()) {
0165             const auto objectifiedImClient = imClient.toObject();
0166             imClients.append(fromJSON(objectifiedImClient));
0167         }
0168     }
0169 
0170     return imClients;
0171 }
0172 
0173 QJsonValue ImClient::toJSON() const
0174 {
0175     QJsonObject obj;
0176 
0177     PeopleUtils::addValueToJsonObjectIfValid(obj, "protocol", d->protocol);
0178     PeopleUtils::addValueToJsonObjectIfValid(obj, "username", d->username);
0179     PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
0180     // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
0181     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType"}, d->formattedType);
0182     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedProtocol"}, d->formattedProtocol);
0183     return obj;
0184 }
0185 
0186 } // namespace KGAPI2::People