Kgapi

profilemetadata.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 "profilemetadata.h"
11#include "peopleservice.h"
12
13#include <QJsonArray>
14#include <QJsonObject>
15#include <QJsonValue>
16#include <QSharedData>
17
18#include <algorithm>
19
20namespace KGAPI2::People
21{
22class ProfileMetadata::Private : public QSharedData
23{
24public:
25 explicit Private() = default;
26 Private(const Private &) = default;
27 Private(Private &&) noexcept = delete;
28 Private &operator=(const Private &) = delete;
29 Private &operator=(Private &&) noexcept = delete;
30 ~Private() = default;
31
32 bool operator==(const Private &other) const
33 {
34 return userTypes == other.userTypes && objectType == other.objectType;
35 }
36
37 bool operator!=(const Private &other) const
38 {
39 return !(*this == other);
40 }
41
43 ProfileMetadata::ObjectType objectType{};
44};
45
47 : d(new Private)
48{
49}
50
53ProfileMetadata &ProfileMetadata::operator=(const ProfileMetadata &) = default;
54ProfileMetadata &ProfileMetadata::operator=(ProfileMetadata &&) noexcept = default;
56
57bool ProfileMetadata::operator==(const ProfileMetadata &other) const
58{
59 return *d == *other.d;
60}
61
62bool ProfileMetadata::operator!=(const ProfileMetadata &other) const
63{
64 return !(*this == other);
65}
66
68{
69 return d->userTypes;
70}
71ProfileMetadata::ProfileMetadata::ObjectType ProfileMetadata::objectType() const
72{
73 return d->objectType;
74}
75
76ProfileMetadata ProfileMetadata::fromJSON(const QJsonObject &obj)
77{
78 ProfileMetadata profileMetadata;
79
80 if(!obj.isEmpty()) {
81 const auto objectTypeEnumString = obj.value(QStringLiteral("objectType"));
82 if (objectTypeEnumString == QLatin1StringView("PERSON")) {
83 profileMetadata.d->objectType = ObjectType::PERSON;
84 } else if (objectTypeEnumString == QLatin1StringView("PAGE")) {
85 profileMetadata.d->objectType = ObjectType::PAGE;
86 } else {
87 profileMetadata.d->objectType = ObjectType::OBJECT_TYPE_UNSPECIFIED;
88 }
89
90 if(obj.value(QStringLiteral("userTypes")).isArray()) {
91 const auto userTypesJsonArray = obj.value(QStringLiteral("userTypes")).toArray();
92
93 for (const auto &userType : userTypesJsonArray) {
94 if(userType == obj.value(QStringLiteral("GOOGLE_USER"))) {
95 profileMetadata.d->userTypes.append(UserTypes::GOOGLE_USER);
96 } else if(userType == obj.value(QStringLiteral("GPLUS_USER"))) {
97 profileMetadata.d->userTypes.append(UserTypes::GPLUS_USER);
98 } else if(userType == obj.value(QStringLiteral("GOOGLE_APPS_USER"))) {
99 profileMetadata.d->userTypes.append(UserTypes::GOOGLE_APPS_USER);
100 } else {
101 profileMetadata.d->userTypes.append(UserTypes::USER_TYPE_UNKNOWN);
102 }
103 }
104 } else {
105 profileMetadata.d->userTypes.append(UserTypes::USER_TYPE_UNKNOWN);
106 }
107 }
108
109 return profileMetadata;
110}
111
112QJsonValue ProfileMetadata::toJSON() const
113{
114 QJsonObject obj;
115
116 {
117 QJsonArray arr;
118 std::transform(d->userTypes.cbegin(), d->userTypes.cend(), std::back_inserter(arr), [](const auto &val) {
119 switch (val) {
120 default:
121 case UserTypes::USER_TYPE_UNKNOWN:
122 return QStringLiteral("USER_TYPE_UNKNOWN");
123 case UserTypes::GOOGLE_USER:
124 return QStringLiteral("GOOGLE_USER");
125 case UserTypes::GPLUS_USER:
126 return QStringLiteral("GPLUS_USER");
127 case UserTypes::GOOGLE_APPS_USER:
128 return QStringLiteral("GOOGLE_APPS_USER");
129 }
130 });
131 if (!arr.isEmpty()) {
132 PeopleUtils::addValueToJsonObjectIfValid(obj, "userTypes", std::move(arr));
133 }
134 }
135 switch (d->objectType) {
136 case ObjectType::OBJECT_TYPE_UNSPECIFIED:
137 PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("OBJECT_TYPE_UNSPECIFIED"));
138 break;
139 case ObjectType::PERSON:
140 PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("PERSON"));
141 break;
142 case ObjectType::PAGE:
143 PeopleUtils::addValueToJsonObjectIfValid(obj, "objectType", QStringLiteral("PAGE"));
144 break;
145 }
146 return obj;
147}
148
149} // namespace KGAPI2::People
The metadata about a profile.
ProfileMetadata::ObjectType objectType() const
Output only.
QList< ProfileMetadata::UserTypes > userTypes() const
Output only.
ProfileMetadata()
Constructs a new ProfileMetadata.
@ GPLUS_USER
The user is a Currents user.
@ GOOGLE_APPS_USER
The user is a Google Workspace user.
@ GOOGLE_USER
The user is a Google user.
@ USER_TYPE_UNKNOWN
The user type is not known.
bool isEmpty() const const
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:08:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.