KContacts

email.cpp
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
33};
34
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(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;
111};
112
113static const email_type_name email_type_names[] = {
114 {"HOME", Email::Home},
115 {"WORK", Email::Work},
116 {"OTHER", Email::Other},
117};
118
119Email::Type KContacts::Email::type() const
120{
121 const auto it = d->mParamMap.findParam(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(std::begin(email_type_names), std::end(email_type_names), [&s](const email_type_name &t) {
129 return QLatin1String(t.name) == s;
130 });
131 if (it != std::end(email_type_names)) {
132 type |= (*it).type;
133 }
134 }
135 return type;
136}
137
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(paramName);
147 if (it != d->mParamMap.end()) {
148 theIt = it;
149 } else {
150 theIt = d->mParamMap.insertParam({paramName, {}});
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(QLatin1String(t.name));
160 } else {
161 theIt->paramValues.removeAll(QLatin1String(t.name));
162 }
163 }
164}
165
166bool Email::isPreferred() const
167{
168 auto it = d->mParamMap.findParam(QLatin1String("pref"));
169 if (it != d->mParamMap.end()) {
170 return !it->paramValues.isEmpty() && it->paramValues.at(0) == QLatin1Char('1');
171 }
172
173 it = d->mParamMap.findParam(QLatin1String("type"));
174 if (it != d->mParamMap.end()) {
175 return it->paramValues.contains(QLatin1String("PREF"), 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(paramName);
190 QStringList types = typeIt != d->mParamMap.end() ? typeIt->paramValues : QStringList{};
191
192 if (!preferred) {
193 auto prefIt = d->mParamMap.findParam(QLatin1String("pref"));
194 if (prefIt != d->mParamMap.end()) {
195 d->mParamMap.erase(prefIt);
196 }
197
198 types.removeAll(QLatin1String("PREF"));
199 } else {
200 types.push_back(QStringLiteral("PREF"));
201 }
202
203 typeIt = d->mParamMap.findParam(paramName);
204 if (typeIt != d->mParamMap.end()) {
205 typeIt->paramValues = types;
206 } else {
207 d->mParamMap.insertParam({paramName, 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"
Class that holds a Email for a contact.
Definition email.h:28
Email()
Creates an empty email object.
Definition email.cpp:35
@ Home
Personal email.
Definition email.h:58
@ Work
Work email.
Definition email.h:59
@ Other
Other email.
Definition email.h:60
void setType(Type type)
Sets the email type.
Definition email.cpp:138
void setPreferred(bool preferred)
Sets that this is the preferred email address.
Definition email.cpp:181
Type type(const QSqlDatabase &db)
QAction * mail(const QObject *recvr, const char *slot, QObject *parent)
QString name(StandardAction id)
void push_back(parameter_type value)
qsizetype removeAll(const AT &t)
QString arg(Args &&... args) const const
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.