1/*
2 This file is part of the KContacts framework.
3 SPDX-FileCopyrightText: 2015-2019 Laurent Montel <montel@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "email.h"
9#include "parametermap_p.h"
10
11#include <QDataStream>
12#include <QStringList>
13
14using namespace KContacts;
15
16class Q_DECL_HIDDEN Email::Private : public QSharedData
17{
18public:
19 Private()
20 {
21 }
22
23 Private(const Private &other)
24 : QSharedData(other)
25 {
26 mParamMap = other.mParamMap;
27 mail = other.mail;
28 }
29
30 ParameterMap mParamMap;
31
32 QString mail;
33};
34
35Email::Email()
36 : d(new Private)
37{
38}
39
40Email::Email(const QString &mail)
41 : d(new Private)
42{
43 d->mail = mail;
44}
45
46Email::Email(const Email &other)
47 : d(other.d)
48{
49}
50
51Email::~Email()
52{
53}
54
55bool Email::operator==(const Email &other) const
56{
57 return (d->mParamMap == other.d->mParamMap) && (d->mail == other.mail());
58}
59
60bool Email::operator!=(const Email &other) const
61{
62 return !(other == *this);
63}
64
65Email &Email::operator=(const Email &other)
66{
67 if (this != &other) {
68 d = other.d;
69 }
70
71 return *this;
72}
73
74QString Email::toString() const
75{
76 QString str = QLatin1String("Email {\n");
77 str += QStringLiteral(" mail: %1\n").arg(a: d->mail);
78 str += d->mParamMap.toString();
79 str += QLatin1String("}\n");
80 return str;
81}
82
83void Email::setParams(const ParameterMap &params)
84{
85 d->mParamMap = params;
86}
87
88ParameterMap Email::params() const
89{
90 return d->mParamMap;
91}
92
93void Email::setEmail(const QString &mail)
94{
95 d->mail = mail;
96}
97
98QString Email::mail() const
99{
100 return d->mail;
101}
102
103bool Email::isValid() const
104{
105 return !d->mail.isEmpty();
106}
107
108struct email_type_name {
109 const char *name;
110 Email::Type type;
111};
112
113static const email_type_name email_type_names[] = {
114 {.name: "HOME", .type: Email::Home},
115 {.name: "WORK", .type: Email::Work},
116 {.name: "OTHER", .type: Email::Other},
117};
118
119Email::Type KContacts::Email::type() const
120{
121 const auto it = d->mParamMap.findParam(param: QLatin1String("type"));
122 if (it == d->mParamMap.end()) {
123 return Unknown;
124 }
125
126 Type type = Unknown;
127 for (const auto &s : it->paramValues) {
128 const auto it = std::find_if(first: std::begin(arr: email_type_names), last: std::end(arr: email_type_names), pred: [&s](const email_type_name &t) {
129 return QLatin1String(t.name) == s;
130 });
131 if (it != std::end(arr: email_type_names)) {
132 type |= (*it).type;
133 }
134 }
135 return type;
136}
137
138void Email::setType(Type type)
139{
140 const auto oldType = this->type();
141
142 const QString paramName(QStringLiteral("type"));
143
144 ParameterMap::iterator theIt;
145
146 auto it = d->mParamMap.findParam(param: paramName);
147 if (it != d->mParamMap.end()) {
148 theIt = it;
149 } else {
150 theIt = d->mParamMap.insertParam(newdata: {.param: paramName, .paramValues: {}});
151 }
152
153 for (const auto &t : email_type_names) {
154 if (((type ^ oldType) & t.type) == 0) {
155 continue; // no change
156 }
157
158 if (type & t.type) {
159 theIt->paramValues.push_back(t: QLatin1String(t.name));
160 } else {
161 theIt->paramValues.removeAll(t: QLatin1String(t.name));
162 }
163 }
164}
165
166bool Email::isPreferred() const
167{
168 auto it = d->mParamMap.findParam(param: QLatin1String("pref"));
169 if (it != d->mParamMap.end()) {
170 return !it->paramValues.isEmpty() && it->paramValues.at(i: 0) == QLatin1Char('1');
171 }
172
173 it = d->mParamMap.findParam(param: QLatin1String("type"));
174 if (it != d->mParamMap.end()) {
175 return it->paramValues.contains(str: QLatin1String("PREF"), cs: Qt::CaseInsensitive);
176 }
177
178 return false;
179}
180
181void Email::setPreferred(bool preferred)
182{
183 if (preferred == isPreferred()) {
184 return;
185 }
186
187 const QString paramName(QStringLiteral("type"));
188
189 auto typeIt = d->mParamMap.findParam(param: paramName);
190 QStringList types = typeIt != d->mParamMap.end() ? typeIt->paramValues : QStringList{};
191
192 if (!preferred) {
193 auto prefIt = d->mParamMap.findParam(param: QLatin1String("pref"));
194 if (prefIt != d->mParamMap.end()) {
195 d->mParamMap.erase(position: prefIt);
196 }
197
198 types.removeAll(t: QLatin1String("PREF"));
199 } else {
200 types.push_back(QStringLiteral("PREF"));
201 }
202
203 typeIt = d->mParamMap.findParam(param: paramName);
204 if (typeIt != d->mParamMap.end()) {
205 typeIt->paramValues = types;
206 } else {
207 d->mParamMap.insertParam(newdata: {.param: paramName, .paramValues: types});
208 }
209}
210
211QDataStream &KContacts::operator<<(QDataStream &s, const Email &email)
212{
213 return s << email.d->mParamMap << email.d->mail;
214}
215
216QDataStream &KContacts::operator>>(QDataStream &s, Email &email)
217{
218 s >> email.d->mParamMap >> email.d->mail;
219 return s;
220}
221
222#include "moc_email.cpp"
223

source code of kcontacts/src/email.cpp