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