35 #include "ldifconverter.h"
36 #include "vcardconverter.h"
38 #include "addressee.h"
43 #include <klocalizedstring.h>
45 #include <QtCore/QRegExp>
46 #include <QtCore/QStringList>
47 #include <QtCore/QTextCodec>
48 #include <QtCore/QTextStream>
56 AddresseeList::ConstIterator it;
57 AddresseeList::ConstIterator end( addrList.constEnd() );
58 for ( it = addrList.constBegin(); it != end; ++it ) {
64 static void ldif_out( QTextStream &t,
const QString &formatStr,
65 const QString &value )
67 if ( value.isEmpty() ) {
74 t << QString::fromUtf8( txt ) <<
"\n";
80 if (!contactGroupList.isEmpty()) {
81 result = contactGroupToLDIF( contactGroupList, str );
86 bool LDIFConverter::contactGroupToLDIF(
const ContactGroup &contactGroup, QString &str )
91 QTextStream t( &str, QIODevice::WriteOnly|QIODevice::Append );
92 t.setCodec( QTextCodec::codecForName(
"UTF-8" ) );
93 t <<
"objectclass: top\n";
94 t <<
"objectclass: groupOfNames\n";
96 for (
unsigned int i = 0; i < contactGroup.
dataCount(); ++i) {
98 const QString value = QString::fromLatin1(
"cn=%1,mail=%2").arg(data.
name()).arg(data.
email());
99 ldif_out( t, QLatin1String(
"member" ), value );
106 bool LDIFConverter::contactGroupToLDIF(
const ContactGroup::List &contactGroupList, QString &str )
108 ContactGroup::List::ConstIterator it;
109 ContactGroup::List::ConstIterator end( contactGroupList.constEnd() );
110 for ( it = contactGroupList.constBegin(); it != end; ++it ) {
111 contactGroupToLDIF( *it, str );
124 QTextStream t( &str, QIODevice::WriteOnly|QIODevice::Append );
125 t.setCodec( QTextCodec::codecForName(
"UTF-8" ) );
130 ldif_out( t, QLatin1String(
"dn" ), QString::fromLatin1(
"cn=%1,mail=%2" ).
134 t <<
"objectclass: top\n";
135 t <<
"objectclass: person\n";
136 t <<
"objectclass: organizationalPerson\n";
139 ldif_out( t, QLatin1String(
"givenname" ), addr.
givenName() );
140 ldif_out( t, QLatin1String(
"sn" ), addr.
familyName() );
141 ldif_out( t, QLatin1String(
"cn" ), addr.
formattedName().simplified() );
142 ldif_out( t, QLatin1String(
"uid" ), addr.
uid() );
143 ldif_out( t, QLatin1String(
"nickname" ), addr.
nickName() );
144 ldif_out( t, QLatin1String(
"xmozillanickname" ), addr.
nickName() );
145 ldif_out( t, QLatin1String(
"mozillanickname" ), addr.
nickName() );
148 for (
int i = 1; i < addr.
emails().count(); ++i) {
152 ldif_out( t, QLatin1String(
"mozillasecondemail" ), addr.
emails()[ 1 ] );
154 ldif_out( t, QLatin1String(
"othermailbox" ), addr.
emails()[ i ] );
159 ldif_out( t, QLatin1String(
"telephonenumber" ),
161 ldif_out( t, QLatin1String(
"facsimiletelephonenumber" ),
163 ldif_out( t, QLatin1String(
"homephone" ),
165 ldif_out( t, QLatin1String(
"mobile" ),
167 ldif_out( t, QLatin1String(
"cellphone" ),
169 ldif_out( t, QLatin1String(
"pager" ),
171 ldif_out( t, QLatin1String(
"pagerphone" ),
174 ldif_out( t, QLatin1String(
"streethomeaddress" ), homeAddr.
street() );
175 ldif_out( t, QLatin1String(
"postalcode" ), workAddr.
postalCode() );
176 ldif_out( t, QLatin1String(
"postofficebox" ), workAddr.
postOfficeBox() );
178 QStringList streets = homeAddr.
street().split( QLatin1Char(
'\n' ) );
179 const int numberOfStreets( streets.count() );
180 if ( numberOfStreets > 0 ) {
181 ldif_out( t, QLatin1String(
"homepostaladdress" ), streets.at(0) );
183 if ( numberOfStreets > 1 ) {
184 ldif_out( t, QLatin1String(
"mozillahomepostaladdress2" ), streets.at(1) );
186 ldif_out( t, QLatin1String(
"mozillahomelocalityname" ), homeAddr.
locality() );
187 ldif_out( t, QLatin1String(
"mozillahomestate" ), homeAddr.
region() );
188 ldif_out( t, QLatin1String(
"mozillahomepostalcode" ), homeAddr.
postalCode() );
189 ldif_out( t, QLatin1String(
"mozillahomecountryname" ),
191 ldif_out( t, QLatin1String(
"locality" ), workAddr.
locality() );
192 ldif_out( t, QLatin1String(
"streetaddress" ), workAddr.
street() );
194 streets = workAddr.
street().split( QLatin1Char(
'\n' ) );
195 if ( streets.count() > 0 ) {
196 ldif_out( t, QLatin1String(
"street" ), streets.at(0) );
198 if ( streets.count() > 1 ) {
199 ldif_out( t, QLatin1String(
"mozillaworkstreet2" ), streets.at(1) );
202 ldif_out( t, QLatin1String(
"l" ), workAddr.
locality() );
204 ldif_out( t, QLatin1String(
"st" ), workAddr.
region() );
206 ldif_out( t, QLatin1String(
"title" ), addr.
title() );
207 ldif_out( t, QLatin1String(
"vocation" ), addr.
prefix() );
208 ldif_out( t, QLatin1String(
"ou" ), addr.
role() );
209 ldif_out( t, QLatin1String(
"o" ), addr.
organization() );
210 ldif_out( t, QLatin1String(
"organization" ), addr.
organization() );
211 ldif_out( t, QLatin1String(
"organizationname" ), addr.
organization() );
215 ldif_out( t, QLatin1String(
"department" ), addr.
department() );
217 ldif_out( t, QLatin1String(
"department" ), addr.
custom( QLatin1String(
"KADDRESSBOOK" ),
218 QLatin1String(
"X-Department" ) ) );
221 ldif_out( t, QLatin1String(
"workurl" ), addr.
url().prettyUrl() );
222 ldif_out( t, QLatin1String(
"homeurl" ), addr.
url().prettyUrl() );
223 ldif_out( t, QLatin1String(
"mozillahomeurl" ), addr.
url().prettyUrl() );
225 ldif_out( t, QLatin1String(
"description" ), addr.
note() );
230 const QDateTime birthday = addr.
birthday();
231 if ( birthday.date().isValid() ) {
232 const QDate date = birthday.date();
233 ldif_out( t, QLatin1String(
"birthyear" ), QString::number( date.year() ) );
234 ldif_out( t, QLatin1String(
"birthmonth" ), QString::number( date.month() ) );
235 ldif_out( t, QLatin1String(
"birthday" ), QString::number( date.day() ) );
246 const QDateTime &dt )
248 if ( str.isEmpty() ) {
251 bool endldif =
false, end =
false;
253 Ldif::ParseValue ret;
261 ldif.setLdif( str.toLatin1() );
263 if ( !qdt.isValid() ) {
264 qdt = QDateTime::currentDateTime();
271 ret = ldif.nextItem();
275 QString fieldname = ldif.attr().toLower();
276 QString value = QString::fromUtf8( ldif.value(), ldif.value().size() );
277 evaluatePair( a, homeAddr, workAddr, fieldname, value, birthday, birthmonth, birthyear, contactGroup );
282 if (contactGroup.
count() == 0) {
284 QDateTime birthDate( QDate( birthyear, birthmonth, birthday ) );
285 if ( birthDate.isValid() ) {
297 addrList.append( a );
300 contactGroupList.append(contactGroup);
328 const QDateTime &dt )
336 QString &fieldname, QString &value,
337 int &birthday,
int &birthmonth,
int &birthyear,
ContactGroup &contactGroup )
339 if ( fieldname == QLatin1String(
"dn" ) ) {
343 if ( fieldname.startsWith( QLatin1Char(
'#' ) ) ) {
347 if ( fieldname.isEmpty() && !a.
note().isEmpty() ) {
350 a.
setNote( a.
note() + QLatin1Char(
'\n' ) + value );
354 if ( fieldname == QLatin1String(
"givenname" ) ) {
359 if ( fieldname == QLatin1String(
"xmozillanickname" ) ||
360 fieldname == QLatin1String(
"nickname" ) ||
361 fieldname == QLatin1String(
"mozillanickname" ) ) {
366 if ( fieldname == QLatin1String(
"sn" ) ) {
371 if ( fieldname == QLatin1String(
"uid" ) ) {
375 if ( fieldname == QLatin1String(
"mail" ) ||
376 fieldname == QLatin1String(
"mozillasecondemail" ) ||
377 fieldname == QLatin1String(
"othermailbox" ) ) {
378 if ( a.
emails().indexOf( value ) == -1 ) {
384 if ( fieldname == QLatin1String(
"title" ) ) {
389 if ( fieldname == QLatin1String(
"vocation" ) ) {
394 if ( fieldname == QLatin1String(
"cn" ) ) {
399 if ( fieldname == QLatin1String(
"o" ) ||
400 fieldname == QLatin1String(
"organization" ) ||
401 fieldname == QLatin1String(
"organizationname" ) ) {
406 if ( fieldname == QLatin1String(
"description" ) ||
407 fieldname == QLatin1String(
"mozillacustom1" ) ||
408 fieldname == QLatin1String(
"mozillacustom2" ) ||
409 fieldname == QLatin1String(
"mozillacustom3" ) ||
410 fieldname == QLatin1String(
"mozillacustom4" ) ||
411 fieldname == QLatin1String(
"custom1" ) ||
412 fieldname == QLatin1String(
"custom2" ) ||
413 fieldname == QLatin1String(
"custom3" ) ||
414 fieldname == QLatin1String(
"custom4" ) ) {
415 if ( !a.
note().isEmpty() ) {
422 if ( fieldname == QLatin1String(
"homeurl" ) ||
423 fieldname == QLatin1String(
"workurl" ) ||
424 fieldname == QLatin1String(
"mozillahomeurl" ) ) {
425 if ( a.
url().isEmpty() ) {
426 a.
setUrl( KUrl( value ) );
429 if ( a.
url().prettyUrl() == KUrl( value ).prettyUrl() ) {
436 if ( fieldname == QLatin1String(
"homephone" ) ) {
441 if ( fieldname == QLatin1String(
"telephonenumber" ) ) {
446 if ( fieldname == QLatin1String(
"mobile" ) ||
447 fieldname == QLatin1String(
"cellphone" )) {
452 if ( fieldname == QLatin1String(
"pager" ) ||
453 fieldname == QLatin1String(
"pagerphone" ) ) {
458 if ( fieldname == QLatin1String(
"facsimiletelephonenumber" ) ) {
463 if ( fieldname == QLatin1String(
"xmozillaanyphone" ) ) {
468 if ( fieldname == QLatin1String(
"streethomeaddress" ) ||
469 fieldname == QLatin1String(
"mozillahomestreet" ) ) {
474 if ( fieldname == QLatin1String(
"street" ) ||
475 fieldname == QLatin1String(
"postaladdress" ) ) {
480 if ( fieldname == QLatin1String(
"mozillapostaladdress2" ) ||
481 fieldname == QLatin1String(
"mozillaworkstreet2" ) ) {
482 workAddr.
setStreet( workAddr.
street() + QLatin1String(
"\n" ) + value );
486 if ( fieldname == QLatin1String(
"postalcode" ) ) {
491 if ( fieldname == QLatin1String(
"postofficebox" ) ) {
496 if ( fieldname == QLatin1String(
"homepostaladdress" ) ) {
501 if ( fieldname == QLatin1String(
"mozillahomepostaladdress2" ) ) {
502 homeAddr.
setStreet( homeAddr.
street() + QLatin1String(
"\n" ) + value );
506 if ( fieldname == QLatin1String(
"mozillahomelocalityname" ) ) {
511 if ( fieldname == QLatin1String(
"mozillahomestate" ) ) {
516 if ( fieldname == QLatin1String(
"mozillahomepostalcode" ) ) {
521 if ( fieldname == QLatin1String(
"mozillahomecountryname" ) ) {
522 if ( value.length() <= 2 ) {
529 if ( fieldname == QLatin1String(
"locality" ) ) {
534 if ( fieldname == QLatin1String(
"streetaddress" ) ) {
539 if ( fieldname == QLatin1String(
"countryname" ) ||
540 fieldname == QLatin1String(
"c" ) ) {
541 if ( value.length() <= 2 ) {
548 if ( fieldname == QLatin1String(
"l" ) ) {
553 if ( fieldname == QLatin1String(
"st" ) ) {
558 if ( fieldname == QLatin1String(
"ou" ) ) {
563 if ( fieldname == QLatin1String(
"department" ) ) {
568 if ( fieldname == QLatin1String(
"member" ) ) {
570 QStringList list = value.split( QLatin1Char(
',' ) );
573 QStringList::ConstIterator it;
574 QStringList::ConstIterator end(list.constEnd());
575 for ( it = list.constBegin(); it != end; ++it ) {
576 if ( ( *it ).startsWith( QLatin1String(
"cn=" ) ) ) {
577 name = ( *it ).mid( 3 ).trimmed();
579 if ( ( *it ).startsWith( QLatin1String(
"mail=" ) ) ) {
580 email = ( *it ).mid( 5 ).trimmed();
583 if ( !name.isEmpty() && !email.isEmpty() ) {
584 email = QLatin1String(
" <" ) + email + QLatin1Char(
'>' );
589 contactGroup.
append(data);
593 if ( fieldname == QLatin1String(
"modifytimestamp" ) ) {
594 if ( value == QLatin1String(
"0Z" ) ) {
598 if ( dt.isValid() ) {
604 if ( fieldname == QLatin1String(
"display-name" ) ) {
609 if ( fieldname == QLatin1String(
"objectclass" ) ) {
613 if ( fieldname == QLatin1String(
"birthyear" ) ) {
614 birthyear = value.toInt();
617 if ( fieldname == QLatin1String(
"birthmonth" ) ) {
618 birthmonth = value.toInt();
621 if ( fieldname == QLatin1String(
"birthday" ) ) {
622 birthday = value.toInt();
625 if ( fieldname == QLatin1String(
"xbatbirthday")) {
626 const QDate dt = QDate::fromString(value, QString::fromLatin1(
"yyyyMMdd"));
633 kWarning( 5700 ) << QString::fromLatin1(
"LDIFConverter: Unknown field for '%1': '%2=%3'\n" ).