Kgapi

imclient.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 "imclient.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{
24
25struct ImClientDefinition {
26 FieldMetadata metadata;
27 QString username;
28 QString type;
29 QString formattedType;
30 QString protocol;
31 QString formattedProtocol;
32};
33
34class ImClient::Private : public QSharedData
35{
36public:
37 explicit Private() = default;
38 Private(const Private &) = default;
39 Private(Private &&) noexcept = delete;
40 Private &operator=(const Private &) = delete;
41 Private &operator=(Private &&) noexcept = delete;
42 ~Private() = default;
43
44 bool operator==(const Private &other) const
45 {
46 return protocol == other.protocol && username == other.username && type == other.type && metadata == other.metadata
47 && formattedType == other.formattedType && formattedProtocol == other.formattedProtocol;
48 }
49
50 bool operator!=(const Private &other) const
51 {
52 return !(*this == other);
53 }
54
55 QString protocol{};
56 QString username{};
57 QString type{};
58 FieldMetadata metadata{};
59 QString formattedType{};
60 QString formattedProtocol{};
61};
62
64 : d(new Private)
65{
66}
67
68ImClient::ImClient(const ImClientDefinition &definition)
69 : d(new Private)
70{
71 d->metadata = definition.metadata;
72 d->username = definition.username;
73 d->type = definition.type;
74 d->formattedType = definition.formattedType;
75 d->protocol = definition.protocol;
76 d->formattedProtocol = definition.formattedProtocol;
77}
78
79ImClient::ImClient(const ImClient &) = default;
80ImClient::ImClient(ImClient &&) noexcept = default;
81ImClient &ImClient::operator=(const ImClient &) = default;
82ImClient &ImClient::operator=(ImClient &&) noexcept = default;
83ImClient::~ImClient() = default;
84
85bool ImClient::operator==(const ImClient &other) const
86{
87 return *d == *other.d;
88}
89
90bool ImClient::operator!=(const ImClient &other) const
91{
92 return !(*this == other);
93}
94
96{
97 return d->protocol;
98}
99
101{
102 d->protocol = value;
103}
105{
106 return d->username;
107}
108
110{
111 d->username = value;
112}
114{
115 return d->type;
116}
117
118void ImClient::setType(const QString &value)
119{
120 d->type = value;
121}
123{
124 return d->metadata;
125}
126
128{
129 d->metadata = value;
130}
132{
133 return d->formattedType;
134}
136{
137 return d->formattedProtocol;
138}
139
140ImClient ImClient::fromJSON(const QJsonObject &obj)
141{
142 if(obj.isEmpty()) {
143 return ImClient();
144 }
145
146 ImClientDefinition definition;
147
148 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
149 definition.metadata = FieldMetadata::fromJSON(metadata);
150 definition.username = obj.value(QStringLiteral("username")).toString();
151 definition.type = obj.value(QStringLiteral("type")).toString();
152 definition.formattedType = obj.value(QStringLiteral("formattedType")).toString();
153 definition.protocol = obj.value(QStringLiteral("protocol")).toString();
154 definition.formattedProtocol = obj.value(QStringLiteral("formattedProtocol")).toString();
155
156 return ImClient(definition);
157}
158
159QList<ImClient> ImClient::fromJSONArray(const QJsonArray &data)
160{
161 QList<ImClient> imClients;
162
163 for(const auto &imClient : data) {
164 if(imClient.isObject()) {
165 const auto objectifiedImClient = imClient.toObject();
166 imClients.append(fromJSON(objectifiedImClient));
167 }
168 }
169
170 return imClients;
171}
172
173QJsonValue ImClient::toJSON() const
174{
175 QJsonObject obj;
176
177 PeopleUtils::addValueToJsonObjectIfValid(obj, "protocol", d->protocol);
178 PeopleUtils::addValueToJsonObjectIfValid(obj, "username", d->username);
179 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
180 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
181 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType"}, d->formattedType);
182 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedProtocol"}, d->formattedProtocol);
183 return obj;
184}
185
186} // namespace KGAPI2::People
Metadata about a field.
A person's instant messaging client.
Definition imclient.h:35
void setUsername(const QString &value)
Sets value of the username property.
Definition imclient.cpp:109
void setType(const QString &value)
Sets value of the type property.
Definition imclient.cpp:118
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition imclient.cpp:127
QString formattedType() const
Output only.
Definition imclient.cpp:131
QString type() const
The type of the IM client.
Definition imclient.cpp:113
QString protocol() const
The protocol of the IM client.
Definition imclient.cpp:95
QString formattedProtocol() const
Output only.
Definition imclient.cpp:135
QString username() const
The user name used in the IM client.
Definition imclient.cpp:104
void setProtocol(const QString &value)
Sets value of the protocol property.
Definition imclient.cpp:100
FieldMetadata metadata() const
Metadata about the IM client.
Definition imclient.cpp:122
ImClient()
Constructs a new ImClient.
Definition imclient.cpp:63
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:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.