00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <kmdcodec.h>
00024 #include <qpixmap.h>
00025 #include <qimage.h>
00026 #include <qlabel.h>
00027 #include <qfile.h>
00028
00029 #include <kprotocolinfo.h>
00030 #include <kconfig.h>
00031 #include <kapplication.h>
00032
00033 #include <kdebug.h>
00034
00035 #include "ldapclient.h"
00036
00037 using namespace KABC;
00038
00039 QString LdapObject::toString() const
00040 {
00041 QString result = QString::fromLatin1( "\ndn: %1\n" ).arg( dn );
00042 for ( LdapAttrMap::ConstIterator it = attrs.begin(); it != attrs.end(); ++it ) {
00043 QString attr = it.key();
00044 for ( LdapAttrValue::ConstIterator it2 = (*it).begin(); it2 != (*it).end(); ++it2 ) {
00045 if ( attr == "jpegPhoto" ) {
00046 QByteArray buf = *it2;
00047 #if 0
00048 qDebug( "Trying to load image from buf with size %d", (*it2).size() );
00049 QPixmap pix;
00050 pix.loadFromData( buf, "JPEG" );
00051 qDebug( "Image loaded successfully" );
00052 QLabel* l = new QLabel( 0 );
00053 QFile f( "tmp.jpg" );
00054 f.open( IO_WriteOnly );
00055 f.writeBlock( buf );
00056 f.close();
00057
00058
00059 #endif
00060 } else {
00061 result += QString("%1: %2\n").arg( attr).arg( *it2 );
00062 }
00063 }
00064 }
00065
00066 return result;
00067 }
00068
00069 void LdapObject::clear()
00070 {
00071 dn = QString::null;
00072 attrs.clear();
00073 }
00074
00075 void LdapObject::assign( const LdapObject& that )
00076 {
00077 if ( &that != this ) {
00078 dn = that.dn;
00079 attrs = that.attrs;
00080 }
00081 }
00082
00083 LdapClient::LdapClient( QObject* parent, const char* name )
00084 : QObject( parent, name ), mJob( 0 ), mActive( false )
00085 {
00086 }
00087
00088 LdapClient::~LdapClient()
00089 {
00090 cancelQuery();
00091 }
00092
00093 void LdapClient::setHost( const QString& host )
00094 {
00095 mHost = host;
00096 }
00097
00098 void LdapClient::setPort( const QString& port )
00099 {
00100 mPort = port;
00101 }
00102
00103 void LdapClient::setBase( const QString& base )
00104 {
00105 mBase = base;
00106 }
00107
00108 void LdapClient::setAttrs( const QStringList& attrs )
00109 {
00110 mAttrs = attrs;
00111 }
00112
00113 void LdapClient::startQuery( const QString& filter )
00114 {
00115 cancelQuery();
00116 QString query;
00117 if ( mScope.isEmpty() )
00118 mScope = "sub";
00119
00120 QString host = mHost;
00121 if ( !mPort.isEmpty() ) {
00122 host += ':';
00123 host += mPort;
00124 }
00125
00126 if ( mAttrs.empty() ) {
00127 query = QString("ldap://%1/%2?*?%3?(%4)").arg( host ).arg( mBase ).arg( mScope ).arg( filter );
00128 } else {
00129 query = QString("ldap://%1/%2?%3?%4?(%5)").arg( host ).arg( mBase )
00130 .arg( mAttrs.join(",") ).arg( mScope ).arg( filter );
00131 }
00132 kdDebug(5700) << "Doing query" << query.latin1() << endl;
00133
00134 startParseLDIF();
00135 mActive = true;
00136 mJob = KIO::get( KURL( query ), false, false );
00137 connect( mJob, SIGNAL( data( KIO::Job*, const QByteArray& ) ),
00138 this, SLOT( slotData( KIO::Job*, const QByteArray& ) ) );
00139 connect( mJob, SIGNAL( infoMessage( KIO::Job*, const QString& ) ),
00140 this, SLOT( slotInfoMessage( KIO::Job*, const QString& ) ) );
00141 connect( mJob, SIGNAL( result( KIO::Job* ) ),
00142 this, SLOT( slotDone() ) );
00143 }
00144
00145 void LdapClient::cancelQuery()
00146 {
00147 if ( mJob ) {
00148 mJob->kill();
00149 mJob = 0;
00150 }
00151
00152 mActive = false;
00153 }
00154
00155 void LdapClient::slotData( KIO::Job*, const QByteArray& data )
00156 {
00157 QString str( data );
00158 kdDebug(5700) << "Got" << str.latin1() << endl;
00159 parseLDIF( data );
00160 }
00161
00162 void LdapClient::slotInfoMessage( KIO::Job*, const QString & )
00163 {
00164
00165 }
00166
00167 void LdapClient::slotDone()
00168 {
00169 endParseLDIF();
00170 mActive = false;
00171 #if 0
00172 for ( QValueList<LdapObject>::Iterator it = mObjects.begin(); it != mObjects.end(); ++it ) {
00173 qDebug( (*it).toString().latin1() );
00174 }
00175 #endif
00176 int err = mJob->error();
00177 if ( err ) {
00178 emit error( KIO::buildErrorString( err, QString("%1:%2").arg( mHost ).arg( mPort ) ) );
00179 }
00180 emit done();
00181 }
00182
00183 void LdapClient::startParseLDIF()
00184 {
00185 mCurrentObject.clear();
00186 mLastAttrName = 0;
00187 mLastAttrValue = 0;
00188 mIsBase64 = false;
00189 }
00190
00191 void LdapClient::endParseLDIF()
00192 {
00193 if ( !mCurrentObject.dn.isEmpty() ) {
00194 if ( !mLastAttrName.isNull() && !mLastAttrValue.isNull() ) {
00195 if ( mIsBase64 ) {
00196 QByteArray out;
00197 KCodecs::base64Decode( mLastAttrValue, out );
00198
00199 mCurrentObject.attrs[ mLastAttrName ].append( out );
00200 } else {
00201 mCurrentObject.attrs[ mLastAttrName ].append( mLastAttrValue );
00202 }
00203 }
00204 emit result( mCurrentObject );
00205 }
00206 }
00207
00208 void LdapClient::parseLDIF( const QByteArray& data )
00209 {
00210
00211 if ( data.isNull() || data.isEmpty() )
00212 return;
00213 mBuf += QCString( data );
00214 int nl;
00215 while ( (nl = mBuf.find('\n')) != -1 ) {
00216
00217
00218
00219
00220 QCString line = mBuf.left( nl );
00221 if ( mBuf.length() > (unsigned int)(nl+1) )
00222 mBuf = mBuf.mid( nl+1 );
00223 else
00224 mBuf = "";
00225
00226 if ( line.length() > 0 ) {
00227 if ( line[ 0 ] == '#' ) {
00228 continue;
00229 } else if ( line[ 0 ] == ' ' || line[ 0 ] == '\t' ) {
00230 line = line.stripWhiteSpace();
00231
00232 mLastAttrValue += line;
00233 continue;
00234 }
00235 } else
00236 continue;
00237
00238 int colon = line.find(':');
00239 if ( colon != -1 ) {
00240 if ( mLastAttrName == "dn" ) {
00241 if ( !mCurrentObject.dn.isNull() ) {
00242 emit result( mCurrentObject );
00243 mCurrentObject.clear();
00244 }
00245 mCurrentObject.dn = mLastAttrValue;
00246 mLastAttrValue = 0;
00247 mLastAttrName = 0;
00248 } else if ( !mLastAttrName.isEmpty() ) {
00249
00250 if ( mIsBase64 ) {
00251 QByteArray out;
00252 KCodecs::base64Decode( mLastAttrValue, out );
00253
00254 mCurrentObject.attrs[ mLastAttrName ].append( out );
00255 } else {
00256 mCurrentObject.attrs[ mLastAttrName ].append( mLastAttrValue );
00257 }
00258 }
00259
00260 mLastAttrName = line.left( colon ).stripWhiteSpace();
00261
00262 ++colon;
00263 if ( line[colon] == ':' ) {
00264 mIsBase64 = true;
00265
00266 ++colon;
00267 } else {
00268
00269 mIsBase64 = false;
00270 }
00271
00272 mLastAttrValue = line.mid( colon ).stripWhiteSpace();
00273 }
00274 }
00275 }
00276
00277 LdapSearch::LdapSearch()
00278 : mActiveClients( 0 ), mNoLDAPLookup( false )
00279 {
00280 if ( !KProtocolInfo::isKnownProtocol( KURL("ldap://localhost") ) ) {
00281 mNoLDAPLookup = true;
00282 return;
00283 }
00284
00285
00286 KConfig config( "kaddressbookrc", true );
00287 config.setGroup( "LDAP" );
00288 int numHosts = config.readUnsignedNumEntry( "NumSelectedHosts");
00289 if ( !numHosts ) {
00290 mNoLDAPLookup = true;
00291 return;
00292 } else {
00293 for ( int j = 0; j < numHosts; j++ ) {
00294 LdapClient* ldapClient = new LdapClient( this );
00295
00296 QString host = config.readEntry( QString( "SelectedHost%1" ).arg( j ), "" ).stripWhiteSpace();
00297 if ( host != "" )
00298 ldapClient->setHost( host );
00299
00300 QString port = QString::number( config.readUnsignedNumEntry( QString( "SelectedPort%1" ).arg( j ) ) );
00301 if ( !port.isEmpty() )
00302 ldapClient->setPort( port );
00303
00304 QString base = config.readEntry( QString( "SelectedBase%1" ).arg( j ), "" ).stripWhiteSpace();
00305 if ( base != "" )
00306 ldapClient->setBase( base );
00307
00308 QStringList attrs;
00309 attrs << "cn" << "mail" << "givenname" << "sn";
00310 ldapClient->setAttrs( attrs );
00311
00312 connect( ldapClient, SIGNAL( result( const KABC::LdapObject& ) ),
00313 this, SLOT( slotLDAPResult( const KABC::LdapObject& ) ) );
00314 connect( ldapClient, SIGNAL( done() ),
00315 this, SLOT( slotLDAPDone() ) );
00316 connect( ldapClient, SIGNAL( error( const QString& ) ),
00317 this, SLOT( slotLDAPError( const QString& ) ) );
00318
00319 mClients.append( ldapClient );
00320 }
00321 }
00322
00323 connect( &mDataTimer, SIGNAL( timeout() ), SLOT( slotDataTimer() ) );
00324 }
00325
00326 void LdapSearch::startSearch( const QString& txt )
00327 {
00328 if ( mNoLDAPLookup )
00329 return;
00330
00331 cancelSearch();
00332 int pos = txt.find( '\"' );
00333 if( pos >= 0 )
00334 {
00335 ++pos;
00336 int pos2 = txt.find( '\"', pos );
00337 if( pos2 >= 0 )
00338 mSearchText = txt.mid( pos , pos2 - pos );
00339 else
00340 mSearchText = txt.mid( pos );
00341 } else
00342 mSearchText = txt;
00343
00344 QString filter = QString( "|(cn=%1*)(mail=%2*)(givenName=%3*)(sn=%4*)" )
00345 .arg( mSearchText ).arg( mSearchText ).arg( mSearchText ).arg( mSearchText );
00346
00347 QValueList< LdapClient* >::Iterator it;
00348 for ( it = mClients.begin(); it != mClients.end(); ++it ) {
00349 (*it)->startQuery( filter );
00350 ++mActiveClients;
00351 }
00352 }
00353
00354 void LdapSearch::cancelSearch()
00355 {
00356 QValueList< LdapClient* >::Iterator it;
00357 for ( it = mClients.begin(); it != mClients.end(); ++it )
00358 (*it)->cancelQuery();
00359
00360 mActiveClients = 0;
00361 mResults.clear();
00362 }
00363
00364 void LdapSearch::slotLDAPResult( const KABC::LdapObject& obj )
00365 {
00366 mResults.append( obj );
00367 if ( !mDataTimer.isActive() )
00368 mDataTimer.start( 500, true );
00369 }
00370
00371 void LdapSearch::slotLDAPError( const QString& )
00372 {
00373 slotLDAPDone();
00374 }
00375
00376 void LdapSearch::slotLDAPDone()
00377 {
00378 if ( --mActiveClients > 0 )
00379 return;
00380
00381 finish();
00382 }
00383
00384 void LdapSearch::slotDataTimer()
00385 {
00386 emit searchData( makeSearchData() );
00387 }
00388
00389 void LdapSearch::finish()
00390 {
00391 mDataTimer.stop();
00392
00393 emit searchData( makeSearchData() );
00394 emit searchDone();
00395 }
00396
00397 QStringList LdapSearch::makeSearchData()
00398 {
00399 QStringList ret;
00400 QString search_text_upper = mSearchText.upper();
00401
00402 QValueList< KABC::LdapObject >::ConstIterator it1;
00403 for ( it1 = mResults.begin(); it1 != mResults.end(); ++it1 ) {
00404 QString name, mail, givenname, sn;
00405
00406 LdapAttrMap::ConstIterator it2;
00407 for ( it2 = (*it1).attrs.begin(); it2 != (*it1).attrs.end(); ++it2 ) {
00408 if ( it2.key() == "cn" )
00409 name = QString( "%1" ).arg( (*it2).first() );
00410 else if( it2.key() == "mail" )
00411 mail = QString( "%1" ).arg( (*it2).first() );
00412 else if( it2.key() == "givenName" )
00413 givenname = QString( "%1" ).arg( (*it2).first() );
00414 else if( it2.key() == "sn" )
00415 sn = QString( "%1" ).arg( (*it2).first() );
00416 }
00417
00418 if( mail.isEmpty())
00419 ;
00420 else if ( name.isEmpty() )
00421 ret.append( mail );
00422 else {
00423 kdDebug() << "<" << name << "><" << mail << ">" << endl;
00424 ret.append( QString( "%1 <%2>" ).arg( name ).arg( mail ) );
00425
00426 if ( givenname.upper().startsWith( search_text_upper ) )
00427 ret.append( QString( "$$%1$%2 <%3>" ).arg( givenname ).arg( name ).arg( mail ) );
00428 if ( sn.upper().startsWith( search_text_upper ) )
00429 ret.append( QString( "$$%1$%2 <%3>" ).arg( sn ).arg( name ).arg( mail ) );
00430 }
00431 }
00432
00433 mResults.clear();
00434
00435 return ret;
00436 }
00437
00438 bool LdapSearch::isAvailable() const
00439 {
00440 return !mNoLDAPLookup;
00441 }
00442
00443 #include "ldapclient.moc"