diff --git a/kabc/errorhandler.cpp b/kabc/errorhandler.cpp index 9656d379b..eb9223375 100644 --- a/kabc/errorhandler.cpp +++ b/kabc/errorhandler.cpp @@ -1,78 +1,78 @@ /* This file is part of libkabc. Copyright (c) 2002 Tobias Koenig Copyright (c) 2003 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "errorhandler.h" #include #include #include #include using namespace KABC; ErrorHandler::~ErrorHandler() { } ConsoleErrorHandler::ConsoleErrorHandler() : d( 0 ) { } ConsoleErrorHandler::~ConsoleErrorHandler() { } void ConsoleErrorHandler::error( const QString &msg ) { // no debug area is ok here - kError() << msg; + kError(5700) << msg; } class GuiErrorHandler::Private { public: Private( QWidget *widget ) : mWidget( widget ) { } QWidget *mWidget; }; GuiErrorHandler::GuiErrorHandler( QWidget *widget ) : d( new Private( widget ) ) { } GuiErrorHandler::~GuiErrorHandler() { delete d; } void GuiErrorHandler::error( const QString &msg ) { if ( qApp ) { KMessageBox::error( d->mWidget, msg ); } } diff --git a/kabc/formatfactory.cpp b/kabc/formatfactory.cpp index 3e966e375..f1a366d8f 100644 --- a/kabc/formatfactory.cpp +++ b/kabc/formatfactory.cpp @@ -1,195 +1,195 @@ /* This file is part of libkabc. Copyright (c) 2002 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "formatfactory.h" #include "vcardformat.h" #include #include #include #include #include #include #include using namespace KABC; class FormatFactory::Private { public: ~Private() { mFormatList.clear(); qRemovePostRoutine( cleanupFormatFactory ); } KLibrary *openLibrary( const QString &libName ); QHash mFormatList; static FormatFactory *sSelf; static void cleanupFormatFactory() { delete sSelf; sSelf = 0; } }; FormatFactory *FormatFactory::Private::sSelf = 0; KLibrary *FormatFactory::Private::openLibrary( const QString &libName ) { KLibrary *library = 0; QString path = KLibLoader::findLibrary( libName ); if ( path.isEmpty() ) { - kDebug( 5700 ) << "No format plugin library was found!"; + kDebug(5700) << "No format plugin library was found!"; return 0; } library = KLibLoader::self()->library( path ); if ( !library ) { - kDebug( 5700 ) << "Could not load library '" << libName << "'"; + kDebug(5700) << "Could not load library '" << libName << "'"; return 0; } return library; } FormatFactory *FormatFactory::self() { kDebug(5700) << "FormatFactory::self()"; static Private p; if ( !p.sSelf ) { p.sSelf = new FormatFactory; qAddPostRoutine( Private::cleanupFormatFactory ); } return p.sSelf; } FormatFactory::FormatFactory() : d( new Private ) { // dummy entry for default format FormatInfo info; info.library = ""; info.nameLabel = i18n( "vCard" ); info.descriptionLabel = i18n( "vCard Format" ); d->mFormatList.insert( "vcard", info ); const QStringList list = KGlobal::dirs()->findAllResources( "data","kabc/formats/*.desktop", KStandardDirs::Recursive | KStandardDirs::NoDuplicates ); for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) { KConfig config( *it, KConfig::SimpleConfig ); if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) ) { continue; } KConfigGroup group = config.group( "Plugin" ); QString type = group.readEntry( "Type" ); info.library = group.readEntry( "X-KDE-Library" ); group = config.group( "Misc" ); info.nameLabel = group.readEntry( "Name" ); info.descriptionLabel = group.readEntry( "Comment", i18n( "No description available." ) ); d->mFormatList.insert( type, info ); } } FormatFactory::~FormatFactory() { delete d; } QStringList FormatFactory::formats() { QStringList retval; // make sure 'vcard' is the first entry retval << "vcard"; QHashIterator it( d->mFormatList ); while ( it.hasNext() ) { it.next(); if ( it.key() != "vcard" ) { retval << it.key(); } } return retval; } FormatInfo FormatFactory::info( const QString &type ) const { if ( type.isEmpty() || !d->mFormatList.contains( type ) ) { return FormatInfo(); } else { return d->mFormatList[ type ]; } } Format *FormatFactory::format( const QString &type ) { Format *format = 0; if ( type.isEmpty() ) { return 0; } if ( type == "vcard" ) { format = new VCardFormat; format->setType( type ); format->setNameLabel( i18n( "vCard" ) ); format->setDescriptionLabel( i18n( "vCard Format" ) ); return format; } if ( !d->mFormatList.contains( type ) ) { return 0; } FormatInfo fi = d->mFormatList[ type ]; QString libName = fi.library; KLibrary *library = d->openLibrary( libName ); if ( !library ) { return 0; } KLibrary::void_function_ptr format_func = library->resolveFunction( "format" ); if ( format_func ) { format = ( (Format *(*)())format_func )(); format->setType( type ); format->setNameLabel( fi.nameLabel ); format->setDescriptionLabel( fi.descriptionLabel ); } else { - kDebug( 5700 ) << "'" << libName << "' is not a format plugin."; + kDebug(5700) << "'" << libName << "' is not a format plugin."; return 0; } return format; } diff --git a/kabc/formats/binaryformat.cpp b/kabc/formats/binaryformat.cpp index 36bd071d8..fd354ef08 100644 --- a/kabc/formats/binaryformat.cpp +++ b/kabc/formats/binaryformat.cpp @@ -1,222 +1,222 @@ /* This file is part of libkabc. Copyright (c) 2002 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "binaryformat.h" #include "kabc/addressbook.h" #include "kabc/addressee.h" #include "kabc/picture.h" #include "kabc/sound.h" #include #include #include #include #include #define BINARY_FORMAT_VERSION 1 using namespace KABC; extern "C" { KDE_EXPORT Format *format() { return new BinaryFormat; } } bool BinaryFormat::load( Addressee &addressee, QFile *file ) { kDebug(5700) << "BinaryFormat::load()"; QDataStream stream( file ); if ( !checkHeader( stream ) ) { return false; } loadAddressee( addressee, stream ); return true; } -bool BinaryFormat::loadAll( AddressBook*, Resource *resource, QFile *file ) +bool BinaryFormat::loadAll( AddressBook *, Resource *resource, QFile *file ) { kDebug(5700) << "BinaryFormat::loadAll()"; QDataStream stream( file ); if ( !checkHeader( stream ) ) { return false; } quint32 entries; stream >> entries; for ( uint i = 0; i < entries; ++i ) { Addressee addressee; loadAddressee( addressee, stream ); addressee.setResource( resource ); addressee.setChanged( false ); resource->insertAddressee( addressee ); } return true; } void BinaryFormat::save( const Addressee &addressee, QFile *file ) { kDebug(5700) << "BinaryFormat::save()"; QDataStream stream( file ); writeHeader( stream ); quint32 entries = 1; stream << entries; saveAddressee( addressee, stream ); } -void BinaryFormat::saveAll( AddressBook*, Resource *resource, QFile *file ) +void BinaryFormat::saveAll( AddressBook *, Resource *resource, QFile *file ) { kDebug(5700) << "BinaryFormat::saveAll()"; quint32 counter = 0; QDataStream stream( file ); writeHeader( stream ); // set dummy number of entries stream << counter; Resource::Iterator it; for ( it = resource->begin(); it != resource->end(); ++it ) { saveAddressee( (*it), stream ); counter++; (*it).setChanged( false ); } // set real number of entries stream.device()->seek( 2 * sizeof( quint32 ) ); stream << counter; } bool BinaryFormat::checkFormat( QFile *file ) const { kDebug(5700) << "BinaryFormat::checkFormat()"; QDataStream stream( file ); return checkHeader( stream ); } bool BinaryFormat::checkHeader( QDataStream &stream ) const { quint32 magic, version; stream >> magic >> version; QFile *file = dynamic_cast( stream.device() ); if ( !file ) { - kError() << i18n( "Not a file?" ); + kError(5700) << i18n( "Not a file?" ); return false; } if ( magic != 0x2e93e ) { - kError() << i18n( "File '%1' is not binary format.", file->fileName() ); + kError(5700) << i18n( "File '%1' is not binary format.", file->fileName() ); return false; } if ( version != BINARY_FORMAT_VERSION ) { - kError() << i18n( "File '%1' is the wrong version.", file->fileName() ); + kError(5700) << i18n( "File '%1' is the wrong version.", file->fileName() ); return false; } return true; } void BinaryFormat::writeHeader( QDataStream &stream ) { quint32 magic, version; magic = 0x2e93e; version = BINARY_FORMAT_VERSION; stream << magic << version; } void BinaryFormat::loadAddressee( Addressee &addressee, QDataStream &stream ) { stream >> addressee; /* // load pictures Picture photo = addressee.photo(); Picture logo = addressee.logo(); if ( photo.isIntern() ) { QImage img; if ( !img.load( locateLocal( "data", "kabc/photos/" ) + addressee.uid() ) ) kDebug(5700) << "No photo available for '" << addressee.uid() << "'."; addressee.setPhoto( img ); } if ( logo.isIntern() ) { QImage img; if ( !img.load( locateLocal( "data", "kabc/logos/" ) + addressee.uid() ) ) kDebug(5700) << "No logo available for '" << addressee.uid() << "'."; addressee.setLogo( img ); } // load sound // TODO: load sound data from file */ } void BinaryFormat::saveAddressee( const Addressee &addressee, QDataStream &stream ) { stream << addressee; /* // load pictures Picture photo = addressee.photo(); Picture logo = addressee.logo(); if ( photo.isIntern() ) { QImage img = photo.data(); QString fileName = locateLocal( "data", "kabc/photos/" ) + addressee.uid(); if ( !img.save( fileName, "PNG" ) ) kDebug(5700) << "Unable to save photo for '" << addressee.uid() << "'."; } if ( logo.isIntern() ) { QImage img = logo.data(); QString fileName = locateLocal( "data", "kabc/logos/" ) + addressee.uid(); if ( !img.save( fileName, "PNG" ) ) kDebug(5700) << "Unable to save logo for '" << addressee.uid() << "'."; } // save sound // TODO: save the sound data to file */ } diff --git a/kabc/ldifconverter.cpp b/kabc/ldifconverter.cpp index aa4073592..a6436bc56 100644 --- a/kabc/ldifconverter.cpp +++ b/kabc/ldifconverter.cpp @@ -1,513 +1,514 @@ /* This file is part of libkabc. Copyright (c) 2003 Helge Deller This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* Useful links: - http://tldp.org/HOWTO/LDAP-Implementation-HOWTO/schemas.html - http://www.faqs.org/rfcs/rfc2849.html Not yet handled items: - objectclass microsoftaddressbook - info, - initials, - otherfacsimiletelephonenumber, - otherpager, - physicaldeliveryofficename, */ #include "ldifconverter.h" #include "vcardconverter.h" #include "address.h" #include "addressee.h" #include "kldap/ldif.h" #include #include #include #include #include #include using namespace KABC; /* generate LDIF stream */ bool LDIFConverter::addresseeToLDIF( const AddresseeList &addrList, QString &str ) { AddresseeList::ConstIterator it; for ( it = addrList.begin(); it != addrList.end(); ++it ) { addresseeToLDIF( *it, str ); } return true; } static void ldif_out( QTextStream &t, const QString &formatStr, const QString &value ) { if ( value.isEmpty() ) { return; } QByteArray txt = KLDAP::Ldif::assembleLine( formatStr, value, 72 ); // write the string t << QString::fromUtf8( txt ) << "\n"; } bool LDIFConverter::addresseeToLDIF( const Addressee &addr, QString &str ) { if ( addr.isEmpty() ) { return false; } QTextStream t( &str, QIODevice::WriteOnly|QIODevice::Append ); t.setCodec( QTextCodec::codecForName( "UTF-8" ) ); const Address homeAddr = addr.address( Address::Home ); const Address workAddr = addr.address( Address::Work ); ldif_out( t, "dn", QString( "cn=%1,mail=%2" ). arg( addr.formattedName().simplified() ). arg( addr.preferredEmail() ) ); ldif_out( t, "givenname", addr.givenName() ); ldif_out( t, "sn", addr.familyName() ); ldif_out( t, "cn", addr.formattedName().simplified() ); ldif_out( t, "uid", addr.uid() ); ldif_out( t, "nickname", addr.nickName() ); ldif_out( t, "xmozillanickname", addr.nickName() ); ldif_out( t, "mail", addr.preferredEmail() ); if ( addr.emails().count() > 1 ) { ldif_out( t, "mozillasecondemail", addr.emails()[ 1 ] ); } //ldif_out( t, "mozilla_AIMScreenName: %1\n", "screen_name" ); ldif_out( t, "telephonenumber", addr.phoneNumber( PhoneNumber::Work ).number() ); ldif_out( t, "facsimiletelephonenumber", addr.phoneNumber( PhoneNumber::Fax ).number() ); ldif_out( t, "homephone", addr.phoneNumber( PhoneNumber::Home ).number() ); ldif_out( t, "mobile", addr.phoneNumber( PhoneNumber::Cell ).number() ); // Netscape 7 ldif_out( t, "cellphone", addr.phoneNumber( PhoneNumber::Cell ).number() ); // Netscape 4.x ldif_out( t, "pager", addr.phoneNumber( PhoneNumber::Pager ).number() ); ldif_out( t, "pagerphone", addr.phoneNumber( PhoneNumber::Pager ).number() ); ldif_out( t, "streethomeaddress", homeAddr.street() ); ldif_out( t, "postalcode", workAddr.postalCode() ); ldif_out( t, "postofficebox", workAddr.postOfficeBox() ); QStringList streets = homeAddr.street().split( '\n' ); if ( streets.count() > 0 ) { ldif_out( t, "homepostaladdress", streets[ 0 ] ); // Netscape 7 } if ( streets.count() > 1 ) { ldif_out( t, "mozillahomepostaladdress2", streets[ 1 ] ); // Netscape 7 } ldif_out( t, "mozillahomelocalityname", homeAddr.locality() ); // Netscape 7 ldif_out( t, "mozillahomestate", homeAddr.region() ); ldif_out( t, "mozillahomepostalcode", homeAddr.postalCode() ); ldif_out( t, "mozillahomecountryname", Address::ISOtoCountry( homeAddr.country() ) ); ldif_out( t, "locality", workAddr.locality() ); ldif_out( t, "streetaddress", workAddr.street() ); // Netscape 4.x streets = workAddr.street().split( '\n' ); if ( streets.count() > 0 ) { ldif_out( t, "postaladdress", streets[ 0 ] ); } if ( streets.count() > 1 ) { ldif_out( t, "mozillapostaladdress2", streets[ 1 ] ); } ldif_out( t, "countryname", Address::ISOtoCountry( workAddr.country() ) ); ldif_out( t, "l", workAddr.locality() ); ldif_out( t, "c", Address::ISOtoCountry( workAddr.country() ) ); ldif_out( t, "st", workAddr.region() ); ldif_out( t, "title", addr.title() ); ldif_out( t, "vocation", addr.prefix() ); ldif_out( t, "ou", addr.role() ); ldif_out( t, "o", addr.organization() ); ldif_out( t, "organization", addr.organization() ); ldif_out( t, "organizationname", addr.organization() ); // Compatibility with older kabc versions. - if ( !addr.department().isEmpty() ) + if ( !addr.department().isEmpty() ) { ldif_out( t, "department", addr.department() ); - else - ldif_out( t, "department", addr.custom("KADDRESSBOOK", "X-Department") ); + } else { + ldif_out( t, "department", addr.custom( "KADDRESSBOOK", "X-Department" ) ); + } ldif_out( t, "workurl", addr.url().prettyUrl() ); ldif_out( t, "homeurl", addr.url().prettyUrl() ); ldif_out( t, "description", addr.note() ); if ( addr.revision().isValid() ) { ldif_out( t, "modifytimestamp", dateToVCardString( addr.revision() ) ); } t << "objectclass: top\n"; t << "objectclass: person\n"; t << "objectclass: organizationalPerson\n"; t << "\n"; return true; } /* convert from LDIF stream */ bool LDIFConverter::LDIFToAddressee( const QString &str, AddresseeList &addrList, const QDateTime &dt ) { if ( str.isEmpty() ) { return true; } bool endldif = false, end = false; KLDAP::Ldif ldif; KLDAP::Ldif::ParseValue ret; Addressee a; Address homeAddr, workAddr; ldif.setLdif( str.toLatin1() ); QDateTime qdt = dt; if ( !qdt.isValid() ) { qdt = QDateTime::currentDateTime(); } a.setRevision( qdt ); homeAddr = Address( Address::Home ); workAddr = Address( Address::Work ); do { ret = ldif.nextItem(); switch ( ret ) { case KLDAP::Ldif::Item: { QString fieldname = ldif.attr().toLower(); QString value = QString::fromUtf8( ldif.value(), ldif.value().size() ); evaluatePair( a, homeAddr, workAddr, fieldname, value ); break; } case KLDAP::Ldif::EndEntry: // if the new address is not empty, append it if ( !a.formattedName().isEmpty() || !a.name().isEmpty() || !a.familyName().isEmpty() ) { if ( !homeAddr.isEmpty() ) { a.insertAddress( homeAddr ); } if ( !workAddr.isEmpty() ) { a.insertAddress( workAddr ); } addrList.append( a ); } a = Addressee(); a.setRevision( qdt ); homeAddr = Address( Address::Home ); workAddr = Address( Address::Work ); break; case KLDAP::Ldif::MoreData: { if ( endldif ) { end = true; } else { ldif.endLdif(); endldif = true; break; } } default: break; } } while ( !end ); return true; } bool LDIFConverter::evaluatePair( Addressee &a, Address &homeAddr, Address &workAddr, QString &fieldname, QString &value ) { if ( fieldname == QLatin1String( "dn" ) ) { // ignore & return false! return false; } if ( fieldname.startsWith( '#' ) ) { return true; } if ( fieldname.isEmpty() && !a.note().isEmpty() ) { // some LDIF export filters are borken and add additional // comments on stand-alone lines. Just add them to the notes for now. a.setNote( a.note() + '\n' + value ); return true; } if ( fieldname == QLatin1String( "givenname" ) ) { a.setGivenName( value ); return true; } if ( fieldname == QLatin1String( "xmozillanickname" ) || fieldname == QLatin1String( "nickname" ) ) { a.setNickName( value ); return true; } if ( fieldname == QLatin1String( "sn" ) ) { a.setFamilyName( value ); return true; } if ( fieldname == QLatin1String( "uid" ) ) { a.setUid( value ); return true; } if ( fieldname == QLatin1String( "mail" ) || fieldname == QLatin1String( "mozillasecondemail" ) ) { // mozilla if ( a.emails().indexOf( value ) == -1 ) { a.insertEmail( value ); } return true; } if ( fieldname == QLatin1String( "title" ) ) { a.setTitle( value ); return true; } if ( fieldname == QLatin1String( "vocation" ) ) { a.setPrefix( value ); return true; } if ( fieldname == QLatin1String( "cn" ) ) { a.setFormattedName( value ); return true; } if ( fieldname == QLatin1String( "o" ) || fieldname == QLatin1String( "organization" ) || // Exchange fieldname == QLatin1String( "organizationname" ) ) { // Exchange a.setOrganization( value ); return true; } if ( fieldname == QLatin1String( "description" ) ) { addComment: if ( !a.note().isEmpty() ) { a.setNote( a.note() + '\n' ); } a.setNote( a.note() + value ); return true; } if ( fieldname == QLatin1String( "custom1" ) || fieldname == QLatin1String( "custom2" ) || fieldname == QLatin1String( "custom3" ) || fieldname == QLatin1String( "custom4" ) ) { goto addComment; } if ( fieldname == QLatin1String( "homeurl" ) || fieldname == QLatin1String( "workurl" ) ) { if ( a.url().isEmpty() ) { a.setUrl( KUrl( value ) ); return true; } if ( a.url().prettyUrl() == KUrl( value ).prettyUrl() ) { return true; } // TODO: current version of kabc only supports one URL. // TODO: change this with KDE 4 } if ( fieldname == QLatin1String( "homephone" ) ) { a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Home ) ); return true; } if ( fieldname == QLatin1String( "telephonenumber" ) ) { a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Work ) ); return true; } if ( fieldname == QLatin1String( "mobile" ) ) { // mozilla/Netscape 7 a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Cell ) ); return true; } if ( fieldname == QLatin1String( "cellphone" ) ) { a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Cell ) ); return true; } if ( fieldname == QLatin1String( "pager" ) || // mozilla fieldname == QLatin1String( "pagerphone" ) ) { // mozilla a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Pager ) ); return true; } if ( fieldname == QLatin1String( "facsimiletelephonenumber" ) ) { a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Fax ) ); return true; } if ( fieldname == QLatin1String( "xmozillaanyphone" ) ) { // mozilla a.insertPhoneNumber( PhoneNumber( value, PhoneNumber::Work ) ); return true; } if ( fieldname == QLatin1String( "street" ) || fieldname == QLatin1String( "streethomeaddress" ) ) { homeAddr.setStreet( value ); return true; } if ( fieldname == QLatin1String( "postaladdress" ) ) { // mozilla workAddr.setStreet( value ); return true; } if ( fieldname == QLatin1String( "mozillapostaladdress2" ) ) { // mozilla workAddr.setStreet( workAddr.street() + QLatin1String( "\n" ) + value ); return true; } if ( fieldname == QLatin1String( "postalcode" ) ) { workAddr.setPostalCode( value ); return true; } if ( fieldname == QLatin1String( "postofficebox" ) ) { workAddr.setPostOfficeBox( value ); return true; } if ( fieldname == QLatin1String( "homepostaladdress" ) ) { // Netscape 7 homeAddr.setStreet( value ); return true; } if ( fieldname == QLatin1String( "mozillahomepostaladdress2" ) ) { // mozilla homeAddr.setStreet( homeAddr.street() + QLatin1String( "\n" ) + value ); return true; } if ( fieldname == QLatin1String( "mozillahomelocalityname" ) ) { // mozilla homeAddr.setLocality( value ); return true; } if ( fieldname == QLatin1String( "mozillahomestate" ) ) { // mozilla homeAddr.setRegion( value ); return true; } if ( fieldname == QLatin1String( "mozillahomepostalcode" ) ) { // mozilla homeAddr.setPostalCode( value ); return true; } if ( fieldname == QLatin1String( "mozillahomecountryname" ) ) { // mozilla if ( value.length() <= 2 ) { value = Address::ISOtoCountry( value ); } homeAddr.setCountry( value ); return true; } if ( fieldname == QLatin1String( "locality" ) ) { workAddr.setLocality( value ); return true; } if ( fieldname == QLatin1String( "streetaddress" ) ) { // Netscape 4.x workAddr.setStreet( value ); return true; } if ( fieldname == QLatin1String( "countryname" ) || fieldname == QLatin1String( "c" ) ) { // mozilla if ( value.length() <= 2 ) { value = Address::ISOtoCountry( value ); } workAddr.setCountry( value ); return true; } if ( fieldname == QLatin1String( "l" ) ) { // mozilla workAddr.setLocality( value ); return true; } if ( fieldname == QLatin1String( "st" ) ) { workAddr.setRegion( value ); return true; } if ( fieldname == QLatin1String( "ou" ) ) { a.setRole( value ); return true; } if ( fieldname == QLatin1String( "department" ) ) { a.setDepartment( value ); return true; } if ( fieldname == QLatin1String( "member" ) ) { // this is a mozilla list member (cn=xxx, mail=yyy) QStringList list = value.split( ',' ); QString name, email; QStringList::Iterator it; for ( it = list.begin(); it != list.end(); ++it ) { if ( (*it).startsWith( "cn=" ) ) { name = (*it).mid( 3 ).trimmed(); } if ( (*it).startsWith( "mail=" ) ) { email = (*it).mid( 5 ).trimmed(); } } if ( !name.isEmpty() && !email.isEmpty() ) { email = " <" + email + '>'; } a.insertEmail( name + email ); a.insertCategory( i18n( "List of Emails" ) ); return true; } if ( fieldname == QLatin1String( "modifytimestamp" ) ) { if ( value == QLatin1String( "0Z" ) ) { // ignore return true; } QDateTime dt = VCardStringToDate( value ); if ( dt.isValid() ) { a.setRevision( dt ); return true; } } if ( fieldname == QLatin1String( "objectclass" ) ) { // ignore return true; } - kWarning() << QString( "LDIFConverter: Unknown field for '%1': '%2=%3'\n" ). + kWarning(5700) << QString( "LDIFConverter: Unknown field for '%1': '%2=%3'\n" ). arg( a.formattedName() ).arg( fieldname ).arg( value ); return true; } diff --git a/kabc/locknull.cpp b/kabc/locknull.cpp index 33b686972..eff1aa526 100644 --- a/kabc/locknull.cpp +++ b/kabc/locknull.cpp @@ -1,78 +1,78 @@ /* This file is part of libkabc. Copyright (c) 2003 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "locknull.h" #include #include using namespace KABC; class LockNull::Private { public: Private( bool allowAccess ) : mAllowAccess( allowAccess ) { } bool mAllowAccess; }; LockNull::LockNull( bool allowAccess ) : Lock( QString() ), d( new Private( allowAccess ) ) { } LockNull::~LockNull() { unlock(); delete d; } bool LockNull::lock() { if ( !d->mAllowAccess ) { return false; } - kWarning() << "LockNull::lock() force success. Doesn't actually lock."; + kWarning(5700) << "LockNull::lock() force success. Doesn't actually lock."; emit locked(); return true; } bool LockNull::unlock() { emit unlocked(); return true; } QString LockNull::error() const { if ( d->mAllowAccess ) { return i18n( "LockNull: All locks succeed but no actual locking is done." ); } else { return i18n( "LockNull: All locks fail." ); } } diff --git a/kabc/plugins/ldapkio/resourceldapkio.cpp b/kabc/plugins/ldapkio/resourceldapkio.cpp index c5c06b4ee..32da0a5ff 100644 --- a/kabc/plugins/ldapkio/resourceldapkio.cpp +++ b/kabc/plugins/ldapkio/resourceldapkio.cpp @@ -1,1125 +1,1125 @@ // -*- c-basic-offset: 2 -*- /* This file is part of libkabc. Copyright (c) 2003 Tobias Koenig Copyright (c) 2004 Szombathelyi György This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "resourceldapkio.h" #include "resourceldapkioconfig.h" #include "kldap/ldif.h" #include "kldap/ldapdn.h" #include "kldap/ldapurl.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace KABC; class ResourceLDAPKIO::Private { public: Private( ResourceLDAPKIO *parent ) : mParent( parent ), mPort( 389 ), mAnonymous( true ), mTLS( false ), mSSL( false ), mSubTree( false ), mSASL( false ), mVer( 3 ), mRDNPrefix( 0 ), mTimeLimit( 0 ), mSizeLimit( 0 ), mCachePolicy( Cache_No ), mAutoCache( true ) { } KIO::Job *loadFromCache(); void createCache(); void activateCache(); void enter_loop(); QByteArray addEntry( const QString &attr, const QString &value, bool mod ); QString findUid( const QString &uid ); bool AddresseeToLDIF( QByteArray &ldif, const Addressee &addr, const QString &olddn ); ResourceLDAPKIO *mParent; QString mUser; QString mPassword; QString mDn; QString mHost; QString mFilter; int mPort; bool mAnonymous; QMap mAttributes; QString mErrorMsg; KLDAP::Ldif mLdif; bool mTLS, mSSL, mSubTree; QString mResultDn; Addressee mAddr; Address mAd; Resource::Iterator mSaveIt; bool mSASL; QString mMech; QString mRealm, mBindDN; KLDAP::LdapUrl mLDAPUrl; int mVer; int mRDNPrefix; int mTimeLimit; int mSizeLimit; int mError; int mCachePolicy; bool mReadOnly; bool mAutoCache; QString mCacheDst; KTemporaryFile *mTmp; }; ResourceLDAPKIO::ResourceLDAPKIO() : Resource(), d( new Private( this ) ) { d->mCacheDst = KGlobal::dirs()->saveLocation( "cache", "ldapkio" ) + '/' + type() + '_' + identifier(); init(); } ResourceLDAPKIO::ResourceLDAPKIO( const KConfigGroup &group ) : Resource( group ), d( new Private( this ) ) { QMap attrList; QStringList attributes = group.readEntry( "LdapAttributes", QStringList() ); for ( int pos = 0; pos < attributes.count(); pos += 2 ) { d->mAttributes.insert( attributes[ pos ], attributes[ pos + 1 ] ); } d->mUser = group.readEntry( "LdapUser" ); d->mPassword = KStringHandler::obscure( group.readEntry( "LdapPassword" ) ); d->mDn = group.readEntry( "LdapDn" ); d->mHost = group.readEntry( "LdapHost" ); d->mPort = group.readEntry( "LdapPort", 389 ); d->mFilter = group.readEntry( "LdapFilter" ); d->mAnonymous = group.readEntry( "LdapAnonymous", false ); d->mTLS = group.readEntry( "LdapTLS", false ); d->mSSL = group.readEntry( "LdapSSL", false ); d->mSubTree = group.readEntry( "LdapSubTree", false ); d->mSASL = group.readEntry( "LdapSASL", false ); d->mMech = group.readEntry( "LdapMech" ); d->mRealm = group.readEntry( "LdapRealm" ); d->mBindDN = group.readEntry( "LdapBindDN" ); d->mVer = group.readEntry( "LdapVer", 3 ); d->mTimeLimit = group.readEntry( "LdapTimeLimit", 0 ); d->mSizeLimit = group.readEntry( "LdapSizeLimit", 0 ); d->mRDNPrefix = group.readEntry( "LdapRDNPrefix", 0 ); d->mCachePolicy = group.readEntry( "LdapCachePolicy", 0 ); d->mAutoCache = group.readEntry( "LdapAutoCache", true ); d->mCacheDst = KGlobal::dirs()->saveLocation( "cache", "ldapkio" ) + '/' + type() + '_' + identifier(); init(); } ResourceLDAPKIO::~ResourceLDAPKIO() { delete d; } void ResourceLDAPKIO::Private::enter_loop() { QEventLoop eventLoop; mParent->connect( mParent, SIGNAL( leaveModality() ), &eventLoop, SLOT( quit() ) ); eventLoop.exec( QEventLoop::ExcludeUserInputEvents ); } -void ResourceLDAPKIO::entries( KIO::Job*, const KIO::UDSEntryList & list ) +void ResourceLDAPKIO::entries( KIO::Job *, const KIO::UDSEntryList &list ) { KIO::UDSEntryList::ConstIterator it = list.begin(); KIO::UDSEntryList::ConstIterator end = list.end(); for ( ; it != end; ++it ) { const QString urlStr = (*it).stringValue( KIO::UDSEntry::UDS_URL ); if ( !urlStr.isEmpty() ) { KUrl tmpurl( urlStr ); d->mResultDn = tmpurl.path(); - kDebug(7125) << "findUid():" << d->mResultDn; + kDebug(5700) << "findUid():" << d->mResultDn; if ( d->mResultDn.startsWith( '/' ) ) { d->mResultDn.remove( 0, 1 ); } return; } } } void ResourceLDAPKIO::listResult( KJob *job ) { d->mError = job->error(); if ( d->mError && d->mError != KIO::ERR_USER_CANCELED ) { d->mErrorMsg = job->errorString(); } else { d->mErrorMsg = ""; } emit leaveModality(); } QString ResourceLDAPKIO::Private::findUid( const QString &uid ) { KLDAP::LdapUrl url( mLDAPUrl ); KIO::UDSEntry entry; mErrorMsg.clear(); mResultDn.clear(); url.setAttributes( QStringList( "dn" ) ); url.setFilter( '(' + mAttributes[ "uid" ] + '=' + uid + ')' + mFilter ); url.setExtension( "x-dir", "one" ); - kDebug(7125) << "ResourceLDAPKIO::findUid() uid:" << uid << "url" << + kDebug(5700) << "ResourceLDAPKIO::findUid() uid:" << uid << "url" << url.prettyUrl(); KIO::ListJob *listJob = KIO::listDir( url, KIO::HideProgressInfo ); mParent->connect( listJob, SIGNAL( entries( KIO::Job *, const KIO::UDSEntryList& ) ), SLOT( entries( KIO::Job*, const KIO::UDSEntryList& ) ) ); mParent->connect( listJob, SIGNAL( result( KJob* ) ), mParent, SLOT( listResult( KJob* ) ) ); enter_loop(); return mResultDn; } QByteArray ResourceLDAPKIO::Private::addEntry( const QString &attr, const QString &value, bool mod ) { QByteArray tmp; if ( !attr.isEmpty() ) { if ( mod ) { tmp += KLDAP::Ldif::assembleLine( "replace", attr ) + '\n'; } tmp += KLDAP::Ldif::assembleLine( attr, value ) + '\n'; if ( mod ) { tmp += "-\n"; } } return tmp; } bool ResourceLDAPKIO::Private::AddresseeToLDIF( QByteArray &ldif, const Addressee &addr, const QString &olddn ) { QByteArray tmp; QString dn; QByteArray data; bool mod = false; if ( olddn.isEmpty() ) { //insert new entry switch ( mRDNPrefix ) { case 1: dn = mAttributes[ "uid" ] + '=' + addr.uid() + ',' + mDn; break; case 0: default: dn = mAttributes[ "commonName" ] + '=' + addr.assembledName() + ',' + mDn; break; } } else { //modify existing entry mod = true; if ( olddn.startsWith( mAttributes[ "uid" ] ) ) { dn = mAttributes[ "uid" ] + '=' + addr.uid() + ',' + olddn.section( ',', 1 ); } else if ( olddn.startsWith( mAttributes[ "commonName" ] ) ) { dn = mAttributes[ "commonName" ] + '=' + addr.assembledName() + ',' + olddn.section( ',', 1 ); } else { dn = olddn; } if ( olddn.toLower() != dn.toLower() ) { tmp = KLDAP::Ldif::assembleLine( "dn", olddn ) + '\n'; tmp += "changetype: modrdn\n"; tmp += KLDAP::Ldif::assembleLine( "newrdn", dn.section( ',', 0, 0 ) ) + '\n'; tmp += "deleteoldrdn: 1\n\n"; } } tmp += KLDAP::Ldif::assembleLine( "dn", dn ) + '\n'; if ( mod ) { tmp += "changetype: modify\n"; } if ( !mod ) { tmp += "objectClass: top\n"; QStringList obclass = mAttributes[ "objectClass" ].split( ',', QString::SkipEmptyParts ); for ( QStringList::const_iterator it = obclass.constBegin(); it != obclass.constEnd(); ++it ) { tmp += KLDAP::Ldif::assembleLine( "objectClass", *it ) + '\n'; } } tmp += addEntry( mAttributes[ "commonName" ], addr.assembledName(), mod ); tmp += addEntry( mAttributes[ "formattedName" ], addr.formattedName(), mod ); tmp += addEntry( mAttributes[ "givenName" ], addr.givenName(), mod ); tmp += addEntry( mAttributes[ "familyName" ], addr.familyName(), mod ); tmp += addEntry( mAttributes[ "uid" ], addr.uid(), mod ); PhoneNumber number; number = addr.phoneNumber( PhoneNumber::Home ); tmp += addEntry( mAttributes[ "phoneNumber" ], number.number().toUtf8(), mod ); number = addr.phoneNumber( PhoneNumber::Work ); tmp += addEntry( mAttributes[ "telephoneNumber" ], number.number().toUtf8(), mod ); number = addr.phoneNumber( PhoneNumber::Fax ); tmp += addEntry( mAttributes[ "facsimileTelephoneNumber" ], number.number().toUtf8(), mod ); number = addr.phoneNumber( PhoneNumber::Cell ); tmp += addEntry( mAttributes[ "mobile" ], number.number().toUtf8(), mod ); number = addr.phoneNumber( PhoneNumber::Pager ); tmp += addEntry( mAttributes[ "pager" ], number.number().toUtf8(), mod ); tmp += addEntry( mAttributes[ "description" ], addr.note(), mod ); tmp += addEntry( mAttributes[ "title" ], addr.title(), mod ); tmp += addEntry( mAttributes[ "organization" ], addr.organization(), mod ); Address ad = addr.address( Address::Home ); if ( !ad.isEmpty() ) { tmp += addEntry( mAttributes[ "street" ], ad.street(), mod ); tmp += addEntry( mAttributes[ "state" ], ad.region(), mod ); tmp += addEntry( mAttributes[ "city" ], ad.locality(), mod ); tmp += addEntry( mAttributes[ "postalcode" ], ad.postalCode(), mod ); } QStringList emails = addr.emails(); QStringList::ConstIterator mailIt = emails.begin(); if ( !mAttributes[ "mail" ].isEmpty() ) { if ( mod ) { tmp += KLDAP::Ldif::assembleLine( "replace", mAttributes[ "mail" ] ) + '\n'; } if ( mailIt != emails.end() ) { tmp += KLDAP::Ldif::assembleLine( mAttributes[ "mail" ], *mailIt ) + '\n'; mailIt ++; } if ( mod && mAttributes[ "mail" ] != mAttributes[ "mailAlias" ] ) { tmp += "-\n"; } } if ( !mAttributes[ "mailAlias" ].isEmpty() ) { if ( mod && mAttributes[ "mail" ] != mAttributes[ "mailAlias" ] ) { tmp += KLDAP::Ldif::assembleLine( "replace", mAttributes[ "mailAlias" ] ) + '\n'; } for ( ; mailIt != emails.end(); ++mailIt ) { tmp += KLDAP::Ldif::assembleLine( mAttributes[ "mailAlias" ], *mailIt ) + '\n'; } if ( mod ) { tmp += "-\n"; } } if ( !mAttributes[ "jpegPhoto" ].isEmpty() ) { QByteArray pic; QBuffer buffer( &pic ); buffer.open( QIODevice::WriteOnly ); addr.photo().data().save( &buffer, "JPEG" ); if ( mod ) { tmp += KLDAP::Ldif::assembleLine( "replace", mAttributes[ "jpegPhoto" ] ) + '\n'; } tmp += KLDAP::Ldif::assembleLine( mAttributes[ "jpegPhoto" ], pic, 76 ) + '\n'; if ( mod ) { tmp += "-\n"; } } tmp += '\n'; - kDebug(7125) << "ldif:" << QString::fromUtf8( tmp ); + kDebug(5700) << "ldif:" << QString::fromUtf8( tmp ); ldif = tmp; return true; } void ResourceLDAPKIO::setReadOnly( bool value ) { //save the original readonly flag, because offline using disables writing d->mReadOnly = true; Resource::setReadOnly( value ); } void ResourceLDAPKIO::init() { if ( d->mPort == 0 ) { d->mPort = 389; } /** If you want to add new attributes, append them here, add a translation string in the ctor of AttributesDialog and handle them in the load() method below. These are the default values */ if ( !d->mAttributes.contains( "objectClass" ) ) { d->mAttributes.insert( "objectClass", "inetOrgPerson" ); } if ( !d->mAttributes.contains( "commonName" ) ) { d->mAttributes.insert( "commonName", "cn" ); } if ( !d->mAttributes.contains( "formattedName" ) ) { d->mAttributes.insert( "formattedName", "displayName" ); } if ( !d->mAttributes.contains( "familyName" ) ) { d->mAttributes.insert( "familyName", "sn" ); } if ( !d->mAttributes.contains( "givenName" ) ) { d->mAttributes.insert( "givenName", "givenName" ); } if ( !d->mAttributes.contains( "mail" ) ) { d->mAttributes.insert( "mail", "mail" ); } if ( !d->mAttributes.contains( "mailAlias" ) ) { d->mAttributes.insert( "mailAlias", "" ); } if ( !d->mAttributes.contains( "phoneNumber" ) ) { d->mAttributes.insert( "phoneNumber", "homePhone" ); } if ( !d->mAttributes.contains( "telephoneNumber" ) ) { d->mAttributes.insert( "telephoneNumber", "telephoneNumber" ); } if ( !d->mAttributes.contains( "facsimileTelephoneNumber" ) ) { d->mAttributes.insert( "facsimileTelephoneNumber", "facsimileTelephoneNumber" ); } if ( !d->mAttributes.contains( "mobile" ) ) { d->mAttributes.insert( "mobile", "mobile" ); } if ( !d->mAttributes.contains( "pager" ) ) { d->mAttributes.insert( "pager", "pager" ); } if ( !d->mAttributes.contains( "description" ) ) { d->mAttributes.insert( "description", "description" ); } if ( !d->mAttributes.contains( "title" ) ) { d->mAttributes.insert( "title", "title" ); } if ( !d->mAttributes.contains( "street" ) ) { d->mAttributes.insert( "street", "street" ); } if ( !d->mAttributes.contains( "state" ) ) { d->mAttributes.insert( "state", "st" ); } if ( !d->mAttributes.contains( "city" ) ) { d->mAttributes.insert( "city", "l" ); } if ( !d->mAttributes.contains( "organization" ) ) { d->mAttributes.insert( "organization", "o" ); } if ( !d->mAttributes.contains( "postalcode" ) ) { d->mAttributes.insert( "postalcode", "postalCode" ); } if ( !d->mAttributes.contains( "uid" ) ) { d->mAttributes.insert( "uid", "uid" ); } if ( !d->mAttributes.contains( "jpegPhoto" ) ) { d->mAttributes.insert( "jpegPhoto", "jpegPhoto" ); } d->mLDAPUrl = KLDAP::LdapUrl( KUrl() ); if ( !d->mAnonymous ) { d->mLDAPUrl.setUser( d->mUser ); d->mLDAPUrl.setPass( d->mPassword ); } d->mLDAPUrl.setProtocol( d->mSSL ? "ldaps" : "ldap" ); d->mLDAPUrl.setHost( d->mHost ); d->mLDAPUrl.setPort( d->mPort ); d->mLDAPUrl.setDn( KLDAP::LdapDN( d->mDn ) ); if ( !d->mAttributes.empty() ) { QMap::Iterator it; QStringList attr; for ( it = d->mAttributes.begin(); it != d->mAttributes.end(); ++it ) { if ( !it.value().isEmpty() && it.key() != "objectClass" ) { attr.append( it.value() ); } } d->mLDAPUrl.setAttributes( attr ); } d->mLDAPUrl.setScope( d->mSubTree ? KLDAP::LdapUrl::Sub : KLDAP::LdapUrl::One ); if ( !d->mFilter.isEmpty() && d->mFilter != "(objectClass=*)" ) { d->mLDAPUrl.setFilter( d->mFilter ); } d->mLDAPUrl.setExtension( "x-dir", "base" ); if ( d->mTLS ) { d->mLDAPUrl.setExtension( "x-tls", "" ); } d->mLDAPUrl.setExtension( "x-ver", QString::number( d->mVer ) ); if ( d->mSizeLimit ) { d->mLDAPUrl.setExtension( "x-sizelimit", QString::number( d->mSizeLimit ) ); } if ( d->mTimeLimit ) { d->mLDAPUrl.setExtension( "x-timelimit", QString::number( d->mTimeLimit ) ); } if ( d->mSASL ) { d->mLDAPUrl.setExtension( "x-sasl", "" ); if ( !d->mBindDN.isEmpty() ) { d->mLDAPUrl.setExtension( "bindname", d->mBindDN ); } if ( !d->mMech.isEmpty() ) { d->mLDAPUrl.setExtension( "x-mech", d->mMech ); } if ( !d->mRealm.isEmpty() ) { d->mLDAPUrl.setExtension( "x-realm", d->mRealm ); } } d->mReadOnly = readOnly(); - kDebug(7125) << "resource_ldapkio url:" << d->mLDAPUrl.prettyUrl(); + kDebug(5700) << "resource_ldapkio url:" << d->mLDAPUrl.prettyUrl(); } void ResourceLDAPKIO::writeConfig( KConfigGroup &group ) { Resource::writeConfig( group ); group.writeEntry( "LdapUser", d->mUser ); group.writeEntry( "LdapPassword", KStringHandler::obscure( d->mPassword ) ); group.writeEntry( "LdapDn", d->mDn ); group.writeEntry( "LdapHost", d->mHost ); group.writeEntry( "LdapPort", d->mPort ); group.writeEntry( "LdapFilter", d->mFilter ); group.writeEntry( "LdapAnonymous", d->mAnonymous ); group.writeEntry( "LdapTLS", d->mTLS ); group.writeEntry( "LdapSSL", d->mSSL ); group.writeEntry( "LdapSubTree", d->mSubTree ); group.writeEntry( "LdapSASL", d->mSASL ); group.writeEntry( "LdapMech", d->mMech ); group.writeEntry( "LdapVer", d->mVer ); group.writeEntry( "LdapTimeLimit", d->mTimeLimit ); group.writeEntry( "LdapSizeLimit", d->mSizeLimit ); group.writeEntry( "LdapRDNPrefix", d->mRDNPrefix ); group.writeEntry( "LdapRealm", d->mRealm ); group.writeEntry( "LdapBindDN", d->mBindDN ); group.writeEntry( "LdapCachePolicy", d->mCachePolicy ); group.writeEntry( "LdapAutoCache", d->mAutoCache ); QStringList attributes; QMap::const_iterator it; for ( it = d->mAttributes.constBegin(); it != d->mAttributes.constEnd(); ++it ) { attributes << it.key() << it.value(); } group.writeEntry( "LdapAttributes", attributes ); } Ticket *ResourceLDAPKIO::requestSaveTicket() { if ( !addressBook() ) { - kDebug(7125) << "no addressbook"; + kDebug(5700) << "no addressbook"; return 0; } return createTicket( this ); } void ResourceLDAPKIO::releaseSaveTicket( Ticket *ticket ) { delete ticket; } bool ResourceLDAPKIO::doOpen() { return true; } void ResourceLDAPKIO::doClose() { } void ResourceLDAPKIO::Private::createCache() { mTmp = 0; if ( mCachePolicy == Cache_NoConnection && mAutoCache ) { mTmp = new KTemporaryFile; mTmp->setPrefix( mCacheDst ); mTmp->setSuffix( "tmp" ); mTmp->open(); } } void ResourceLDAPKIO::Private::activateCache() { if ( mTmp && mError == 0 ) { QString filename = mTmp->fileName(); delete mTmp; mTmp = 0; rename( QFile::encodeName( filename ), QFile::encodeName( mCacheDst ) ); } } KIO::Job *ResourceLDAPKIO::Private::loadFromCache() { KIO::Job *job = 0; if ( mCachePolicy == Cache_Always || ( mCachePolicy == Cache_NoConnection && mError == KIO::ERR_COULD_NOT_CONNECT ) ) { mAddr = Addressee(); mAd = Address( Address::Home ); //initialize ldif parser mLdif.startParsing(); mParent->Resource::setReadOnly( true ); KUrl url( mCacheDst ); job = KIO::get( url, KIO::Reload, KIO::HideProgressInfo ); mParent->connect( job, SIGNAL( data( KIO::Job*, const QByteArray& ) ), mParent, SLOT( data( KIO::Job*, const QByteArray& ) ) ); } return job; } bool ResourceLDAPKIO::load() { - kDebug(7125) << "ResourceLDAPKIO::load()"; + kDebug(5700) << "ResourceLDAPKIO::load()"; KIO::Job *job; clear(); //clear the addressee d->mAddr = Addressee(); d->mAd = Address( Address::Home ); //initialize ldif parser d->mLdif.startParsing(); //set to original settings, offline use will disable writing Resource::setReadOnly( d->mReadOnly ); d->createCache(); if ( d->mCachePolicy != Cache_Always ) { job = KIO::get( d->mLDAPUrl, KIO::Reload, KIO::HideProgressInfo ); connect( job, SIGNAL( data( KIO::Job*, const QByteArray& ) ), this, SLOT( data( KIO::Job*, const QByteArray& ) ) ); connect( job, SIGNAL( result( KJob* ) ), this, SLOT( syncLoadSaveResult( KJob* ) ) ); d->enter_loop(); } job = d->loadFromCache(); if ( job ) { connect( job, SIGNAL( result( KJob* ) ), this, SLOT( syncLoadSaveResult( KJob* ) ) ); d->enter_loop(); } if ( d->mErrorMsg.isEmpty() ) { - kDebug(7125) << "ResourceLDAPKIO load ok!"; + kDebug(5700) << "ResourceLDAPKIO load ok!"; return true; } else { - kDebug(7125) << "ResourceLDAPKIO load finished with error:" << d->mErrorMsg; + kDebug(5700) << "ResourceLDAPKIO load finished with error:" << d->mErrorMsg; addressBook()->error( d->mErrorMsg ); return false; } } bool ResourceLDAPKIO::asyncLoad() { clear(); //clear the addressee d->mAddr = Addressee(); d->mAd = Address( Address::Home ); //initialize ldif parser d->mLdif.startParsing(); Resource::setReadOnly( d->mReadOnly ); d->createCache(); if ( d->mCachePolicy != Cache_Always ) { KIO::Job *job = KIO::get( d->mLDAPUrl, KIO::Reload, KIO::HideProgressInfo ); connect( job, SIGNAL( data( KIO::Job*, const QByteArray& ) ), this, SLOT( data( KIO::Job*, const QByteArray& ) ) ); connect( job, SIGNAL( result( KJob* ) ), this, SLOT( result( KJob* ) ) ); } else { result( NULL ); } return true; } void ResourceLDAPKIO::data( KIO::Job *job, const QByteArray &data ) { Q_UNUSED( job ); if ( data.size() ) { d->mLdif.setLdif( data ); if ( d->mTmp ) { d->mTmp->write( data ); } } else { d->mLdif.endLdif(); } KLDAP::Ldif::ParseValue ret; QString name; QByteArray value; do { ret = d->mLdif.nextItem(); switch ( ret ) { case KLDAP::Ldif::NewEntry: - kDebug(7125) << "new entry:" << d->mLdif.dn().toString(); + kDebug(5700) << "new entry:" << d->mLdif.dn().toString(); break; case KLDAP::Ldif::Item: name = d->mLdif.attr().toLower(); value = d->mLdif.value(); if ( name == d->mAttributes[ "commonName" ].toLower() ) { if ( !d->mAddr.formattedName().isEmpty() ) { QString fn = d->mAddr.formattedName(); d->mAddr.setNameFromString( QString::fromUtf8( value, value.size() ) ); d->mAddr.setFormattedName( fn ); } else { d->mAddr.setNameFromString( QString::fromUtf8( value, value.size() ) ); } } else if ( name == d->mAttributes[ "formattedName" ].toLower() ) { d->mAddr.setFormattedName( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "givenName" ].toLower() ) { d->mAddr.setGivenName( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "mail" ].toLower() ) { d->mAddr.insertEmail( QString::fromUtf8( value, value.size() ), true ); } else if ( name == d->mAttributes[ "mailAlias" ].toLower() ) { d->mAddr.insertEmail( QString::fromUtf8( value, value.size() ), false ); } else if ( name == d->mAttributes[ "phoneNumber" ].toLower() ) { PhoneNumber phone; phone.setNumber( QString::fromUtf8( value, value.size() ) ); d->mAddr.insertPhoneNumber( phone ); } else if ( name == d->mAttributes[ "telephoneNumber" ].toLower() ) { PhoneNumber phone( QString::fromUtf8( value, value.size() ), PhoneNumber::Work ); d->mAddr.insertPhoneNumber( phone ); } else if ( name == d->mAttributes[ "facsimileTelephoneNumber" ].toLower() ) { PhoneNumber phone( QString::fromUtf8( value, value.size() ), PhoneNumber::Fax ); d->mAddr.insertPhoneNumber( phone ); } else if ( name == d->mAttributes[ "mobile" ].toLower() ) { PhoneNumber phone( QString::fromUtf8( value, value.size() ), PhoneNumber::Cell ); d->mAddr.insertPhoneNumber( phone ); } else if ( name == d->mAttributes[ "pager" ].toLower() ) { PhoneNumber phone( QString::fromUtf8( value, value.size() ), PhoneNumber::Pager ); d->mAddr.insertPhoneNumber( phone ); } else if ( name == d->mAttributes[ "description" ].toLower() ) { d->mAddr.setNote( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "title" ].toLower() ) { d->mAddr.setTitle( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "street" ].toLower() ) { d->mAd.setStreet( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "state" ].toLower() ) { d->mAd.setRegion( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "city" ].toLower() ) { d->mAd.setLocality( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "postalcode" ].toLower() ) { d->mAd.setPostalCode( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "organization" ].toLower() ) { d->mAddr.setOrganization( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "familyName" ].toLower() ) { d->mAddr.setFamilyName( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "uid" ].toLower() ) { d->mAddr.setUid( QString::fromUtf8( value, value.size() ) ); } else if ( name == d->mAttributes[ "jpegPhoto" ].toLower() ) { KABC::Picture photo; QImage img = QImage::fromData( value ); if ( !img.isNull() ) { photo.setData( img ); photo.setType( "image/jpeg" ); d->mAddr.setPhoto( photo ); } } break; case KLDAP::Ldif::EndEntry: { d->mAddr.setResource( this ); d->mAddr.insertAddress( d->mAd ); d->mAddr.setChanged( false ); insertAddressee( d->mAddr ); //clear the addressee d->mAddr = Addressee(); d->mAd = Address( Address::Home ); } break; default: break; } } while ( ret != KLDAP::Ldif::MoreData ); } void ResourceLDAPKIO::loadCacheResult( KJob *job ) { d->mErrorMsg.clear(); d->mError = job->error(); if ( d->mError && d->mError != KIO::ERR_USER_CANCELED ) { d->mErrorMsg = job->errorString(); } if ( !d->mErrorMsg.isEmpty() ) { emit loadingError( this, d->mErrorMsg ); } else { emit loadingFinished( this ); } } void ResourceLDAPKIO::result( KJob *job ) { d->mErrorMsg.clear(); if ( job ) { d->mError = job->error(); if ( d->mError && d->mError != KIO::ERR_USER_CANCELED ) { d->mErrorMsg = job->errorString(); } } else { d->mError = 0; } d->activateCache(); KIO::Job *cjob; cjob = d->loadFromCache(); if ( cjob ) { connect( cjob, SIGNAL( result( KJob* ) ), this, SLOT( loadCacheResult( KJob* ) ) ); } else { if ( !d->mErrorMsg.isEmpty() ) { emit loadingError( this, d->mErrorMsg ); } else { emit loadingFinished( this ); } } } bool ResourceLDAPKIO::save( Ticket *ticket ) { Q_UNUSED( ticket ); - kDebug(7125) << "ResourceLDAPKIO save"; + kDebug(5700) << "ResourceLDAPKIO save"; d->mSaveIt = begin(); KIO::Job *job = KIO::put( d->mLDAPUrl, -1, KIO::Overwrite | KIO::HideProgressInfo ); connect( job, SIGNAL( dataReq( KIO::Job*, QByteArray& ) ), this, SLOT( saveData( KIO::Job*, QByteArray& ) ) ); connect( job, SIGNAL( result( KJob* ) ), this, SLOT( syncLoadSaveResult( KJob* ) ) ); d->enter_loop(); if ( d->mErrorMsg.isEmpty() ) { - kDebug(7125) << "ResourceLDAPKIO save ok!"; + kDebug(5700) << "ResourceLDAPKIO save ok!"; return true; } else { - kDebug(7125) << "ResourceLDAPKIO finished with error:" << d->mErrorMsg; + kDebug(5700) << "ResourceLDAPKIO finished with error:" << d->mErrorMsg; addressBook()->error( d->mErrorMsg ); return false; } } bool ResourceLDAPKIO::asyncSave( Ticket *ticket ) { Q_UNUSED( ticket ); - kDebug(7125) << "ResourceLDAPKIO asyncSave"; + kDebug(5700) << "ResourceLDAPKIO asyncSave"; d->mSaveIt = begin(); KIO::Job *job = KIO::put( d->mLDAPUrl, -1, KIO::Overwrite | KIO::HideProgressInfo ); connect( job, SIGNAL( dataReq( KIO::Job*, QByteArray& ) ), this, SLOT( saveData( KIO::Job*, QByteArray& ) ) ); connect( job, SIGNAL( result( KJob* ) ), this, SLOT( saveResult( KJob* ) ) ); return true; } void ResourceLDAPKIO::syncLoadSaveResult( KJob *job ) { d->mError = job->error(); if ( d->mError && d->mError != KIO::ERR_USER_CANCELED ) { d->mErrorMsg = job->errorString(); } else { d->mErrorMsg.clear(); } d->activateCache(); emit leaveModality(); } void ResourceLDAPKIO::saveResult( KJob *job ) { d->mError = job->error(); if ( d->mError && d->mError != KIO::ERR_USER_CANCELED ) { emit savingError( this, job->errorString() ); } else { emit savingFinished( this ); } } void ResourceLDAPKIO::saveData( KIO::Job *job, QByteArray &data ) { Q_UNUSED( job ); while ( d->mSaveIt != end() && !(*d->mSaveIt).changed() ) { d->mSaveIt++; } if ( d->mSaveIt == end() ) { - kDebug(7125) << "ResourceLDAPKIO endData"; + kDebug(5700) << "ResourceLDAPKIO endData"; data.resize( 0 ); return; } - kDebug(7125) << "ResourceLDAPKIO saveData:" << (*d->mSaveIt).assembledName(); + kDebug(5700) << "ResourceLDAPKIO saveData:" << (*d->mSaveIt).assembledName(); d->AddresseeToLDIF( data, *d->mSaveIt, d->findUid( (*d->mSaveIt).uid() ) ); -// kDebug(7125) << "ResourceLDAPKIO save LDIF:" << QString::fromUtf8(data); +// kDebug(5700) << "ResourceLDAPKIO save LDIF:" << QString::fromUtf8(data); // mark as unchanged (*d->mSaveIt).setChanged( false ); d->mSaveIt++; } void ResourceLDAPKIO::removeAddressee( const Addressee &addr ) { QString dn = d->findUid( addr.uid() ); - kDebug(7125) << "ResourceLDAPKIO: removeAddressee:" << dn; + kDebug(5700) << "ResourceLDAPKIO: removeAddressee:" << dn; if ( !d->mErrorMsg.isEmpty() ) { addressBook()->error( d->mErrorMsg ); return; } if ( !dn.isEmpty() ) { - kDebug(7125) << "ResourceLDAPKIO: found uid:" << dn; + kDebug(5700) << "ResourceLDAPKIO: found uid:" << dn; KLDAP::LdapUrl url( d->mLDAPUrl ); url.setPath( '/' + dn ); url.setExtension( "x-dir", "base" ); url.setScope( KLDAP::LdapUrl::Base ); if ( KIO::NetAccess::del( url, NULL ) ) { mAddrMap.remove( addr.uid() ); } } else { //maybe it's not saved yet mAddrMap.remove( addr.uid() ); } } void ResourceLDAPKIO::setUser( const QString &user ) { d->mUser = user; } QString ResourceLDAPKIO::user() const { return d->mUser; } void ResourceLDAPKIO::setPassword( const QString &password ) { d->mPassword = password; } QString ResourceLDAPKIO::password() const { return d->mPassword; } void ResourceLDAPKIO::setDn( const QString &dn ) { d->mDn = dn; } QString ResourceLDAPKIO::dn() const { return d->mDn; } void ResourceLDAPKIO::setHost( const QString &host ) { d->mHost = host; } QString ResourceLDAPKIO::host() const { return d->mHost; } void ResourceLDAPKIO::setPort( int port ) { d->mPort = port; } int ResourceLDAPKIO::port() const { return d->mPort; } void ResourceLDAPKIO::setVer( int ver ) { d->mVer = ver; } int ResourceLDAPKIO::ver() const { return d->mVer; } void ResourceLDAPKIO::setSizeLimit( int sizelimit ) { d->mSizeLimit = sizelimit; } int ResourceLDAPKIO::sizeLimit() { return d->mSizeLimit; } void ResourceLDAPKIO::setTimeLimit( int timelimit ) { d->mTimeLimit = timelimit; } int ResourceLDAPKIO::timeLimit() { return d->mTimeLimit; } void ResourceLDAPKIO::setFilter( const QString &filter ) { d->mFilter = filter; } QString ResourceLDAPKIO::filter() const { return d->mFilter; } void ResourceLDAPKIO::setIsAnonymous( bool value ) { d->mAnonymous = value; } bool ResourceLDAPKIO::isAnonymous() const { return d->mAnonymous; } void ResourceLDAPKIO::setIsTLS( bool value ) { d->mTLS = value; } bool ResourceLDAPKIO::isTLS() const { return d->mTLS; } void ResourceLDAPKIO::setIsSSL( bool value ) { d->mSSL = value; } bool ResourceLDAPKIO::isSSL() const { return d->mSSL; } void ResourceLDAPKIO::setIsSubTree( bool value ) { d->mSubTree = value; } bool ResourceLDAPKIO::isSubTree() const { return d->mSubTree; } void ResourceLDAPKIO::setAttributes( const QMap &attributes ) { d->mAttributes = attributes; } QMap ResourceLDAPKIO::attributes() const { return d->mAttributes; } void ResourceLDAPKIO::setRDNPrefix( int value ) { d->mRDNPrefix = value; } int ResourceLDAPKIO::RDNPrefix() const { return d->mRDNPrefix; } void ResourceLDAPKIO::setIsSASL( bool value ) { d->mSASL = value; } bool ResourceLDAPKIO::isSASL() const { return d->mSASL; } void ResourceLDAPKIO::setMech( const QString &mech ) { d->mMech = mech; } QString ResourceLDAPKIO::mech() const { return d->mMech; } void ResourceLDAPKIO::setRealm( const QString &realm ) { d->mRealm = realm; } QString ResourceLDAPKIO::realm() const { return d->mRealm; } void ResourceLDAPKIO::setBindDN( const QString &binddn ) { d->mBindDN = binddn; } QString ResourceLDAPKIO::bindDN() const { return d->mBindDN; } void ResourceLDAPKIO::setCachePolicy( int pol ) { d->mCachePolicy = pol; } int ResourceLDAPKIO::cachePolicy() const { return d->mCachePolicy; } void ResourceLDAPKIO::setAutoCache( bool value ) { d->mAutoCache = value; } bool ResourceLDAPKIO::autoCache() { return d->mAutoCache; } QString ResourceLDAPKIO::cacheDst() const { return d->mCacheDst; } #include "resourceldapkio.moc" diff --git a/kabc/plugins/ldapkio/resourceldapkioconfig.cpp b/kabc/plugins/ldapkio/resourceldapkioconfig.cpp index 6ea5c602c..d7b6c5428 100644 --- a/kabc/plugins/ldapkio/resourceldapkioconfig.cpp +++ b/kabc/plugins/ldapkio/resourceldapkioconfig.cpp @@ -1,450 +1,450 @@ /* This file is part of libkabc. Copyright (c) 2002 - 2003 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "resourceldapkioconfig.h" #include "resourceldapkio.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "resourceldapkioconfig.moc" using namespace KABC; ResourceLDAPKIOConfig::ResourceLDAPKIOConfig( QWidget *parent ) : KRES::ConfigWidget( parent ) { QBoxLayout *mainLayout = new QVBoxLayout( this ); mainLayout->setMargin( 0 ); mainLayout->setSpacing( KDialog::spacingHint() ); cfg = new KLDAP::LdapConfigWidget( KLDAP::LdapConfigWidget::W_USER | KLDAP::LdapConfigWidget::W_PASS | KLDAP::LdapConfigWidget::W_BINDDN | KLDAP::LdapConfigWidget::W_REALM | KLDAP::LdapConfigWidget::W_HOST | KLDAP::LdapConfigWidget::W_PORT | KLDAP::LdapConfigWidget::W_VER | KLDAP::LdapConfigWidget::W_DN | KLDAP::LdapConfigWidget::W_FILTER | KLDAP::LdapConfigWidget::W_SECBOX | KLDAP::LdapConfigWidget::W_AUTHBOX | KLDAP::LdapConfigWidget::W_TIMELIMIT | KLDAP::LdapConfigWidget::W_SIZELIMIT, this ); mSubTree = new QCheckBox( i18n( "Sub-tree query" ), this ); KHBox *box = new KHBox( this ); box->setSpacing( KDialog::spacingHint() ); mEditButton = new QPushButton( i18n( "Edit Attributes..." ), box ); mCacheButton = new QPushButton( i18n( "Offline Use..." ), box ); mainLayout->addWidget( cfg ); mainLayout->addWidget( mSubTree ); mainLayout->addWidget( box ); connect( mEditButton, SIGNAL( clicked() ), SLOT( editAttributes() ) ); connect( mCacheButton, SIGNAL( clicked() ), SLOT( editCache() ) ); } void ResourceLDAPKIOConfig::loadSettings( KRES::Resource *res ) { ResourceLDAPKIO *resource = dynamic_cast( res ); if ( !resource ) { kDebug(5700) << "ResourceLDAPKIOConfig::loadSettings(): cast failed"; return; } cfg->setUser( resource->user() ); cfg->setPassword( resource->password() ); cfg->setRealm( resource->realm() ); cfg->setBindDn( resource->bindDN() ); cfg->setHost( resource->host() ); cfg->setPort( resource->port() ); cfg->setVersion( resource->ver() ); cfg->setTimeLimit( resource->timeLimit() ); cfg->setSizeLimit( resource->sizeLimit() ); cfg->setDn( KLDAP::LdapDN( resource->dn() ) ); cfg->setFilter( resource->filter() ); cfg->setMech( resource->mech() ); if ( resource->isTLS() ) { cfg->setSecurity( KLDAP::LdapConfigWidget::TLS ); } else if ( resource->isSSL() ) { cfg->setSecurity( KLDAP::LdapConfigWidget::SSL ); } else { cfg->setSecurity( KLDAP::LdapConfigWidget::None ); } if ( resource->isAnonymous() ) { cfg->setAuth( KLDAP::LdapConfigWidget::Anonymous ); } else if ( resource->isSASL() ) { cfg->setAuth( KLDAP::LdapConfigWidget::SASL ); } else { cfg->setAuth( KLDAP::LdapConfigWidget::Simple ); } mSubTree->setChecked( resource->isSubTree() ); mAttributes = resource->attributes(); mRDNPrefix = resource->RDNPrefix(); mCachePolicy = resource->cachePolicy(); mCacheDst = resource->cacheDst(); mAutoCache = resource->autoCache(); } void ResourceLDAPKIOConfig::saveSettings( KRES::Resource *res ) { ResourceLDAPKIO *resource = dynamic_cast( res ); if ( !resource ) { kDebug(5700) << "ResourceLDAPKIOConfig::saveSettings(): cast failed"; return; } resource->setUser( cfg->user() ); resource->setPassword( cfg->password() ); resource->setRealm( cfg->realm() ); resource->setBindDN( cfg->bindDn() ); resource->setHost( cfg->host() ); resource->setPort( cfg->port() ); resource->setVer( cfg->version() ); resource->setTimeLimit( cfg->timeLimit() ); resource->setSizeLimit( cfg->sizeLimit() ); resource->setDn( cfg->dn().toString() ); resource->setFilter( cfg->filter() ); resource->setIsAnonymous( cfg->auth() == KLDAP::LdapConfigWidget::Anonymous ); resource->setIsSASL( cfg->auth() == KLDAP::LdapConfigWidget::SASL ); resource->setMech( cfg->mech() ); resource->setIsTLS( cfg->security() == KLDAP::LdapConfigWidget::TLS ); resource->setIsSSL( cfg->security() == KLDAP::LdapConfigWidget::SSL ); resource->setIsSubTree( mSubTree->isChecked() ); resource->setAttributes( mAttributes ); resource->setRDNPrefix( mRDNPrefix ); resource->setCachePolicy( mCachePolicy ); resource->init(); } void ResourceLDAPKIOConfig::editAttributes() { AttributesDialog dlg( mAttributes, mRDNPrefix, this ); if ( dlg.exec() ) { mAttributes = dlg.attributes(); mRDNPrefix = dlg.rdnprefix(); } } void ResourceLDAPKIOConfig::editCache() { KLDAP::LdapUrl src; QStringList attr; src = cfg->url(); src.setScope( mSubTree->isChecked() ? KLDAP::LdapUrl::Sub : KLDAP::LdapUrl::One ); if ( !mAttributes.empty() ) { QMap::Iterator it; QStringList attr; for ( it = mAttributes.begin(); it != mAttributes.end(); ++it ) { if ( !it.value().isEmpty() && it.key() != "objectClass" ) { attr.append( it.value() ); } } src.setAttributes( attr ); } src.setExtension( "x-dir", "base" ); OfflineDialog dlg( mAutoCache, mCachePolicy, src, mCacheDst, this ); if ( dlg.exec() ) { mCachePolicy = dlg.cachePolicy(); mAutoCache = dlg.autoCache(); } } AttributesDialog::AttributesDialog( const QMap &attributes, int rdnprefix, QWidget *parent ) : KDialog( parent ) { setCaption( i18n( "Attributes Configuration" ) ); setButtons( Ok | Cancel ); setDefaultButton( Ok ); setModal( true ); showButtonSeparator( true ); mNameDict.insert( "objectClass", i18n( "Object classes" ) ); mNameDict.insert( "commonName", i18n( "Common name" ) ); mNameDict.insert( "formattedName", i18n( "Formatted name" ) ); mNameDict.insert( "familyName", i18n( "Family name" ) ); mNameDict.insert( "givenName", i18n( "Given name" ) ); mNameDict.insert( "organization", i18n( "Organization" ) ); mNameDict.insert( "title", i18n( "Title" ) ); mNameDict.insert( "street", i18n( "Street" ) ); mNameDict.insert( "state", i18n( "State" ) ); mNameDict.insert( "city", i18n( "City" ) ); mNameDict.insert( "postalcode", i18n( "Postal code" ) ); mNameDict.insert( "mail", i18n( "Email" ) ); mNameDict.insert( "mailAlias", i18n( "Email alias" ) ); mNameDict.insert( "phoneNumber", i18n( "Telephone number" ) ); mNameDict.insert( "telephoneNumber", i18n( "Work telephone number" ) ); mNameDict.insert( "facsimileTelephoneNumber", i18n( "Fax number" ) ); mNameDict.insert( "mobile", i18n( "Cell phone number" ) ); mNameDict.insert( "pager", i18n( "Pager" ) ); mNameDict.insert( "description", i18n( "Note" ) ); mNameDict.insert( "uid", i18n( "UID" ) ); mNameDict.insert( "jpegPhoto", i18n( "Photo" ) ); // default map mDefaultMap.insert( "objectClass", "inetOrgPerson" ); mDefaultMap.insert( "commonName", "cn" ); mDefaultMap.insert( "formattedName", "displayName" ); mDefaultMap.insert( "familyName", "sn" ); mDefaultMap.insert( "givenName", "givenName" ); mDefaultMap.insert( "title", "title" ); mDefaultMap.insert( "street", "street" ); mDefaultMap.insert( "state", "st" ); mDefaultMap.insert( "city", "l" ); mDefaultMap.insert( "organization", "o" ); mDefaultMap.insert( "postalcode", "postalCode" ); mDefaultMap.insert( "mail", "mail" ); mDefaultMap.insert( "mailAlias", "" ); mDefaultMap.insert( "phoneNumber", "homePhone" ); mDefaultMap.insert( "telephoneNumber", "telephoneNumber" ); mDefaultMap.insert( "facsimileTelephoneNumber", "facsimileTelephoneNumber" ); mDefaultMap.insert( "mobile", "mobile" ); mDefaultMap.insert( "pager", "pager" ); mDefaultMap.insert( "description", "description" ); mDefaultMap.insert( "uid", "uid" ); mDefaultMap.insert( "jpegPhoto", "jpegPhoto" ); // overwrite the default values here QMap kolabMap, netscapeMap, evolutionMap, outlookMap; // kolab kolabMap.insert( "formattedName", "display-name" ); kolabMap.insert( "mailAlias", "mailalias" ); // evolution evolutionMap.insert( "formattedName", "fileAs" ); mMapList.append( attributes ); mMapList.append( kolabMap ); mMapList.append( netscapeMap ); mMapList.append( evolutionMap ); mMapList.append( outlookMap ); QFrame *page = new QFrame( this ); setMainWidget( page ); QGridLayout *layout = new QGridLayout( page ); QLabel *label = new QLabel( i18n( "Template:" ), page ); layout->addWidget( label, 0, 0 ); mMapCombo = new KComboBox( page ); layout->addWidget( mMapCombo, 0, 1 ); mMapCombo->addItem( i18n( "User Defined" ) ); mMapCombo->addItem( i18n( "Kolab" ) ); mMapCombo->addItem( i18n( "Netscape" ) ); mMapCombo->addItem( i18n( "Evolution" ) ); mMapCombo->addItem( i18n( "Outlook" ) ); connect( mMapCombo, SIGNAL( activated( int ) ), SLOT( mapChanged( int ) ) ); label = new QLabel( i18n( "RDN prefix attribute:" ), page ); layout->addWidget( label, 1, 0 ); mRDNCombo = new KComboBox( page ); layout->addWidget( mRDNCombo, 1, 1 ); mRDNCombo->addItem( i18n( "commonName" ) ); mRDNCombo->addItem( i18n( "UID" ) ); mRDNCombo->setCurrentIndex( rdnprefix ); QMap::ConstIterator it; int i, j = 0; for ( i = 2, it = attributes.begin(); it != attributes.end(); ++it, ++i ) { if ( mNameDict[ it.key() ] == 0 ) { i--; continue; } if ( ( i - 2 ) == ( mNameDict.count() >> 1 ) ) { i = 0; j = 2; } - kDebug(7125) << "itkey:" << it.key() << "i:" << i; + kDebug(5700) << "itkey:" << it.key() << "i:" << i; label = new QLabel( mNameDict[ it.key() ] + ':', page ); KLineEdit *lineedit = new KLineEdit( page ); mLineEditDict.insert( it.key(), lineedit ); lineedit->setText( it.value() ); label->setBuddy( lineedit ); layout->addWidget( label, i, j ); layout->addWidget( lineedit, i, j+1 ); } for ( i = 1; i < mMapCombo->count(); i++ ) { QHash::const_iterator it2 = mLineEditDict.constBegin(); while ( it2 != mLineEditDict.constEnd() ) { if ( mMapList[ i ].contains( it2.key() ) ) { if ( mMapList[ i ][ it2.key() ] != it2.value()->text() ) { break; } } else { if ( mDefaultMap[ it2.key() ] != it2.value()->text() ) { break; } } ++it2; } if ( it2 != mLineEditDict.constEnd() ) { mMapCombo->setCurrentIndex( i ); break; } } KAcceleratorManager::manage( this ); } AttributesDialog::~AttributesDialog() { mNameDict.clear(); } QMap AttributesDialog::attributes() const { QMap map; QHash::const_iterator it = mLineEditDict.constBegin(); while ( it != mLineEditDict.constEnd() ) { map.insert( it.key(), it.value()->text() ); ++it; } return map; } int AttributesDialog::rdnprefix() const { return mRDNCombo->currentIndex(); } void AttributesDialog::mapChanged( int pos ) { // apply first the default and than the spezific changes QMap::Iterator it; for ( it = mDefaultMap.begin(); it != mDefaultMap.end(); ++it ) { mLineEditDict[ it.key() ]->setText( it.value() ); } for ( it = mMapList[ pos ].begin(); it != mMapList[ pos ].end(); ++it ) { if ( !it.value().isEmpty() ) { KLineEdit *le = mLineEditDict[ it.key() ]; if ( le ) { le->setText( it.value() ); } } } } OfflineDialog::OfflineDialog( bool autoCache, int cachePolicy, const KUrl &src, const QString &dst, QWidget *parent ) : KDialog( parent ) { setCaption( i18n( "Offline Configuration" ) ); setButtons( Ok | Cancel ); setDefaultButton( Ok ); setModal( true ); showButtonSeparator( true ); QFrame *page = new QFrame( this ); setMainWidget( page ); QVBoxLayout *layout = new QVBoxLayout( page ); mSrc = src; mDst = dst; mCacheBox = new QGroupBox( i18n( "Offline Cache Policy" ), page ); QVBoxLayout *cacheBoxLayout = new QVBoxLayout( mCacheBox ); mCacheGroup = new QButtonGroup( this ); QRadioButton *bt; bt = new QRadioButton( i18n( "Do not use offline cache" ), mCacheBox ); cacheBoxLayout->addWidget( bt ); bt->setDown(true); mCacheGroup->addButton( bt ); bt = new QRadioButton( i18n( "Use local copy if no connection" ), mCacheBox ); cacheBoxLayout->addWidget( bt ); mCacheGroup->addButton( bt ); bt = new QRadioButton( i18n( "Always use local copy" ), mCacheBox ); cacheBoxLayout->addWidget( bt ); mCacheGroup->addButton( bt ); if ( mCacheGroup->button( cachePolicy ) ) { mCacheGroup->button( cachePolicy )->setDown( true ); } mAutoCache = new QCheckBox( i18n( "Refresh offline cache automatically" ), page ); mAutoCache->setChecked( autoCache ); mAutoCache->setEnabled( bt->isChecked() ); connect( bt, SIGNAL(toggled(bool)), mAutoCache, SLOT(setEnabled(bool)) ); QPushButton *lcache = new QPushButton( i18n( "Load into Cache" ), page ); connect( lcache, SIGNAL( clicked() ), SLOT( loadCache() ) ); layout->addWidget( mCacheBox ); layout->addWidget( mAutoCache ); layout->addWidget( lcache ); } OfflineDialog::~OfflineDialog() { } bool OfflineDialog::autoCache() const { return mAutoCache->isChecked(); } int OfflineDialog::cachePolicy() const { return mCacheGroup->checkedId(); } void OfflineDialog::loadCache() { if ( KIO::NetAccess::download( mSrc, mDst, this ) ) { KMessageBox::information( this, i18n( "Successfully downloaded directory server contents." ) ); } else { KMessageBox::error( this, i18n( "An error occurred downloading directory server contents into file %1.", mDst ) ); } } diff --git a/kabc/resourcecached.cpp b/kabc/resourcecached.cpp index d7a12236f..cee542a2e 100644 --- a/kabc/resourcecached.cpp +++ b/kabc/resourcecached.cpp @@ -1,302 +1,302 @@ /* This file is part of kabc. Copyright (c) 2004 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "resourcecached.h" #include #include #include #include #include using namespace KABC; class ResourceCached::Private { public: Private( ResourceCached *parent ) : mParent( parent ), mIdMapper( "kabc/uidmaps/" ) { } void loadChangesCache( QMap &map, const QString &type ); void saveChangesCache( const QMap &map, const QString &type ); ResourceCached *mParent; KRES::IdMapper mIdMapper; QMap mAddedAddressees; QMap mChangedAddressees; QMap mDeletedAddressees; }; void ResourceCached::Private::saveChangesCache( const QMap &map, const QString &type ) { QFile file( mParent->changesCacheFile( type ) ); const KABC::Addressee::List list = map.values(); if ( list.isEmpty() ) { file.remove(); } else { if ( !file.open( QIODevice::WriteOnly ) ) { - kError() << "Can't open changes cache file '" << file.fileName() << "' for saving."; + kError(5700) << "Can't open changes cache file '" << file.fileName() << "' for saving."; return; } KABC::VCardConverter converter; const QByteArray vCards = converter.createVCards( list ); file.write( vCards ); } } void ResourceCached::Private::loadChangesCache( QMap &map, const QString &type ) { QFile file( mParent->changesCacheFile( type ) ); if ( !file.open( QIODevice::ReadOnly ) ) { return; } KABC::VCardConverter converter; const KABC::Addressee::List list = converter.parseVCards( file.readAll() ); KABC::Addressee::List::ConstIterator it; for ( it = list.begin(); it != list.end(); ++it ) { map.insert( (*it).uid(), *it ); } file.close(); } ResourceCached::ResourceCached() : KABC::Resource(), d( new Private( this ) ) { } ResourceCached::ResourceCached( const KConfigGroup &group ) : KABC::Resource( group ), d( new Private( this ) ) { } ResourceCached::~ResourceCached() { delete d; } void ResourceCached::writeConfig( KConfigGroup &group ) { KABC::Resource::writeConfig( group ); } void ResourceCached::insertAddressee( const Addressee &addr ) { if ( !mAddrMap.contains( addr.uid() ) ) { // new contact if ( d->mDeletedAddressees.contains( addr.uid() ) ) { // it was first removed, then added, so it's an update... d->mDeletedAddressees.remove( addr.uid() ); mAddrMap.insert( addr.uid(), addr ); d->mChangedAddressees.insert( addr.uid(), addr ); return; } mAddrMap.insert( addr.uid(), addr ); d->mAddedAddressees.insert( addr.uid(), addr ); } else { KABC::Addressee oldAddressee = mAddrMap.find( addr.uid() ).value(); if ( oldAddressee != addr ) { mAddrMap.remove( addr.uid() ); mAddrMap.insert( addr.uid(), addr ); d->mChangedAddressees.insert( addr.uid(), addr ); } } } void ResourceCached::removeAddressee( const Addressee &addr ) { if ( d->mAddedAddressees.contains( addr.uid() ) ) { d->mAddedAddressees.remove( addr.uid() ); return; } if ( d->mDeletedAddressees.find( addr.uid() ) == d->mDeletedAddressees.end() ) { d->mDeletedAddressees.insert( addr.uid(), addr ); } mAddrMap.remove( addr.uid() ); } bool ResourceCached::loadFromCache() { mAddrMap.clear(); setIdMapperIdentifier(); d->mIdMapper.load(); // load cache QFile file( cacheFile() ); if ( !file.open( QIODevice::ReadOnly ) ) { return false; } KABC::VCardConverter converter; KABC::Addressee::List list = converter.parseVCards( file.readAll() ); KABC::Addressee::List::Iterator it; for ( it = list.begin(); it != list.end(); ++it ) { (*it).setResource( this ); (*it).setChanged( false ); mAddrMap.insert( (*it).uid(), *it ); } file.close(); return true; } void ResourceCached::saveToCache() { setIdMapperIdentifier(); d->mIdMapper.save(); // save cache QFile file( cacheFile() ); if ( !file.open( QIODevice::WriteOnly ) ) { return; } KABC::Addressee::List list = mAddrMap.values(); KABC::VCardConverter converter; QByteArray vCard = converter.createVCards( list ); file.write( vCard ); file.close(); } void ResourceCached::cleanUpCache( const KABC::Addressee::List &addrList ) { // load cache QFile file( cacheFile() ); if ( !file.open( QIODevice::ReadOnly ) ) { return; } KABC::VCardConverter converter; KABC::Addressee::List list = converter.parseVCards( file.readAll() ); KABC::Addressee::List::Iterator cacheIt; KABC::Addressee::List::ConstIterator it; for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) { bool found = false; for ( it = addrList.begin(); it != addrList.end(); ++it ) { if ( (*it).uid() == (*cacheIt).uid() ) { found = true; } } if ( !found ) { d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt).uid() ) ); mAddrMap.remove( (*cacheIt).uid() ); } } file.close(); } KRES::IdMapper &ResourceCached::idMapper() { return d->mIdMapper; } bool ResourceCached::hasChanges() const { return !( d->mAddedAddressees.isEmpty() && d->mChangedAddressees.isEmpty() && d->mDeletedAddressees.isEmpty() ); } void ResourceCached::clearChanges() { d->mAddedAddressees.clear(); d->mChangedAddressees.clear(); d->mDeletedAddressees.clear(); } void ResourceCached::clearChange( const KABC::Addressee &addr ) { d->mAddedAddressees.remove( addr.uid() ); d->mChangedAddressees.remove( addr.uid() ); d->mDeletedAddressees.remove( addr.uid() ); } void ResourceCached::clearChange( const QString &uid ) { d->mAddedAddressees.remove( uid ); d->mChangedAddressees.remove( uid ); d->mDeletedAddressees.remove( uid ); } KABC::Addressee::List ResourceCached::addedAddressees() const { return d->mAddedAddressees.values(); } KABC::Addressee::List ResourceCached::changedAddressees() const { return d->mChangedAddressees.values(); } KABC::Addressee::List ResourceCached::deletedAddressees() const { return d->mDeletedAddressees.values(); } QString ResourceCached::cacheFile() const { return KStandardDirs::locateLocal( "cache", "kabc/kresources/" + identifier() ); } QString ResourceCached::changesCacheFile( const QString &type ) const { return KStandardDirs::locateLocal( "cache", "kabc/changescache/" + identifier() + '_' + type ); } void ResourceCached::saveChangesCache() { d->saveChangesCache( d->mAddedAddressees, "added" ); d->saveChangesCache( d->mDeletedAddressees, "deleted" ); d->saveChangesCache( d->mChangedAddressees, "changed" ); } void ResourceCached::loadChangesCache() { d->loadChangesCache( d->mAddedAddressees, "added" ); d->loadChangesCache( d->mDeletedAddressees, "deleted" ); d->loadChangesCache( d->mChangedAddressees, "changed" ); } void ResourceCached::setIdMapperIdentifier() { d->mIdMapper.setIdentifier( type() + '_' + identifier() ); } #include "resourcecached.moc"