KContacts

phonenumber.cpp
1/*
2 This file is part of the KContacts framework.
3 SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "phonenumber.h"
9#include "parametermap_p.h"
10
11#include <KLocalizedString>
12#include <krandom.h>
13
14#include <QDataStream>
15#include <QSharedData>
16
17using namespace KContacts;
18
19static QString cleanupNumber(const QString &input)
20{
21 return input.simplified();
22}
23
24class Q_DECL_HIDDEN PhoneNumber::Private : public QSharedData
25{
26public:
27 Private(Type type)
28 : mId(KRandom::randomString(8))
29 , mType(type)
30 {
31 }
32
33 Private(const Private &other)
34 : QSharedData(other)
35 {
36 mId = other.mId;
37 mType = other.mType;
38 mNumber = other.mNumber;
39 }
40
41 QString mId;
42 QString mNumber;
43 Type mType;
44 ParameterMap mParamMap;
45};
46
48 : d(new Private(Home))
49{
50}
51
53 : d(new Private(type))
54{
55 d->mNumber = cleanupNumber(number);
56}
57
59 : d(other.d)
60{
61}
62
66
67bool PhoneNumber::operator==(const PhoneNumber &other) const
68{
69 if (d->mId != other.d->mId) {
70 return false;
71 }
72
73 if (d->mNumber != other.d->mNumber) {
74 return false;
75 }
76
77 if (d->mType != other.d->mType) {
78 return false;
79 }
80
81 if (d->mParamMap != other.d->mParamMap) {
82 return false;
83 }
84
85 return true;
86}
87
88bool PhoneNumber::operator!=(const PhoneNumber &other) const
89{
90 return !(other == *this);
91}
92
94{
95 if (this != &other) {
96 d = other.d;
97 }
98
99 return *this;
100}
101
102bool PhoneNumber::isEmpty() const
103{
104 return d->mNumber.isEmpty();
105}
106
108{
109 d->mId = id;
110}
111
112QString PhoneNumber::id() const
113{
114 return d->mId;
115}
116
118{
119 d->mNumber = cleanupNumber(number);
120}
121
122QString PhoneNumber::number() const
123{
124 return d->mNumber;
125}
126
127QString PhoneNumber::normalizedNumber() const
128{
129 QString result;
130 result.reserve(d->mNumber.size());
131 for (const auto &c : d->mNumber) {
132 if (c.isDigit() || (c == QLatin1Char('+') && result.isEmpty())) {
133 result.push_back(c);
134 }
135 }
136 return result;
137}
138
140{
141 d->mType = type;
142}
143
144PhoneNumber::Type PhoneNumber::type() const
145{
146 return d->mType;
147}
148
149QString PhoneNumber::typeLabel() const
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
204QString PhoneNumber::typeLabel(Type type)
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 for (const auto f : list) {
232 // these are actually flags
233 const TypeFlag flag = static_cast<TypeFlag>(static_cast<int>(f));
234 if (type & flag) {
235 if (!first) {
236 label.append(QLatin1Char('/'));
237 }
238
239 label.append(typeFlagLabel(flag));
240
241 if (first) {
242 first = false;
243 }
244 }
245 }
246
247 return label;
248}
249
250bool PhoneNumber::isPreferred() const
251{
252 return type() & Pref;
253}
254
255bool PhoneNumber::supportsSms() const
256{
257 return type() & Cell;
258}
259
261{
262 QString str = QLatin1String("PhoneNumber {\n");
263 str += QStringLiteral(" Id: %1\n").arg(d->mId);
264 str += QStringLiteral(" Type: %1\n").arg(typeLabel(d->mType));
265 str = d->mParamMap.toString();
266 str += QStringLiteral(" Number: %1\n").arg(d->mNumber);
267 str += QLatin1String("}\n");
268
269 return str;
270}
271
272void PhoneNumber::setParams(const ParameterMap &params)
273{
274 d->mParamMap = params;
275}
276
277ParameterMap PhoneNumber::params() const
278{
279 return d->mParamMap;
280}
281
282QDataStream &KContacts::operator<<(QDataStream &s, const PhoneNumber &phone)
283{
284 return s << phone.d->mId << static_cast<uint>(phone.d->mType) << phone.d->mNumber << phone.d->mParamMap;
285}
286
287QDataStream &KContacts::operator>>(QDataStream &s, PhoneNumber &phone)
288{
289 uint type;
290 s >> phone.d->mId >> type >> phone.d->mNumber >> phone.d->mParamMap;
291 phone.d->mType = PhoneNumber::Type(type);
292
293 return s;
294}
295
296#include "moc_phonenumber.cpp"
Phonenumber information.
Definition phonenumber.h:31
void setType(Type type)
Sets the type.
void setId(const QString &identifier)
Sets the unique identifier.
void setNumber(const QString &number)
Sets the phone number.
PhoneNumber()
Creates an empty phone number object.
TypeFlag
Phone number types.
Definition phonenumber.h:51
@ Work
Office number.
Definition phonenumber.h:53
@ Isdn
ISDN connection.
Definition phonenumber.h:63
@ Video
Video phone.
Definition phonenumber.h:59
@ Pcs
Personal Communication Service.
Definition phonenumber.h:64
@ Pref
Preferred number.
Definition phonenumber.h:55
bool operator==(const PhoneNumber &other) const
Equality operator.
static TypeList typeList()
Returns a list of all available types.
QString toString() const
Returns a string representation of the phone number.
static QString typeFlagLabel(TypeFlag type)
Returns the translated label for phone number type.
bool operator!=(const PhoneNumber &other) const
Not-Equal operator.
PhoneNumber & operator=(const PhoneNumber &other)
Assignment operator.
~PhoneNumber()
Destroys the phone number.
QFlags< TypeFlag > Type
Stores a combination of TypeFlag values.
Definition phonenumber.h:73
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
KCOREADDONS_EXPORT QString randomString(int length)
bool isEmpty() const const
QString & append(QChar ch)
QString arg(Args &&... args) const const
bool isEmpty() const const
void push_back(QChar ch)
void reserve(qsizetype size)
QString simplified() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:06:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.