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 "url.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 class Url::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 value == other.value && type == other.type && metadata == other.metadata && formattedType == other.formattedType; 0037 } 0038 0039 bool operator!=(const Private &other) const 0040 { 0041 return !(*this == other); 0042 } 0043 0044 QString value{}; 0045 QString type{}; 0046 FieldMetadata metadata{}; 0047 QString formattedType{}; 0048 }; 0049 0050 Url::Url() 0051 : d(new Private) 0052 { 0053 } 0054 0055 Url::Url(const Url &) = default; 0056 Url::Url(Url &&) noexcept = default; 0057 Url &Url::operator=(const Url &) = default; 0058 Url &Url::operator=(Url &&) noexcept = default; 0059 Url::~Url() = default; 0060 0061 bool Url::operator==(const Url &other) const 0062 { 0063 return *d == *other.d; 0064 } 0065 0066 bool Url::operator!=(const Url &other) const 0067 { 0068 return !(*this == other); 0069 } 0070 0071 QString Url::value() const 0072 { 0073 return d->value; 0074 } 0075 0076 void Url::setValue(const QString &value) 0077 { 0078 d->value = value; 0079 } 0080 QString Url::type() const 0081 { 0082 return d->type; 0083 } 0084 0085 void Url::setType(const QString &value) 0086 { 0087 d->type = value; 0088 } 0089 FieldMetadata Url::metadata() const 0090 { 0091 return d->metadata; 0092 } 0093 0094 void Url::setMetadata(const FieldMetadata &value) 0095 { 0096 d->metadata = value; 0097 } 0098 QString Url::formattedType() const 0099 { 0100 return d->formattedType; 0101 } 0102 0103 Url Url::fromJSON(const QJsonObject &obj) 0104 { 0105 Url url; 0106 0107 if(!obj.isEmpty()) { 0108 const auto metadata = obj.value(QStringLiteral("metadata")).toObject(); 0109 url.d->metadata = FieldMetadata::fromJSON(metadata); 0110 url.d->value = obj.value(QStringLiteral("value")).toString(); 0111 url.d->type = obj.value(QStringLiteral("type")).toString(); 0112 url.d->formattedType = obj.value(QStringLiteral("formattedType")).toString(); 0113 } 0114 0115 return url; 0116 } 0117 0118 QList<Url> Url::fromJSONArray(const QJsonArray &data) 0119 { 0120 QList<Url> urls; 0121 0122 for(const auto &url : data) { 0123 if(url.isObject()) { 0124 const auto objectifiedUrl = url.toObject(); 0125 urls.append(fromJSON(objectifiedUrl)); 0126 } 0127 } 0128 0129 return urls; 0130 } 0131 0132 QJsonValue Url::toJSON() const 0133 { 0134 QJsonObject obj; 0135 0136 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value); 0137 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type); 0138 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON()); 0139 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType", d->formattedType); 0140 return obj; 0141 } 0142 0143 } // namespace KGAPI2::People