Kgapi

userdefined.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 "userdefined.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 UserDefined::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 && key == other.key && metadata == other.metadata;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 QString value{};
45 QString key{};
46 FieldMetadata metadata{};
47};
48
50 : d(new Private)
51{
52}
53
54UserDefined::UserDefined(const UserDefined &) = default;
55UserDefined::UserDefined(UserDefined &&) noexcept = default;
56UserDefined &UserDefined::operator=(const UserDefined &) = default;
57UserDefined &UserDefined::operator=(UserDefined &&) noexcept = default;
58UserDefined::~UserDefined() = default;
59
60bool UserDefined::operator==(const UserDefined &other) const
61{
62 return *d == *other.d;
63}
64
65bool UserDefined::operator!=(const UserDefined &other) const
66{
67 return !(*this == other);
68}
69
71{
72 return d->value;
73}
74
76{
77 d->value = value;
78}
80{
81 return d->key;
82}
83
84void UserDefined::setKey(const QString &value)
85{
86 d->key = value;
87}
89{
90 return d->metadata;
91}
92
94{
95 d->metadata = value;
96}
97
98UserDefined UserDefined::fromJSON(const QJsonObject &obj)
99{
100 UserDefined userDefined;
101
102 if(!obj.isEmpty()) {
103 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
104 userDefined.setMetadata(FieldMetadata::fromJSON(metadata));
105 userDefined.setKey(obj.value(QStringLiteral("key")).toString());
106 userDefined.setValue(obj.value(QStringLiteral("value")).toString());
107 }
108
109 return userDefined;
110}
111
112QList<UserDefined> UserDefined::fromJSONArray(const QJsonArray &data)
113{
114 QList<UserDefined> userDefineds;
115
116 for(const auto &userDefined : data) {
117 if(userDefined.isObject()) {
118 const auto objectifiedUserDefined = userDefined.toObject();
119 userDefineds.append(fromJSON(objectifiedUserDefined));
120 }
121 }
122
123 return userDefineds;
124}
125
126QJsonValue UserDefined::toJSON() const
127{
128 QJsonObject obj;
129
130 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
131 PeopleUtils::addValueToJsonObjectIfValid(obj, "key", d->key);
132 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
133 return obj;
134}
135
136} // namespace KGAPI2::People
Metadata about a field.
Arbitrary user data that is populated by the end users.
Definition userdefined.h:34
UserDefined()
Constructs a new UserDefined.
void setKey(const QString &value)
Sets value of the key property.
QString key() const
The end user specified key of the user defined data.
void setValue(const QString &value)
Sets value of the value property.
FieldMetadata metadata() const
Metadata about the user defined data.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
QString value() const
The end user specified value of the user defined data.
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 Fri Dec 6 2024 12:11:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.