File indexing completed on 2024-05-12 05:22:25
0001 /* 0002 * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org> 0003 * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.1-only 0006 * SPDX-License-Identifier: LGPL-3.0-only 0007 * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL 0008 */ 0009 0010 #include "birthday.h" 0011 0012 #include "fieldmetadata.h" 0013 0014 #include <QJsonArray> 0015 #include <QJsonObject> 0016 #include <QJsonValue> 0017 #include <QSharedData> 0018 0019 #include <algorithm> 0020 0021 namespace KGAPI2::People 0022 { 0023 class Birthday::Private : public QSharedData 0024 { 0025 public: 0026 explicit Private() = default; 0027 Private(const Private &) = default; 0028 Private(Private &&) noexcept = delete; 0029 Private &operator=(const Private &) = delete; 0030 Private &operator=(Private &&) noexcept = delete; 0031 ~Private() = default; 0032 0033 bool operator==(const Private &other) const 0034 { 0035 return text == other.text && metadata == other.metadata && date == other.date; 0036 } 0037 0038 bool operator!=(const Private &other) const 0039 { 0040 return !(*this == other); 0041 } 0042 0043 QString text{}; 0044 FieldMetadata metadata{}; 0045 QDate date{}; 0046 }; 0047 0048 Birthday::Birthday() 0049 : d(new Private) 0050 { 0051 } 0052 0053 Birthday::Birthday(const Birthday &) = default; 0054 Birthday::Birthday(Birthday &&) noexcept = default; 0055 Birthday &Birthday::operator=(const Birthday &) = default; 0056 Birthday &Birthday::operator=(Birthday &&) noexcept = default; 0057 Birthday::~Birthday() = default; 0058 0059 bool Birthday::operator==(const Birthday &other) const 0060 { 0061 return *d == *other.d; 0062 } 0063 0064 bool Birthday::operator!=(const Birthday &other) const 0065 { 0066 return !(*this == other); 0067 } 0068 0069 QString Birthday::text() const 0070 { 0071 return d->text; 0072 } 0073 0074 void Birthday::setText(const QString &value) 0075 { 0076 d->text = value; 0077 } 0078 FieldMetadata Birthday::metadata() const 0079 { 0080 return d->metadata; 0081 } 0082 0083 void Birthday::setMetadata(const FieldMetadata &value) 0084 { 0085 d->metadata = value; 0086 } 0087 QDate Birthday::date() const 0088 { 0089 return d->date; 0090 } 0091 0092 void Birthday::setDate(const QDate &value) 0093 { 0094 d->date = value; 0095 } 0096 0097 Birthday Birthday::fromJSON(const QJsonObject &obj) 0098 { 0099 Birthday birthday; 0100 0101 if(!obj.isEmpty()) { 0102 const auto jsonMetadata = obj.value(QStringLiteral("metadata")).toObject(); 0103 birthday.setMetadata(FieldMetadata::fromJSON(jsonMetadata)); 0104 0105 const auto jsonDate = obj.value(QStringLiteral("date")).toObject(); 0106 const auto year = jsonDate.value(QStringLiteral("year")).toInt(); 0107 const auto month = jsonDate.value(QStringLiteral("month")).toInt(); 0108 const auto day = jsonDate.value(QStringLiteral("day")).toInt(); 0109 QDate date(year, month, day); 0110 birthday.setDate(date); 0111 } 0112 0113 return birthday; 0114 } 0115 0116 QList<Birthday> Birthday::fromJSONArray(const QJsonArray &data) 0117 { 0118 QList<Birthday> birthdays; 0119 0120 for(const auto &birthday : data) { 0121 if(birthday.isObject()) { 0122 const auto objectifiedBirthday = birthday.toObject(); 0123 birthdays.append(fromJSON(objectifiedBirthday)); 0124 } 0125 } 0126 0127 return birthdays; 0128 } 0129 0130 0131 QJsonValue Birthday::toJSON() const 0132 { 0133 // Skip field metadata as is only useful for receiving 0134 return QJsonObject { 0135 { QStringLiteral("date"), QJsonObject { 0136 { QStringLiteral("year"), d->date.year() }, 0137 { QStringLiteral("month"), d->date.month() }, 0138 { QStringLiteral("day"), d->date.day() } 0139 } } 0140 }; 0141 } 0142 0143 } // namespace KGAPI2::People