Kgapi

emailaddress.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 "emailaddress.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 <KContacts/Email>
21
22#include <algorithm>
23
24namespace KGAPI2::People
25{
26class EmailAddress::Private : public QSharedData
27{
28public:
29 explicit Private() = default;
30 Private(const Private &) = default;
31 Private(Private &&) noexcept = delete;
32 Private &operator=(const Private &) = delete;
33 Private &operator=(Private &&) noexcept = delete;
34 ~Private() = default;
35
36 bool operator==(const Private &other) const
37 {
38 return value == other.value && metadata == other.metadata && type == other.type && displayName == other.displayName
39 && formattedType == other.formattedType;
40 }
41
42 bool operator!=(const Private &other) const
43 {
44 return !(*this == other);
45 }
46
47 QString value{};
48 FieldMetadata metadata{};
49 QString type{};
50 QString displayName{};
51 QString formattedType{};
52};
53
55 : d(new Private)
56{
57}
58
60EmailAddress::EmailAddress(EmailAddress &&) noexcept = default;
61EmailAddress &EmailAddress::operator=(const EmailAddress &) = default;
62EmailAddress &EmailAddress::operator=(EmailAddress &&) noexcept = default;
63EmailAddress::~EmailAddress() = default;
64
65bool EmailAddress::operator==(const EmailAddress &other) const
66{
67 return *d == *other.d;
68}
69
70bool EmailAddress::operator!=(const EmailAddress &other) const
71{
72 return !(*this == other);
73}
74
76{
77 return d->value;
78}
79
81{
82 d->value = value;
83}
85{
86 return d->metadata;
87}
88
90{
91 d->metadata = value;
92}
94{
95 return d->type;
96}
97
99{
100 d->type = value;
101}
103{
104 return d->displayName;
105}
106
108{
109 d->displayName = value;
110}
112{
113 return d->formattedType;
114}
115
116EmailAddress EmailAddress::fromJSON(const QJsonObject &obj)
117{
118 EmailAddress emailAddress;
119
120 if(!obj.isEmpty()) {
121 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
122 emailAddress.d->metadata = FieldMetadata::fromJSON(metadata);
123 emailAddress.d->value = obj.value(QStringLiteral("value")).toString();
124 emailAddress.d->type = obj.value(QStringLiteral("type")).toString();
125 emailAddress.d->formattedType = obj.value(QStringLiteral("formattedType")).toString();
126 emailAddress.d->displayName = obj.value(QStringLiteral("displayName")).toString();
127 }
128
129 return emailAddress;
130}
131
132QList<EmailAddress> EmailAddress::fromJSONArray(const QJsonArray &data)
133{
134 QList<EmailAddress> emailAddresses;
135
136 for(const auto &emailAddress : data) {
137 if(emailAddress.isObject()) {
138 const auto objectifiedEmailAddress = emailAddress.toObject();
139 emailAddresses.append(fromJSON(objectifiedEmailAddress));
140 }
141 }
142
143 return emailAddresses;
144}
145
146
147QJsonValue EmailAddress::toJSON() const
148{
149 QJsonObject obj;
150
151 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
152 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
153 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
154 PeopleUtils::addValueToJsonObjectIfValid(obj, "displayName", d->displayName);
155 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType", d->formattedType);
156 return obj;
157}
158
159KContacts::Email EmailAddress::toKContactsEmail() const
160{
161 KContacts::Email convertedEmail;
162 convertedEmail.setEmail(value());
163
164 const auto emailType = type();
165
166 if(QString::compare(emailType, QStringLiteral("home"), Qt::CaseInsensitive)) {
167 convertedEmail.setType(KContacts::Email::Home);
168 } else if(QString::compare(emailType, QStringLiteral("work"), Qt::CaseInsensitive)) {
169 convertedEmail.setType(KContacts::Email::Work);
170 } else if(QString::compare(emailType, QStringLiteral("other"), Qt::CaseInsensitive)) {
171 convertedEmail.setType(KContacts::Email::Other);
172 }
173
174 return convertedEmail;
175}
176
177EmailAddress EmailAddress::fromKContactsEmail(const KContacts::Email &email)
178{
179 EmailAddress convertedEmail;
180 convertedEmail.setValue(email.mail());
181
182 switch(email.type()) {
184 convertedEmail.setType(QStringLiteral("home"));
185 break;
187 convertedEmail.setType(QStringLiteral("work"));
188 break;
190 default:
191 convertedEmail.setType(QStringLiteral("other"));
192 break;
193 }
194
195 return convertedEmail;
196}
197
198QList<EmailAddress> EmailAddress::fromKContactsEmailList(const QList<KContacts::Email> &emailList)
199{
200 QList<EmailAddress> convertedEmails;
201 std::transform(emailList.cbegin(),
202 emailList.cend(),
203 std::back_inserter(convertedEmails),
204 [](const KContacts::Email &email) {
205 return EmailAddress::fromKContactsEmail(email);
206 });
207 return convertedEmails;
208}
209
210} // namespace KGAPI2::People
Type type() const
void setType(Type type)
A person's email address.
QString displayName() const
The display name of the email.
QString type() const
The type of the email address.
void setDisplayName(const QString &value)
Sets value of the displayName property.
void setValue(const QString &value)
Sets value of the value property.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
QString formattedType() const
Output only.
FieldMetadata metadata() const
Metadata about the email address.
void setType(const QString &value)
Sets value of the type property.
EmailAddress()
Constructs a new EmailAddress.
QString value() const
The email address.
Metadata about a field.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QString toString() const const
void append(QList< T > &&value)
const_iterator cbegin() const const
const_iterator cend() const const
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
CaseInsensitive
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.