37#include <tqstringlist.h>
39#include <tqtextstream.h>
49#include "ldifconverter.h"
50#include "vcardconverter.h"
56bool LDIFConverter::addresseeToLDIF(
const AddresseeList &addrList, TQString &str )
58 AddresseeList::ConstIterator it;
59 for ( it = addrList.begin(); it != addrList.end(); ++it ) {
67static void ldif_out( TQTextStream &t, TQString formatStr, TQString value )
69 if ( value.isEmpty() )
72 TQCString txt = LDIF::assembleLine( formatStr, value, 72 );
75 t << TQString::fromUtf8(txt) <<
"\n";
78bool LDIFConverter::addresseeToLDIF(
const Addressee &addr, TQString &str )
83 TQTextStream t( str, IO_WriteOnly|IO_Append );
84 t.setEncoding( TQTextStream::UnicodeUTF8 );
89 ldif_out( t,
"dn", TQString(
"cn=%1,mail=%2" )
90 .arg( TQString(addr.formattedName()).simplifyWhiteSpace() )
92 ldif_out( t,
"givenname", addr.givenName() );
93 ldif_out( t,
"sn", addr.familyName() );
94 ldif_out( t,
"cn", TQString(addr.formattedName()).simplifyWhiteSpace() );
95 ldif_out( t,
"uid", addr.
uid() );
96 ldif_out( t,
"nickname", addr.nickName() );
97 ldif_out( t,
"xmozillanickname", addr.nickName() );
100 if ( addr.
emails().count() > 1 )
101 ldif_out( t,
"mozillasecondemail", addr.
emails()[ 1 ] );
104 ldif_out( t,
"telephonenumber", addr.
phoneNumber( PhoneNumber::Work ).
number() );
105 ldif_out( t,
"facsimiletelephonenumber", addr.
phoneNumber( PhoneNumber::Fax ).
number() );
112 ldif_out( t,
"streethomeaddress", homeAddr.
street() );
113 ldif_out( t,
"postalcode", workAddr.
postalCode() );
116 TQStringList streets = TQStringList::split(
'\n', homeAddr.
street() );
117 if ( streets.count() > 0 )
118 ldif_out( t,
"homepostaladdress", streets[ 0 ] );
119 if ( streets.count() > 1 )
120 ldif_out( t,
"mozillahomepostaladdress2", streets[ 1 ] );
121 ldif_out( t,
"mozillahomelocalityname", homeAddr.
locality() );
122 ldif_out( t,
"mozillahomestate", homeAddr.
region() );
123 ldif_out( t,
"mozillahomepostalcode", homeAddr.
postalCode() );
124 ldif_out( t,
"mozillahomecountryname", Address::ISOtoCountry(homeAddr.
country()) );
125 ldif_out( t,
"locality", workAddr.
locality() );
126 ldif_out( t,
"streetaddress", workAddr.
street() );
128 streets = TQStringList::split(
'\n', workAddr.
street() );
129 if ( streets.count() > 0 )
130 ldif_out( t,
"postaladdress", streets[ 0 ] );
131 if ( streets.count() > 1 )
132 ldif_out( t,
"mozillapostaladdress2", streets[ 1 ] );
133 ldif_out( t,
"countryname", Address::ISOtoCountry(workAddr.
country()) );
134 ldif_out( t,
"l", workAddr.
locality() );
135 ldif_out( t,
"c", Address::ISOtoCountry(workAddr.
country()) );
136 ldif_out( t,
"st", workAddr.
region() );
138 ldif_out( t,
"title", addr.title() );
139 ldif_out( t,
"vocation", addr.prefix() );
140 ldif_out( t,
"ou", addr.role() );
141 ldif_out( t,
"o", addr.organization() );
142 ldif_out( t,
"organization", addr.organization() );
143 ldif_out( t,
"organizationname", addr.organization() );
146 if ( !addr.department().
isEmpty() )
147 ldif_out( t,
"department", addr.department() );
149 ldif_out( t,
"department", addr.
custom(
"KADDRESSBOOK",
"X-Department") );
151 ldif_out( t,
"workurl", addr.url().prettyURL() );
152 ldif_out( t,
"homeurl", addr.url().prettyURL() );
153 ldif_out( t,
"description", addr.note() );
154 if (addr.revision().isValid())
157 t <<
"objectclass: top\n";
158 t <<
"objectclass: person\n";
159 t <<
"objectclass: organizationalPerson\n";
169bool LDIFConverter::LDIFToAddressee(
const TQString &str,
AddresseeList &addrList, TQDateTime dt )
174 bool endldif =
false, end =
false;
177 const char *latinstr = str.latin1();
178 int latinstrlen = tqstrlen( latinstr );
183 data.setRawData( latinstr, latinstrlen );
186 dt = TQDateTime::currentDateTime();
188 homeAddr =
Address( Address::Home );
189 workAddr =
Address( Address::Work );
195 TQString fieldname = ldif.
attr().lower();
196 TQString value = TQString::fromUtf8( ldif.
val(), ldif.
val().size() );
197 evaluatePair( a, homeAddr, workAddr, fieldname, value );
208 addrList.append( a );
212 homeAddr =
Address( Address::Home );
213 workAddr =
Address( Address::Work );
215 case LDIF::MoreData: {
229 data.resetRawData( latinstr, latinstrlen );
236 TQString &fieldname, TQString &value )
238 if ( fieldname == TQString::fromLatin1(
"dn" ) )
241 if ( fieldname.startsWith(
"#") ) {
245 if ( fieldname.isEmpty() && !a.note().
isEmpty() ) {
248 a.setNote( a.note() +
"\n" + value );
252 if ( fieldname == TQString::fromLatin1(
"givenname" ) ) {
253 a.setGivenName( value );
257 if ( fieldname == TQString::fromLatin1(
"xmozillanickname") ||
258 fieldname == TQString::fromLatin1(
"nickname") ) {
259 a.setNickName( value );
263 if ( fieldname == TQString::fromLatin1(
"sn" ) ) {
264 a.setFamilyName( value );
268 if ( fieldname == TQString::fromLatin1(
"uid" ) ) {
272 if ( fieldname == TQString::fromLatin1(
"mail" ) ||
273 fieldname == TQString::fromLatin1(
"mozillasecondemail" ) ) {
274 if ( a.
emails().findIndex( value ) == -1 )
279 if ( fieldname == TQString::fromLatin1(
"title" ) ) {
284 if ( fieldname == TQString::fromLatin1(
"vocation" ) ) {
285 a.setPrefix( value );
289 if ( fieldname == TQString::fromLatin1(
"cn" ) ) {
290 a.setFormattedName( value );
294 if ( fieldname == TQString::fromLatin1(
"o" ) ||
295 fieldname == TQString::fromLatin1(
"organization" ) ||
296 fieldname == TQString::fromLatin1(
"organizationname" ) ) {
297 a.setOrganization( value );
301 if ( fieldname == TQString::fromLatin1(
"description" ) ) {
304 a.setNote( a.note() +
"\n" );
305 a.setNote( a.note() + value );
309 if ( fieldname == TQString::fromLatin1(
"custom1" ) ||
310 fieldname == TQString::fromLatin1(
"custom2" ) ||
311 fieldname == TQString::fromLatin1(
"custom3" ) ||
312 fieldname == TQString::fromLatin1(
"custom4" ) ) {
316 if ( fieldname == TQString::fromLatin1(
"homeurl" ) ||
317 fieldname == TQString::fromLatin1(
"workurl" ) ) {
319 a.setUrl(
KURL( value ) );
322 if ( a.url().prettyURL() ==
KURL(value).prettyURL() )
328 if ( fieldname == TQString::fromLatin1(
"homephone" ) ) {
333 if ( fieldname == TQString::fromLatin1(
"telephonenumber" ) ) {
338 if ( fieldname == TQString::fromLatin1(
"mobile" ) ) {
343 if ( fieldname == TQString::fromLatin1(
"cellphone" ) ) {
348 if ( fieldname == TQString::fromLatin1(
"pager" ) ||
349 fieldname == TQString::fromLatin1(
"pagerphone" ) ) {
354 if ( fieldname == TQString::fromLatin1(
"facsimiletelephonenumber" ) ) {
359 if ( fieldname == TQString::fromLatin1(
"xmozillaanyphone" ) ) {
364 if ( fieldname == TQString::fromLatin1(
"street" ) ||
365 fieldname == TQString::fromLatin1(
"streethomeaddress" ) ) {
370 if ( fieldname == TQString::fromLatin1(
"postaladdress" ) ) {
375 if ( fieldname == TQString::fromLatin1(
"mozillapostaladdress2" ) ) {
376 workAddr.
setStreet( workAddr.
street() + TQString::fromLatin1(
"\n" ) + value );
380 if ( fieldname == TQString::fromLatin1(
"postalcode" ) ) {
385 if ( fieldname == TQString::fromLatin1(
"postofficebox" ) ) {
390 if ( fieldname == TQString::fromLatin1(
"homepostaladdress" ) ) {
395 if ( fieldname == TQString::fromLatin1(
"mozillahomepostaladdress2" ) ) {
396 homeAddr.
setStreet( homeAddr.
street() + TQString::fromLatin1(
"\n" ) + value );
400 if ( fieldname == TQString::fromLatin1(
"mozillahomelocalityname" ) ) {
405 if ( fieldname == TQString::fromLatin1(
"mozillahomestate" ) ) {
410 if ( fieldname == TQString::fromLatin1(
"mozillahomepostalcode" ) ) {
415 if ( fieldname == TQString::fromLatin1(
"mozillahomecountryname" ) ) {
416 if ( value.length() <= 2 )
417 value = Address::ISOtoCountry(value);
422 if ( fieldname == TQString::fromLatin1(
"locality" ) ) {
427 if ( fieldname == TQString::fromLatin1(
"streetaddress" ) ) {
432 if ( fieldname == TQString::fromLatin1(
"countryname" ) ||
433 fieldname == TQString::fromLatin1(
"c" ) ) {
434 if ( value.length() <= 2 )
435 value = Address::ISOtoCountry(value);
440 if ( fieldname == TQString::fromLatin1(
"l" ) ) {
445 if ( fieldname == TQString::fromLatin1(
"st" ) ) {
450 if ( fieldname == TQString::fromLatin1(
"ou" ) ) {
455 if ( fieldname == TQString::fromLatin1(
"department" ) ) {
456 a.setDepartment( value );
460 if ( fieldname == TQString::fromLatin1(
"member" ) ) {
462 TQStringList list( TQStringList::split(
',', value ) );
463 TQString
name, email;
465 TQStringList::Iterator it;
466 for ( it = list.begin(); it != list.end(); ++it ) {
467 if ( (*it).startsWith(
"cn=" ) )
468 name = (*it).mid( 3 ).stripWhiteSpace();
469 if ( (*it).startsWith(
"mail=" ) )
470 email = (*it).mid( 5 ).stripWhiteSpace();
472 if ( !
name.isEmpty() && !email.isEmpty() )
473 email =
" <" + email +
">";
479 if ( fieldname == TQString::fromLatin1(
"modifytimestamp" ) ) {
480 if (value == TQString::fromLatin1(
"0Z"))
483 if ( dt.isValid() ) {
489 if ( fieldname == TQString::fromLatin1(
"objectclass" ) )
492 kdWarning() << TQString(TQString(
"LDIFConverter: Unknown field for '%1': '%2=%3'\n")
493 .arg(a.formattedName()).arg(fieldname).arg(value));
502 Address &workAddr, TQString &line )
504 if ( line.isEmpty() )
507 TQString fieldname, value;
510 LDIF::splitLine( line.latin1(), fieldname, val );
511 value = TQString::fromUtf8( val.data(), val.size() );
512 return evaluatePair( a, homeAddr, workAddr, fieldname, value);
516bool LDIFConverter::splitLine( TQString &line, TQString &fieldname, TQString &value)
519 bool ret = LDIF::splitLine( line.latin1(), fieldname, val );
520 value = TQString::fromUtf8( val.data(), val.size() );
525TQString LDIFConverter::makeLDIFfieldString( TQString formatStr, TQString value,
bool allowEncode )
527 if ( value.isEmpty() )
531 if (formatStr.find(
':') == -1)
532 formatStr.append(
": %1\n");
535 bool printable =
true;
537 len = value.length();
538 for (i = 0; i<len; ++i ) {
539 if (!value[i].isPrint()) {
546 printable = (value.find(
'\n') == -1);
548 if (!printable && allowEncode) {
551 int p = formatStr.find(
':');
553 formatStr.insert(p,
':');
557 TQCString txt = TQString(formatStr.arg(value)).utf8();
561 if (len && txt[len-1] ==
'\n')
565 txt.insert(i,
"\n ");
571 return TQString::fromUtf8(txt);
static TQCString base64Encode(const TQByteArray &in, bool insertLFs=false)
Postal address information.
TQString street() const
Returns the street.
TQString postOfficeBox() const
Returns the post office box.
void setPostalCode(const TQString &)
Sets the postal code.
bool isEmpty() const
Returns true, if the address is empty.
TQString country() const
Returns the country.
void setLocality(const TQString &)
Sets the locality, e.g.
TQString postalCode() const
Returns the postal code.
void setRegion(const TQString &)
Sets the region, e.g.
TQString region() const
Returns the region.
void setStreet(const TQString &)
Sets the street (including number).
TQString locality() const
Returns the locality.
void setCountry(const TQString &)
Sets the country.
void setPostOfficeBox(const TQString &)
Sets the post office box.
a TQValueList of Addressee, with sorting functionality
void setUid(const TQString &uid)
Set unique identifier.
TQStringList emails() const
Return list of all email addresses.
void insertPhoneNumber(const PhoneNumber &phoneNumber)
Insert a phone number.
bool isEmpty() const
Return, if the address book entry is empty.
void insertCategory(const TQString &)
Insert category.
Address address(int type) const
Return address, which matches the given type.
void insertEmail(const TQString &email, bool preferred=false)
Insert an email address.
void insertAddress(const Address &address)
Insert an address.
PhoneNumber phoneNumber(int type) const
Return phone number, which matches the given type.
TQString custom(const TQString &app, const TQString &name) const
Return value of custom entry, identified by app and entry name.
TQString uid() const
Return unique identifier.
TQString preferredEmail() const
Return preferred email address.
void setLDIF(const TQByteArray &ldif)
Sets a chunk of LDIF.
void endLDIF()
Indicates the end of the LDIF file/stream.
const TQByteArray & val() const
Returns the attribute value.
ParseVal nextItem()
Process the LDIF until a complete item can be returned.
const TQString & attr() const
Returns the attribute name.
TQString number() const
Returns the number.
kdbgstream kdWarning(int area=0)
KABC_EXPORT bool addresseeToLDIF(const AddresseeList &addrList, TQString &str)
Converts a list of addressees to a LDIF string.
static data, shared by ALL addressee objects
KABC_EXPORT TQString dateToVCardString(const TQDateTime &dateTime)
Helper functions.
KABC_EXPORT TQDateTime VCardStringToDate(const TQString &dateString)
Converts a date string as it is used in VCard and LDIF files to a TQDateTime value.
TQString name(StdAccel id)