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";
83 QTextStream t( &str, QIODevice::WriteOnly|QIODevice::Append );
84 t.setCodec( QTextCodec::codecForName(
"UTF-8" ) );
89 ldif_out( t, QLatin1String(
"dn" ), QString::fromLatin1(
"cn=%1,mail=%2" ).
92 ldif_out( t, QLatin1String(
"givenname" ), addr.
givenName() );
93 ldif_out( t, QLatin1String(
"sn" ), addr.
familyName() );
94 ldif_out( t, QLatin1String(
"cn" ), addr.
formattedName().simplified() );
95 ldif_out( t, QLatin1String(
"uid" ), addr.
uid() );
96 ldif_out( t, QLatin1String(
"nickname" ), addr.
nickName() );
97 ldif_out( t, QLatin1String(
"xmozillanickname" ), addr.
nickName() );
98 ldif_out( t, QLatin1String(
"mozillanickname" ), addr.
nickName() );
101 if ( addr.
emails().count() > 1 ) {
102 ldif_out( t, QLatin1String(
"mozillasecondemail" ), addr.
emails()[ 1 ] );
106 ldif_out( t, QLatin1String(
"telephonenumber" ),
108 ldif_out( t, QLatin1String(
"facsimiletelephonenumber" ),
110 ldif_out( t, QLatin1String(
"homephone" ),
112 ldif_out( t, QLatin1String(
"mobile" ),
114 ldif_out( t, QLatin1String(
"cellphone" ),
116 ldif_out( t, QLatin1String(
"pager" ),
118 ldif_out( t, QLatin1String(
"pagerphone" ),
121 ldif_out( t, QLatin1String(
"streethomeaddress" ), homeAddr.
street() );
122 ldif_out( t, QLatin1String(
"postalcode" ), workAddr.
postalCode() );
123 ldif_out( t, QLatin1String(
"postofficebox" ), workAddr.
postOfficeBox() );
125 QStringList streets = homeAddr.
street().split( QLatin1Char(
'\n' ) );
126 const int numberOfStreets( streets.count() );
127 if ( numberOfStreets > 0 ) {
128 ldif_out( t, QLatin1String(
"homepostaladdress" ), streets[ 0 ] );
130 if ( numberOfStreets > 1 ) {
131 ldif_out( t, QLatin1String(
"mozillahomepostaladdress2" ), streets[ 1 ] );
133 ldif_out( t, QLatin1String(
"mozillahomelocalityname" ), homeAddr.
locality() );
134 ldif_out( t, QLatin1String(
"mozillahomestate" ), homeAddr.
region() );
135 ldif_out( t, QLatin1String(
"mozillahomepostalcode" ), homeAddr.
postalCode() );
136 ldif_out( t, QLatin1String(
"mozillahomecountryname" ),
138 ldif_out( t, QLatin1String(
"locality" ), workAddr.
locality() );
139 ldif_out( t, QLatin1String(
"streetaddress" ), workAddr.
street() );
141 streets = workAddr.
street().split( QLatin1Char(
'\n' ) );
142 if ( streets.count() > 0 ) {
143 ldif_out( t, QLatin1String(
"postaladdress" ), streets[ 0 ] );
145 if ( streets.count() > 1 ) {
146 ldif_out( t, QLatin1String(
"mozillapostaladdress2" ), streets[ 1 ] );
149 ldif_out( t, QLatin1String(
"l" ), workAddr.
locality() );
151 ldif_out( t, QLatin1String(
"st" ), workAddr.
region() );
153 ldif_out( t, QLatin1String(
"title" ), addr.
title() );
154 ldif_out( t, QLatin1String(
"vocation" ), addr.
prefix() );
155 ldif_out( t, QLatin1String(
"ou" ), addr.
role() );
156 ldif_out( t, QLatin1String(
"o" ), addr.
organization() );
157 ldif_out( t, QLatin1String(
"organization" ), addr.
organization() );
158 ldif_out( t, QLatin1String(
"organizationname" ), addr.
organization() );
162 ldif_out( t, QLatin1String(
"department" ), addr.
department() );
164 ldif_out( t, QLatin1String(
"department" ), addr.
custom( QLatin1String(
"KADDRESSBOOK" ),
165 QLatin1String(
"X-Department" ) ) );
168 ldif_out( t, QLatin1String(
"workurl" ), addr.
url().prettyUrl() );
169 ldif_out( t, QLatin1String(
"homeurl" ), addr.
url().prettyUrl() );
170 ldif_out( t, QLatin1String(
"mozillahomeurl" ), addr.
url().prettyUrl() );
172 ldif_out( t, QLatin1String(
"description" ), addr.
note() );
174 ldif_out( t, QLatin1String(
"modifytimestamp" ), dateToVCardString( addr.
revision() ) );
177 const QDateTime birthday = addr.
birthday();
178 if ( birthday.isValid() ) {
179 const QDate date = birthday.date();
180 ldif_out( t, QLatin1String(
"birthyear" ), QString::number( date.year() ) );
181 ldif_out( t, QLatin1String(
"birthmonth" ), QString::number( date.month() ) );
182 ldif_out( t, QLatin1String(
"birthday" ), QString::number( date.day() ) );
185 t <<
"objectclass: top\n";
186 t <<
"objectclass: person\n";
187 t <<
"objectclass: organizationalPerson\n";
197 const QDateTime &dt )
199 if ( str.isEmpty() ) {
203 bool endldif =
false, end =
false;
205 Ldif::ParseValue ret;
212 ldif.setLdif( str.toLatin1() );
214 if ( !qdt.isValid() ) {
215 qdt = QDateTime::currentDateTime();
222 ret = ldif.nextItem();
226 QString fieldname = ldif.attr().toLower();
227 QString value = QString::fromUtf8( ldif.value(), ldif.value().size() );
228 evaluatePair( a, homeAddr, workAddr, fieldname, value, birthday, birthmonth, birthyear );
234 QDateTime birthDate( QDate( birthyear, birthmonth, birthday ) );
235 if ( birthDate.isValid() ) {
247 addrList.append( a );
275 QString &fieldname, QString &value,
276 int &birthday,
int &birthmonth,
int &birthyear )
278 if ( fieldname == QLatin1String(
"dn" ) ) {
282 if ( fieldname.startsWith( QLatin1Char(
'#' ) ) ) {
286 if ( fieldname.isEmpty() && !a.
note().isEmpty() ) {
289 a.
setNote( a.
note() + QLatin1Char(
'\n' ) + value );
293 if ( fieldname == QLatin1String(
"givenname" ) ) {
298 if ( fieldname == QLatin1String(
"xmozillanickname" ) ||
299 fieldname == QLatin1String(
"nickname" ) ||
300 fieldname == QLatin1String(
"mozillanickname" ) ) {
305 if ( fieldname == QLatin1String(
"sn" ) ) {
310 if ( fieldname == QLatin1String(
"uid" ) ) {
314 if ( fieldname == QLatin1String(
"mail" ) ||
315 fieldname == QLatin1String(
"mozillasecondemail" ) ) {
316 if ( a.
emails().indexOf( value ) == -1 ) {
322 if ( fieldname == QLatin1String(
"title" ) ) {
327 if ( fieldname == QLatin1String(
"vocation" ) ) {
332 if ( fieldname == QLatin1String(
"cn" ) ) {
337 if ( fieldname == QLatin1String(
"o" ) ||
338 fieldname == QLatin1String(
"organization" ) ||
339 fieldname == QLatin1String(
"organizationname" ) ) {
344 if ( fieldname == QLatin1String(
"description" ) ) {
346 if ( !a.
note().isEmpty() ) {
353 if ( fieldname == QLatin1String(
"custom1" ) ||
354 fieldname == QLatin1String(
"custom2" ) ||
355 fieldname == QLatin1String(
"custom3" ) ||
356 fieldname == QLatin1String(
"custom4" ) ) {
360 if ( fieldname == QLatin1String(
"homeurl" ) ||
361 fieldname == QLatin1String(
"workurl" ) ||
362 fieldname == QLatin1String(
"mozillahomeurl" ) ) {
363 if ( a.
url().isEmpty() ) {
364 a.
setUrl( KUrl( value ) );
367 if ( a.
url().prettyUrl() == KUrl( value ).prettyUrl() ) {
374 if ( fieldname == QLatin1String(
"homephone" ) ) {
379 if ( fieldname == QLatin1String(
"telephonenumber" ) ) {
384 if ( fieldname == QLatin1String(
"mobile" ) ) {
389 if ( fieldname == QLatin1String(
"cellphone" ) ) {
394 if ( fieldname == QLatin1String(
"pager" ) ||
395 fieldname == QLatin1String(
"pagerphone" ) ) {
400 if ( fieldname == QLatin1String(
"facsimiletelephonenumber" ) ) {
405 if ( fieldname == QLatin1String(
"xmozillaanyphone" ) ) {
410 if ( fieldname == QLatin1String(
"streethomeaddress" ) ||
411 fieldname == QLatin1String(
"mozillahomestreet" ) ) {
416 if ( fieldname == QLatin1String(
"street" ) ||
417 fieldname == QLatin1String(
"postaladdress" ) ) {
422 if ( fieldname == QLatin1String(
"mozillapostaladdress2" ) ) {
423 workAddr.
setStreet( workAddr.
street() + QLatin1String(
"\n" ) + value );
427 if ( fieldname == QLatin1String(
"postalcode" ) ) {
432 if ( fieldname == QLatin1String(
"postofficebox" ) ) {
437 if ( fieldname == QLatin1String(
"homepostaladdress" ) ) {
442 if ( fieldname == QLatin1String(
"mozillahomepostaladdress2" ) ) {
443 homeAddr.
setStreet( homeAddr.
street() + QLatin1String(
"\n" ) + value );
447 if ( fieldname == QLatin1String(
"mozillahomelocalityname" ) ) {
452 if ( fieldname == QLatin1String(
"mozillahomestate" ) ) {
457 if ( fieldname == QLatin1String(
"mozillahomepostalcode" ) ) {
462 if ( fieldname == QLatin1String(
"mozillahomecountryname" ) ) {
463 if ( value.length() <= 2 ) {
470 if ( fieldname == QLatin1String(
"locality" ) ) {
475 if ( fieldname == QLatin1String(
"streetaddress" ) ) {
480 if ( fieldname == QLatin1String(
"countryname" ) ||
481 fieldname == QLatin1String(
"c" ) ) {
482 if ( value.length() <= 2 ) {
489 if ( fieldname == QLatin1String(
"l" ) ) {
494 if ( fieldname == QLatin1String(
"st" ) ) {
499 if ( fieldname == QLatin1String(
"ou" ) ) {
504 if ( fieldname == QLatin1String(
"department" ) ) {
509 if ( fieldname == QLatin1String(
"member" ) ) {
511 QStringList list = value.split( QLatin1Char(
',' ) );
514 QStringList::Iterator it;
515 for ( it = list.begin(); it != list.end(); ++it ) {
516 if ( ( *it ).startsWith( QLatin1String(
"cn=" ) ) ) {
517 name = ( *it ).mid( 3 ).trimmed();
519 if ( ( *it ).startsWith( QLatin1String(
"mail=" ) ) ) {
520 email = ( *it ).mid( 5 ).trimmed();
523 if ( !name.isEmpty() && !email.isEmpty() ) {
524 email = QLatin1String(
" <" ) + email + QLatin1Char(
'>' );
531 if ( fieldname == QLatin1String(
"modifytimestamp" ) ) {
532 if ( value == QLatin1String(
"0Z" ) ) {
535 QDateTime dt = VCardStringToDate( value );
536 if ( dt.isValid() ) {
542 if ( fieldname == QLatin1String(
"objectclass" ) ) {
546 if ( fieldname == QLatin1String(
"birthyear" ) ) {
547 birthyear = value.toInt();
550 if ( fieldname == QLatin1String(
"birthmonth" ) ) {
551 birthmonth = value.toInt();
554 if ( fieldname == QLatin1String(
"birthday" ) ) {
555 birthday = value.toInt();
559 kWarning( 5700 ) << QString::fromLatin1(
"LDIFConverter: Unknown field for '%1': '%2=%3'\n" ).
QString custom(const QString &app, const QString &name) const
Return value of custom entry, identified by app and entry name.
QString department() const
Return department.
Postal address information.
void setNickName(const QString &nickName)
Set nick name.
QString name() const
Return name.
void insertEmail(const QString &email, bool preferred=false)
Insert an email address.
QString street() const
Returns the street.
QString title() const
Return title.
void insertPhoneNumber(const PhoneNumber &phoneNumber)
Insert a phone number.
a QValueList of Addressee, with sorting functionality
QString number() const
Returns the phone number.
void setFormattedName(const QString &formattedName)
Set formatted name.
void setRole(const QString &role)
Set role.
QString familyName() const
Return family name.
QString prefix() const
Return honorific prefixes.
void setGivenName(const QString &givenName)
Set given name.
static QByteArray assembleLine(const QString &fieldname, const QByteArray &value, uint linelen=0, bool url=false)
Assembles fieldname and value into a valid Ldif line, BASE64 encodes the value if necessary and optio...
QString formattedName() const
Return formatted name.
void setStreet(const QString &street)
Sets the street (including house number).
QString note() const
Return note.
void setFamilyName(const QString &familyName)
Set family name.
QString postalCode() const
Returns the postal code.
QDateTime birthday() const
Return birthday.
QString country() const
Returns the country.
QString preferredEmail() const
Return preferred email address.
QStringList emails() const
Return list of all email addresses.
void insertCategory(const QString &)
Insert category.
void setPrefix(const QString &prefix)
Set honorific prefixes.
static QString ISOtoCountry(const QString &ISOname)
Returns a localized country name for a ISO code.
QString role() const
Return role.
PhoneNumber phoneNumber(PhoneNumber::Type type) const
Return phone number, which matches the given type.
void setTitle(const QString &title)
Set title.
void setDepartment(const QString &department)
Set department.
void setPostOfficeBox(const QString &postOfficeBox)
Sets the post office box.
void setUid(const QString &uid)
Set unique identifier.
bool LDIFToAddressee(const QString &str, AddresseeList &addrList, const QDateTime &dt=QDateTime::currentDateTime())
Converts a LDIF string to a list of addressees.
void setOrganization(const QString &organization)
Set organization.
void setUrl(const KUrl &url)
Set homepage.
QString givenName() const
Return given name.
void setRegion(const QString ®ion)
Sets the region, e.g.
bool isEmpty() const
Return, if the address book entry is empty.
void setNote(const QString ¬e)
Set note.
void insertAddress(const Address &address)
Insert an address.
QString organization() const
Return organization.
bool addresseeToLDIF(const AddresseeList &addrList, QString &str)
Converts a list of addressees to a LDIF string.
bool evaluatePair(Addressee &a, Address &homeAddr, Address &workAddr, QString &fieldname, QString &value, int &birthday, int &birthmonth, int &birthyear)
void setCountry(const QString &country)
Sets the country.
void setRevision(const QDateTime &revision)
Set revision date.
QString locality() const
Returns the locality.
QString region() const
Returns the region.
QString uid() const
Return unique identifier.
void setLocality(const QString &locality)
Sets the locality, e.g.
void setPostalCode(const QString &code)
Sets the postal code.
void setBirthday(const QDateTime &birthday)
Set birthday.
KUrl url() const
Return homepage.
bool isEmpty() const
Returns true, if the address is empty.
QDateTime revision() const
Return revision date.
Address address(Address::Type type) const
Return address, which matches the given type.
QString postOfficeBox() const
Returns the post office box.
QString nickName() const
Return nick name.