Kgapi

sipaddress.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 "sipaddress.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 <algorithm>
21
22namespace KGAPI2::People
23{
24class SipAddress::Private : public QSharedData
25{
26public:
27 explicit Private() = default;
28 Private(const Private &) = default;
29 Private(Private &&) noexcept = delete;
30 Private &operator=(const Private &) = delete;
31 Private &operator=(Private &&) noexcept = delete;
32 ~Private() = default;
33
34 bool operator==(const Private &other) const
35 {
36 return value == other.value && metadata == other.metadata && type == other.type && formattedType == other.formattedType;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 QString value{};
45 FieldMetadata metadata{};
46 QString type{};
47 QString formattedType{};
48};
49
51 : d(new Private)
52{
53}
54
55SipAddress::SipAddress(const SipAddress &) = default;
56SipAddress::SipAddress(SipAddress &&) noexcept = default;
57SipAddress &SipAddress::operator=(const SipAddress &) = default;
58SipAddress &SipAddress::operator=(SipAddress &&) noexcept = default;
59SipAddress::~SipAddress() = default;
60
61bool SipAddress::operator==(const SipAddress &other) const
62{
63 return *d == *other.d;
64}
65
66bool SipAddress::operator!=(const SipAddress &other) const
67{
68 return !(*this == other);
69}
70
72{
73 return d->value;
74}
75
76void SipAddress::setValue(const QString &value)
77{
78 d->value = value;
79}
81{
82 return d->metadata;
83}
84
86{
87 d->metadata = value;
88}
90{
91 return d->type;
92}
93
94void SipAddress::setType(const QString &value)
95{
96 d->type = value;
97}
99{
100 return d->formattedType;
101}
102
103SipAddress SipAddress::fromJSON(const QJsonObject &obj)
104{
105 SipAddress sipAddress;
106
107 if(!obj.isEmpty()) {
108 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
109 sipAddress.d->metadata = FieldMetadata::fromJSON(metadata);
110 sipAddress.d->value = obj.value(QStringLiteral("value")).toString();
111 sipAddress.d->type = obj.value(QStringLiteral("type")).toString();
112 sipAddress.d->formattedType = obj.value(QStringLiteral("formattedType")).toString();
113 }
114 return sipAddress;
115}
116
117QList<SipAddress> SipAddress::fromJSONArray(const QJsonArray &data)
118{
119 QList<SipAddress> sipAddresses;
120
121 for(const auto &sipAddress : data) {
122 if(sipAddress.isObject()) {
123 const auto objectifiedSipAddress = sipAddress.toObject();
124 sipAddresses.append(fromJSON(objectifiedSipAddress));
125 }
126 }
127
128 return sipAddresses;
129}
130
131QJsonValue SipAddress::toJSON() const
132{
133 QJsonObject obj;
134
135 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
136 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
137 PeopleUtils::addValueToJsonObjectIfValid(obj, "type", d->type);
138 // Output only -> PeopleUtils::addValueToJsonObjectIfValid(obj, "formattedType", d->formattedType);
139 return obj;
140}
141
142} // namespace KGAPI2::People
Metadata about a field.
A person's SIP address.
Definition sipaddress.h:35
QString formattedType() const
Output only.
void setType(const QString &value)
Sets value of the type property.
QString type() const
The type of the SIP address.
void setValue(const QString &value)
Sets value of the value property.
QString value() const
The SIP address in the RFC 3261 19.1 SIP URI format.
SipAddress()
Constructs a new SipAddress.
FieldMetadata metadata() const
Metadata about the SIP address.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QString toString() const const
void append(QList< T > &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.