Kgapi

coverphoto.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 "coverphoto.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 CoverPhoto::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 isDefault == other.isDefault && metadata == other.metadata && url == other.url;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 bool isDefault{};
45 FieldMetadata metadata{};
46 QString url{};
47};
48
50 : d(new Private)
51{
52}
53
54CoverPhoto::CoverPhoto(const CoverPhoto &) = default;
55CoverPhoto::CoverPhoto(CoverPhoto &&) noexcept = default;
56CoverPhoto &CoverPhoto::operator=(const CoverPhoto &) = default;
57CoverPhoto &CoverPhoto::operator=(CoverPhoto &&) noexcept = default;
58CoverPhoto::~CoverPhoto() = default;
59
60bool CoverPhoto::operator==(const CoverPhoto &other) const
61{
62 return *d == *other.d;
63}
64
65bool CoverPhoto::operator!=(const CoverPhoto &other) const
66{
67 return !(*this == other);
68}
69
71{
72 return d->isDefault;
73}
74
76{
77 d->isDefault = value;
78}
80{
81 return d->metadata;
82}
83
85{
86 d->metadata = value;
87}
89{
90 return d->url;
91}
92
93void CoverPhoto::setUrl(const QString &value)
94{
95 d->url = value;
96}
97
98CoverPhoto CoverPhoto::fromJSON(const QJsonObject &obj)
99{
100 CoverPhoto coverPhoto;
101
102 if(!obj.isEmpty()) {
103 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
104 coverPhoto.setMetadata(FieldMetadata::fromJSON(metadata));
105 coverPhoto.setUrl(obj.value(QStringLiteral("url")).toString());
106 coverPhoto.setIsDefault(obj.value(QStringLiteral("default")).toBool());
107 }
108
109 return coverPhoto;
110}
111
112QList<CoverPhoto> CoverPhoto::fromJSONArray(const QJsonArray &data)
113{
114 QList<CoverPhoto> coverPhotos;
115
116 for(const auto &coverPhoto : data) {
117 if(coverPhoto.isObject()) {
118 const auto objectifiedCoverPhoto = coverPhoto.toObject();
119 coverPhotos.append(fromJSON(objectifiedCoverPhoto));
120 }
121 }
122
123 return coverPhotos;
124}
125
126QJsonValue CoverPhoto::toJSON() const
127{
128 QJsonObject obj;
129
130 PeopleUtils::addValueToJsonObjectIfValid(obj, "default", d->isDefault);
131 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
132 PeopleUtils::addValueToJsonObjectIfValid(obj, "url", d->url);
133 return obj;
134}
135
136} // namespace KGAPI2::People
A person's cover photo.
Definition coverphoto.h:35
CoverPhoto()
Constructs a new CoverPhoto.
FieldMetadata metadata() const
Metadata about the cover photo.
bool isDefault() const
True if the cover photo is the default cover photo; false if the cover photo is a user-provided cover...
void setIsDefault(bool value)
Sets value of the isDefault property.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
void setUrl(const QString &value)
Sets value of the url property.
QString url() const
The URL of the cover photo.
Metadata about a field.
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 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.