• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeabc
 

tdeabc

  • tdeabc
address.cpp
1/*
2 This file is part of libtdeabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
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
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "address.h"
22
23#include <tdeapplication.h>
24#include <kdebug.h>
25#include <tdelocale.h>
26#include <ksimpleconfig.h>
27#include <tdestandarddirs.h>
28#include <kstaticdeleter.h>
29
30#include <tqfile.h>
31
32using namespace TDEABC;
33
34TQMap<TQString, TQString> *Address::mISOMap = 0;
35static KStaticDeleter< TQMap<TQString, TQString> > isoMapDeleter;
36
37Address::Address() :
38 mEmpty( true ), mType( 0 )
39{
40 mId = TDEApplication::randomString( 10 );
41}
42
43Address::Address( int type ) :
44 mEmpty( true ), mType( type )
45{
46 mId = TDEApplication::randomString( 10 );
47}
48
49bool Address::operator==( const Address &a ) const
50{
51 if ( mPostOfficeBox != a.mPostOfficeBox ) return false;
52 if ( mExtended != a.mExtended ) return false;
53 if ( mStreet != a.mStreet ) return false;
54 if ( mLocality != a.mLocality ) return false;
55 if ( mRegion != a.mRegion ) return false;
56 if ( mPostalCode != a.mPostalCode ) return false;
57 if ( mCountry != a.mCountry ) return false;
58 if ( mLabel != a.mLabel ) return false;
59
60 return true;
61}
62
63bool Address::operator!=( const Address &a ) const
64{
65 return !( a == *this );
66}
67
68bool Address::isEmpty() const
69{
70 if ( mPostOfficeBox.isEmpty() &&
71 mExtended.isEmpty() &&
72 mStreet.isEmpty() &&
73 mLocality.isEmpty() &&
74 mRegion.isEmpty() &&
75 mPostalCode.isEmpty() &&
76 mCountry.isEmpty() &&
77 mLabel.isEmpty() ) {
78 return true;
79 }
80 return false;
81}
82
83void Address::clear()
84{
85 *this = Address();
86}
87
88void Address::setId( const TQString &id )
89{
90 mEmpty = false;
91
92 mId = id;
93}
94
95TQString Address::id() const
96{
97 return mId;
98}
99
100void Address::setType( int type )
101{
102 mEmpty = false;
103
104 mType = type;
105}
106
107int Address::type() const
108{
109 return mType;
110}
111
112TQString Address::typeLabel() const
113{
114 TQString label;
115 bool first = true;
116
117 const TypeList list = typeList();
118
119 TypeList::ConstIterator it;
120 for ( it = list.begin(); it != list.end(); ++it ) {
121 if ( ( type() & (*it) ) && ( (*it) != Pref ) ) {
122 label.append( ( first ? "" : "/" ) + typeLabel( *it ) );
123 if ( first )
124 first = false;
125 }
126 }
127
128 return label;
129}
130
131void Address::setPostOfficeBox( const TQString &s )
132{
133 mEmpty = false;
134
135 mPostOfficeBox = s;
136}
137
138TQString Address::postOfficeBox() const
139{
140 return mPostOfficeBox;
141}
142
143TQString Address::postOfficeBoxLabel()
144{
145 return i18n("Post Office Box");
146}
147
148
149void Address::setExtended( const TQString &s )
150{
151 mEmpty = false;
152
153 mExtended = s;
154}
155
156TQString Address::extended() const
157{
158 return mExtended;
159}
160
161TQString Address::extendedLabel()
162{
163 return i18n("Extended Address Information");
164}
165
166
167void Address::setStreet( const TQString &s )
168{
169 mEmpty = false;
170
171 mStreet = s;
172}
173
174TQString Address::street() const
175{
176 return mStreet;
177}
178
179TQString Address::streetLabel()
180{
181 return i18n("Street");
182}
183
184
185void Address::setLocality( const TQString &s )
186{
187 mEmpty = false;
188
189 mLocality = s;
190}
191
192TQString Address::locality() const
193{
194 return mLocality;
195}
196
197TQString Address::localityLabel()
198{
199 return i18n("Locality");
200}
201
202
203void Address::setRegion( const TQString &s )
204{
205 mEmpty = false;
206
207 mRegion = s;
208}
209
210TQString Address::region() const
211{
212 return mRegion;
213}
214
215TQString Address::regionLabel()
216{
217 return i18n("Region");
218}
219
220
221void Address::setPostalCode( const TQString &s )
222{
223 mEmpty = false;
224
225 mPostalCode = s;
226}
227
228TQString Address::postalCode() const
229{
230 return mPostalCode;
231}
232
233TQString Address::postalCodeLabel()
234{
235 return i18n("Postal Code");
236}
237
238
239void Address::setCountry( const TQString &s )
240{
241 mEmpty = false;
242
243 mCountry = s;
244}
245
246TQString Address::country() const
247{
248 return mCountry;
249}
250
251TQString Address::countryLabel()
252{
253 return i18n("Country");
254}
255
256
257void Address::setLabel( const TQString &s )
258{
259 mEmpty = false;
260
261 mLabel = s;
262}
263
264TQString Address::label() const
265{
266 return mLabel;
267}
268
269TQString Address::labelLabel()
270{
271 return i18n("Delivery Label");
272}
273
274Address::TypeList Address::typeList()
275{
276 static TypeList list;
277
278 if ( list.isEmpty() )
279 list << Dom << Intl << Postal << Parcel << Home << Work << Pref;
280
281 return list;
282}
283
284TQString Address::typeLabel( int type )
285{
286 if ( type & Pref )
287 return i18n( "Preferred address", "Preferred" );
288
289 switch ( type ) {
290 case Dom:
291 return i18n("Domestic");
292 break;
293 case Intl:
294 return i18n("International");
295 break;
296 case Postal:
297 return i18n("Postal");
298 break;
299 case Parcel:
300 return i18n("Parcel");
301 break;
302 case Home:
303 return i18n("Home Address", "Home");
304 break;
305 case Work:
306 return i18n("Work Address", "Work");
307 break;
308 case Pref:
309 return i18n("Preferred Address");
310 break;
311 default:
312 return i18n("Other");
313 break;
314 }
315}
316
317void Address::dump() const
318{
319 kdDebug(5700) << " Address {" << endl;
320 kdDebug(5700) << " Id: " << id() << endl;
321 kdDebug(5700) << " Extended: " << extended() << endl;
322 kdDebug(5700) << " Street: " << street() << endl;
323 kdDebug(5700) << " Postal Code: " << postalCode() << endl;
324 kdDebug(5700) << " Locality: " << locality() << endl;
325 kdDebug(5700) << " }" << endl;
326}
327
328
329TQString Address::formattedAddress( const TQString &realName,
330 const TQString &orgaName ) const
331{
332 TQString ciso;
333 TQString addrTemplate;
334 TQString ret;
335
336 // FIXME: first check for iso-country-field and prefer that one
337 if ( !country().isEmpty() ) {
338 ciso = countryToISO( country() );
339 } else {
340 // fall back to our own country
341 ciso = TDEGlobal::locale()->country();
342 }
343 KSimpleConfig entry( locate( "locale",
344 TQString( "l10n/" ) + ciso + TQString( "/entry.desktop" ) ) );
345 entry.setGroup( "KCM Locale" );
346
347 // decide whether this needs special business address formatting
348 if ( orgaName.isEmpty() ) {
349 addrTemplate = entry.readEntry( "AddressFormat" );
350 } else {
351 addrTemplate = entry.readEntry( "BusinessAddressFormat" );
352 if ( addrTemplate.isEmpty() )
353 addrTemplate = entry.readEntry( "AddressFormat" );
354 }
355
356 // in the case there's no format found at all, default to what we've always
357 // used:
358 if ( addrTemplate.isEmpty() ) {
359 kdWarning(5700) << "address format database incomplete "
360 << "(no format for locale " << ciso
361 << " found). Using default address formatting." << endl;
362 addrTemplate = "%0(%n\\n)%0(%cm\\n)%0(%s\\n)%0(PO BOX %p\\n)%0(%l%w%r)%,%z";
363 }
364
365 // scan
366 parseAddressTemplateSection( addrTemplate, ret, realName, orgaName );
367
368 // now add the country line if needed (formatting this time according to
369 // the rules of our own system country )
370 if ( !country().isEmpty() ) {
371 KSimpleConfig entry( locate( "locale", TQString( "l10n/" )
372 + TDEGlobal::locale()->country() + TQString( "/entry.desktop" ) ) );
373 entry.setGroup( "KCM Locale" );
374 TQString cpos = entry.readEntry( "AddressCountryPosition" );
375 if ( "BELOW" == cpos || cpos.isEmpty() ) {
376 ret = ret + "\n\n" + country().upper();
377 } else if ( "below" == cpos ) {
378 ret = ret + "\n\n" + country();
379 } else if ( "ABOVE" == cpos ) {
380 ret = country().upper() + "\n\n" + ret;
381 } else if ( "above" == cpos ) {
382 ret = country() + "\n\n" + ret;
383 }
384 }
385
386 return ret;
387}
388
389bool Address::parseAddressTemplateSection( const TQString &tsection,
390 TQString &result, const TQString &realName, const TQString &orgaName ) const
391{
392 // This method first parses and substitutes any bracketed sections and
393 // after that replaces any tags with their values. If a bracketed section
394 // or a tag evaluate to zero, they are not just removed but replaced
395 // with a placeholder. This is because in the last step conditionals are
396 // resolved which depend on information about zero-evaluations.
397 result = tsection;
398 int stpos = 0;
399 bool ret = false;
400
401 // first check for brackets that have to be evaluated first
402 int fpos = result.find( KABC_FMTTAG_purgeempty, stpos );
403 while ( -1 != fpos ) {
404 int bpos1 = fpos + KABC_FMTTAG_purgeempty.length();
405 int bpos2;
406 // expect opening bracket and find next balanced closing bracket. If
407 // next char is no opening bracket, continue parsing (no valid tag)
408 if ( '(' == result[bpos1] ) {
409 bpos2 = findBalancedBracket( result, bpos1 );
410 if ( -1 != bpos2 ) {
411 // we have balanced brackets, recursively parse:
412 TQString rplstr;
413 bool purge = !parseAddressTemplateSection( result.mid( bpos1+1,
414 bpos2-bpos1-1 ), rplstr,
415 realName, orgaName );
416 if ( purge ) {
417 // purge -> remove all
418 // replace with !_P_!, so conditional tags work later
419 result.replace( fpos, bpos2 - fpos + 1, "!_P_!" );
420 // leave stpos as it is
421 } else {
422 // no purge -> replace with recursively parsed string
423 result.replace( fpos, bpos2 - fpos + 1, rplstr );
424 ret = true;
425 stpos = fpos + rplstr.length();
426 }
427 } else {
428 // unbalanced brackets: keep on parsing (should not happen
429 // and will result in bad formatting)
430 stpos = bpos1;
431 }
432 }
433 fpos = result.find( KABC_FMTTAG_purgeempty, stpos );
434 }
435
436 // after sorting out all purge tags, we just search'n'replace the rest,
437 // keeping track of whether at least one tag evaluates to something.
438 // The following macro needs TQString for R_FIELD
439 // It substitutes !_P_! for empty fields so conditional tags work later
440#define REPLTAG(R_TAG,R_FIELD) \
441 if ( result.find(R_TAG, false) != -1 ) { \
442 TQString rpl = R_FIELD.isEmpty() ? TQString("!_P_!") : R_FIELD; \
443 result.replace( R_TAG, rpl ); \
444 if ( !R_FIELD.isEmpty() ) { \
445 ret = true; \
446 } \
447 }
448 REPLTAG( KABC_FMTTAG_realname, realName );
449 REPLTAG( KABC_FMTTAG_REALNAME, realName.upper() );
450 REPLTAG( KABC_FMTTAG_company, orgaName );
451 REPLTAG( KABC_FMTTAG_COMPANY, orgaName.upper() );
452 REPLTAG( KABC_FMTTAG_pobox, postOfficeBox() );
453 REPLTAG( KABC_FMTTAG_street, street() );
454 REPLTAG( KABC_FMTTAG_STREET, street().upper() );
455 REPLTAG( KABC_FMTTAG_zipcode, postalCode() );
456 REPLTAG( KABC_FMTTAG_location, locality() );
457 REPLTAG( KABC_FMTTAG_LOCATION, locality().upper() );
458 REPLTAG( KABC_FMTTAG_region, region() );
459 REPLTAG( KABC_FMTTAG_REGION, region().upper() );
460 result.replace( KABC_FMTTAG_newline, "\n" );
461#undef REPLTAG
462
463 // conditional comma
464 fpos = result.find( KABC_FMTTAG_condcomma, 0 );
465 while ( -1 != fpos ) {
466 TQString str1 = result.mid( fpos - 5, 5 );
467 TQString str2 = result.mid( fpos + 2, 5 );
468 if ( str1 != "!_P_!" && str2 != "!_P_!" ) {
469 result.replace( fpos, 2, ", " );
470 } else {
471 result.remove( fpos, 2 );
472 }
473 fpos = result.find( KABC_FMTTAG_condcomma, fpos );
474 }
475 // conditional whitespace
476 fpos = result.find( KABC_FMTTAG_condwhite, 0 );
477 while ( -1 != fpos ) {
478 TQString str1 = result.mid( fpos - 5, 5 );
479 TQString str2 = result.mid( fpos + 2, 5 );
480 if ( str1 != "!_P_!" && str2 != "!_P_!" ) {
481 result.replace( fpos, 2, " " );
482 } else {
483 result.remove( fpos, 2 );
484 }
485 fpos = result.find( KABC_FMTTAG_condwhite, fpos );
486 }
487
488 // remove purged:
489 result.remove( "!_P_!" );
490
491 return ret;
492}
493
494int Address::findBalancedBracket( const TQString &tsection, int pos ) const
495{
496 int balancecounter = 0;
497 for( unsigned int i = pos + 1; i < tsection.length(); i++ ) {
498 if ( ')' == tsection[i] && 0 == balancecounter ) {
499 // found end of brackets
500 return i;
501 } else
502 if ( '(' == tsection[i] ) {
503 // nested brackets
504 balancecounter++;
505 }
506 }
507 return -1;
508}
509
510TQString Address::countryToISO( const TQString &cname )
511{
512 // we search a map file for translations from country names to
513 // iso codes, storing caching things in a TQMap for faster future
514 // access.
515 if ( !mISOMap )
516 isoMapDeleter.setObject( mISOMap, new TQMap<TQString, TQString>() );
517
518 TQMap<TQString, TQString>::ConstIterator it;
519 it = mISOMap->find( cname );
520 if ( it != mISOMap->end() )
521 return it.data();
522
523 TQString mapfile = TDEGlobal::dirs()->findResource( "data",
524 TQString::fromLatin1( "tdeabc/countrytransl.map" ) );
525
526 TQFile file( mapfile );
527 if ( file.open( IO_ReadOnly ) ) {
528 TQTextStream s( &file );
529 TQString strbuf = s.readLine();
530 while( !strbuf.isEmpty() ) {
531 TQStringList countryInfo = TQStringList::split( '\t', strbuf, true );
532 if ( countryInfo[ 0 ] == cname ) {
533 file.close();
534 mISOMap->insert( cname, countryInfo[ 1 ] );
535 return countryInfo[ 1 ];
536 }
537 strbuf = s.readLine();
538 }
539 file.close();
540 }
541
542 // fall back to system country
543 mISOMap->insert( cname, TDEGlobal::locale()->country() );
544 return TDEGlobal::locale()->country();
545}
546
547TQString Address::ISOtoCountry( const TQString &ISOname )
548{
549 // get country name from ISO country code (e.g. "no" -> i18n("Norway"))
550 if ( ISOname.simplifyWhiteSpace().isEmpty() )
551 return TQString::null;
552
553 TQString mapfile = TDEGlobal::dirs()->findResource( "data",
554 TQString::fromLatin1( "tdeabc/countrytransl.map" ) );
555
556 TQFile file( mapfile );
557 if ( file.open( IO_ReadOnly ) ) {
558 TQTextStream s( &file );
559 TQString searchStr = "\t" + ISOname.simplifyWhiteSpace().lower();
560 TQString strbuf = s.readLine();
561 int pos;
562 while ( !strbuf.isEmpty() ) {
563 if ( (pos = strbuf.find( searchStr )) != -1 ) {
564 file.close();
565 return i18n( strbuf.left( pos ).utf8() );
566 }
567 strbuf = s.readLine();
568 }
569 file.close();
570 }
571
572 return ISOname;
573}
574
575TQDataStream &TDEABC::operator<<( TQDataStream &s, const Address &addr )
576{
577 return s << addr.mId << addr.mType << addr.mPostOfficeBox <<
578 addr.mExtended << addr.mStreet << addr.mLocality <<
579 addr.mRegion << addr.mPostalCode << addr.mCountry <<
580 addr.mLabel;
581}
582
583TQDataStream &TDEABC::operator>>( TQDataStream &s, Address &addr )
584{
585 s >> addr.mId >> addr.mType >> addr.mPostOfficeBox >> addr.mExtended >>
586 addr.mStreet >> addr.mLocality >> addr.mRegion >>
587 addr.mPostalCode >> addr.mCountry >> addr.mLabel;
588
589 addr.mEmpty = false;
590
591 return s;
592}
KSimpleConfig
KStaticDeleter
TDEABC::Address
Postal address information.
Definition: address.h:56
TDEABC::Address::typeList
static TypeList typeList()
Returns the list of available types.
Definition: address.cpp:274
TDEABC::Address::street
TQString street() const
Returns the street.
Definition: address.cpp:174
TDEABC::Address::postOfficeBox
TQString postOfficeBox() const
Returns the post office box.
Definition: address.cpp:138
TDEABC::Address::setPostalCode
void setPostalCode(const TQString &)
Sets the postal code.
Definition: address.cpp:221
TDEABC::Address::setExtended
void setExtended(const TQString &)
Sets the extended address information.
Definition: address.cpp:149
TDEABC::Address::extendedLabel
static TQString extendedLabel()
Returns the translated label for extended field.
Definition: address.cpp:161
TDEABC::Address::isEmpty
bool isEmpty() const
Returns true, if the address is empty.
Definition: address.cpp:68
TDEABC::Address::postOfficeBoxLabel
static TQString postOfficeBoxLabel()
Returns the translated label for post office box field.
Definition: address.cpp:143
TDEABC::Address::country
TQString country() const
Returns the country.
Definition: address.cpp:246
TDEABC::Address::labelLabel
static TQString labelLabel()
Returns the translated label for delivery label field.
Definition: address.cpp:269
TDEABC::Address::ISOtoCountry
static TQString ISOtoCountry(const TQString &ISOname)
Returns a localized country name for a ISO code.
Definition: address.cpp:547
TDEABC::Address::type
int type() const
Returns the type of address.
Definition: address.cpp:107
TDEABC::Address::setLocality
void setLocality(const TQString &)
Sets the locality, e.g.
Definition: address.cpp:185
TDEABC::Address::setType
void setType(int type)
Sets the type of address.
Definition: address.cpp:100
TDEABC::Address::setId
void setId(const TQString &)
Sets the unique id.
Definition: address.cpp:88
TDEABC::Address::formattedAddress
TQString formattedAddress(const TQString &realName=TQString::null, const TQString &orgaName=TQString::null) const
Returns this address formatted according to the country-specific address formatting rules.
Definition: address.cpp:329
TDEABC::Address::postalCode
TQString postalCode() const
Returns the postal code.
Definition: address.cpp:228
TDEABC::Address::Address
Address()
Constructor that creates an empty Address, which is initialized with a unique id (see id()).
Definition: address.cpp:37
TDEABC::Address::typeLabel
TQString typeLabel() const
Returns a translated string of all types the address has.
Definition: address.cpp:112
TDEABC::Address::regionLabel
static TQString regionLabel()
Returns the translated label for region field.
Definition: address.cpp:215
TDEABC::Address::label
TQString label() const
Returns the delivery label.
Definition: address.cpp:264
TDEABC::Address::extended
TQString extended() const
Returns the extended address information.
Definition: address.cpp:156
TDEABC::Address::setRegion
void setRegion(const TQString &)
Sets the region, e.g.
Definition: address.cpp:203
TDEABC::Address::setLabel
void setLabel(const TQString &)
Sets the delivery label.
Definition: address.cpp:257
TDEABC::Address::clear
void clear()
Clears all entries of the address.
Definition: address.cpp:83
TDEABC::Address::countryLabel
static TQString countryLabel()
Returns the translated label for country field.
Definition: address.cpp:251
TDEABC::Address::region
TQString region() const
Returns the region.
Definition: address.cpp:210
TDEABC::Address::countryToISO
static TQString countryToISO(const TQString &cname)
Returns ISO code for a localized country name.
Definition: address.cpp:510
TDEABC::Address::setStreet
void setStreet(const TQString &)
Sets the street (including number).
Definition: address.cpp:167
TDEABC::Address::locality
TQString locality() const
Returns the locality.
Definition: address.cpp:192
TDEABC::Address::postalCodeLabel
static TQString postalCodeLabel()
Returns the translated label for postal code field.
Definition: address.cpp:233
TDEABC::Address::dump
void dump() const
Used for debug output.
Definition: address.cpp:317
TDEABC::Address::setCountry
void setCountry(const TQString &)
Sets the country.
Definition: address.cpp:239
TDEABC::Address::setPostOfficeBox
void setPostOfficeBox(const TQString &)
Sets the post office box.
Definition: address.cpp:131
TDEABC::Address::streetLabel
static TQString streetLabel()
Returns the translated label for street field.
Definition: address.cpp:179
TDEABC::Address::localityLabel
static TQString localityLabel()
Returns the translated label for locality field.
Definition: address.cpp:197
TDEApplication::randomString
static TQString randomString(int length)
TDEConfigBase::readEntry
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
TDEConfigBase::setGroup
void setGroup(const TQString &group)
TDEGlobal::dirs
static TDEStandardDirs * dirs()
TDEGlobal::locale
static TDELocale * locale()
TDELocale::country
TQString country() const
TDEStandardDirs::findResource
TQString findResource(const char *type, const TQString &filename) const
kdWarning
kdbgstream kdWarning(int area=0)
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
locate
TQString locate(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48
tdelocale.h

tdeabc

Skip menu "tdeabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeabc

Skip menu "tdeabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeabc by doxygen 1.9.4
This website is maintained by Timothy Pearson.