29 #include <tqtextstream.h>
32 #include <tdeapplication.h>
33 #include <tdeconfig.h>
36 #include <kprotocolinfo.h>
38 #include "ldapclient.h"
44 class LdapClient::LdapClientPrivate{
51 TQString LdapObject::toString()
const
53 TQString result = TQString::fromLatin1(
"\ndn: %1\n" ).arg( dn );
54 for ( LdapAttrMap::ConstIterator it = attrs.begin(); it != attrs.end(); ++it ) {
55 TQString attr = it.key();
56 for ( LdapAttrValue::ConstIterator it2 = (*it).begin(); it2 != (*it).end(); ++it2 ) {
64 void LdapObject::clear()
70 void LdapObject::assign(
const LdapObject& that )
72 if ( &that !=
this ) {
79 LdapClient::LdapClient( TQObject* parent,
const char* name )
80 : TQObject( parent,
name ), mJob( 0 ), mActive( false )
82 d =
new LdapClientPrivate;
85 LdapClient::~LdapClient()
113 d->pwdBindDN = pwdBindDN;
133 url.
setScope( mScope ==
"one" ? LDAPUrl::One : LDAPUrl::Sub );
140 mJob = TDEIO::get( url,
false,
false );
141 connect( mJob, TQ_SIGNAL( data( TDEIO::Job*,
const TQByteArray& ) ),
142 this, TQ_SLOT( slotData( TDEIO::Job*,
const TQByteArray& ) ) );
143 connect( mJob, TQ_SIGNAL( infoMessage( TDEIO::Job*,
const TQString& ) ),
144 this, TQ_SLOT( slotInfoMessage( TDEIO::Job*,
const TQString& ) ) );
145 connect( mJob, TQ_SIGNAL(
result( TDEIO::Job* ) ),
146 this, TQ_SLOT( slotDone() ) );
159 void LdapClient::slotData( TDEIO::Job*,
const TQByteArray& data )
168 void LdapClient::slotInfoMessage( TDEIO::Job*,
const TQString & )
173 void LdapClient::slotDone()
178 for ( TQValueList<LdapObject>::Iterator it = mObjects.begin(); it != mObjects.end(); ++it ) {
179 tqDebug( (*it).toString().latin1() );
182 int err = mJob->error();
183 if ( err && err != TDEIO::ERR_USER_CANCELED ) {
184 emit
error( TDEIO::buildErrorString( err, TQString(
"%1:%2").arg( mHost ).arg( mPort ) ) );
189 void LdapClient::startParseLDIF()
191 mCurrentObject.clear();
195 d->ldif.startParsing();
198 void LdapClient::endParseLDIF()
202 void LdapClient::parseLDIF(
const TQByteArray& data )
205 d->ldif.setLDIF( data );
213 ret = d->ldif.nextItem();
217 name = d->ldif.attr();
219 TQByteArray value = d->ldif.val().copy();
220 mCurrentObject.attrs[
name ].append( value );
224 mCurrentObject.dn = d->ldif.dn();
225 mCurrentObject.client =
this;
226 emit
result( mCurrentObject );
227 mCurrentObject.clear();
232 }
while ( ret != LDIF::MoreData );
235 TQString LdapClient::bindDN()
const
240 TQString LdapClient::pwdBindDN()
const
245 LdapSearch::LdapSearch()
246 : mActiveClients( 0 ), mNoLDAPLookup( false )
248 if ( !KProtocolInfo::isKnownProtocol(
KURL(
"ldap://localhost") ) ) {
249 mNoLDAPLookup =
true;
255 config.setGroup(
"LDAP" );
256 int numHosts = config.readUnsignedNumEntry(
"NumSelectedHosts");
258 mNoLDAPLookup =
true;
261 for (
int j = 0; j < numHosts; j++ ) {
264 TQString host = config.readEntry( TQString(
"SelectedHost%1" ).arg( j ),
"" ).stripWhiteSpace();
265 if ( !host.isEmpty() )
268 TQString port = TQString::number( config.readUnsignedNumEntry( TQString(
"SelectedPort%1" ).arg( j ) ) );
269 if ( !port.isEmpty() )
272 TQString base = config.readEntry( TQString(
"SelectedBase%1" ).arg( j ),
"" ).stripWhiteSpace();
273 if ( !base.isEmpty() )
276 TQString bindDN = config.readEntry( TQString(
"SelectedBind%1" ).arg( j ) ).stripWhiteSpace();
277 if ( !bindDN.isEmpty() )
280 TQString pwdBindDN = config.readEntry( TQString(
"SelectedPwdBind%1" ).arg( j ) );
281 if ( !pwdBindDN.isEmpty() )
285 attrs <<
"cn" <<
"mail" <<
"givenname" <<
"sn";
290 connect( ldapClient, TQ_SIGNAL(
done() ),
291 this, TQ_SLOT( slotLDAPDone() ) );
292 connect( ldapClient, TQ_SIGNAL(
error(
const TQString& ) ),
293 this, TQ_SLOT( slotLDAPError(
const TQString& ) ) );
295 mClients.append( ldapClient );
299 connect( &mDataTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotDataTimer() ) );
302 void LdapSearch::startSearch(
const TQString& txt )
309 int pos = txt.find(
'\"' );
313 int pos2 = txt.find(
'\"', pos );
315 mSearchText = txt.mid( pos , pos2 - pos );
317 mSearchText = txt.mid( pos );
321 TQString filter = TQString(
"|(cn=%1*)(mail=%2*)(givenName=%3*)(sn=%4*)" )
322 .arg( mSearchText ).arg( mSearchText ).arg( mSearchText ).arg( mSearchText );
324 TQValueList< LdapClient* >::Iterator it;
325 for ( it = mClients.begin(); it != mClients.end(); ++it ) {
326 (*it)->startQuery( filter );
331 void LdapSearch::cancelSearch()
333 TQValueList< LdapClient* >::Iterator it;
334 for ( it = mClients.begin(); it != mClients.end(); ++it )
335 (*it)->cancelQuery();
343 mResults.append( obj );
344 if ( !mDataTimer.isActive() )
345 mDataTimer.start( 500,
true );
348 void LdapSearch::slotLDAPError(
const TQString& )
353 void LdapSearch::slotLDAPDone()
355 if ( --mActiveClients > 0 )
361 void LdapSearch::slotDataTimer()
364 LdapResultList reslist;
365 makeSearchData( lst, reslist );
366 if ( !lst.isEmpty() )
368 if ( !reslist.isEmpty() )
372 void LdapSearch::finish()
380 void LdapSearch::makeSearchData( TQStringList& ret, LdapResultList& resList )
382 TQString search_text_upper = mSearchText.upper();
384 TQValueList< TDEABC::LdapObject >::ConstIterator it1;
385 for ( it1 = mResults.begin(); it1 != mResults.end(); ++it1 ) {
388 LdapAttrMap::ConstIterator it2;
389 for ( it2 = (*it1).attrs.begin(); it2 != (*it1).attrs.end(); ++it2 ) {
390 TQString tmp = TQString::fromUtf8( (*it2).first(), (*it2).first().size() );
391 if ( it2.key() ==
"cn" )
393 else if( it2.key() ==
"mail" )
395 else if( it2.key() ==
"givenName" )
397 else if( it2.key() ==
"sn" )
403 else if (
name.isEmpty() )
407 ret.append( TQString(
"%1 <%2>" ).arg( name ).arg( mail ) );
414 resList.append( sr );
420 bool LdapSearch::isAvailable()
const
422 return !mNoLDAPLookup;
427 #include "ldapclient.moc"
void setUser(const TQString &_txt)
void setHost(const TQString &_txt)
void setPass(const TQString &_txt)
void setPort(unsigned short int _p)
TQString prettyURL(int _trailing=0) const
void setProtocol(const TQString &_txt)
void setScope(Scope scope)
Sets the scope part of the LDAP Url.
void setAttributes(const TQStringList &attributes)
Sets the attributes part of the LDAP Url.
void setDn(const TQString &dn)
Sets the the dn part of the LDAP Url.
void setFilter(TQString filter)
Sets the filter part of the LDAP Url.
static TQCString assembleLine(const TQString &fieldname, const TQByteArray &value, uint linelen=0, bool url=false)
Assembles fieldname and value into a valid LDIF line, BASE64 encodes the value if neccessary and opti...
void error(const TQString &)
void setHost(const TQString &host)
void setBase(const TQString &base)
void setAttrs(const TQStringList &attrs)
void setPwdBindDN(const TQString &pwdBindDN)
void result(const TDEABC::LdapObject &)
void setPort(const TQString &port)
void setBindDN(const TQString &bindDN)
void startQuery(const TQString &filter)
void searchData(const TQStringList &)
Results, assembled as "Full Name <email>" (This signal can be emitted many times)
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
TDEAction * mail(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
static data, shared by ALL addressee objects
TQString name(StdAccel id)
Structure describing one result returned by a LDAP query.
int clientNumber
for sorting