Kgapi

misckeyword.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 "misckeyword.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 MiscKeywordDefinition
26{
27 FieldMetadata metadata;
28 QString value;
30 QString formattedType;
31};
32
33class MiscKeyword::Private : public QSharedData
34{
35public:
36 explicit Private() = default;
37 Private(const Private &) = default;
38 Private(Private &&) noexcept = delete;
39 Private &operator=(const Private &) = delete;
40 Private &operator=(Private &&) noexcept = delete;
41 ~Private() = default;
42
43 bool operator==(const Private &other) const
44 {
45 return metadata == other.metadata && value == other.value && type == other.type && formattedType == other.formattedType;
46 }
47
48 bool operator!=(const Private &other) const
49 {
50 return !(*this == other);
51 }
52
53 FieldMetadata metadata{};
54 QString value{};
55 MiscKeyword::Type type{};
56 QString formattedType{};
57};
58
60 : d(new Private)
61{
62}
63
64MiscKeyword::MiscKeyword(const MiscKeywordDefinition &definition)
65 : d(new Private)
66{
67 d->metadata = definition.metadata;
68 d->value = definition.value;
69 d->type = definition.type;
70 d->formattedType = definition.formattedType;
71}
72
73MiscKeyword::MiscKeyword(const MiscKeyword &) = default;
74MiscKeyword::MiscKeyword(MiscKeyword &&) noexcept = default;
75MiscKeyword &MiscKeyword::operator=(const MiscKeyword &) = default;
76MiscKeyword &MiscKeyword::operator=(MiscKeyword &&) noexcept = default;
77MiscKeyword::~MiscKeyword() = default;
78
79bool MiscKeyword::operator==(const MiscKeyword &other) const
80{
81 return *d == *other.d;
82}
83
84bool MiscKeyword::operator!=(const MiscKeyword &other) const
85{
86 return !(*this == other);
87}
88
90{
91 return d->metadata;
92}
93
95{
96 d->metadata = value;
97}
99{
100 return d->value;
101}
102
104{
105 d->value = value;
106}
107MiscKeyword::MiscKeyword::Type MiscKeyword::type() const
108{
109 return d->type;
110}
111
113{
114 d->type = value;
115}
117{
118 return d->formattedType;
119}
120
121MiscKeyword MiscKeyword::fromJSON(const QJsonObject &obj)
122{
123 if(obj.isEmpty()) {
124 return MiscKeyword();
125 }
126
127 MiscKeywordDefinition definition;
128
129 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
130 definition.metadata = FieldMetadata::fromJSON(metadata);
131 definition.value = obj.value(QStringLiteral("value")).toString();
132
133 const auto type = obj.value(QStringLiteral("type")).toString();
134 if (type == QLatin1StringView("OUTLOOK_BILLING_INFORMATION")) {
135 definition.type = Type::OUTLOOK_BILLING_INFORMATION;
136 } else if (type == QLatin1StringView("OUTLOOK_DIRECTORY_SERVER")) {
137 definition.type = Type::OUTLOOK_DIRECTORY_SERVER;
138 } else if (type == QLatin1StringView("OUTLOOK_KEYWORD")) {
139 definition.type = Type::OUTLOOK_KEYWORD;
140 } else if (type == QLatin1StringView("OUTLOOK_MILEAGE")) {
141 definition.type = Type::OUTLOOK_MILEAGE;
142 } else if (type == QLatin1StringView("OUTLOOK_PRIORITY")) {
143 definition.type = Type::OUTLOOK_PRIORITY;
144 } else if (type == QLatin1StringView("OUTLOOK_SENSITIVITY")) {
145 definition.type = Type::OUTLOOK_SENSITIVITY;
146 } else if (type == QLatin1StringView("OUTLOOK_SUBJECT")) {
147 definition.type = Type::OUTLOOK_SUBJECT;
148 } else if (type == QLatin1StringView("OUTLOOK_USER")) {
149 definition.type = Type::OUTLOOK_USER;
150 } else if (type == QLatin1StringView("HOME")) {
151 definition.type = Type::HOME;
152 } else if (type == QLatin1StringView("WORK")) {
153 definition.type = Type::WORK;
154 } else if (type == QLatin1StringView("OTHER")) {
155 definition.type = Type::OTHER;
156 } else {
157 definition.type = Type::TYPE_UNSPECIFIED;
158 }
159
160 definition.formattedType = obj.value(QStringLiteral("formattedType")).toString();
161
162 return MiscKeyword(definition);
163}
164
165QList<MiscKeyword> MiscKeyword::fromJSONArray(const QJsonArray &data)
166{
167 QList<MiscKeyword> miscKeywords;
168
169 for(const auto &miscKeyword : data) {
170 if(miscKeyword.isObject()) {
171 const auto objectifiedMiscKeyword = miscKeyword.toObject();
172 miscKeywords.append(fromJSON(objectifiedMiscKeyword));
173 }
174 }
175
176 return miscKeywords;
177}
178
179QJsonValue MiscKeyword::toJSON() const
180{
181 QJsonObject obj;
182
183 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
184 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
185 switch (d->type) {
187 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("TYPE_UNSPECIFIED"));
188 break;
190 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_BILLING_INFORMATION"));
191 break;
193 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_DIRECTORY_SERVER"));
194 break;
196 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_KEYWORD"));
197 break;
199 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_MILEAGE"));
200 break;
202 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_PRIORITY"));
203 break;
205 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_SENSITIVITY"));
206 break;
208 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_SUBJECT"));
209 break;
211 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_USER"));
212 break;
213 case Type::HOME:
214 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("HOME"));
215 break;
216 case Type::WORK:
217 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("WORK"));
218 break;
219 case Type::OTHER:
220 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OTHER"));
221 break;
222 }
223 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType"}, d->formattedType);
224 return obj;
225}
226
227} // namespace KGAPI2::People
Metadata about a field.
A person's miscellaneous keyword.
Definition misckeyword.h:35
QString formattedType() const
Output only.
FieldMetadata metadata() const
Metadata about the miscellaneous keyword.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
@ OUTLOOK_USER
Outlook field for user.
@ OUTLOOK_DIRECTORY_SERVER
Outlook field for directory server.
@ OUTLOOK_MILEAGE
Outlook field for mileage.
@ OUTLOOK_PRIORITY
Outlook field for priority.
@ OUTLOOK_KEYWORD
Outlook field for keyword.
@ OUTLOOK_SENSITIVITY
Outlook field for sensitivity.
@ OUTLOOK_SUBJECT
Outlook field for subject.
@ OUTLOOK_BILLING_INFORMATION
Outlook field for billing information.
void setValue(const QString &value)
Sets value of the value property.
MiscKeyword()
Constructs a new MiscKeyword.
MiscKeyword::Type type() const
The miscellaneous keyword type.
QString value() const
The value of the miscellaneous keyword.
void setType(MiscKeyword::Type value)
Sets value of the type property.
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 Fri Nov 29 2024 11:54:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.