Kgapi

birthday.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 "birthday.h"
11
12#include "fieldmetadata.h"
13
14#include <QJsonArray>
15#include <QJsonObject>
16#include <QJsonValue>
17#include <QSharedData>
18
19#include <algorithm>
20
21namespace KGAPI2::People
22{
23class Birthday::Private : public QSharedData
24{
25public:
26 explicit Private() = default;
27 Private(const Private &) = default;
28 Private(Private &&) noexcept = delete;
29 Private &operator=(const Private &) = delete;
30 Private &operator=(Private &&) noexcept = delete;
31 ~Private() = default;
32
33 bool operator==(const Private &other) const
34 {
35 return text == other.text && metadata == other.metadata && date == other.date;
36 }
37
38 bool operator!=(const Private &other) const
39 {
40 return !(*this == other);
41 }
42
43 QString text{};
44 FieldMetadata metadata{};
45 QDate date{};
46};
47
49 : d(new Private)
50{
51}
52
53Birthday::Birthday(const Birthday &) = default;
54Birthday::Birthday(Birthday &&) noexcept = default;
55Birthday &Birthday::operator=(const Birthday &) = default;
56Birthday &Birthday::operator=(Birthday &&) noexcept = default;
57Birthday::~Birthday() = default;
58
59bool Birthday::operator==(const Birthday &other) const
60{
61 return *d == *other.d;
62}
63
64bool Birthday::operator!=(const Birthday &other) const
65{
66 return !(*this == other);
67}
68
70{
71 return d->text;
72}
73
74void Birthday::setText(const QString &value)
75{
76 d->text = value;
77}
79{
80 return d->metadata;
81}
82
84{
85 d->metadata = value;
86}
88{
89 return d->date;
90}
91
92void Birthday::setDate(const QDate &value)
93{
94 d->date = value;
95}
96
97Birthday Birthday::fromJSON(const QJsonObject &obj)
98{
99 Birthday birthday;
100
101 if(!obj.isEmpty()) {
102 const auto jsonMetadata = obj.value(QStringLiteral("metadata")).toObject();
103 birthday.setMetadata(FieldMetadata::fromJSON(jsonMetadata));
104
105 const auto jsonDate = obj.value(QStringLiteral("date")).toObject();
106 const auto year = jsonDate.value(QStringLiteral("year")).toInt();
107 const auto month = jsonDate.value(QStringLiteral("month")).toInt();
108 const auto day = jsonDate.value(QStringLiteral("day")).toInt();
109 QDate date(year, month, day);
110 birthday.setDate(date);
111 }
112
113 return birthday;
114}
115
116QList<Birthday> Birthday::fromJSONArray(const QJsonArray &data)
117{
118 QList<Birthday> birthdays;
119
120 for(const auto &birthday : data) {
121 if(birthday.isObject()) {
122 const auto objectifiedBirthday = birthday.toObject();
123 birthdays.append(fromJSON(objectifiedBirthday));
124 }
125 }
126
127 return birthdays;
128}
129
130
131QJsonValue Birthday::toJSON() const
132{
133 // Skip field metadata as is only useful for receiving
134 return QJsonObject {
135 { QStringLiteral("date"), QJsonObject {
136 { QStringLiteral("year"), d->date.year() },
137 { QStringLiteral("month"), d->date.month() },
138 { QStringLiteral("day"), d->date.day() }
139 } }
140 };
141}
142
143} // namespace KGAPI2::People
A person's birthday.
Definition birthday.h:37
void setDate(const QDate &value)
Sets value of the date property.
Definition birthday.cpp:92
Birthday()
Constructs a new Birthday.
Definition birthday.cpp:48
FieldMetadata metadata() const
Metadata about the birthday.
Definition birthday.cpp:78
QString text() const
A free-form string representing the user's birthday.
Definition birthday.cpp:69
void setText(const QString &value)
Sets value of the text property.
Definition birthday.cpp:74
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition birthday.cpp:83
QDate date() const
The date of the birthday.
Definition birthday.cpp:87
Metadata about a field.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
int toInt(int defaultValue) 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 Sat Dec 21 2024 17:00:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.