File indexing completed on 2024-05-12 05:22:27
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 "misckeyword.h" 0011 0012 #include "fieldmetadata.h" 0013 #include "peopleservice.h" 0014 0015 #include <QJsonArray> 0016 #include <QJsonObject> 0017 #include <QJsonValue> 0018 #include <QSharedData> 0019 0020 #include <algorithm> 0021 0022 namespace KGAPI2::People 0023 { 0024 0025 struct MiscKeywordDefinition 0026 { 0027 FieldMetadata metadata; 0028 QString value; 0029 MiscKeyword::Type type; 0030 QString formattedType; 0031 }; 0032 0033 class MiscKeyword::Private : public QSharedData 0034 { 0035 public: 0036 explicit Private() = default; 0037 Private(const Private &) = default; 0038 Private(Private &&) noexcept = delete; 0039 Private &operator=(const Private &) = delete; 0040 Private &operator=(Private &&) noexcept = delete; 0041 ~Private() = default; 0042 0043 bool operator==(const Private &other) const 0044 { 0045 return metadata == other.metadata && value == other.value && type == other.type && formattedType == other.formattedType; 0046 } 0047 0048 bool operator!=(const Private &other) const 0049 { 0050 return !(*this == other); 0051 } 0052 0053 FieldMetadata metadata{}; 0054 QString value{}; 0055 MiscKeyword::Type type{}; 0056 QString formattedType{}; 0057 }; 0058 0059 MiscKeyword::MiscKeyword() 0060 : d(new Private) 0061 { 0062 } 0063 0064 MiscKeyword::MiscKeyword(const MiscKeywordDefinition &definition) 0065 : d(new Private) 0066 { 0067 d->metadata = definition.metadata; 0068 d->value = definition.value; 0069 d->type = definition.type; 0070 d->formattedType = definition.formattedType; 0071 } 0072 0073 MiscKeyword::MiscKeyword(const MiscKeyword &) = default; 0074 MiscKeyword::MiscKeyword(MiscKeyword &&) noexcept = default; 0075 MiscKeyword &MiscKeyword::operator=(const MiscKeyword &) = default; 0076 MiscKeyword &MiscKeyword::operator=(MiscKeyword &&) noexcept = default; 0077 MiscKeyword::~MiscKeyword() = default; 0078 0079 bool MiscKeyword::operator==(const MiscKeyword &other) const 0080 { 0081 return *d == *other.d; 0082 } 0083 0084 bool MiscKeyword::operator!=(const MiscKeyword &other) const 0085 { 0086 return !(*this == other); 0087 } 0088 0089 FieldMetadata MiscKeyword::metadata() const 0090 { 0091 return d->metadata; 0092 } 0093 0094 void MiscKeyword::setMetadata(const FieldMetadata &value) 0095 { 0096 d->metadata = value; 0097 } 0098 QString MiscKeyword::value() const 0099 { 0100 return d->value; 0101 } 0102 0103 void MiscKeyword::setValue(const QString &value) 0104 { 0105 d->value = value; 0106 } 0107 MiscKeyword::MiscKeyword::Type MiscKeyword::type() const 0108 { 0109 return d->type; 0110 } 0111 0112 void MiscKeyword::setType(MiscKeyword::Type value) 0113 { 0114 d->type = value; 0115 } 0116 QString MiscKeyword::formattedType() const 0117 { 0118 return d->formattedType; 0119 } 0120 0121 MiscKeyword MiscKeyword::fromJSON(const QJsonObject &obj) 0122 { 0123 if(obj.isEmpty()) { 0124 return MiscKeyword(); 0125 } 0126 0127 MiscKeywordDefinition definition; 0128 0129 const auto metadata = obj.value(QStringLiteral("metadata")).toObject(); 0130 definition.metadata = FieldMetadata::fromJSON(metadata); 0131 definition.value = obj.value(QStringLiteral("value")).toString(); 0132 0133 const auto type = obj.value(QStringLiteral("type")).toString(); 0134 if (type == QLatin1StringView("OUTLOOK_BILLING_INFORMATION")) { 0135 definition.type = Type::OUTLOOK_BILLING_INFORMATION; 0136 } else if (type == QLatin1StringView("OUTLOOK_DIRECTORY_SERVER")) { 0137 definition.type = Type::OUTLOOK_DIRECTORY_SERVER; 0138 } else if (type == QLatin1StringView("OUTLOOK_KEYWORD")) { 0139 definition.type = Type::OUTLOOK_KEYWORD; 0140 } else if (type == QLatin1StringView("OUTLOOK_MILEAGE")) { 0141 definition.type = Type::OUTLOOK_MILEAGE; 0142 } else if (type == QLatin1StringView("OUTLOOK_PRIORITY")) { 0143 definition.type = Type::OUTLOOK_PRIORITY; 0144 } else if (type == QLatin1StringView("OUTLOOK_SENSITIVITY")) { 0145 definition.type = Type::OUTLOOK_SENSITIVITY; 0146 } else if (type == QLatin1StringView("OUTLOOK_SUBJECT")) { 0147 definition.type = Type::OUTLOOK_SUBJECT; 0148 } else if (type == QLatin1StringView("OUTLOOK_USER")) { 0149 definition.type = Type::OUTLOOK_USER; 0150 } else if (type == QLatin1StringView("HOME")) { 0151 definition.type = Type::HOME; 0152 } else if (type == QLatin1StringView("WORK")) { 0153 definition.type = Type::WORK; 0154 } else if (type == QLatin1StringView("OTHER")) { 0155 definition.type = Type::OTHER; 0156 } else { 0157 definition.type = Type::TYPE_UNSPECIFIED; 0158 } 0159 0160 definition.formattedType = obj.value(QStringLiteral("formattedType")).toString(); 0161 0162 return MiscKeyword(definition); 0163 } 0164 0165 QList<MiscKeyword> MiscKeyword::fromJSONArray(const QJsonArray &data) 0166 { 0167 QList<MiscKeyword> miscKeywords; 0168 0169 for(const auto &miscKeyword : data) { 0170 if(miscKeyword.isObject()) { 0171 const auto objectifiedMiscKeyword = miscKeyword.toObject(); 0172 miscKeywords.append(fromJSON(objectifiedMiscKeyword)); 0173 } 0174 } 0175 0176 return miscKeywords; 0177 } 0178 0179 QJsonValue MiscKeyword::toJSON() const 0180 { 0181 QJsonObject obj; 0182 0183 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON()); 0184 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value); 0185 switch (d->type) { 0186 case Type::TYPE_UNSPECIFIED: 0187 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("TYPE_UNSPECIFIED")); 0188 break; 0189 case Type::OUTLOOK_BILLING_INFORMATION: 0190 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_BILLING_INFORMATION")); 0191 break; 0192 case Type::OUTLOOK_DIRECTORY_SERVER: 0193 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_DIRECTORY_SERVER")); 0194 break; 0195 case Type::OUTLOOK_KEYWORD: 0196 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_KEYWORD")); 0197 break; 0198 case Type::OUTLOOK_MILEAGE: 0199 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_MILEAGE")); 0200 break; 0201 case Type::OUTLOOK_PRIORITY: 0202 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_PRIORITY")); 0203 break; 0204 case Type::OUTLOOK_SENSITIVITY: 0205 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_SENSITIVITY")); 0206 break; 0207 case Type::OUTLOOK_SUBJECT: 0208 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_SUBJECT")); 0209 break; 0210 case Type::OUTLOOK_USER: 0211 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OUTLOOK_USER")); 0212 break; 0213 case Type::HOME: 0214 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("HOME")); 0215 break; 0216 case Type::WORK: 0217 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("WORK")); 0218 break; 0219 case Type::OTHER: 0220 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", QStringLiteral("OTHER")); 0221 break; 0222 } 0223 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType"}, d->formattedType); 0224 return obj; 0225 } 0226 0227 } // namespace KGAPI2::People