KContacts

email.cpp
1 /*
2  This file is part of the KContacts framework.
3  SPDX-FileCopyrightText: 2015-2019 Laurent Montel <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "email.h"
9 
10 #include <QMap>
11 #include <QStringList>
12 #include <QDataStream>
13 
14 using namespace KContacts;
15 
16 class Q_DECL_HIDDEN Email::Private : public QSharedData
17 {
18 public:
19  Private()
20  {
21  }
22 
23  Private(const Private &other)
24  : QSharedData(other)
25  {
26  parameters = other.parameters;
27  mail = other.mail;
28  }
29 
30  QMap<QString, QStringList> parameters;
31  QString mail;
32 };
33 
35  : d(new Private)
36 {
37 }
38 
39 Email::Email(const QString &mail)
40  : d(new Private)
41 {
42  d->mail = mail;
43 }
44 
45 Email::Email(const Email &other)
46  : d(other.d)
47 {
48 }
49 
50 Email::~Email()
51 {
52 }
53 
54 QMap<QString, QStringList> Email::parameters() const
55 {
56  return d->parameters;
57 }
58 
59 bool Email::operator==(const Email &other) const
60 {
61  return (d->parameters == other.parameters()) && (d->mail == other.mail());
62 }
63 
64 bool Email::operator!=(const Email &other) const
65 {
66  return !(other == *this);
67 }
68 
69 Email &Email::operator=(const Email &other)
70 {
71  if (this != &other) {
72  d = other.d;
73  }
74 
75  return *this;
76 }
77 
78 QString Email::toString() const
79 {
80  QString str = QLatin1String("Email {\n");
81  str += QStringLiteral(" mail: %1\n").arg(d->mail);
82  if (!d->parameters.isEmpty()) {
83  QString param;
84  QMap<QString, QStringList>::const_iterator it = d->parameters.constBegin();
85  const QMap<QString, QStringList>::const_iterator end = d->parameters.constEnd();
86  while (it != end) {
87  param += QStringLiteral("%1 %2").arg(it.key(), it.value().join(QLatin1Char(',')));
88  ++it;
89  }
90  str += QStringLiteral(" parameters: %1\n").arg(param);
91  }
92  str += QLatin1String("}\n");
93  return str;
94 }
95 
96 void Email::setParameters(const QMap<QString, QStringList> &params)
97 {
98  d->parameters = params;
99 }
100 
101 void Email::setEmail(const QString &mail)
102 {
103  d->mail = mail;
104 }
105 
106 QString Email::mail() const
107 {
108  return d->mail;
109 }
110 
111 bool Email::isValid() const
112 {
113  return !d->mail.isEmpty();
114 }
115 
116 struct email_type_name
117 {
118  const char* name;
120 };
121 
122 static const email_type_name email_type_names[] = {
123  { "HOME", Email::Home },
124  { "WORK", Email::Work },
125  { "OTHER", Email::Other }
126 };
127 
129 {
130  const auto it = d->parameters.constFind(QLatin1String("type"));
131  if (it == d->parameters.end()) {
132  return Unknown;
133  }
134 
135  Type type = Unknown;
136  for (const auto &s : it.value()) {
137  const auto it = std::find_if(std::begin(email_type_names), std::end(email_type_names), [s](const email_type_name &t) {
138  return QLatin1String(t.name) == s;
139  });
140  if (it != std::end(email_type_names)) {
141  type |= (*it).type;
142  }
143  }
144  return type;
145 }
146 
148 {
149  const auto oldType = this->type();
150 
151  auto types = d->parameters.value(QLatin1String("type"));
152  for (const auto &t : email_type_names) {
153  if (((type ^ oldType) & t.type) == 0) {
154  continue; // no change
155  }
156 
157  if (type & t.type) {
158  types.push_back(QLatin1String(t.name));
159  } else {
160  types.removeAll(QLatin1String(t.name));
161  }
162  }
163 
164  d->parameters.insert(QLatin1String("type"), types);
165 }
166 
167 bool Email::isPreferred() const
168 {
169  auto it = d->parameters.constFind(QLatin1String("pref"));
170  if (it != d->parameters.end() && !it.value().isEmpty()) {
171  return it.value().at(0) == QLatin1Char('1');
172  }
173 
174  it = d->parameters.constFind(QLatin1String("type"));
175  if (it != d->parameters.end()) {
176  return it.value().contains(QLatin1String("PREF"), Qt::CaseInsensitive);
177  }
178 
179  return false;
180 }
181 
182 void Email::setPreferred(bool preferred)
183 {
184  if (preferred == isPreferred()) {
185  return;
186  }
187 
188  auto types = d->parameters.value(QLatin1String("type"));
189  if (!preferred) {
190  d->parameters.remove(QLatin1String("pref"));
191  types.removeAll(QLatin1String("PREF"));
192  } else {
193  types.push_back(QLatin1String("PREF"));
194  }
195  d->parameters.insert(QLatin1String("type"), types);
196 }
197 
198 QDataStream &KContacts::operator<<(QDataStream &s, const Email &email)
199 {
200  return s << email.d->parameters << email.d->mail;
201 }
202 
203 QDataStream &KContacts::operator>>(QDataStream &s, Email &email)
204 {
205  s >> email.d->parameters >> email.d->mail;
206  return s;
207 }
void setType(Type type)
Sets the email type.
Definition: email.cpp:147
Class that holds a Email for a contact.
Definition: email.h:23
QAction * mail(const QObject *recvr, const char *slot, QObject *parent)
Other email.
Definition: email.h:54
Personal email.
Definition: email.h:52
Type type() const
Returns the type of the email.
Work email.
Definition: email.h:53
bool isPreferred() const
Returns whether this is the preferred email address.
CaseInsensitive
void setPreferred(bool preferred)
Sets that this is the preferred email address.
Definition: email.cpp:182
Email()
Creates an empty email object.
Definition: email.cpp:34
const Key key(const T &value, const Key &defaultKey) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
No or unknown email type is set.
Definition: email.h:51
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 9 2020 22:54:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.