Kgapi

people/event.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 "event.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 Event::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 metadata == other.metadata && date == other.date && type == other.type && formattedType == other.formattedType;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 FieldMetadata metadata{};
45 QDate date{};
46 QString type{};
47 QString formattedType{};
48};
49
51 : d(new Private)
52{
53}
54
55Event::Event(const Event &) = default;
56Event::Event(Event &&) noexcept = default;
57Event &Event::operator=(const Event &) = default;
58Event &Event::operator=(Event &&) noexcept = default;
59Event::~Event() = default;
60
61bool Event::operator==(const Event &other) const
62{
63 return *d == *other.d;
64}
65
66bool Event::operator!=(const Event &other) const
67{
68 return !(*this == other);
69}
70
72{
73 return d->metadata;
74}
75
77{
78 d->metadata = value;
79}
81{
82 return d->date;
83}
84
85void Event::setDate(const QDate &value)
86{
87 d->date = value;
88}
90{
91 return d->type;
92}
93
94void Event::setType(const QString &value)
95{
96 d->type = value;
97}
99{
100 return d->formattedType;
101}
102
103Event Event::fromJSON(const QJsonObject &obj)
104{
105 Event event;
106
107 if(!obj.isEmpty()) {
108 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
109 event.d->metadata = FieldMetadata::fromJSON(metadata);
110
111 const auto jsonDate = obj.value(QStringLiteral("date")).toObject();
112 const auto year = jsonDate.value(QStringLiteral("year")).toInt();
113 const auto month = jsonDate.value(QStringLiteral("month")).toInt();
114 const auto day = jsonDate.value(QStringLiteral("day")).toInt();
115 event.d->date = QDate(year, month, day);
116
117 event.d->type = obj.value(QStringLiteral("type")).toString();
118 event.d->formattedType = obj.value(QStringLiteral("formattedType")).toString();
119 }
120
121 return event;
122}
123
124QList<Event> Event::fromJSONArray(const QJsonArray &data)
125{
126 QList<Event> events;
127
128 for(const auto &event : data) {
129 if(event.isObject()) {
130 const auto objectifiedEvent = event.toObject();
131 events.append(fromJSON(objectifiedEvent));
132 }
133 }
134
135 return events;
136}
137
138QJsonValue Event::toJSON() const
139{
140 // Skip field metadata as is only useful for receiving
141 // formattedType is output only
142 QJsonObject obj {
143 { QStringLiteral("date"), QJsonObject {
144 { QStringLiteral("year"), d->date.year() },
145 { QStringLiteral("month"), d->date.month() },
146 { QStringLiteral("day"), d->date.day() }
147 } }
148 };
149
150 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
151
152 return obj;
153}
154
155} // namespace KGAPI2::People
Represents a single event from Google Calendar.
An event related to the person.
QDate date() const
The date of the event.
void setType(const QString &value)
Sets value of the type property.
QString formattedType() const
Output only.
QString type() const
The type of the event.
void setDate(const QDate &value)
Sets value of the date property.
Event()
Constructs a new Event.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
FieldMetadata metadata() const
Metadata about the event.
Metadata about a field.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
int toInt(int defaultValue) 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 May 3 2024 11:50:41 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.