Kgapi

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

KDE's Doxygen guidelines are available online.