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 
139 {
140  d->mType = type;
141 }
142 
144 {
145  return d->mType;
146 }
147 
149 {
150  return typeLabel(type());
151 }
152 
154 {
155  static TypeList list;
156 
157  if (list.isEmpty()) {
158  list << Home << Work << Msg << Pref << Voice << Fax << Cell << Video //
159  << Bbs << Modem << Car << Isdn << Pcs << Pager << Undefined;
160  }
161 
162  return list;
163 }
164 
166 {
167  switch (type) {
168  case Undefined:
169  return i18nc("Undefined phone type", "Telephone number");
170  case Home:
171  return i18nc("Home phone", "Home");
172  case Work:
173  return i18nc("Work phone", "Work");
174  case Msg:
175  return i18n("Messenger");
176  case Pref:
177  return i18nc("Preferred phone", "Preferred");
178  case Voice:
179  return i18n("Voice");
180  case Fax:
181  return i18n("Fax");
182  case Cell:
183  return i18nc("Mobile Phone", "Mobile");
184  case Video:
185  return i18nc("Video phone", "Video");
186  case Bbs:
187  return i18n("Mailbox");
188  case Modem:
189  return i18n("Modem");
190  case Car:
191  return i18nc("Car Phone", "Car");
192  case Isdn:
193  return i18n("ISDN");
194  case Pcs:
195  return i18n("PCS");
196  case Pager:
197  return i18n("Pager");
198  default:
199  return i18nc("another type of phone", "Other");
200  }
201 }
202 
204 {
205  QString label;
206  bool first = true;
207 
208  // special cases
209  // Pref stand alone -> Preferred Number
210  // Home+Fax or Work+Fax -> combine as initial string
211  if (type == Pref) {
212  return i18n("Preferred Number");
213  }
214 
215  if (type & Fax) {
216  if (type & Home) {
217  label = i18n("Home Fax");
218  first = false;
219  type &= ~Fax;
220  type &= ~Home;
221  } else if (type & Work) {
222  label = i18n("Work Fax");
223  first = false;
224  type &= ~Fax;
225  type &= ~Work;
226  }
227  }
228 
229  const TypeList list = typeList();
230 
232  TypeList::ConstIterator end(list.constEnd());
233  for (it = list.constBegin(); it != end; ++it) {
234  // these are actually flags
235  const TypeFlag flag = static_cast<TypeFlag>(static_cast<int>(*it));
236  if (type & flag) {
237  if (!first) {
238  label.append(QLatin1Char('/'));
239  }
240 
241  label.append(typeFlagLabel(flag));
242 
243  if (first) {
244  first = false;
245  }
246  }
247  }
248 
249  return label;
250 }
251 
252 bool PhoneNumber::isPreferred() const
253 {
254  return type() & Pref;
255 }
256 
257 bool PhoneNumber::supportsSms() const
258 {
259  return type() & Cell;
260 }
261 
263 {
264  QString str = QLatin1String("PhoneNumber {\n");
265  str += QStringLiteral(" Id: %1\n").arg(d->mId);
266  str += QStringLiteral(" Type: %1\n").arg(typeLabel(d->mType));
267  if (!d->mParameters.isEmpty()) {
268  QString param;
269  QMap<QString, QStringList>::const_iterator it = d->mParameters.constBegin();
270  const QMap<QString, QStringList>::const_iterator end = d->mParameters.constEnd();
271  while (it != end) {
272  param += QStringLiteral("%1 %2").arg(it.key(), it.value().join(QLatin1Char(',')));
273  ++it;
274  }
275  str += QStringLiteral(" parameters: %1\n").arg(param);
276  }
277  str += QStringLiteral(" Number: %1\n").arg(d->mNumber);
278  str += QLatin1String("}\n");
279 
280  return str;
281 }
282 
283 void PhoneNumber::setParameters(const QMap<QString, QStringList> &params)
284 {
285  d->mParameters = params;
286 }
287 
288 QMap<QString, QStringList> PhoneNumber::parameters() const
289 {
290  return d->mParameters;
291 }
292 
293 QDataStream &KContacts::operator<<(QDataStream &s, const PhoneNumber &phone)
294 {
295  return s << phone.d->mId << (uint)phone.d->mType << phone.d->mNumber << phone.d->mParameters;
296 }
297 
298 QDataStream &KContacts::operator>>(QDataStream &s, PhoneNumber &phone)
299 {
300  uint type;
301  s >> phone.d->mId >> type >> phone.d->mNumber >> phone.d->mParameters;
302  phone.d->mType = PhoneNumber::Type(type);
303 
304  return s;
305 }
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:70
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:61
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:48
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:52
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:28
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.