Kgapi

personmetadata.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 "personmetadata.h"
11
12#include "peopleservice.h"
13#include "source.h"
14
15#include <QJsonArray>
16#include <QJsonObject>
17#include <QJsonValue>
18#include <QSharedData>
19
20#include <algorithm>
21
22namespace KGAPI2::People
23{
24class PersonMetadata::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 linkedPeopleResourceNames == other.linkedPeopleResourceNames && objectType == other.objectType
37 && previousResourceNames == other.previousResourceNames && deleted == other.deleted && sources == other.sources;
38 }
39
40 bool operator!=(const Private &other) const
41 {
42 return !(*this == other);
43 }
44
45 QList<QString> linkedPeopleResourceNames{};
46 PersonMetadata::ObjectType objectType{};
47 QList<QString> previousResourceNames{};
48 bool deleted{};
49 QList<Source> sources{};
50};
51
53 : d(new Private)
54{
55}
56
59PersonMetadata &PersonMetadata::operator=(const PersonMetadata &) = default;
60PersonMetadata &PersonMetadata::operator=(PersonMetadata &&) noexcept = default;
61PersonMetadata::~PersonMetadata() = default;
62
63bool PersonMetadata::operator==(const PersonMetadata &other) const
64{
65 return *d == *other.d;
66}
67
68bool PersonMetadata::operator!=(const PersonMetadata &other) const
69{
70 return !(*this == other);
71}
72
74{
75 return d->linkedPeopleResourceNames;
76}
77PersonMetadata::PersonMetadata::ObjectType PersonMetadata::objectType() const
78{
79 return d->objectType;
80}
82{
83 return d->previousResourceNames;
84}
86{
87 return d->deleted;
88}
90{
91 return d->sources;
92}
93
95{
96 d->sources = value;
97}
98
100{
101 d->sources.push_back(value);
102}
103
105{
106 d->sources.removeOne(value);
107}
108
110{
111 d->sources.clear();
112}
113
114PersonMetadata PersonMetadata::fromJSON(const QJsonObject &obj)
115{
116 PersonMetadata personMetadata;
117
118 if(!obj.isEmpty()) {
119 for(const auto &jsonSource : obj.value(QStringLiteral("sources")).toArray()) {
120 personMetadata.d->sources.append(Source::fromJSON(jsonSource.toObject()));
121 }
122
123 for(const auto &jsonPrevResName : obj.value(QStringLiteral("previousResourceNames")).toArray()) {
124 personMetadata.d->previousResourceNames.append(jsonPrevResName.toString());
125 }
126
127 for(const auto &jsonLinkedPeopleResName : obj.value(QStringLiteral("linkedPeopleResourceNames")).toArray()) {
128 personMetadata.d->linkedPeopleResourceNames.append(jsonLinkedPeopleResName.toString());
129 }
130
131 personMetadata.d->deleted = obj.value(QStringLiteral("deleted")).toBool();
132
133 // Don't add objectType here, is deprecated
134 }
135
136 return personMetadata;
137}
138
139QJsonValue PersonMetadata::toJSON() const
140{
141 QJsonObject obj;
142
143 /* Output only
144 {
145 QJsonArray arr;
146 std::transform(d->linkedPeopleResourceNames.cbegin(), d->linkedPeopleResourceNames.cend(), std::back_inserter(arr), [](const auto &val) {
147 return val;
148 });
149 PeopleUtils::addValueToJsonObjectIfValid(obj, "linkedPeopleResourceNames", std::move(arr));
150 }
151 */
152 /* Output only
153 switch (d->objectType) {
154 case ObjectType::OBJECT_TYPE_UNSPECIFIED:
155 PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("OBJECT_TYPE_UNSPECIFIED"));
156 break;
157 case ObjectType::PERSON:
158 PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("PERSON"));
159 break;
160 case ObjectType::PAGE:
161 PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("PAGE"));
162 break;
163 }*/
164 /* Output only
165 {
166 QJsonArray arr;
167 std::transform(d->previousResourceNames.cbegin(), d->previousResourceNames.cend(), std::back_inserter(arr), [](const auto &val) {
168 return val;
169 });
170 PeopleUtils::addValueToJsonObjectIfValid(obj, "previousResourceNames", std::move(arr));
171 }
172 */
173 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "deleted", d->deleted);
174 {
175 QJsonArray arr;
176 std::transform(d->sources.cbegin(), d->sources.cend(), std::back_inserter(arr), [](const auto &val) {
177 return val.toJSON();
178 });
179 if (!arr.isEmpty()) {
180 PeopleUtils::addValueToJsonObjectIfValid(obj, "sources", std::move(arr));
181 }
182 }
183
184 return obj;
185}
186
187} // namespace KGAPI2::People
The metadata about a person.
PersonMetadata::ObjectType objectType() const
Output only.
QList< QString > linkedPeopleResourceNames() const
Output only.
void clearSources()
Clears the list of sources.
void addSource(const Source &value)
Appends the given value to the list of sources.
PersonMetadata()
Constructs a new PersonMetadata.
void removeSource(const Source &value)
Removes the given value from the list of sources if it exists.
bool deleted() const
Output only.
QList< QString > previousResourceNames() const
Output only.
void setSources(const QList< Source > &value)
Sets value of the sources property.
QList< Source > sources() const
The sources of data for the person.
The source of a field.
Definition source.h:33
bool isEmpty() const const
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
bool toBool(bool defaultValue) const const
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.