-rw-r--r-- | kabc/address.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/kabc/address.cpp b/kabc/address.cpp index 5ffe511..6151f9e 100644 --- a/kabc/address.cpp +++ b/kabc/address.cpp | |||
@@ -1,664 +1,691 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libkabc. | 2 | This file is part of libkabc. |
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | 3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public | 6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. | 8 | version 2 of the License, or (at your option) any later version. |
9 | 9 | ||
10 | This library is distributed in the hope that it will be useful, | 10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. | 13 | Library General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to | 16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /* | 21 | /* |
22 | Enhanced Version of the file for platform independent KDE tools. | 22 | Enhanced Version of the file for platform independent KDE tools. |
23 | Copyright (c) 2004 Ulf Schenk | 23 | Copyright (c) 2004 Ulf Schenk |
24 | 24 | ||
25 | $Id$ | 25 | $Id$ |
26 | */ | 26 | */ |
27 | 27 | ||
28 | //US added kglobal.h | 28 | //US added kglobal.h |
29 | #include <kglobal.h> | 29 | #include <kglobal.h> |
30 | 30 | ||
31 | #include <kapplication.h> | 31 | #include <kapplication.h> |
32 | #include <kdebug.h> | 32 | #include <kdebug.h> |
33 | #include <klocale.h> | 33 | #include <klocale.h> |
34 | #include <ksimpleconfig.h> | 34 | #include <ksimpleconfig.h> |
35 | #include <kstandarddirs.h> | 35 | #include <kstandarddirs.h> |
36 | 36 | ||
37 | #include <qfile.h> | 37 | #include <qfile.h> |
38 | 38 | ||
39 | #include "address.h" | 39 | #include "address.h" |
40 | 40 | ||
41 | using namespace KABC; | 41 | using namespace KABC; |
42 | 42 | ||
43 | QMap<QString, QString> Address::mISOMap; | 43 | QMap<QString, QString> Address::mISOMap; |
44 | 44 | ||
45 | Address::Address() : | 45 | Address::Address() : |
46 | mEmpty( true ), mType( 0 ) | 46 | mEmpty( true ), mType( 0 ) |
47 | { | 47 | { |
48 | mId = KApplication::randomString( 10 ); | 48 | mId = KApplication::randomString( 10 ); |
49 | } | 49 | } |
50 | 50 | ||
51 | Address::Address( int type ) : | 51 | Address::Address( int type ) : |
52 | mEmpty( true ), mType( type ) | 52 | mEmpty( true ), mType( type ) |
53 | { | 53 | { |
54 | mId = KApplication::randomString( 10 ); | 54 | mId = KApplication::randomString( 10 ); |
55 | } | 55 | } |
56 | 56 | ||
57 | bool Address::operator==( const Address &a ) const | 57 | bool Address::operator==( const Address &a ) const |
58 | { | 58 | { |
59 | if ( mPostOfficeBox != a.mPostOfficeBox ) return false; | 59 | if ( mPostOfficeBox != a.mPostOfficeBox ) return false; |
60 | if ( mExtended != a.mExtended ) return false; | 60 | if ( mExtended != a.mExtended ) return false; |
61 | if ( mStreet != a.mStreet ) return false; | 61 | if ( mStreet != a.mStreet ) return false; |
62 | if ( mLocality != a.mLocality ) return false; | 62 | if ( mLocality != a.mLocality ) return false; |
63 | if ( mRegion != a.mRegion ) return false; | 63 | if ( mRegion != a.mRegion ) return false; |
64 | if ( mPostalCode != a.mPostalCode ) return false; | 64 | if ( mPostalCode != a.mPostalCode ) return false; |
65 | if ( mCountry != a.mCountry ) return false; | 65 | if ( mCountry != a.mCountry ) return false; |
66 | if ( mLabel != a.mLabel ) return false; | 66 | if ( mLabel != a.mLabel ) return false; |
67 | 67 | ||
68 | return true; | 68 | return true; |
69 | } | 69 | } |
70 | 70 | ||
71 | bool Address::operator!=( const Address &a ) const | 71 | bool Address::operator!=( const Address &a ) const |
72 | { | 72 | { |
73 | return !( a == *this ); | 73 | return !( a == *this ); |
74 | } | 74 | } |
75 | 75 | ||
76 | bool Address::isEmpty() const | 76 | bool Address::isEmpty() const |
77 | { | 77 | { |
78 | if ( mPostOfficeBox.isEmpty() && | 78 | if ( mPostOfficeBox.isEmpty() && |
79 | mExtended.isEmpty() && | 79 | mExtended.isEmpty() && |
80 | mStreet.isEmpty() && | 80 | mStreet.isEmpty() && |
81 | mLocality.isEmpty() && | 81 | mLocality.isEmpty() && |
82 | mRegion.isEmpty() && | 82 | mRegion.isEmpty() && |
83 | mPostalCode.isEmpty() && | 83 | mPostalCode.isEmpty() && |
84 | mCountry.isEmpty() && | 84 | mCountry.isEmpty() && |
85 | mLabel.isEmpty() ) { | 85 | mLabel.isEmpty() ) { |
86 | return true; | 86 | return true; |
87 | } | 87 | } |
88 | return false; | 88 | return false; |
89 | } | 89 | } |
90 | 90 | ||
91 | QStringList Address::asList() | 91 | QStringList Address::asList() |
92 | { | 92 | { |
93 | QStringList result; | 93 | QStringList result; |
94 | if ( ! mPostOfficeBox.isEmpty() )result.append(mPostOfficeBox); | 94 | if ( ! mPostOfficeBox.isEmpty() )result.append(mPostOfficeBox); |
95 | if ( ! mExtended.isEmpty())result.append(mExtended); | 95 | if ( ! mExtended.isEmpty())result.append(mExtended); |
96 | if ( ! mStreet.isEmpty())result.append(mStreet); | 96 | if ( ! mStreet.isEmpty())result.append(mStreet); |
97 | if ( ! mLocality.isEmpty() )result.append(mLocality); | 97 | if ( ! mLocality.isEmpty() )result.append(mLocality); |
98 | if ( ! mRegion.isEmpty())result.append(mRegion); | 98 | if ( ! mRegion.isEmpty())result.append(mRegion); |
99 | if ( ! mPostalCode.isEmpty())result.append(mPostalCode); | 99 | if ( ! mPostalCode.isEmpty())result.append(mPostalCode); |
100 | if ( ! mCountry.isEmpty())result.append(mCountry); | 100 | if ( ! mCountry.isEmpty())result.append(mCountry); |
101 | if ( ! mLabel.isEmpty() )result.append(mLabel); | 101 | if ( ! mLabel.isEmpty() )result.append(mLabel); |
102 | return result; | 102 | return result; |
103 | } | 103 | } |
104 | |||
105 | bool Address::matchAddress( QRegExp* re ) const | ||
106 | { | ||
107 | |||
108 | #if QT_VERSION >= 0x030000 | ||
109 | if (re->search( mPostOfficeBox ) == 0) return true; | ||
110 | if (re->search( mExtended ) == 0) return true; | ||
111 | if (re->search( mStreet ) == 0) return true; | ||
112 | if (re->search( mLocality ) == 0) return true; | ||
113 | if (re->search( mRegion ) == 0) return true; | ||
114 | if (re->search( mPostalCode ) == 0) return true; | ||
115 | if (re->search( mCountry ) == 0) return true; | ||
116 | if (re->search( mLabel ) == 0) return true; | ||
117 | #else | ||
118 | if (re->match( mPostOfficeBox ) == 0) return true; | ||
119 | if (re->match( mExtended ) == 0) return true; | ||
120 | if (re->match( mStreet ) == 0) return true; | ||
121 | if (re->match( mLocality ) == 0) return true; | ||
122 | if (re->match( mRegion ) == 0) return true; | ||
123 | if (re->match( mPostalCode ) == 0) return true; | ||
124 | if (re->match( mCountry ) == 0) return true; | ||
125 | if (re->match( mLabel ) == 0) return true; | ||
126 | #endif | ||
127 | |||
128 | return false; | ||
129 | } | ||
130 | |||
104 | void Address::clear() | 131 | void Address::clear() |
105 | { | 132 | { |
106 | *this = Address(); | 133 | *this = Address(); |
107 | } | 134 | } |
108 | 135 | ||
109 | void Address::setId( const QString &id ) | 136 | void Address::setId( const QString &id ) |
110 | { | 137 | { |
111 | mEmpty = false; | 138 | mEmpty = false; |
112 | 139 | ||
113 | mId = id; | 140 | mId = id; |
114 | } | 141 | } |
115 | 142 | ||
116 | QString Address::id() const | 143 | QString Address::id() const |
117 | { | 144 | { |
118 | return mId; | 145 | return mId; |
119 | } | 146 | } |
120 | 147 | ||
121 | void Address::setType( int type ) | 148 | void Address::setType( int type ) |
122 | { | 149 | { |
123 | mEmpty = false; | 150 | mEmpty = false; |
124 | 151 | ||
125 | mType = type; | 152 | mType = type; |
126 | } | 153 | } |
127 | 154 | ||
128 | int Address::type() const | 155 | int Address::type() const |
129 | { | 156 | { |
130 | return mType; | 157 | return mType; |
131 | } | 158 | } |
132 | 159 | ||
133 | QString Address::typeLabel() const | 160 | QString Address::typeLabel() const |
134 | { | 161 | { |
135 | QString label; | 162 | QString label; |
136 | bool first = true; | 163 | bool first = true; |
137 | 164 | ||
138 | TypeList list = typeList(); | 165 | TypeList list = typeList(); |
139 | 166 | ||
140 | TypeList::Iterator it; | 167 | TypeList::Iterator it; |
141 | for ( it = list.begin(); it != list.end(); ++it ) { | 168 | for ( it = list.begin(); it != list.end(); ++it ) { |
142 | if ( ( type() & (*it) ) && ( (*it) != Pref ) ) { | 169 | if ( ( type() & (*it) ) && ( (*it) != Pref ) ) { |
143 | label.append( ( first ? "" : "/" ) + typeLabel( *it ) ); | 170 | label.append( ( first ? "" : "/" ) + typeLabel( *it ) ); |
144 | if ( first ) | 171 | if ( first ) |
145 | first = false; | 172 | first = false; |
146 | } | 173 | } |
147 | } | 174 | } |
148 | 175 | ||
149 | return label; | 176 | return label; |
150 | } | 177 | } |
151 | 178 | ||
152 | void Address::setPostOfficeBox( const QString &s ) | 179 | void Address::setPostOfficeBox( const QString &s ) |
153 | { | 180 | { |
154 | mEmpty = false; | 181 | mEmpty = false; |
155 | 182 | ||
156 | mPostOfficeBox = s; | 183 | mPostOfficeBox = s; |
157 | } | 184 | } |
158 | 185 | ||
159 | QString Address::postOfficeBox() const | 186 | QString Address::postOfficeBox() const |
160 | { | 187 | { |
161 | return mPostOfficeBox; | 188 | return mPostOfficeBox; |
162 | } | 189 | } |
163 | 190 | ||
164 | QString Address::postOfficeBoxLabel() | 191 | QString Address::postOfficeBoxLabel() |
165 | { | 192 | { |
166 | return i18n("Post Office Box"); | 193 | return i18n("Post Office Box"); |
167 | } | 194 | } |
168 | 195 | ||
169 | 196 | ||
170 | void Address::setExtended( const QString &s ) | 197 | void Address::setExtended( const QString &s ) |
171 | { | 198 | { |
172 | mEmpty = false; | 199 | mEmpty = false; |
173 | 200 | ||
174 | mExtended = s; | 201 | mExtended = s; |
175 | } | 202 | } |
176 | 203 | ||
177 | QString Address::extended() const | 204 | QString Address::extended() const |
178 | { | 205 | { |
179 | return mExtended; | 206 | return mExtended; |
180 | } | 207 | } |
181 | 208 | ||
182 | QString Address::extendedLabel() | 209 | QString Address::extendedLabel() |
183 | { | 210 | { |
184 | return i18n("Extended Address Information"); | 211 | return i18n("Extended Address Information"); |
185 | } | 212 | } |
186 | 213 | ||
187 | 214 | ||
188 | void Address::setStreet( const QString &s ) | 215 | void Address::setStreet( const QString &s ) |
189 | { | 216 | { |
190 | mEmpty = false; | 217 | mEmpty = false; |
191 | 218 | ||
192 | mStreet = s; | 219 | mStreet = s; |
193 | } | 220 | } |
194 | 221 | ||
195 | QString Address::street() const | 222 | QString Address::street() const |
196 | { | 223 | { |
197 | return mStreet; | 224 | return mStreet; |
198 | } | 225 | } |
199 | 226 | ||
200 | QString Address::streetLabel() | 227 | QString Address::streetLabel() |
201 | { | 228 | { |
202 | return i18n("Street"); | 229 | return i18n("Street"); |
203 | } | 230 | } |
204 | 231 | ||
205 | 232 | ||
206 | void Address::setLocality( const QString &s ) | 233 | void Address::setLocality( const QString &s ) |
207 | { | 234 | { |
208 | mEmpty = false; | 235 | mEmpty = false; |
209 | 236 | ||
210 | mLocality = s; | 237 | mLocality = s; |
211 | } | 238 | } |
212 | 239 | ||
213 | QString Address::locality() const | 240 | QString Address::locality() const |
214 | { | 241 | { |
215 | return mLocality; | 242 | return mLocality; |
216 | } | 243 | } |
217 | 244 | ||
218 | QString Address::localityLabel() | 245 | QString Address::localityLabel() |
219 | { | 246 | { |
220 | return i18n("Locality"); | 247 | return i18n("Locality"); |
221 | } | 248 | } |
222 | 249 | ||
223 | 250 | ||
224 | void Address::setRegion( const QString &s ) | 251 | void Address::setRegion( const QString &s ) |
225 | { | 252 | { |
226 | mEmpty = false; | 253 | mEmpty = false; |
227 | 254 | ||
228 | mRegion = s; | 255 | mRegion = s; |
229 | } | 256 | } |
230 | 257 | ||
231 | QString Address::region() const | 258 | QString Address::region() const |
232 | { | 259 | { |
233 | return mRegion; | 260 | return mRegion; |
234 | } | 261 | } |
235 | 262 | ||
236 | QString Address::regionLabel() | 263 | QString Address::regionLabel() |
237 | { | 264 | { |
238 | return i18n("Region"); | 265 | return i18n("Region"); |
239 | } | 266 | } |
240 | 267 | ||
241 | 268 | ||
242 | void Address::setPostalCode( const QString &s ) | 269 | void Address::setPostalCode( const QString &s ) |
243 | { | 270 | { |
244 | mEmpty = false; | 271 | mEmpty = false; |
245 | 272 | ||
246 | mPostalCode = s; | 273 | mPostalCode = s; |
247 | } | 274 | } |
248 | 275 | ||
249 | QString Address::postalCode() const | 276 | QString Address::postalCode() const |
250 | { | 277 | { |
251 | return mPostalCode; | 278 | return mPostalCode; |
252 | } | 279 | } |
253 | 280 | ||
254 | QString Address::postalCodeLabel() | 281 | QString Address::postalCodeLabel() |
255 | { | 282 | { |
256 | return i18n("Postal Code"); | 283 | return i18n("Postal Code"); |
257 | } | 284 | } |
258 | 285 | ||
259 | 286 | ||
260 | void Address::setCountry( const QString &s ) | 287 | void Address::setCountry( const QString &s ) |
261 | { | 288 | { |
262 | mEmpty = false; | 289 | mEmpty = false; |
263 | 290 | ||
264 | mCountry = s; | 291 | mCountry = s; |
265 | } | 292 | } |
266 | 293 | ||
267 | QString Address::country() const | 294 | QString Address::country() const |
268 | { | 295 | { |
269 | return mCountry; | 296 | return mCountry; |
270 | } | 297 | } |
271 | 298 | ||
272 | QString Address::countryLabel() | 299 | QString Address::countryLabel() |
273 | { | 300 | { |
274 | return i18n("Country"); | 301 | return i18n("Country"); |
275 | } | 302 | } |
276 | 303 | ||
277 | 304 | ||
278 | void Address::setLabel( const QString &s ) | 305 | void Address::setLabel( const QString &s ) |
279 | { | 306 | { |
280 | mEmpty = false; | 307 | mEmpty = false; |
281 | 308 | ||
282 | mLabel = s; | 309 | mLabel = s; |
283 | } | 310 | } |
284 | 311 | ||
285 | QString Address::label() const | 312 | QString Address::label() const |
286 | { | 313 | { |
287 | return mLabel; | 314 | return mLabel; |
288 | } | 315 | } |
289 | 316 | ||
290 | QString Address::labelLabel() | 317 | QString Address::labelLabel() |
291 | { | 318 | { |
292 | return i18n("Delivery Label"); | 319 | return i18n("Delivery Label"); |
293 | } | 320 | } |
294 | 321 | ||
295 | Address::TypeList Address::typeList() | 322 | Address::TypeList Address::typeList() |
296 | { | 323 | { |
297 | TypeList list; | 324 | TypeList list; |
298 | 325 | ||
299 | list << Dom << Intl << Postal << Parcel << Home << Work << Pref; | 326 | list << Dom << Intl << Postal << Parcel << Home << Work << Pref; |
300 | 327 | ||
301 | return list; | 328 | return list; |
302 | } | 329 | } |
303 | 330 | ||
304 | QString Address::typeLabel( int type ) | 331 | QString Address::typeLabel( int type ) |
305 | { | 332 | { |
306 | QString label; | 333 | QString label; |
307 | if ( type & Dom ) | 334 | if ( type & Dom ) |
308 | label += i18n("Domestic")+" "; | 335 | label += i18n("Domestic")+" "; |
309 | if ( type & Intl ) | 336 | if ( type & Intl ) |
310 | label += i18n("International")+" "; | 337 | label += i18n("International")+" "; |
311 | if ( type & Postal ) | 338 | if ( type & Postal ) |
312 | label += i18n("Postal")+" "; | 339 | label += i18n("Postal")+" "; |
313 | if ( type & Parcel ) | 340 | if ( type & Parcel ) |
314 | label += i18n("Parcel")+" "; | 341 | label += i18n("Parcel")+" "; |
315 | if ( type & Work ) | 342 | if ( type & Work ) |
316 | label += i18n("Work Address", "Work")+" "; | 343 | label += i18n("Work Address", "Work")+" "; |
317 | if ( type & Home ) | 344 | if ( type & Home ) |
318 | label += i18n("Home Address", "Home") +" "; | 345 | label += i18n("Home Address", "Home") +" "; |
319 | if ( type & Pref ) | 346 | if ( type & Pref ) |
320 | label += i18n("Preferred Address", "(p)"); | 347 | label += i18n("Preferred Address", "(p)"); |
321 | if ( label.isEmpty() ) | 348 | if ( label.isEmpty() ) |
322 | label = i18n("Other"); | 349 | label = i18n("Other"); |
323 | return label; | 350 | return label; |
324 | 351 | ||
325 | #if 0 | 352 | #if 0 |
326 | switch ( type ) { | 353 | switch ( type ) { |
327 | case Dom: | 354 | case Dom: |
328 | return i18n("Domestic"); | 355 | return i18n("Domestic"); |
329 | break; | 356 | break; |
330 | case Intl: | 357 | case Intl: |
331 | return i18n("International"); | 358 | return i18n("International"); |
332 | break; | 359 | break; |
333 | case Postal: | 360 | case Postal: |
334 | return i18n("Postal"); | 361 | return i18n("Postal"); |
335 | break; | 362 | break; |
336 | case Parcel: | 363 | case Parcel: |
337 | return i18n("Parcel"); | 364 | return i18n("Parcel"); |
338 | break; | 365 | break; |
339 | case Home: | 366 | case Home: |
340 | return i18n("Home Address", "Home"); | 367 | return i18n("Home Address", "Home"); |
341 | break; | 368 | break; |
342 | case Work: | 369 | case Work: |
343 | return i18n("Work Address", "Work"); | 370 | return i18n("Work Address", "Work"); |
344 | break; | 371 | break; |
345 | case Pref: | 372 | case Pref: |
346 | return i18n("Preferred Address"); | 373 | return i18n("Preferred Address"); |
347 | break; | 374 | break; |
348 | default: | 375 | default: |
349 | return i18n("Other"); | 376 | return i18n("Other"); |
350 | break; | 377 | break; |
351 | } | 378 | } |
352 | #endif | 379 | #endif |
353 | } | 380 | } |
354 | 381 | ||
355 | void Address::dump() const | 382 | void Address::dump() const |
356 | { | 383 | { |
357 | qDebug("Address::dump() +++++++++++++++++ "); | 384 | qDebug("Address::dump() +++++++++++++++++ "); |
358 | #if 0 | 385 | #if 0 |
359 | kdDebug(5700) << " Address {" << endl; | 386 | kdDebug(5700) << " Address {" << endl; |
360 | kdDebug(5700) << " Id: " << id() << endl; | 387 | kdDebug(5700) << " Id: " << id() << endl; |
361 | kdDebug(5700) << " Extended: " << extended() << endl; | 388 | kdDebug(5700) << " Extended: " << extended() << endl; |
362 | kdDebug(5700) << " Street: " << street() << endl; | 389 | kdDebug(5700) << " Street: " << street() << endl; |
363 | kdDebug(5700) << " Postal Code: " << postalCode() << endl; | 390 | kdDebug(5700) << " Postal Code: " << postalCode() << endl; |
364 | kdDebug(5700) << " Locality: " << locality() << endl; | 391 | kdDebug(5700) << " Locality: " << locality() << endl; |
365 | kdDebug(5700) << " }" << endl; | 392 | kdDebug(5700) << " }" << endl; |
366 | #endif | 393 | #endif |
367 | } | 394 | } |
368 | 395 | ||
369 | 396 | ||
370 | QString Address::formattedAddress( const QString &realName | 397 | QString Address::formattedAddress( const QString &realName |
371 | , const QString &orgaName ) const | 398 | , const QString &orgaName ) const |
372 | { | 399 | { |
373 | QString ciso; | 400 | QString ciso; |
374 | QString addrTemplate; | 401 | QString addrTemplate; |
375 | QString ret; | 402 | QString ret; |
376 | 403 | ||
377 | // ************************************************************** | 404 | // ************************************************************** |
378 | // LR: currently we have no iso handling - we will format the address manually here | 405 | // LR: currently we have no iso handling - we will format the address manually here |
379 | 406 | ||
380 | QString text; | 407 | QString text; |
381 | if ( !street().isEmpty() ) | 408 | if ( !street().isEmpty() ) |
382 | text += street() + "\n"; | 409 | text += street() + "\n"; |
383 | 410 | ||
384 | if ( !postOfficeBox().isEmpty() ) | 411 | if ( !postOfficeBox().isEmpty() ) |
385 | text += postOfficeBox() + "\n"; | 412 | text += postOfficeBox() + "\n"; |
386 | 413 | ||
387 | text += locality() + QString(" ") + region(); | 414 | text += locality() + QString(" ") + region(); |
388 | 415 | ||
389 | if ( !postalCode().isEmpty() ) | 416 | if ( !postalCode().isEmpty() ) |
390 | text += QString(", ") + postalCode(); | 417 | text += QString(", ") + postalCode(); |
391 | 418 | ||
392 | text += "\n"; | 419 | text += "\n"; |
393 | 420 | ||
394 | if ( !country().isEmpty() ) | 421 | if ( !country().isEmpty() ) |
395 | text += country() + "\n"; | 422 | text += country() + "\n"; |
396 | 423 | ||
397 | text += extended(); | 424 | text += extended(); |
398 | 425 | ||
399 | 426 | ||
400 | return text; | 427 | return text; |
401 | // ************************************************************** | 428 | // ************************************************************** |
402 | 429 | ||
403 | // FIXME: first check for iso-country-field and prefer that one | 430 | // FIXME: first check for iso-country-field and prefer that one |
404 | if ( !country().isEmpty() ) { | 431 | if ( !country().isEmpty() ) { |
405 | ciso = countryToISO( country() ); | 432 | ciso = countryToISO( country() ); |
406 | } else { | 433 | } else { |
407 | // fall back to our own country | 434 | // fall back to our own country |
408 | ciso = KGlobal::locale()->country(); | 435 | ciso = KGlobal::locale()->country(); |
409 | } | 436 | } |
410 | //qDebug("ciso %s ",ciso.latin1() ); | 437 | //qDebug("ciso %s ",ciso.latin1() ); |
411 | KSimpleConfig entry( locate( "locale", | 438 | KSimpleConfig entry( locate( "locale", |
412 | QString( "l10n/" ) + ciso + QString( "/entry.desktop" ) ) ); | 439 | QString( "l10n/" ) + ciso + QString( "/entry.desktop" ) ) ); |
413 | entry.setGroup( "KCM Locale" ); | 440 | entry.setGroup( "KCM Locale" ); |
414 | 441 | ||
415 | // decide whether this needs special business address formatting | 442 | // decide whether this needs special business address formatting |
416 | if ( orgaName.isNull() ) { | 443 | if ( orgaName.isNull() ) { |
417 | addrTemplate = entry.readEntry( "AddressFormat" ); | 444 | addrTemplate = entry.readEntry( "AddressFormat" ); |
418 | } else { | 445 | } else { |
419 | addrTemplate = entry.readEntry( "BusinessAddressFormat" ); | 446 | addrTemplate = entry.readEntry( "BusinessAddressFormat" ); |
420 | if ( addrTemplate.isEmpty() ) | 447 | if ( addrTemplate.isEmpty() ) |
421 | addrTemplate = entry.readEntry( "AddressFormat" ); | 448 | addrTemplate = entry.readEntry( "AddressFormat" ); |
422 | } | 449 | } |
423 | 450 | ||
424 | // in the case there's no format found at all, default to what we've always | 451 | // in the case there's no format found at all, default to what we've always |
425 | // used: | 452 | // used: |
426 | if ( addrTemplate.isEmpty() ) { | 453 | if ( addrTemplate.isEmpty() ) { |
427 | qDebug("address format database incomplete****************** "); | 454 | qDebug("address format database incomplete****************** "); |
428 | kdWarning(5700) << "address format database incomplete " | 455 | kdWarning(5700) << "address format database incomplete " |
429 | << "(no format for locale " << ciso | 456 | << "(no format for locale " << ciso |
430 | << " found). Using default address formatting." << endl; | 457 | << " found). Using default address formatting." << endl; |
431 | addrTemplate = "%0(%n\\n)%0(%cm\\n)%0(%s\\n)%0(PO BOX %p\\n)%0(%l%w%r)%,%z"; | 458 | addrTemplate = "%0(%n\\n)%0(%cm\\n)%0(%s\\n)%0(PO BOX %p\\n)%0(%l%w%r)%,%z"; |
432 | } | 459 | } |
433 | 460 | ||
434 | // scan | 461 | // scan |
435 | parseAddressTemplateSection( addrTemplate, ret, realName, orgaName ); | 462 | parseAddressTemplateSection( addrTemplate, ret, realName, orgaName ); |
436 | 463 | ||
437 | // now add the country line if needed (formatting this time according to | 464 | // now add the country line if needed (formatting this time according to |
438 | // the rules of our own system country ) | 465 | // the rules of our own system country ) |
439 | if ( !country().isEmpty() ) { | 466 | if ( !country().isEmpty() ) { |
440 | KSimpleConfig entry( locate( "locale", QString( "l10n/" ) | 467 | KSimpleConfig entry( locate( "locale", QString( "l10n/" ) |
441 | + KGlobal::locale()->country() + QString( "/entry.desktop" ) ) ); | 468 | + KGlobal::locale()->country() + QString( "/entry.desktop" ) ) ); |
442 | entry.setGroup( "KCM Locale" ); | 469 | entry.setGroup( "KCM Locale" ); |
443 | QString cpos = entry.readEntry( "AddressCountryPosition" ); | 470 | QString cpos = entry.readEntry( "AddressCountryPosition" ); |
444 | if ( "BELOW" == cpos || cpos.isEmpty() ) { | 471 | if ( "BELOW" == cpos || cpos.isEmpty() ) { |
445 | ret = ret + "\n\n" + country().upper(); | 472 | ret = ret + "\n\n" + country().upper(); |
446 | } else if ( "below" == cpos ) { | 473 | } else if ( "below" == cpos ) { |
447 | ret = ret + "\n\n" + country(); | 474 | ret = ret + "\n\n" + country(); |
448 | } else if ( "ABOVE" == cpos ) { | 475 | } else if ( "ABOVE" == cpos ) { |
449 | ret = country().upper() + "\n\n" + ret; | 476 | ret = country().upper() + "\n\n" + ret; |
450 | } else if ( "above" == cpos ) { | 477 | } else if ( "above" == cpos ) { |
451 | ret = country() + "\n\n" + ret; | 478 | ret = country() + "\n\n" + ret; |
452 | } | 479 | } |
453 | } | 480 | } |
454 | 481 | ||
455 | return ret; | 482 | return ret; |
456 | } | 483 | } |
457 | 484 | ||
458 | bool Address::parseAddressTemplateSection( const QString &tsection, | 485 | bool Address::parseAddressTemplateSection( const QString &tsection, |
459 | QString &result, const QString &realName, const QString &orgaName ) const | 486 | QString &result, const QString &realName, const QString &orgaName ) const |
460 | { | 487 | { |
461 | // This method first parses and substitutes any bracketed sections and | 488 | // This method first parses and substitutes any bracketed sections and |
462 | // after that replaces any tags with their values. If a bracketed section | 489 | // after that replaces any tags with their values. If a bracketed section |
463 | // or a tag evaluate to zero, they are not just removed but replaced | 490 | // or a tag evaluate to zero, they are not just removed but replaced |
464 | // with a placeholder. This is because in the last step conditionals are | 491 | // with a placeholder. This is because in the last step conditionals are |
465 | // resolved which depend on information about zero-evaluations. | 492 | // resolved which depend on information about zero-evaluations. |
466 | result = tsection; | 493 | result = tsection; |
467 | int stpos = 0; | 494 | int stpos = 0; |
468 | bool ret = false; | 495 | bool ret = false; |
469 | 496 | ||
470 | // first check for brackets that have to be evaluated first | 497 | // first check for brackets that have to be evaluated first |
471 | int fpos = result.find( KABC_FMTTAG_purgeempty, stpos ); | 498 | int fpos = result.find( KABC_FMTTAG_purgeempty, stpos ); |
472 | while ( -1 != fpos ) { | 499 | while ( -1 != fpos ) { |
473 | int bpos1 = fpos + KABC_FMTTAG_purgeempty.length(); | 500 | int bpos1 = fpos + KABC_FMTTAG_purgeempty.length(); |
474 | int bpos2; | 501 | int bpos2; |
475 | // expect opening bracket and find next balanced closing bracket. If | 502 | // expect opening bracket and find next balanced closing bracket. If |
476 | // next char is no opening bracket, continue parsing (no valid tag) | 503 | // next char is no opening bracket, continue parsing (no valid tag) |
477 | if ( '(' == result[bpos1] ) { | 504 | if ( '(' == result[bpos1] ) { |
478 | bpos2 = findBalancedBracket( result, bpos1 ); | 505 | bpos2 = findBalancedBracket( result, bpos1 ); |
479 | if ( -1 != bpos2 ) { | 506 | if ( -1 != bpos2 ) { |
480 | // we have balanced brackets, recursively parse: | 507 | // we have balanced brackets, recursively parse: |
481 | QString rplstr; | 508 | QString rplstr; |
482 | bool purge = !parseAddressTemplateSection( result.mid( bpos1+1, | 509 | bool purge = !parseAddressTemplateSection( result.mid( bpos1+1, |
483 | bpos2-bpos1-1 ), rplstr, | 510 | bpos2-bpos1-1 ), rplstr, |
484 | realName, orgaName ); | 511 | realName, orgaName ); |
485 | if ( purge ) { | 512 | if ( purge ) { |
486 | // purge -> remove all | 513 | // purge -> remove all |
487 | // replace with !_P_!, so conditional tags work later | 514 | // replace with !_P_!, so conditional tags work later |
488 | result.replace( fpos, bpos2 - fpos + 1, "!_P_!" ); | 515 | result.replace( fpos, bpos2 - fpos + 1, "!_P_!" ); |
489 | // leave stpos as it is | 516 | // leave stpos as it is |
490 | } else { | 517 | } else { |
491 | // no purge -> replace with recursively parsed string | 518 | // no purge -> replace with recursively parsed string |
492 | result.replace( fpos, bpos2 - fpos + 1, rplstr ); | 519 | result.replace( fpos, bpos2 - fpos + 1, rplstr ); |
493 | ret = true; | 520 | ret = true; |
494 | stpos = fpos + rplstr.length(); | 521 | stpos = fpos + rplstr.length(); |
495 | } | 522 | } |
496 | } else { | 523 | } else { |
497 | // unbalanced brackets: keep on parsing (should not happen | 524 | // unbalanced brackets: keep on parsing (should not happen |
498 | // and will result in bad formatting) | 525 | // and will result in bad formatting) |
499 | stpos = bpos1; | 526 | stpos = bpos1; |
500 | } | 527 | } |
501 | } | 528 | } |
502 | fpos = result.find( KABC_FMTTAG_purgeempty, stpos ); | 529 | fpos = result.find( KABC_FMTTAG_purgeempty, stpos ); |
503 | } | 530 | } |
504 | 531 | ||
505 | // after sorting out all purge tags, we just search'n'replace the rest, | 532 | // after sorting out all purge tags, we just search'n'replace the rest, |
506 | // keeping track of whether at least one tag evaluates to something. | 533 | // keeping track of whether at least one tag evaluates to something. |
507 | // The following macro needs QString for R_FIELD | 534 | // The following macro needs QString for R_FIELD |
508 | // It substitutes !_P_! for empty fields so conditional tags work later | 535 | // It substitutes !_P_! for empty fields so conditional tags work later |
509 | #define REPLTAG(R_TAG,R_FIELD) \ | 536 | #define REPLTAG(R_TAG,R_FIELD) \ |
510 | if ( result.contains(R_TAG, false) ) { \ | 537 | if ( result.contains(R_TAG, false) ) { \ |
511 | QString rpl = R_FIELD.isEmpty() ? QString("!_P_!") : R_FIELD; \ | 538 | QString rpl = R_FIELD.isEmpty() ? QString("!_P_!") : R_FIELD; \ |
512 | result.replace( R_TAG, rpl ); \ | 539 | result.replace( R_TAG, rpl ); \ |
513 | if ( !R_FIELD.isEmpty() ) { \ | 540 | if ( !R_FIELD.isEmpty() ) { \ |
514 | ret = true; \ | 541 | ret = true; \ |
515 | } \ | 542 | } \ |
516 | } | 543 | } |
517 | REPLTAG( KABC_FMTTAG_realname, realName ); | 544 | REPLTAG( KABC_FMTTAG_realname, realName ); |
518 | REPLTAG( KABC_FMTTAG_REALNAME, realName.upper() ); | 545 | REPLTAG( KABC_FMTTAG_REALNAME, realName.upper() ); |
519 | REPLTAG( KABC_FMTTAG_company, orgaName ); | 546 | REPLTAG( KABC_FMTTAG_company, orgaName ); |
520 | REPLTAG( KABC_FMTTAG_COMPANY, orgaName.upper() ); | 547 | REPLTAG( KABC_FMTTAG_COMPANY, orgaName.upper() ); |
521 | REPLTAG( KABC_FMTTAG_pobox, postOfficeBox() ); | 548 | REPLTAG( KABC_FMTTAG_pobox, postOfficeBox() ); |
522 | REPLTAG( KABC_FMTTAG_street, street() ); | 549 | REPLTAG( KABC_FMTTAG_street, street() ); |
523 | REPLTAG( KABC_FMTTAG_STREET, street().upper() ); | 550 | REPLTAG( KABC_FMTTAG_STREET, street().upper() ); |
524 | REPLTAG( KABC_FMTTAG_zipcode, postalCode() ); | 551 | REPLTAG( KABC_FMTTAG_zipcode, postalCode() ); |
525 | REPLTAG( KABC_FMTTAG_location, locality() ); | 552 | REPLTAG( KABC_FMTTAG_location, locality() ); |
526 | REPLTAG( KABC_FMTTAG_LOCATION, locality().upper() ); | 553 | REPLTAG( KABC_FMTTAG_LOCATION, locality().upper() ); |
527 | REPLTAG( KABC_FMTTAG_region, region() ); | 554 | REPLTAG( KABC_FMTTAG_region, region() ); |
528 | REPLTAG( KABC_FMTTAG_REGION, region().upper() ); | 555 | REPLTAG( KABC_FMTTAG_REGION, region().upper() ); |
529 | result.replace( KABC_FMTTAG_newline, "\n" ); | 556 | result.replace( KABC_FMTTAG_newline, "\n" ); |
530 | #undef REPLTAG | 557 | #undef REPLTAG |
531 | 558 | ||
532 | // conditional comma | 559 | // conditional comma |
533 | fpos = result.find( KABC_FMTTAG_condcomma, 0 ); | 560 | fpos = result.find( KABC_FMTTAG_condcomma, 0 ); |
534 | while ( -1 != fpos ) { | 561 | while ( -1 != fpos ) { |
535 | QString str1 = result.mid( fpos - 5, 5 ); | 562 | QString str1 = result.mid( fpos - 5, 5 ); |
536 | QString str2 = result.mid( fpos + 2, 5 ); | 563 | QString str2 = result.mid( fpos + 2, 5 ); |
537 | if ( str1 != "!_P_!" && str2 != "!_P_!" ) { | 564 | if ( str1 != "!_P_!" && str2 != "!_P_!" ) { |
538 | result.replace( fpos, 2, ", " ); | 565 | result.replace( fpos, 2, ", " ); |
539 | } else { | 566 | } else { |
540 | result.remove( fpos, 2 ); | 567 | result.remove( fpos, 2 ); |
541 | } | 568 | } |
542 | fpos = result.find( KABC_FMTTAG_condcomma, fpos ); | 569 | fpos = result.find( KABC_FMTTAG_condcomma, fpos ); |
543 | } | 570 | } |
544 | // conditional whitespace | 571 | // conditional whitespace |
545 | fpos = result.find( KABC_FMTTAG_condwhite, 0 ); | 572 | fpos = result.find( KABC_FMTTAG_condwhite, 0 ); |
546 | while ( -1 != fpos ) { | 573 | while ( -1 != fpos ) { |
547 | QString str1 = result.mid( fpos - 5, 5 ); | 574 | QString str1 = result.mid( fpos - 5, 5 ); |
548 | QString str2 = result.mid( fpos + 2, 5 ); | 575 | QString str2 = result.mid( fpos + 2, 5 ); |
549 | if ( str1 != "!_P_!" && str2 != "!_P_!" ) { | 576 | if ( str1 != "!_P_!" && str2 != "!_P_!" ) { |
550 | result.replace( fpos, 2, " " ); | 577 | result.replace( fpos, 2, " " ); |
551 | } else { | 578 | } else { |
552 | result.remove( fpos, 2 ); | 579 | result.remove( fpos, 2 ); |
553 | } | 580 | } |
554 | fpos = result.find( KABC_FMTTAG_condwhite, fpos ); | 581 | fpos = result.find( KABC_FMTTAG_condwhite, fpos ); |
555 | } | 582 | } |
556 | 583 | ||
557 | // remove purged: | 584 | // remove purged: |
558 | //US my QT version does not support remove. So lets do it the old fashioned way. | 585 | //US my QT version does not support remove. So lets do it the old fashioned way. |
559 | //US result.remove( "!_P_!" ); | 586 | //US result.remove( "!_P_!" ); |
560 | int n = result.find("!_P_!"); | 587 | int n = result.find("!_P_!"); |
561 | if (n >= 0) | 588 | if (n >= 0) |
562 | result.remove( n, 5 ); | 589 | result.remove( n, 5 ); |
563 | 590 | ||
564 | return ret; | 591 | return ret; |
565 | } | 592 | } |
566 | 593 | ||
567 | int Address::findBalancedBracket( const QString &tsection, int pos ) const | 594 | int Address::findBalancedBracket( const QString &tsection, int pos ) const |
568 | { | 595 | { |
569 | int balancecounter = 0; | 596 | int balancecounter = 0; |
570 | for( unsigned int i = pos + 1; i < tsection.length(); i++ ) { | 597 | for( unsigned int i = pos + 1; i < tsection.length(); i++ ) { |
571 | if ( ')' == tsection.at(i) && 0 == balancecounter ) { | 598 | if ( ')' == tsection.at(i) && 0 == balancecounter ) { |
572 | // found end of brackets | 599 | // found end of brackets |
573 | return i; | 600 | return i; |
574 | } else | 601 | } else |
575 | if ( '(' == tsection.at(i) ) { | 602 | if ( '(' == tsection.at(i) ) { |
576 | // nested brackets | 603 | // nested brackets |
577 | balancecounter++; | 604 | balancecounter++; |
578 | } | 605 | } |
579 | } | 606 | } |
580 | return -1; | 607 | return -1; |
581 | } | 608 | } |
582 | 609 | ||
583 | QString Address::countryToISO( const QString &cname ) | 610 | QString Address::countryToISO( const QString &cname ) |
584 | { | 611 | { |
585 | // we search a map file for translations from country names to | 612 | // we search a map file for translations from country names to |
586 | // iso codes, storing caching things in a QMap for faster future | 613 | // iso codes, storing caching things in a QMap for faster future |
587 | // access. | 614 | // access. |
588 | /*US | 615 | /*US |
589 | 616 | ||
590 | QString isoCode = mISOMap[ cname ]; | 617 | QString isoCode = mISOMap[ cname ]; |
591 | if ( !isoCode.isEmpty() ) | 618 | if ( !isoCode.isEmpty() ) |
592 | return isoCode; | 619 | return isoCode; |
593 | 620 | ||
594 | QString mapfile = KGlobal::dirs()->findResource( "data", | 621 | QString mapfile = KGlobal::dirs()->findResource( "data", |
595 | QString::fromLatin1( "kabc/countrytransl.map" ) ); | 622 | QString::fromLatin1( "kabc/countrytransl.map" ) ); |
596 | 623 | ||
597 | QFile file( mapfile ); | 624 | QFile file( mapfile ); |
598 | if ( file.open( IO_ReadOnly ) ) { | 625 | if ( file.open( IO_ReadOnly ) ) { |
599 | QTextStream s( &file ); | 626 | QTextStream s( &file ); |
600 | QString strbuf = s.readLine(); | 627 | QString strbuf = s.readLine(); |
601 | while( !strbuf.isNull() ) { | 628 | while( !strbuf.isNull() ) { |
602 | if ( strbuf.startsWith( cname ) ) { | 629 | if ( strbuf.startsWith( cname ) ) { |
603 | int index = strbuf.findRev('\t'); | 630 | int index = strbuf.findRev('\t'); |
604 | strbuf = strbuf.mid(index+1, 2); | 631 | strbuf = strbuf.mid(index+1, 2); |
605 | file.close(); | 632 | file.close(); |
606 | mISOMap[ cname ] = strbuf; | 633 | mISOMap[ cname ] = strbuf; |
607 | return strbuf; | 634 | return strbuf; |
608 | } | 635 | } |
609 | strbuf = s.readLine(); | 636 | strbuf = s.readLine(); |
610 | } | 637 | } |
611 | file.close(); | 638 | file.close(); |
612 | } | 639 | } |
613 | */ | 640 | */ |
614 | // fall back to system country | 641 | // fall back to system country |
615 | mISOMap[ cname ] = KGlobal::locale()->country(); | 642 | mISOMap[ cname ] = KGlobal::locale()->country(); |
616 | return KGlobal::locale()->country(); | 643 | return KGlobal::locale()->country(); |
617 | } | 644 | } |
618 | 645 | ||
619 | QString Address::ISOtoCountry( const QString &ISOname ) | 646 | QString Address::ISOtoCountry( const QString &ISOname ) |
620 | { | 647 | { |
621 | /*US | 648 | /*US |
622 | // get country name from ISO country code (e.g. "no" -> i18n("Norway")) | 649 | // get country name from ISO country code (e.g. "no" -> i18n("Norway")) |
623 | QString mapfile = KGlobal::dirs()->findResource( "data", | 650 | QString mapfile = KGlobal::dirs()->findResource( "data", |
624 | QString::fromLatin1( "kabc/countrytransl.map" ) ); | 651 | QString::fromLatin1( "kabc/countrytransl.map" ) ); |
625 | 652 | ||
626 | kdWarning() << "MAPFILE : " << mapfile << endl; | 653 | kdWarning() << "MAPFILE : " << mapfile << endl; |
627 | QFile file( mapfile ); | 654 | QFile file( mapfile ); |
628 | if ( file.open( IO_ReadOnly ) ) { | 655 | if ( file.open( IO_ReadOnly ) ) { |
629 | QTextStream s( &file ); | 656 | QTextStream s( &file ); |
630 | QString searchStr = "\t" + ISOname.simplifyWhiteSpace().lower(); | 657 | QString searchStr = "\t" + ISOname.simplifyWhiteSpace().lower(); |
631 | kdWarning() << "Suche : " << searchStr << endl; | 658 | kdWarning() << "Suche : " << searchStr << endl; |
632 | QString strbuf = s.readLine(); | 659 | QString strbuf = s.readLine(); |
633 | int pos; | 660 | int pos; |
634 | while( !strbuf.isNull() ) { | 661 | while( !strbuf.isNull() ) { |
635 | if ( (pos=strbuf.find( searchStr )) != -1 ) { | 662 | if ( (pos=strbuf.find( searchStr )) != -1 ) { |
636 | file.close(); | 663 | file.close(); |
637 | return i18n(strbuf.left(pos).utf8()); | 664 | return i18n(strbuf.left(pos).utf8()); |
638 | } | 665 | } |
639 | strbuf = s.readLine(); | 666 | strbuf = s.readLine(); |
640 | } | 667 | } |
641 | file.close(); | 668 | file.close(); |
642 | } | 669 | } |
643 | */ | 670 | */ |
644 | return ISOname; | 671 | return ISOname; |
645 | } | 672 | } |
646 | 673 | ||
647 | QDataStream &KABC::operator<<( QDataStream &s, const Address &addr ) | 674 | QDataStream &KABC::operator<<( QDataStream &s, const Address &addr ) |
648 | { | 675 | { |
649 | return s << addr.mId << addr.mType << addr.mPostOfficeBox << | 676 | return s << addr.mId << addr.mType << addr.mPostOfficeBox << |
650 | addr.mExtended << addr.mStreet << addr.mLocality << | 677 | addr.mExtended << addr.mStreet << addr.mLocality << |
651 | addr.mRegion << addr.mPostalCode << addr.mCountry << | 678 | addr.mRegion << addr.mPostalCode << addr.mCountry << |
652 | addr.mLabel; | 679 | addr.mLabel; |
653 | } | 680 | } |
654 | 681 | ||
655 | QDataStream &KABC::operator>>( QDataStream &s, Address &addr ) | 682 | QDataStream &KABC::operator>>( QDataStream &s, Address &addr ) |
656 | { | 683 | { |
657 | s >> addr.mId >> addr.mType >> addr.mPostOfficeBox >> addr.mExtended >> | 684 | s >> addr.mId >> addr.mType >> addr.mPostOfficeBox >> addr.mExtended >> |
658 | addr.mStreet >> addr.mLocality >> addr.mRegion >> | 685 | addr.mStreet >> addr.mLocality >> addr.mRegion >> |
659 | addr.mPostalCode >> addr.mCountry >> addr.mLabel; | 686 | addr.mPostalCode >> addr.mCountry >> addr.mLabel; |
660 | 687 | ||
661 | addr.mEmpty = false; | 688 | addr.mEmpty = false; |
662 | 689 | ||
663 | return s; | 690 | return s; |
664 | } | 691 | } |