Kgapi

biography.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
3 * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 * SPDX-License-Identifier: LGPL-3.0-only
7 * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
8 */
9
10#include "biography.h"
11
12#include "fieldmetadata.h"
13#include "peopleservice.h"
14
15#include <QJsonArray>
16#include <QJsonObject>
17#include <QJsonValue>
18#include <QSharedData>
19
20#include <algorithm>
21
22namespace KGAPI2::People
23{
24class Biography::Private : public QSharedData
25{
26public:
27 explicit Private() = default;
28 Private(const Private &) = default;
29 Private(Private &&) noexcept = delete;
30 Private &operator=(const Private &) = delete;
31 Private &operator=(Private &&) noexcept = delete;
32 ~Private() = default;
33
34 bool operator==(const Private &other) const
35 {
36 return metadata == other.metadata && contentType == other.contentType && value == other.value;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 FieldMetadata metadata{};
45 Biography::ContentType contentType{};
46 QString value{};
47};
48
50 : d(new Private)
51{
52}
53
54Biography::Biography(const Biography &) = default;
55Biography::Biography(Biography &&) noexcept = default;
56Biography &Biography::operator=(const Biography &) = default;
57Biography &Biography::operator=(Biography &&) noexcept = default;
58Biography::~Biography() = default;
59
60bool Biography::operator==(const Biography &other) const
61{
62 return *d == *other.d;
63}
64
65bool Biography::operator!=(const Biography &other) const
66{
67 return !(*this == other);
68}
69
71{
72 return d->metadata;
73}
74
76{
77 d->metadata = value;
78}
79Biography::Biography::ContentType Biography::contentType() const
80{
81 return d->contentType;
82}
83
85{
86 d->contentType = value;
87}
89{
90 return d->value;
91}
92
93void Biography::setValue(const QString &value)
94{
95 d->value = value;
96}
97
98Biography Biography::fromJSON(const QJsonObject &obj)
99{
100 Biography biography;
101
102 if(!obj.isEmpty()) {
103 const auto jsonMetadata = obj.value(QStringLiteral("metadata")).toObject();
104
105 biography.setMetadata(FieldMetadata::fromJSON(jsonMetadata));
106 biography.setValue(obj.value(QStringLiteral("value")).toString());
107
108 const auto jsonContentType = obj.value(QStringLiteral("contentType"));
109 if (jsonContentType == QLatin1StringView("TEXT_PLAIN")) {
111 } else if (jsonContentType == QLatin1StringView("TEXT_HTML")) {
113 } else {
115 }
116 }
117
118 return biography;
119}
120
121QList<Biography> Biography::fromJSONArray(const QJsonArray &data)
122{
123 QList<Biography> biographies;
124
125 for(const auto &biography : data) {
126 if(biography.isObject()) {
127 const auto objectifiedBiography = biography.toObject();
128 biographies.append(fromJSON(objectifiedBiography));
129 }
130 }
131
132 return biographies;
133}
134
135QJsonValue Biography::toJSON() const
136{
137 QJsonObject obj;
138
139 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
140 switch (d->contentType) {
142 PeopleUtils::addValueToJsonObjectIfValid(obj, "contentType", QStringLiteral("CONTENT_TYPE_UNSPECIFIED"));
143 break;
145 PeopleUtils::addValueToJsonObjectIfValid(obj, "contentType", QStringLiteral("TEXT_PLAIN"));
146 break;
148 PeopleUtils::addValueToJsonObjectIfValid(obj, "contentType", QStringLiteral("TEXT_HTML"));
149 break;
150 }
151 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
152 return obj;
153}
154
155} // namespace KGAPI2::People
A person's short biography.
Definition biography.h:34
Biography::ContentType contentType() const
The content type of the biography.
Definition biography.cpp:79
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition biography.cpp:75
Biography()
Constructs a new Biography.
Definition biography.cpp:49
void setValue(const QString &value)
Sets value of the value property.
Definition biography.cpp:93
FieldMetadata metadata() const
Metadata about the biography.
Definition biography.cpp:70
QString value() const
The short biography.
Definition biography.cpp:88
void setContentType(Biography::ContentType value)
Sets value of the contentType property.
Definition biography.cpp:84
Metadata about a field.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
void append(QList< T > &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.