diff --git a/kabc/plugins/ldap/resourceldap.cpp b/kabc/plugins/ldap/resourceldap.cpp index 513287af8..25d65006c 100644 --- a/kabc/plugins/ldap/resourceldap.cpp +++ b/kabc/plugins/ldap/resourceldap.cpp @@ -1,322 +1,322 @@ /* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include "resourceconfigwidget.h" #include "resourceldap.h" #include "resourceldapconfig.h" using namespace KABC; extern "C" { ResourceConfigWidget *config_widget( QWidget *parent ) { KGlobal::locale()->insertCatalogue("kabc_ldap"); return new ResourceLDAPConfig( parent, "ResourceLDAPConfig" ); } Resource *resource( AddressBook *ab, const KConfig *config ) { KGlobal::locale()->insertCatalogue("kabc_ldap"); return new ResourceLDAP( ab, config ); } } void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value ); ResourceLDAP::ResourceLDAP( AddressBook *ab, const KConfig *config ) : Resource( ab ) { mLdap = 0; mUser = config->readEntry( "LdapUser" ); mPassword = cryptStr( config->readEntry( "LdapPassword" ) ); mDn = config->readEntry( "LdapDn" ); mHost = config->readEntry( "LdapHost" ); mPort = config->readEntry( "LdapPort" ); mFilter = config->readEntry( "LdapFilter" ); mAnonymous = config->readBoolEntry( "LdapAnonymous" ); } ResourceLDAP::ResourceLDAP( AddressBook *ab, const QString &user, const QString &password, const QString &dn, const QString &host, const QString &port, const QString &filter, const bool anonymous ) : Resource( ab ) { mLdap = 0; mUser = user; mPassword = password; mDn = dn; mHost = host; mPort = port; mFilter = filter; mAnonymous = anonymous; } Ticket *ResourceLDAP::requestSaveTicket() { if ( !addressBook() ) { kdDebug(5700) << "no addressbook" << endl; return 0; } return createTicket( this ); } bool ResourceLDAP::open() { if ( mLdap ) return false; if ( mPort.isEmpty() ) mPort = "389"; - mLdap = ldap_init( mHost.latin1(), mPort.toInt() ); + mLdap = ldap_init( mHost.local8Bit(), mPort.toInt() ); if ( !mLdap ) { addressBook()->error( i18n( "Unable to connect to server '%1' on port '%2'" ).arg( mHost ).arg( mPort ) ); return false; } if ( !mUser.isEmpty() && !mAnonymous ) { - if ( ldap_simple_bind_s( mLdap, mUser.latin1(), mPassword.latin1() ) != LDAP_SUCCESS ) { + if ( ldap_simple_bind_s( mLdap, mUser.local8Bit(), mPassword.local8Bit() ) != LDAP_SUCCESS ) { addressBook()->error( i18n( "Unable to bind to server '%1'" ).arg( mHost ) ); return false; } kdDebug(5700) << "ResourceLDAP: bind to server successfully" << endl; } int deref = LDAP_DEREF_ALWAYS; if ( ldap_set_option( mLdap, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS ) { kdDebug(5700) << "ResourceLDAP: can't set 'deref' option" << endl; return false; } if ( ldap_set_option( mLdap, LDAP_OPT_REFERRALS, LDAP_OPT_ON ) != LDAP_OPT_SUCCESS ) { kdDebug(5700) << "ResourceLDAP: can't set 'referrals' option" << endl; return false; } return true; } void ResourceLDAP::close() { if ( ldap_unbind_s( mLdap ) != LDAP_SUCCESS ) { kdDebug(5700) << "ResourceLDAP: can't unbind from server" << endl; return; } mLdap = 0; } bool ResourceLDAP::load() { LDAPMessage *res; LDAPMessage *msg; BerElement *track; char *name; char **values; const char *LdapSearchAttr[ 9 ] = { "cn", "display-name", "givenname", "mail", "mailalias", "phoneNumber", "sn", "uid", 0 }; - if ( ldap_search_s( mLdap, mDn.latin1(), LDAP_SCOPE_SUBTREE, QString( "(%1)" ).arg( mFilter ).latin1(), + if ( ldap_search_s( mLdap, mDn.local8Bit(), LDAP_SCOPE_SUBTREE, QString( "(%1)" ).arg( mFilter ).local8Bit(), (char **)LdapSearchAttr, 0, &res ) != LDAP_SUCCESS ) { addressBook()->error( i18n( "Unable to search on server '%1'" ).arg( mHost ) ); return false; } for ( msg = ldap_first_entry( mLdap, res ); msg; msg = ldap_next_entry( mLdap, msg ) ) { Addressee addr; addr.setResource( this ); for ( name = ldap_first_attribute( mLdap, msg, &track ); name; name = ldap_next_attribute( mLdap, msg, track ) ) { values = ldap_get_values( mLdap, msg, name ); for ( int i = 0; i < ldap_count_values( values ); ++i ) { if ( qstricmp( name, "cn" ) == 0 ) { if ( !addr.formattedName().isEmpty() ) { QString fn = addr.formattedName(); addr.setNameFromString( values[ i ] ); addr.setFormattedName( fn ); } else addr.setNameFromString( values[ i ] ); continue; } if ( qstricmp( name, "display-name" ) == 0 ) { addr.setFormattedName( values[ i ] ); continue; } if ( qstricmp( name, "givenname" ) == 0 ) { addr.setGivenName( values[ i ] ); continue; } if ( qstricmp( name, "mail" ) == 0 ) { addr.insertEmail( values[ i ], true ); continue; } if ( qstricmp( name, "mailalias" ) == 0 ) { addr.insertEmail( values[ i ], false ); continue; } if ( qstricmp( name, "phoneNumber" ) == 0 ) { PhoneNumber phone; phone.setNumber( values[ i ] ); addr.insertPhoneNumber( phone ); break; // read only the home number } if ( qstricmp( name, "sn" ) == 0 ) { addr.setFamilyName( values[ i ] ); continue; } if ( qstricmp( name, "uid" ) == 0 ) { addr.setUid( values[ i ] ); continue; } } ldap_value_free( values ); } ber_free( track, 0 ); addressBook()->insertAddressee( addr ); } ldap_msgfree( res ); return true; } bool ResourceLDAP::save( Ticket * ) { AddressBook::Iterator it; for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { if ( (*it).resource() == this && (*it).changed() ) { LDAPMod **mods = NULL; - + addModOp( &mods, "objectClass", "organizationalPerson" ); addModOp( &mods, "objectClass", "person" ); addModOp( &mods, "objectClass", "Top" ); addModOp( &mods, "cn", (*it).assembledName() ); addModOp( &mods, "display-name", (*it).formattedName() ); addModOp( &mods, "givenname", (*it).givenName() ); addModOp( &mods, "sn", (*it).familyName() ); addModOp( &mods, "uid", (*it).uid() ); QStringList emails = (*it).emails(); QStringList::ConstIterator mailIt; bool first = true; for ( mailIt = emails.begin(); mailIt != emails.end(); ++mailIt ) { if ( first ) { addModOp( &mods, "mail", (*mailIt) ); first = false; } else addModOp( &mods, "mailalias", (*mailIt) ); } PhoneNumber number = (*it).phoneNumber( PhoneNumber::Home ); addModOp( &mods, "phoneNumber", number.number() ); QString dn = "cn=" + (*it).assembledName() + "," + mDn; int retval; - if ( (retval = ldap_add_s( mLdap, dn.latin1(), mods )) != LDAP_SUCCESS ) + if ( (retval = ldap_add_s( mLdap, dn.local8Bit(), mods )) != LDAP_SUCCESS ) addressBook()->error( i18n( "Unable to modify '%1' on server '%2'" ).arg( (*it).uid() ).arg( mHost ) ); ldap_mods_free( mods, 1 ); // mark as unchanged (*it).setChanged( false ); } } return true; } void ResourceLDAP::removeAddressee( const Addressee &addr ) { LDAPMessage *res; LDAPMessage *msg; QString filter = QString( "(&(uid=%1)(%2))" ).arg( addr.uid() ).arg( mFilter ); kdDebug(5700) << "ldap:removeAddressee" << filter << endl; - ldap_search_s( mLdap, mDn.latin1(), LDAP_SCOPE_SUBTREE, filter.latin1(), + ldap_search_s( mLdap, mDn.local8Bit(), LDAP_SCOPE_SUBTREE, filter.local8Bit(), 0, 0, &res ); for ( msg = ldap_first_entry( mLdap, res ); msg; msg = ldap_next_entry( mLdap, msg ) ) { char *dn = ldap_get_dn( mLdap, msg ); kdDebug(5700) << "found " << dn << endl; if ( ldap_delete_s( mLdap, dn ) != LDAP_SUCCESS ) addressBook()->error( i18n( "Unable to delete '%1' on server '%2'" ).arg( dn ).arg( mHost ) ); ldap_memfree( dn ); } ldap_msgfree( res ); } QString ResourceLDAP::identifier() const { return mHost + "_" + mPort + "_" + mDn + "_" + mFilter; } void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value ) { if ( value.isNull() ) return; LDAPMod **mods; mods = *pmods; uint i = 0; if ( mods != 0 ) for ( ; mods[ i ] != 0; ++i ); if (( mods = (LDAPMod **)realloc( mods, (i + 2) * sizeof( LDAPMod * ))) == 0 ) { kdError() << "ResourceLDAP: realloc" << endl; return; } *pmods = mods; mods[ i + 1 ] = 0; mods[ i ] = new LDAPMod; mods[ i ]->mod_op = 0; - mods[ i ]->mod_type = strdup( attr.latin1() ); + mods[ i ]->mod_type = strdup( attr.local8Bit() ); mods[ i ]->mod_values = new char*[2]; - mods[ i ]->mod_values[0] = strdup( value.latin1() ); + mods[ i ]->mod_values[0] = strdup( value.local8Bit() ); mods[ i ]->mod_values[1] = 0; } diff --git a/kabc/plugins/net/resourcenet.cpp b/kabc/plugins/net/resourcenet.cpp index 4527c8f20..8343bdd6a 100644 --- a/kabc/plugins/net/resourcenet.cpp +++ b/kabc/plugins/net/resourcenet.cpp @@ -1,183 +1,183 @@ #include #include #include #include #include #include #include "addressbook.h" #include "formatfactory.h" #include "resourcenetconfig.h" #include "stdaddressbook.h" #include "resourcenet.h" using namespace KABC; extern "C" { ResourceConfigWidget *config_widget( QWidget *parent ) { - KGlobal::locale()->insertCatalogue("kabc_dir"); + KGlobal::locale()->insertCatalogue("kabc_net"); return new ResourceNetConfig( parent, "ResourceDirConfig" ); } Resource *resource( AddressBook *ab, const KConfig *config ) { - KGlobal::locale()->insertCatalogue("kabc_dir"); + KGlobal::locale()->insertCatalogue("kabc_net"); return new ResourceNet( ab, config ); } } ResourceNet::ResourceNet( AddressBook *addressBook, const KConfig *config ) : Resource( addressBook ) { KURL url = config->readEntry( "NetUrl" ); QString type = config->readEntry( "NetFormat" ); FormatFactory *factory = FormatFactory::self(); FormatPlugin *format = factory->format( type ); init( url, format ); } ResourceNet::ResourceNet( AddressBook *addressBook, const KURL &url, FormatPlugin *format ) : Resource( addressBook ) { init( url, format ); } ResourceNet::~ResourceNet() { delete mFormat; } void ResourceNet::init( const KURL &url, FormatPlugin *format ) { if ( !format ) { FormatFactory *factory = FormatFactory::self(); mFormat = factory->format( "vcard" ); } else { mFormat = format; } setUrl( url ); } Ticket *ResourceNet::requestSaveTicket() { kdDebug(5700) << "ResourceNet::requestSaveTicket()" << endl; if ( !addressBook() ) return 0; return createTicket( this ); } bool ResourceNet::open() { KIO::UDSEntryList entries; if ( !KIO::NetAccess::listDir( mUrl, entries ) ) return false; return true; } void ResourceNet::close() { } bool ResourceNet::load() { kdDebug(5700) << "ResourceNet::load(): '" << mUrl.url() << "'" << endl; KIO::UDSEntryList entries; if ( !KIO::NetAccess::listDir( mUrl, entries, false, false ) ) return false; QStringList files = KIO::convertUDSEntryListToFileNames( entries ); QStringList::Iterator it; bool ok = true; for ( it = files.begin(); it != files.end(); ++it ) { if ( (*it).endsWith( "/" ) ) // is a directory continue; - + QString tmpFile; if ( KIO::NetAccess::download( mUrl.url() + "/" + (*it), tmpFile ) ) { QFile file( tmpFile ); if ( !file.open( IO_ReadOnly ) ) { addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) ); ok = false; } else { if ( !mFormat->loadAll( addressBook(), this, &file ) ) ok = false; } KIO::NetAccess::removeTempFile( tmpFile ); } else { addressBook()->error( i18n( "Unable to open URL '%1' for reading" ).arg( mUrl.url() + "/" + (*it) ) ); ok = false; } } return ok; } bool ResourceNet::save( Ticket *ticket ) { kdDebug(5700) << "ResourceNet::save(): '" << mUrl.url() << "'" << endl; - + AddressBook::Iterator it; bool ok = true; for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { if ( (*it).resource() != this || !(*it).changed() ) continue; KTempFile tmpFile; QFile *file = tmpFile.file(); mFormat->save( *it, file ); // mark as unchanged (*it).setChanged( false ); tmpFile.close(); if ( !KIO::NetAccess::upload( tmpFile.name(), mUrl.url() + "/" + (*it).uid() ) ) { addressBook()->error( i18n( "Unable to save URL '%1'" ).arg( mUrl.url() + "/" + (*it).uid() ) ); ok = false; } tmpFile.unlink(); } delete ticket; return ok; } void ResourceNet::setUrl( const KURL &url ) { mUrl = url; } KURL ResourceNet::url() const { return mUrl; } QString ResourceNet::identifier() const { return url().url(); } void ResourceNet::removeAddressee( const Addressee& addr ) { KIO::NetAccess::del( mUrl.url() + "/" + addr.uid() ); } #include "resourcenet.moc"