kabc Library API Documentation

resourceldapkio.cpp

00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #include <kdebug.h> 00022 #include <kglobal.h> 00023 #include <klineedit.h> 00024 #include <klocale.h> 00025 #include <kconfig.h> 00026 #include <kstringhandler.h> 00027 00028 #include <stdlib.h> 00029 00030 #include <kabc/ldifconverter.h> 00031 00032 #include "resourceldapkio.h" 00033 #include "resourceldapkioconfig.h" 00034 00035 using namespace KABC; 00036 00037 ResourceLDAPKIO::ResourceLDAPKIO( const KConfig *config ) 00038 : Resource( config ), mGetCounter( 0 ), mErrorOccured( false ) 00039 { 00040 if ( config ) { 00041 QMap<QString, QString> attrList; 00042 QStringList attributes = config->readListEntry( "LdapAttributes" ); 00043 for ( uint pos = 0; pos < attributes.count(); pos += 2 ) 00044 mAttributes.insert( attributes[ pos ], attributes[ pos + 1 ] ); 00045 00046 mUser = config->readEntry( "LdapUser" ); 00047 mPassword = KStringHandler::obscure( config->readEntry( "LdapPassword" ) ); 00048 mDn = config->readEntry( "LdapDn" ); 00049 mHost = config->readEntry( "LdapHost" ); 00050 mPort = config->readNumEntry( "LdapPort", 389 ); 00051 mFilter = config->readEntry( "LdapFilter" ); 00052 mAnonymous = config->readBoolEntry( "LdapAnonymous" ); 00053 } else { 00054 mPort = 389; 00055 mAnonymous = true; 00056 } 00057 00058 init(); 00059 } 00060 00061 void ResourceLDAPKIO::init() 00062 { 00069 if ( mPort == 0 ) mPort = 389; 00070 if ( mUser.isEmpty() && mPassword.isEmpty() ) mAnonymous = true; 00071 00072 if ( mAttributes.count() == 0 ) { 00073 mAttributes.insert( "commonName", "cn" ); 00074 mAttributes.insert( "formattedName", "displayName" ); 00075 mAttributes.insert( "familyName", "sn" ); 00076 mAttributes.insert( "givenName", "givenName" ); 00077 mAttributes.insert( "mail", "mail" ); 00078 mAttributes.insert( "mailAlias", "" ); 00079 mAttributes.insert( "phoneNumber", "telephoneNumber" ); 00080 mAttributes.insert( "uid", "uid" ); 00081 } 00082 00083 mLDAPUrl.setProtocol( "ldap" ); 00084 if ( !mAnonymous ) { 00085 mLDAPUrl.setUser( mUser ); 00086 mLDAPUrl.setPass( mPassword ); 00087 } 00088 mLDAPUrl.setHost( mHost ); 00089 mLDAPUrl.setPort( mPort ); 00090 mLDAPUrl.setPath( "/" + mDn ); 00091 00092 QString query = "?dn?sub"; 00093 if ( !mFilter.isEmpty() ) 00094 query += "?" + mFilter; 00095 00096 mLDAPUrl.setQuery( query ); 00097 } 00098 00099 void ResourceLDAPKIO::writeConfig( KConfig *config ) 00100 { 00101 Resource::writeConfig( config ); 00102 00103 config->writeEntry( "LdapUser", mUser ); 00104 config->writeEntry( "LdapPassword", KStringHandler::obscure( mPassword ) ); 00105 config->writeEntry( "LdapDn", mDn ); 00106 config->writeEntry( "LdapHost", mHost ); 00107 config->writeEntry( "LdapPort", mPort ); 00108 config->writeEntry( "LdapFilter", mFilter ); 00109 config->writeEntry( "LdapAnonymous", mAnonymous ); 00110 00111 QStringList attributes; 00112 QMap<QString, QString>::Iterator it; 00113 for ( it = mAttributes.begin(); it != mAttributes.end(); ++it ) 00114 attributes << it.key() << it.data(); 00115 00116 config->writeEntry( "LdapAttributes", attributes ); 00117 } 00118 00119 Ticket *ResourceLDAPKIO::requestSaveTicket() 00120 { 00121 if ( !addressBook() ) { 00122 kdDebug(5700) << "no addressbook" << endl; 00123 return 0; 00124 } 00125 00126 return createTicket( this ); 00127 } 00128 00129 void ResourceLDAPKIO::releaseSaveTicket( Ticket *ticket ) 00130 { 00131 delete ticket; 00132 } 00133 00134 bool ResourceLDAPKIO::doOpen() 00135 { 00136 return true; 00137 } 00138 00139 void ResourceLDAPKIO::doClose() 00140 { 00141 } 00142 00143 bool ResourceLDAPKIO::load() 00144 { 00145 return true; 00146 } 00147 00148 bool ResourceLDAPKIO::asyncLoad() 00149 { 00150 KIO::Job *job = KIO::listDir( mLDAPUrl, false, false ); 00151 00152 connect( job, SIGNAL( entries( KIO::Job*, const KIO::UDSEntryList& ) ), 00153 this, SLOT( entries( KIO::Job*, const KIO::UDSEntryList& ) ) ); 00154 00155 return true; 00156 } 00157 00158 void ResourceLDAPKIO::entries( KIO::Job*, const KIO::UDSEntryList &list ) 00159 { 00160 KIO::UDSEntryList::ConstIterator it; 00161 for ( it = list.begin(); it != list.end(); ++it ) { 00162 KIO::UDSEntry::ConstIterator atomIt; 00163 00164 bool isFile = false; 00165 for ( atomIt = (*it).begin(); atomIt != (*it).end(); ++atomIt ) { 00166 if ( (*atomIt).m_uds == KIO::UDS_FILE_TYPE && (*atomIt).m_long == S_IFREG ) 00167 isFile = true; 00168 } 00169 00170 if ( isFile ) { 00171 for ( atomIt = (*it).begin(); atomIt != (*it).end(); ++atomIt ) { 00172 if ( (*atomIt).m_uds == KIO::UDS_URL ) { 00173 mGetCounter++; 00174 KIO::Job *job = KIO::get( (*atomIt).m_str, true, false ); 00175 connect( job, SIGNAL( data( KIO::Job*, const QByteArray& ) ), 00176 this, SLOT( data( KIO::Job*, const QByteArray& ) ) ); 00177 connect( job, SIGNAL( result( KIO::Job* ) ), 00178 this, SLOT( result( KIO::Job* ) ) ); 00179 00180 mJobMap.insert( job, QByteArray() ); 00181 } 00182 } 00183 } 00184 } 00185 } 00186 00187 void ResourceLDAPKIO::data( KIO::Job *job, const QByteArray &d ) 00188 { 00189 QByteArray &data = mJobMap[ job ]; 00190 00191 unsigned int oldSize = data.size(); 00192 data.resize( oldSize + d.size() ); 00193 memcpy( data.data() + oldSize, d.data(), d.size()); 00194 } 00195 00196 void ResourceLDAPKIO::result( KIO::Job *job ) 00197 { 00198 mGetCounter--; 00199 00200 if ( job->error() ) { 00201 mErrorOccured = true; 00202 mErrorMsg = job->errorString(); 00203 mJobMap.remove( job ); 00204 return; 00205 } 00206 00207 QByteArray &data = mJobMap[ job ]; 00208 00209 AddresseeList addrList; 00210 bool ok = LDIFConverter::LDIFToAddressee( data, addrList ); 00211 00212 if ( !ok ) { 00213 mErrorOccured = true; 00214 mErrorMsg = i18n( "Error while parsing the LDIF file." ); 00215 } else { 00216 AddresseeList::Iterator it; 00217 for ( it = addrList.begin(); it != addrList.end(); ++it ) { 00218 (*it).setChanged( false ); 00219 (*it).setResource( this ); 00220 insertAddressee( *it ); 00221 } 00222 } 00223 00224 mJobMap.remove( job ); 00225 00226 if ( mGetCounter == 0 ) { 00227 if ( mErrorOccured ) 00228 emit loadingError( this, mErrorMsg ); 00229 else 00230 emit loadingFinished( this ); 00231 } 00232 } 00233 00234 bool ResourceLDAPKIO::save( Ticket* ) 00235 { 00236 return false; // readonly 00237 } 00238 00239 bool ResourceLDAPKIO::asyncSave( Ticket* ) 00240 { 00241 return false; // readonly 00242 } 00243 00244 void ResourceLDAPKIO::removeAddressee( const Addressee& ) 00245 { 00246 } 00247 00248 void ResourceLDAPKIO::setUser( const QString &user ) 00249 { 00250 mUser = user; 00251 } 00252 00253 QString ResourceLDAPKIO::user() const 00254 { 00255 return mUser; 00256 } 00257 00258 void ResourceLDAPKIO::setPassword( const QString &password ) 00259 { 00260 mPassword = password; 00261 } 00262 00263 QString ResourceLDAPKIO::password() const 00264 { 00265 return mPassword; 00266 } 00267 00268 void ResourceLDAPKIO::setDn( const QString &dn ) 00269 { 00270 mDn = dn; 00271 } 00272 00273 QString ResourceLDAPKIO::dn() const 00274 { 00275 return mDn; 00276 } 00277 00278 void ResourceLDAPKIO::setHost( const QString &host ) 00279 { 00280 mHost = host; 00281 } 00282 00283 QString ResourceLDAPKIO::host() const 00284 { 00285 return mHost; 00286 } 00287 00288 void ResourceLDAPKIO::setPort( int port ) 00289 { 00290 mPort = port; 00291 } 00292 00293 int ResourceLDAPKIO::port() const 00294 { 00295 return mPort; 00296 } 00297 00298 void ResourceLDAPKIO::setFilter( const QString &filter ) 00299 { 00300 mFilter = filter; 00301 } 00302 00303 QString ResourceLDAPKIO::filter() const 00304 { 00305 return mFilter; 00306 } 00307 00308 void ResourceLDAPKIO::setIsAnonymous( bool value ) 00309 { 00310 mAnonymous = value; 00311 } 00312 00313 bool ResourceLDAPKIO::isAnonymous() const 00314 { 00315 return mAnonymous; 00316 } 00317 00318 void ResourceLDAPKIO::setAttributes( const QMap<QString, QString> &attributes ) 00319 { 00320 mAttributes = attributes; 00321 } 00322 00323 QMap<QString, QString> ResourceLDAPKIO::attributes() const 00324 { 00325 return mAttributes; 00326 } 00327 00328 #include "resourceldapkio.moc"
KDE Logo
This file is part of the documentation for kabc Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Aug 30 22:56:01 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003