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 "name.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 <KContacts/Addressee>
0021 
0022 #include <algorithm>
0023 
0024 namespace KGAPI2::People
0025 {
0026 class Name::Private : public QSharedData
0027 {
0028 public:
0029     explicit Private() = default;
0030     Private(const Private &) = default;
0031     Private(Private &&) noexcept = delete;
0032     Private &operator=(const Private &) = delete;
0033     Private &operator=(Private &&) noexcept = delete;
0034     ~Private() = default;
0035 
0036     bool operator==(const Private &other) const
0037     {
0038         return unstructuredName == other.unstructuredName && familyName == other.familyName && phoneticMiddleName == other.phoneticMiddleName
0039             && middleName == other.middleName && honorificPrefix == other.honorificPrefix && givenName == other.givenName
0040             && phoneticGivenName == other.phoneticGivenName && phoneticHonorificPrefix == other.phoneticHonorificPrefix && displayName == other.displayName
0041             && displayNameLastFirst == other.displayNameLastFirst && phoneticFamilyName == other.phoneticFamilyName && honorificSuffix == other.honorificSuffix
0042             && phoneticHonorificSuffix == other.phoneticHonorificSuffix && phoneticFullName == other.phoneticFullName && metadata == other.metadata;
0043     }
0044 
0045     bool operator!=(const Private &other) const
0046     {
0047         return !(*this == other);
0048     }
0049 
0050     QString unstructuredName{};
0051     QString familyName{};
0052     QString phoneticMiddleName{};
0053     QString middleName{};
0054     QString honorificPrefix{};
0055     QString givenName{};
0056     QString phoneticGivenName{};
0057     QString phoneticHonorificPrefix{};
0058     QString displayName{};
0059     QString displayNameLastFirst{};
0060     QString phoneticFamilyName{};
0061     QString honorificSuffix{};
0062     QString phoneticHonorificSuffix{};
0063     QString phoneticFullName{};
0064     FieldMetadata metadata{};
0065 };
0066 
0067 Name::Name()
0068     : d(new Private)
0069 {
0070 }
0071 
0072 Name::Name(const Name &) = default;
0073 Name::Name(Name &&) noexcept = default;
0074 Name &Name::operator=(const Name &) = default;
0075 Name &Name::operator=(Name &&) noexcept = default;
0076 Name::~Name() = default;
0077 
0078 bool Name::operator==(const Name &other) const
0079 {
0080     return *d == *other.d;
0081 }
0082 
0083 bool Name::operator!=(const Name &other) const
0084 {
0085     return !(*this == other);
0086 }
0087 
0088 QString Name::unstructuredName() const
0089 {
0090     return d->unstructuredName;
0091 }
0092 
0093 void Name::setUnstructuredName(const QString &value)
0094 {
0095     d->unstructuredName = value;
0096 }
0097 QString Name::familyName() const
0098 {
0099     return d->familyName;
0100 }
0101 
0102 void Name::setFamilyName(const QString &value)
0103 {
0104     d->familyName = value;
0105 }
0106 QString Name::phoneticMiddleName() const
0107 {
0108     return d->phoneticMiddleName;
0109 }
0110 
0111 void Name::setPhoneticMiddleName(const QString &value)
0112 {
0113     d->phoneticMiddleName = value;
0114 }
0115 QString Name::middleName() const
0116 {
0117     return d->middleName;
0118 }
0119 
0120 void Name::setMiddleName(const QString &value)
0121 {
0122     d->middleName = value;
0123 }
0124 QString Name::honorificPrefix() const
0125 {
0126     return d->honorificPrefix;
0127 }
0128 
0129 void Name::setHonorificPrefix(const QString &value)
0130 {
0131     d->honorificPrefix = value;
0132 }
0133 QString Name::givenName() const
0134 {
0135     return d->givenName;
0136 }
0137 
0138 void Name::setGivenName(const QString &value)
0139 {
0140     d->givenName = value;
0141 }
0142 QString Name::phoneticGivenName() const
0143 {
0144     return d->phoneticGivenName;
0145 }
0146 
0147 void Name::setPhoneticGivenName(const QString &value)
0148 {
0149     d->phoneticGivenName = value;
0150 }
0151 QString Name::phoneticHonorificPrefix() const
0152 {
0153     return d->phoneticHonorificPrefix;
0154 }
0155 
0156 void Name::setPhoneticHonorificPrefix(const QString &value)
0157 {
0158     d->phoneticHonorificPrefix = value;
0159 }
0160 QString Name::displayName() const
0161 {
0162     return d->displayName;
0163 }
0164 QString Name::displayNameLastFirst() const
0165 {
0166     return d->displayNameLastFirst;
0167 }
0168 QString Name::phoneticFamilyName() const
0169 {
0170     return d->phoneticFamilyName;
0171 }
0172 
0173 void Name::setPhoneticFamilyName(const QString &value)
0174 {
0175     d->phoneticFamilyName = value;
0176 }
0177 QString Name::honorificSuffix() const
0178 {
0179     return d->honorificSuffix;
0180 }
0181 
0182 void Name::setHonorificSuffix(const QString &value)
0183 {
0184     d->honorificSuffix = value;
0185 }
0186 QString Name::phoneticHonorificSuffix() const
0187 {
0188     return d->phoneticHonorificSuffix;
0189 }
0190 
0191 void Name::setPhoneticHonorificSuffix(const QString &value)
0192 {
0193     d->phoneticHonorificSuffix = value;
0194 }
0195 QString Name::phoneticFullName() const
0196 {
0197     return d->phoneticFullName;
0198 }
0199 
0200 void Name::setPhoneticFullName(const QString &value)
0201 {
0202     d->phoneticFullName = value;
0203 }
0204 FieldMetadata Name::metadata() const
0205 {
0206     return d->metadata;
0207 }
0208 
0209 void Name::setMetadata(const FieldMetadata &value)
0210 {
0211     d->metadata = value;
0212 }
0213 
0214 Name Name::fromJSON(const QJsonObject &obj)
0215 {
0216     Name name;
0217 
0218     if(!obj.isEmpty()) {
0219         const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
0220         name.d->metadata = FieldMetadata::fromJSON(metadata);
0221         name.d->displayName = obj.value(QStringLiteral("displayName")).toString();
0222         name.d->displayNameLastFirst = obj.value(QStringLiteral("displayNameLastFirst")).toString();
0223         name.d->unstructuredName = obj.value(QStringLiteral("unstructuredName")).toString();
0224         name.d->familyName = obj.value(QStringLiteral("familyName")).toString();
0225         name.d->givenName = obj.value(QStringLiteral("givenName")).toString();
0226         name.d->middleName = obj.value(QStringLiteral("middleName")).toString();
0227         name.d->honorificPrefix = obj.value(QStringLiteral("honorificPrefix")).toString();
0228         name.d->honorificSuffix = obj.value(QStringLiteral("honorificSuffix")).toString();
0229         name.d->phoneticFullName = obj.value(QStringLiteral("phoneticFullName")).toString();
0230         name.d->phoneticFamilyName = obj.value(QStringLiteral("phoneticFamilyName")).toString();
0231         name.d->phoneticGivenName = obj.value(QStringLiteral("phoneticGivenName")).toString();
0232         name.d->phoneticMiddleName = obj.value(QStringLiteral("phoneticMiddleName")).toString();
0233         name.d->phoneticHonorificPrefix = obj.value(QStringLiteral("phoneticHonorificPrefix")).toString();
0234         name.d->phoneticHonorificSuffix = obj.value(QStringLiteral("phoneticHonorificSuffix")).toString();
0235     }
0236 
0237     return name;
0238 }
0239 
0240 QList<Name> Name::fromJSONArray(const QJsonArray &data)
0241 {
0242     QList<Name> names;
0243 
0244     for(const auto &name : data) {
0245         if(name.isObject()) {
0246             const auto objectifiedName = name.toObject();
0247             names.append(fromJSON(objectifiedName));
0248         }
0249     }
0250 
0251     return names;
0252 }
0253 
0254 QJsonValue Name::toJSON() const
0255 {
0256     QJsonObject obj;
0257 
0258     PeopleUtils::addValueToJsonObjectIfValid(obj, "unstructuredName", d->unstructuredName);
0259     PeopleUtils::addValueToJsonObjectIfValid(obj, "familyName", d->familyName);
0260     PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticMiddleName", d->phoneticMiddleName);
0261     PeopleUtils::addValueToJsonObjectIfValid(obj, "middleName", d->middleName);
0262     PeopleUtils::addValueToJsonObjectIfValid(obj, "honorificPrefix", d->honorificPrefix);
0263     PeopleUtils::addValueToJsonObjectIfValid(obj, "givenName", d->givenName);
0264     PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticGivenName", d->phoneticGivenName);
0265     PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticHonorificPrefix", d->phoneticHonorificPrefix);
0266     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "displayName", d->displayName);
0267     // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "displayNameLastFirst", d->displayNameLastFirst);
0268     PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticFamilyName", d->phoneticFamilyName);
0269     PeopleUtils::addValueToJsonObjectIfValid(obj, "honorificSuffix", d->honorificSuffix);
0270     PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticHonorificSuffix", d->phoneticHonorificSuffix);
0271     PeopleUtils::addValueToJsonObjectIfValid(obj, "phoneticFullName", d->phoneticFullName);
0272     // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
0273     return obj;
0274 }
0275 
0276 Name Name::fromKContactsAddressee(const KContacts::Addressee &addressee)
0277 {
0278     Name name;
0279     name.setFamilyName(addressee.familyName());
0280     name.setGivenName(addressee.givenName());
0281     name.setHonorificPrefix(addressee.prefix());
0282     name.setHonorificSuffix(addressee.suffix());
0283     name.setUnstructuredName(addressee.formattedName());
0284     return name;
0285 }
0286 
0287 void Name::applyToKContactsAddressee(KContacts::Addressee &addressee) const
0288 {
0289     addressee.setName(unstructuredName());
0290     addressee.setFamilyName(familyName());
0291     addressee.setGivenName(givenName());
0292     addressee.setPrefix(honorificPrefix());
0293     addressee.setSuffix(honorificSuffix());
0294     addressee.setFormattedName(displayName());
0295 }
0296 
0297 } // namespace KGAPI2::People