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 | |
17 | using namespace KContacts; |
18 | |
19 | static QString cleanupNumber(const QString &input) |
20 | { |
21 | return input.simplified(); |
22 | } |
23 | |
24 | class Q_DECL_HIDDEN PhoneNumber::Private : public QSharedData |
25 | { |
26 | public: |
27 | Private(Type type) |
28 | : mId(KRandom::randomString(length: 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 | |
47 | PhoneNumber::PhoneNumber() |
48 | : d(new Private(Home)) |
49 | { |
50 | } |
51 | |
52 | PhoneNumber::PhoneNumber(const QString &number, Type type) |
53 | : d(new Private(type)) |
54 | { |
55 | d->mNumber = cleanupNumber(input: number); |
56 | } |
57 | |
58 | PhoneNumber::PhoneNumber(const PhoneNumber &other) |
59 | : d(other.d) |
60 | { |
61 | } |
62 | |
63 | PhoneNumber::~PhoneNumber() |
64 | { |
65 | } |
66 | |
67 | bool 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 | |
88 | bool PhoneNumber::operator!=(const PhoneNumber &other) const |
89 | { |
90 | return !(other == *this); |
91 | } |
92 | |
93 | PhoneNumber &PhoneNumber::operator=(const PhoneNumber &other) |
94 | { |
95 | if (this != &other) { |
96 | d = other.d; |
97 | } |
98 | |
99 | return *this; |
100 | } |
101 | |
102 | bool PhoneNumber::isEmpty() const |
103 | { |
104 | return d->mNumber.isEmpty(); |
105 | } |
106 | |
107 | void PhoneNumber::setId(const QString &id) |
108 | { |
109 | d->mId = id; |
110 | } |
111 | |
112 | QString PhoneNumber::id() const |
113 | { |
114 | return d->mId; |
115 | } |
116 | |
117 | void PhoneNumber::setNumber(const QString &number) |
118 | { |
119 | d->mNumber = cleanupNumber(input: number); |
120 | } |
121 | |
122 | QString PhoneNumber::number() const |
123 | { |
124 | return d->mNumber; |
125 | } |
126 | |
127 | QString PhoneNumber::normalizedNumber() const |
128 | { |
129 | QString result; |
130 | result.reserve(asize: 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 | |
139 | void PhoneNumber::setType(Type type) |
140 | { |
141 | d->mType = type; |
142 | } |
143 | |
144 | PhoneNumber::Type PhoneNumber::type() const |
145 | { |
146 | return d->mType; |
147 | } |
148 | |
149 | QString PhoneNumber::typeLabel() const |
150 | { |
151 | return typeLabel(type: type()); |
152 | } |
153 | |
154 | PhoneNumber::TypeList PhoneNumber::typeList() |
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 | |
166 | QString PhoneNumber::typeFlagLabel(TypeFlag type) |
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 | |
204 | QString 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(c: QLatin1Char('/')); |
237 | } |
238 | |
239 | label.append(s: typeFlagLabel(type: flag)); |
240 | |
241 | if (first) { |
242 | first = false; |
243 | } |
244 | } |
245 | } |
246 | |
247 | return label; |
248 | } |
249 | |
250 | bool PhoneNumber::isPreferred() const |
251 | { |
252 | return type() & Pref; |
253 | } |
254 | |
255 | bool PhoneNumber::supportsSms() const |
256 | { |
257 | return type() & Cell; |
258 | } |
259 | |
260 | QString PhoneNumber::toString() const |
261 | { |
262 | QString str = QLatin1String("PhoneNumber {\n" ); |
263 | str += QStringLiteral(" Id: %1\n" ).arg(a: d->mId); |
264 | str += QStringLiteral(" Type: %1\n" ).arg(a: typeLabel(type: d->mType)); |
265 | str = d->mParamMap.toString(); |
266 | str += QStringLiteral(" Number: %1\n" ).arg(a: d->mNumber); |
267 | str += QLatin1String("}\n" ); |
268 | |
269 | return str; |
270 | } |
271 | |
272 | void PhoneNumber::setParams(const ParameterMap ¶ms) |
273 | { |
274 | d->mParamMap = params; |
275 | } |
276 | |
277 | ParameterMap PhoneNumber::params() const |
278 | { |
279 | return d->mParamMap; |
280 | } |
281 | |
282 | QDataStream &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 | |
287 | QDataStream &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" |
297 | |