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

KDE's Doxygen guidelines are available online.