Kgapi

url.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 "url.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 Url::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 value == other.value && type == other.type && metadata == other.metadata && formattedType == other.formattedType;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 QString value{};
45 QString type{};
46 FieldMetadata metadata{};
47 QString formattedType{};
48};
49
51 : d(new Private)
52{
53}
54
55Url::Url(const Url &) = default;
56Url::Url(Url &&) noexcept = default;
57Url &Url::operator=(const Url &) = default;
58Url &Url::operator=(Url &&) noexcept = default;
59Url::~Url() = default;
60
61bool Url::operator==(const Url &other) const
62{
63 return *d == *other.d;
64}
65
66bool Url::operator!=(const Url &other) const
67{
68 return !(*this == other);
69}
70
72{
73 return d->value;
74}
75
76void Url::setValue(const QString &value)
77{
78 d->value = value;
79}
81{
82 return d->type;
83}
84
85void Url::setType(const QString &value)
86{
87 d->type = value;
88}
90{
91 return d->metadata;
92}
93
95{
96 d->metadata = value;
97}
99{
100 return d->formattedType;
101}
102
103Url Url::fromJSON(const QJsonObject &obj)
104{
105 Url url;
106
107 if(!obj.isEmpty()) {
108 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
109 url.d->metadata = FieldMetadata::fromJSON(metadata);
110 url.d->value = obj.value(QStringLiteral("value")).toString();
111 url.d->type = obj.value(QStringLiteral("type")).toString();
112 url.d->formattedType = obj.value(QStringLiteral("formattedType")).toString();
113 }
114
115 return url;
116}
117
118QList<Url> Url::fromJSONArray(const QJsonArray &data)
119{
120 QList<Url> urls;
121
122 for(const auto &url : data) {
123 if(url.isObject()) {
124 const auto objectifiedUrl = url.toObject();
125 urls.append(fromJSON(objectifiedUrl));
126 }
127 }
128
129 return urls;
130}
131
132QJsonValue Url::toJSON() const
133{
134 QJsonObject obj;
135
136 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
137 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
138 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
139 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType", d->formattedType);
140 return obj;
141}
142
143} // namespace KGAPI2::People
Metadata about a field.
A person's associated URLs.
Definition url.h:34
QString formattedType() const
Output only.
Definition url.cpp:98
void setType(const QString &value)
Sets value of the type property.
Definition url.cpp:85
Url()
Constructs a new Url.
Definition url.cpp:50
void setValue(const QString &value)
Sets value of the value property.
Definition url.cpp:76
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition url.cpp:94
FieldMetadata metadata() const
Metadata about the URL.
Definition url.cpp:89
QString type() const
The type of the URL.
Definition url.cpp:80
QString value() const
The URL.
Definition url.cpp:71
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QString toString() 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 4 2024 16:36:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.