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" ).
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 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.
QString dateToVCardString(const QDateTime &dateTime)
Helper functions.
QDateTime VCardStringToDate(const QString &dateString)
Converts a date string as it is used in VCard and LDIF files to a QDateTime value.
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.
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.
Class that holds a Calendar Url (FBURL/CALADRURI/CALURI)
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.
bool evaluatePair(Addressee &a, Address &homeAddr, Address &workAddr, QString &fieldname, QString &value, int &birthday, int &birthmonth, int &birthyear, ContactGroup &contactGroup)
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.