KContacts

phonenumber.cpp
1 /*
2  This file is part of the KContacts framework.
3  SPDX-FileCopyrightText: 2001 Cornelius Schumacher <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "phonenumber.h"
9 
10 #include <KLocalizedString>
11 #include <krandom.h>
12 
13 #include <QDataStream>
14 #include <QSharedData>
15 
16 using namespace KContacts;
17 
18 static QString cleanupNumber(const QString &input)
19 {
20  return input.simplified();
21 }
22 
23 class Q_DECL_HIDDEN PhoneNumber::Private : public QSharedData
24 {
25 public:
26  Private(Type type)
27  : mId(KRandom::randomString(8))
28  , mType(type)
29  {
30  }
31 
32  Private(const Private &other)
33  : QSharedData(other)
34  {
35  mId = other.mId;
36  mType = other.mType;
37  mNumber = other.mNumber;
38  }
39 
40  QString mId;
41  QString mNumber;
42  Type mType;
43  QMap<QString, QStringList> mParameters;
44 };
45 
47  : d(new Private(Home))
48 {
49 }
50 
52  : d(new Private(type))
53 {
54  d->mNumber = cleanupNumber(number);
55 }
56 
58  : d(other.d)
59 {
60 }
61 
63 {
64 }
65 
66 bool PhoneNumber::operator==(const PhoneNumber &other) const
67 {
68  if (d->mId != other.d->mId) {
69  return false;
70  }
71 
72  if (d->mNumber != other.d->mNumber) {
73  return false;
74  }
75 
76  if (d->mType != other.d->mType) {
77  return false;
78  }
79 
80  if (d->mParameters != other.d->mParameters) {
81  return false;
82  }
83 
84  return true;
85 }
86 
87 bool PhoneNumber::operator!=(const PhoneNumber &other) const
88 {
89  return !(other == *this);
90 }
91 
93 {
94  if (this != &other) {
95  d = other.d;
96  }
97 
98  return *this;
99 }
100 
101 bool PhoneNumber::isEmpty() const
102 {
103  return d->mNumber.isEmpty();
104 }
105 
107 {
108  d->mId = id;
109 }
110 
111 QString PhoneNumber::id() const
112 {
113  return d->mId;
114 }
115 
117 {
118  d->mNumber = cleanupNumber(number);
119 }
120 
122 {
123  return d->mNumber;
124 }
125 
127 {
128  QString result;
129  result.reserve(d->mNumber.size());
130  for (const auto &c : d->mNumber) {
131  if (c.isDigit() || (c == QLatin1Char('+') && result.isEmpty())) {
132  result.push_back(c);
133  }
134  }
135  return result;
136 }
137 
138 
140 {
141  d->mType = type;
142 }
143 
145 {
146  return d->mType;
147 }
148 
150 {
151  return typeLabel(type());
152 }
153 
155 {
156  static TypeList list;
157 
158  if (list.isEmpty()) {
159  list << Home << Work << Msg << Pref << Voice << Fax << Cell << Video
160  << Bbs << Modem << Car << Isdn << Pcs << Pager << Undefined;
161  }
162 
163  return list;
164 }
165 
167 {
168  switch (type) {
169  case Undefined:
170  return i18nc("Undefined phone type", "Telephone number");
171  case Home:
172  return i18nc("Home phone", "Home");
173  case Work:
174  return i18nc("Work phone", "Work");
175  case Msg:
176  return i18n("Messenger");
177  case Pref:
178  return i18nc("Preferred phone", "Preferred");
179  case Voice:
180  return i18n("Voice");
181  case Fax:
182  return i18n("Fax");
183  case Cell:
184  return i18nc("Mobile Phone", "Mobile");
185  case Video:
186  return i18nc("Video phone", "Video");
187  case Bbs:
188  return i18n("Mailbox");
189  case Modem:
190  return i18n("Modem");
191  case Car:
192  return i18nc("Car Phone", "Car");
193  case Isdn:
194  return i18n("ISDN");
195  case Pcs:
196  return i18n("PCS");
197  case Pager:
198  return i18n("Pager");
199  default:
200  return i18nc("another type of phone", "Other");
201  }
202 }
203 
205 {
206  QString label;
207  bool first = true;
208 
209  // special cases
210  // Pref stand alone -> Preferred Number
211  // Home+Fax or Work+Fax -> combine as initial string
212  if (type == Pref) {
213  return i18n("Preferred Number");
214  }
215 
216  if (type & Fax) {
217  if (type & Home) {
218  label = i18n("Home Fax");
219  first = false;
220  type &= ~Fax;
221  type &= ~Home;
222  } else if (type & Work) {
223  label = i18n("Work Fax");
224  first = false;
225  type &= ~Fax;
226  type &= ~Work;
227  }
228  }
229 
230  const TypeList list = typeList();
231 
233  TypeList::ConstIterator end(list.constEnd());
234  for (it = list.constBegin(); it != end; ++it) {
235  // these are actually flags
236  const TypeFlag flag = static_cast<TypeFlag>(static_cast<int>(*it));
237  if (type & flag) {
238  if (!first) {
239  label.append(QLatin1Char('/'));
240  }
241 
242  label.append(typeFlagLabel(flag));
243 
244  if (first) {
245  first = false;
246  }
247  }
248  }
249 
250  return label;
251 }
252 
253 bool PhoneNumber::isPreferred() const
254 {
255  return type() & Pref;
256 }
257 
258 bool PhoneNumber::supportsSms() const
259 {
260  return type() & Cell;
261 }
262 
264 {
265  QString str = QLatin1String("PhoneNumber {\n");
266  str += QStringLiteral(" Id: %1\n").arg(d->mId);
267  str += QStringLiteral(" Type: %1\n").arg(typeLabel(d->mType));
268  if (!d->mParameters.isEmpty()) {
269  QString param;
270  QMap<QString, QStringList>::const_iterator it = d->mParameters.constBegin();
271  const QMap<QString, QStringList>::const_iterator end = d->mParameters.constEnd();
272  while (it != end) {
273  param += QStringLiteral("%1 %2").arg(it.key(), it.value().join(QLatin1Char(',')));
274  ++it;
275  }
276  str += QStringLiteral(" parameters: %1\n").arg(param);
277  }
278  str += QStringLiteral(" Number: %1\n").arg(d->mNumber);
279  str += QLatin1String("}\n");
280 
281  return str;
282 }
283 
284 void PhoneNumber::setParameters(const QMap<QString, QStringList> &params)
285 {
286  d->mParameters = params;
287 }
288 
289 QMap<QString, QStringList> PhoneNumber::parameters() const
290 {
291  return d->mParameters;
292 }
293 
294 QDataStream &KContacts::operator<<(QDataStream &s, const PhoneNumber &phone)
295 {
296  return s << phone.d->mId << (uint)phone.d->mType << phone.d->mNumber << phone.d->mParameters;
297 }
298 
299 QDataStream &KContacts::operator>>(QDataStream &s, PhoneNumber &phone)
300 {
301  uint type;
302  s >> phone.d->mId >> type >> phone.d->mNumber >> phone.d->mParameters;
303  phone.d->mType = PhoneNumber::Type(type);
304 
305  return s;
306 }
void setNumber(const QString &number)
Sets the phone number.
QString & append(QChar ch)
QFlags< TypeFlag > Type
Stores a combination of TypeFlag values.
Definition: phonenumber.h:69
QVector::const_iterator constEnd() const const
QString simplified() const const
bool supportsSms() const
Returns whether this phone number is expected to support receiving SMS messages.
static TypeList typeList()
Returns a list of all available types.
Personal Communication Service.
Definition: phonenumber.h:60
void setType(Type type)
Sets the type.
typedef ConstIterator
QString normalizedNumber() const
Returns the phone number normalized for dialing.
TypeFlag
Phone number types.
Definition: phonenumber.h:47
void setId(const QString &identifier)
Sets the unique identifier.
PhoneNumber()
Creates an empty phone number object.
Definition: phonenumber.cpp:46
PhoneNumber & operator=(const PhoneNumber &other)
Assignment operator.
Definition: phonenumber.cpp:92
QString number() const
Returns the phone number.
Preferred number.
Definition: phonenumber.h:51
QString i18nc(const char *context, const char *text, const TYPE &arg...)
static QString typeFlagLabel(TypeFlag type)
Returns the translated label for phone number type.
QString toString() const
Returns a string representation of the phone number.
QString id() const
Returns the unique identifier.
bool isEmpty() const const
void push_back(QChar ch)
bool operator==(const PhoneNumber &other) const
Equality operator.
Definition: phonenumber.cpp:66
QString typeLabel() const
Returns a translated string of the address&#39; type.
const Key key(const T &value, const Key &defaultKey) const const
QString i18n(const char *text, const TYPE &arg...)
QVector::const_iterator constBegin() const const
bool operator!=(const PhoneNumber &other) const
Not-Equal operator.
Definition: phonenumber.cpp:87
~PhoneNumber()
Destroys the phone number.
Definition: phonenumber.cpp:62
bool isEmpty() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void reserve(int size)
Type type() const
Returns the type.
bool isPreferred() const
Returns whether this phone number is marked as preferred.
KCOREADDONS_EXPORT QString randomString(int length)
bool isEmpty() const
Returns true, if the phone number is empty.
Phonenumber information.
Definition: phonenumber.h:27
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 Sat Aug 8 2020 22:53:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.