Kgapi

phonenumber.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 "phonenumber.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/PhoneNumber>
21
22#include <algorithm>
23
24namespace KGAPI2::People
25{
26class PhoneNumber::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 && formattedType == other.formattedType && canonicalForm == other.canonicalForm && type == other.type
39 && metadata == other.metadata;
40 }
41
42 bool operator!=(const Private &other) const
43 {
44 return !(*this == other);
45 }
46
47 QString value{};
48 QString formattedType{};
49 QString canonicalForm{};
50 QString type{};
51 FieldMetadata metadata{};
52};
53
55 : d(new Private)
56{
57}
58
59PhoneNumber::PhoneNumber(const PhoneNumber &) = default;
60PhoneNumber::PhoneNumber(PhoneNumber &&) noexcept = default;
61PhoneNumber &PhoneNumber::operator=(const PhoneNumber &) = default;
62PhoneNumber &PhoneNumber::operator=(PhoneNumber &&) noexcept = default;
63PhoneNumber::~PhoneNumber() = default;
64
65bool PhoneNumber::operator==(const PhoneNumber &other) const
66{
67 return *d == *other.d;
68}
69
70bool PhoneNumber::operator!=(const PhoneNumber &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->formattedType;
87}
89{
90 return d->canonicalForm;
91}
93{
94 return d->type;
95}
96
97void PhoneNumber::setType(const QString &value)
98{
99 d->type = value;
100}
102{
103 return d->metadata;
104}
105
107{
108 d->metadata = value;
109}
110
111PhoneNumber PhoneNumber::fromJSON(const QJsonObject &obj)
112{
113 PhoneNumber phoneNumber;
114
115 if(!obj.isEmpty()) {
116 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
117 phoneNumber.d->metadata = FieldMetadata::fromJSON(metadata);
118 phoneNumber.d->value = obj.value(QStringLiteral("value")).toString();
119 phoneNumber.d->canonicalForm = obj.value(QStringLiteral("canonicalForm")).toString();
120 phoneNumber.d->type = obj.value(QStringLiteral("type")).toString();
121 phoneNumber.d->formattedType = obj.value(QStringLiteral("formattedType")).toString();
122 }
123
124 return phoneNumber;
125}
126
127QList<PhoneNumber> PhoneNumber::fromJSONArray(const QJsonArray &data)
128{
129 QList<PhoneNumber> phoneNumbers;
130
131 for(const auto &phoneNumber : data) {
132 if(phoneNumber.isObject()) {
133 const auto objectifiedPhoneNumber = phoneNumber.toObject();
134 phoneNumbers.append(fromJSON(objectifiedPhoneNumber));
135 }
136 }
137
138 return phoneNumbers;
139}
140
141QJsonValue PhoneNumber::toJSON() const
142{
143 QJsonObject obj;
144
145 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
146 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType", d->formattedType);
147 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "canonicalForm", d->canonicalForm);
148 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
149 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
150 return obj;
151}
152
153PhoneNumber PhoneNumber::fromKContactsPhoneNumber(const KContacts::PhoneNumber &phoneNumber)
154{
155 PhoneNumber convertedPhoneNumber;
156 convertedPhoneNumber.setValue(phoneNumber.number());
157
158 switch(phoneNumber.type()) {
160 convertedPhoneNumber.setType(QStringLiteral("home"));
161 break;
163 convertedPhoneNumber.setType(QStringLiteral("work"));
164 break;
166 convertedPhoneNumber.setType(QStringLiteral("mobile"));
167 break;
169 convertedPhoneNumber.setType(QStringLiteral("otherFax"));
170 break;
172 convertedPhoneNumber.setType(QStringLiteral("pager"));
173 break;
175 convertedPhoneNumber.setType(QStringLiteral("main"));
176 break;
177 case KContacts::PhoneNumber::Voice: // Unclear if it would be Google Voice
178 default:
179 break;
180 }
181
182 return convertedPhoneNumber;
183}
184
185QList<PhoneNumber> PhoneNumber::fromKContactsPhoneNumberList(const QList<KContacts::PhoneNumber> &phoneNumberList)
186{
187 QList<PhoneNumber> convertedPhoneNumbers;
188 std::transform(phoneNumberList.cbegin(),
189 phoneNumberList.cend(),
190 std::back_inserter(convertedPhoneNumbers),
191 [](const KContacts::PhoneNumber &phoneNumber) {
192 return PhoneNumber::fromKContactsPhoneNumber(phoneNumber);
193 });
194 return convertedPhoneNumbers;
195}
196
197KContacts::PhoneNumber PhoneNumber::toKContactsPhoneNumber() const
198{
199 KContacts::PhoneNumber convertedPhoneNumber;
200 convertedPhoneNumber.setNumber(value());
201
202 if(QString::compare(type(), QStringLiteral("home"), Qt::CaseInsensitive)) {
203 convertedPhoneNumber.setType(KContacts::PhoneNumber::Home);
204 } else if(QString::compare(type(), QStringLiteral("work"), Qt::CaseInsensitive) ||
205 QString::compare(type(), QStringLiteral("workMobile"), Qt::CaseInsensitive) ||
206 QString::compare(type(), QStringLiteral("workPager"), Qt::CaseInsensitive)) {
207 convertedPhoneNumber.setType(KContacts::PhoneNumber::Work);
208 } else if(QString::compare(type(), QStringLiteral("mobile"), Qt::CaseInsensitive)) {
209 convertedPhoneNumber.setType(KContacts::PhoneNumber::Cell);
210 } else if(QString::compare(type(), QStringLiteral("homeFax"), Qt::CaseInsensitive) ||
211 QString::compare(type(), QStringLiteral("workFax"), Qt::CaseInsensitive) ||
212 QString::compare(type(), QStringLiteral("otherFax"), Qt::CaseInsensitive)) {
213 convertedPhoneNumber.setType(KContacts::PhoneNumber::Fax);
214 } else if(QString::compare(type(), QStringLiteral("pager"), Qt::CaseInsensitive)) {
215 convertedPhoneNumber.setType(KContacts::PhoneNumber::Pager);
216 } else if(QString::compare(type(), QStringLiteral("main"), Qt::CaseInsensitive)) {
217 convertedPhoneNumber.setType(KContacts::PhoneNumber::Pref);
218 } else if(QString::compare(type(), QStringLiteral("googleVoice"), Qt::CaseInsensitive)) {
219 convertedPhoneNumber.setType(KContacts::PhoneNumber::Voice);
220 }
221
222 return convertedPhoneNumber;
223}
224
225} // namespace KGAPI2::People
void setType(Type type)
void setNumber(const QString &number)
QString number() const
Metadata about a field.
A person's phone number.
Definition phonenumber.h:38
QString formattedType() const
Output only.
PhoneNumber()
Constructs a new PhoneNumber.
void setValue(const QString &value)
Sets value of the value property.
QString type() const
The type of the phone number.
FieldMetadata metadata() const
Metadata about the phone number.
QString value() const
The phone number.
void setType(const QString &value)
Sets value of the type property.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
QString canonicalForm() const
Output only.
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 Mon Nov 18 2024 12:17:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.