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