• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeabc
 

tdeabc

  • tdeabc
ldapclient.cpp
1/* kldapclient.cpp - LDAP access
2 * Copyright (C) 2002 Klarälvdalens Datakonsult AB
3 *
4 * Author: Steffen Hansen <hansen@kde.org>
5 *
6 * Ported to KABC by Daniel Molkentin <molkentin@kde.org>
7 *
8 * This file is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This file is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23
24
25#include <tqfile.h>
26#include <tqimage.h>
27#include <tqlabel.h>
28#include <tqpixmap.h>
29#include <tqtextstream.h>
30#include <tqurl.h>
31
32#include <tdeapplication.h>
33#include <tdeconfig.h>
34#include <kdebug.h>
35#include <kmdcodec.h>
36#include <kprotocolinfo.h>
37
38#include "ldapclient.h"
39#include "ldif.h"
40#include "ldapurl.h"
41
42using namespace TDEABC;
43
44class LdapClient::LdapClientPrivate{
45public:
46 TQString bindDN;
47 TQString pwdBindDN;
48 LDIF ldif;
49};
50
51TQString LdapObject::toString() const
52{
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 ) {
57 result += TQString::fromUtf8( LDIF::assembleLine( attr, *it2, 76 ) ) + "\n";
58 }
59 }
60
61 return result;
62}
63
64void LdapObject::clear()
65{
66 dn = TQString::null;
67 attrs.clear();
68}
69
70void LdapObject::assign( const LdapObject& that )
71{
72 if ( &that != this ) {
73 dn = that.dn;
74 attrs = that.attrs;
75 client = that.client;
76 }
77}
78
79LdapClient::LdapClient( TQObject* parent, const char* name )
80 : TQObject( parent, name ), mJob( 0 ), mActive( false )
81{
82 d = new LdapClientPrivate;
83}
84
85LdapClient::~LdapClient()
86{
87 cancelQuery();
88 delete d; d = 0;
89}
90
91void LdapClient::setHost( const TQString& host )
92{
93 mHost = host;
94}
95
96void LdapClient::setPort( const TQString& port )
97{
98 mPort = port;
99}
100
101void LdapClient::setBase( const TQString& base )
102{
103 mBase = base;
104}
105
106void LdapClient::setBindDN( const TQString& bindDN )
107{
108 d->bindDN = bindDN;
109}
110
111void LdapClient::setPwdBindDN( const TQString& pwdBindDN )
112{
113 d->pwdBindDN = pwdBindDN;
114}
115
116void LdapClient::setAttrs( const TQStringList& attrs )
117{
118 mAttrs = attrs;
119}
120
121void LdapClient::startQuery( const TQString& filter )
122{
123 cancelQuery();
124 LDAPUrl url;
125
126 url.setProtocol( "ldap" );
127 url.setUser( d->bindDN );
128 url.setPass( d->pwdBindDN );
129 url.setHost( mHost );
130 url.setPort( mPort.toUInt() );
131 url.setDn( mBase );
132 url.setAttributes( mAttrs );
133 url.setScope( mScope == "one" ? LDAPUrl::One : LDAPUrl::Sub );
134 url.setFilter( "("+filter+")" );
135
136 kdDebug(5700) << "Doing query: " << url.prettyURL() << endl;
137
138 startParseLDIF();
139 mActive = true;
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() ) );
147}
148
149void LdapClient::cancelQuery()
150{
151 if ( mJob ) {
152 mJob->kill();
153 mJob = 0;
154 }
155
156 mActive = false;
157}
158
159void LdapClient::slotData( TDEIO::Job*, const TQByteArray& data )
160{
161#ifndef NDEBUG // don't create the TQString
162// TQString str( data );
163// kdDebug(5700) << "LdapClient: Got \"" << str << "\"\n";
164#endif
165 parseLDIF( data );
166}
167
168void LdapClient::slotInfoMessage( TDEIO::Job*, const TQString & )
169{
170 //tqDebug("Job said \"%s\"", info.latin1());
171}
172
173void LdapClient::slotDone()
174{
175 endParseLDIF();
176 mActive = false;
177#if 0
178 for ( TQValueList<LdapObject>::Iterator it = mObjects.begin(); it != mObjects.end(); ++it ) {
179 tqDebug( (*it).toString().latin1() );
180 }
181#endif
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 ) ) );
185 }
186 emit done();
187}
188
189void LdapClient::startParseLDIF()
190{
191 mCurrentObject.clear();
192 mLastAttrName = 0;
193 mLastAttrValue = 0;
194 mIsBase64 = false;
195 d->ldif.startParsing();
196}
197
198void LdapClient::endParseLDIF()
199{
200}
201
202void LdapClient::parseLDIF( const TQByteArray& data )
203{
204 if ( data.size() ) {
205 d->ldif.setLDIF( data );
206 } else {
207 d->ldif.endLDIF();
208 }
209
210 LDIF::ParseVal ret;
211 TQString name;
212 do {
213 ret = d->ldif.nextItem();
214 switch ( ret ) {
215 case LDIF::Item:
216 {
217 name = d->ldif.attr();
218 // Must make a copy! TQByteArray is explicitely shared
219 TQByteArray value = d->ldif.val().copy();
220 mCurrentObject.attrs[ name ].append( value );
221 break;
222 }
223 case LDIF::EndEntry:
224 mCurrentObject.dn = d->ldif.dn();
225 mCurrentObject.client = this;
226 emit result( mCurrentObject );
227 mCurrentObject.clear();
228 break;
229 default:
230 break;
231 }
232 } while ( ret != LDIF::MoreData );
233}
234
235TQString LdapClient::bindDN() const
236{
237 return d->bindDN;
238}
239
240TQString LdapClient::pwdBindDN() const
241{
242 return d->pwdBindDN;
243}
244
245LdapSearch::LdapSearch()
246 : mActiveClients( 0 ), mNoLDAPLookup( false )
247{
248 if ( !KProtocolInfo::isKnownProtocol( KURL("ldap://localhost") ) ) {
249 mNoLDAPLookup = true;
250 return;
251 }
252
253 // stolen from KAddressBook
254 TDEConfig config( "kabldaprc", true );
255 config.setGroup( "LDAP" );
256 int numHosts = config.readUnsignedNumEntry( "NumSelectedHosts");
257 if ( !numHosts ) {
258 mNoLDAPLookup = true;
259 return;
260 } else {
261 for ( int j = 0; j < numHosts; j++ ) {
262 LdapClient* ldapClient = new LdapClient( this );
263
264 TQString host = config.readEntry( TQString( "SelectedHost%1" ).arg( j ), "" ).stripWhiteSpace();
265 if ( !host.isEmpty() )
266 ldapClient->setHost( host );
267
268 TQString port = TQString::number( config.readUnsignedNumEntry( TQString( "SelectedPort%1" ).arg( j ) ) );
269 if ( !port.isEmpty() )
270 ldapClient->setPort( port );
271
272 TQString base = config.readEntry( TQString( "SelectedBase%1" ).arg( j ), "" ).stripWhiteSpace();
273 if ( !base.isEmpty() )
274 ldapClient->setBase( base );
275
276 TQString bindDN = config.readEntry( TQString( "SelectedBind%1" ).arg( j ) ).stripWhiteSpace();
277 if ( !bindDN.isEmpty() )
278 ldapClient->setBindDN( bindDN );
279
280 TQString pwdBindDN = config.readEntry( TQString( "SelectedPwdBind%1" ).arg( j ) );
281 if ( !pwdBindDN.isEmpty() )
282 ldapClient->setPwdBindDN( pwdBindDN );
283
284 TQStringList attrs;
285 attrs << "cn" << "mail" << "givenname" << "sn";
286 ldapClient->setAttrs( attrs );
287
288 connect( ldapClient, TQ_SIGNAL( result( const TDEABC::LdapObject& ) ),
289 this, TQ_SLOT( slotLDAPResult( const TDEABC::LdapObject& ) ) );
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& ) ) );
294
295 mClients.append( ldapClient );
296 }
297 }
298
299 connect( &mDataTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotDataTimer() ) );
300}
301
302void LdapSearch::startSearch( const TQString& txt )
303{
304 if ( mNoLDAPLookup )
305 return;
306
307 cancelSearch();
308
309 int pos = txt.find( '\"' );
310 if( pos >= 0 )
311 {
312 ++pos;
313 int pos2 = txt.find( '\"', pos );
314 if( pos2 >= 0 )
315 mSearchText = txt.mid( pos , pos2 - pos );
316 else
317 mSearchText = txt.mid( pos );
318 } else
319 mSearchText = txt;
320
321 TQString filter = TQString( "|(cn=%1*)(mail=%2*)(givenName=%3*)(sn=%4*)" )
322 .arg( mSearchText ).arg( mSearchText ).arg( mSearchText ).arg( mSearchText );
323
324 TQValueList< LdapClient* >::Iterator it;
325 for ( it = mClients.begin(); it != mClients.end(); ++it ) {
326 (*it)->startQuery( filter );
327 ++mActiveClients;
328 }
329}
330
331void LdapSearch::cancelSearch()
332{
333 TQValueList< LdapClient* >::Iterator it;
334 for ( it = mClients.begin(); it != mClients.end(); ++it )
335 (*it)->cancelQuery();
336
337 mActiveClients = 0;
338 mResults.clear();
339}
340
341void LdapSearch::slotLDAPResult( const TDEABC::LdapObject& obj )
342{
343 mResults.append( obj );
344 if ( !mDataTimer.isActive() )
345 mDataTimer.start( 500, true );
346}
347
348void LdapSearch::slotLDAPError( const TQString& )
349{
350 slotLDAPDone();
351}
352
353void LdapSearch::slotLDAPDone()
354{
355 if ( --mActiveClients > 0 )
356 return;
357
358 finish();
359}
360
361void LdapSearch::slotDataTimer()
362{
363 TQStringList lst;
364 LdapResultList reslist;
365 makeSearchData( lst, reslist );
366 if ( !lst.isEmpty() )
367 emit searchData( lst );
368 if ( !reslist.isEmpty() )
369 emit searchData( reslist );
370}
371
372void LdapSearch::finish()
373{
374 mDataTimer.stop();
375
376 slotDataTimer(); // emit final bunch of data
377 emit searchDone();
378}
379
380void LdapSearch::makeSearchData( TQStringList& ret, LdapResultList& resList )
381{
382 TQString search_text_upper = mSearchText.upper();
383
384 TQValueList< TDEABC::LdapObject >::ConstIterator it1;
385 for ( it1 = mResults.begin(); it1 != mResults.end(); ++it1 ) {
386 TQString name, mail, givenname, sn;
387
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" )
392 name = tmp; // TODO loop?
393 else if( it2.key() == "mail" )
394 mail = tmp;
395 else if( it2.key() == "givenName" )
396 givenname = tmp;
397 else if( it2.key() == "sn" )
398 sn = tmp;
399 }
400
401 if( mail.isEmpty())
402 continue; // nothing, bad entry
403 else if ( name.isEmpty() )
404 ret.append( mail );
405 else {
406 kdDebug(5700) << "<" << name << "><" << mail << ">" << endl;
407 ret.append( TQString( "%1 <%2>" ).arg( name ).arg( mail ) );
408 }
409
410 LdapResult sr;
411 sr.clientNumber = mClients.findIndex( (*it1).client );
412 sr.name = name;
413 sr.email = mail;
414 resList.append( sr );
415 }
416
417 mResults.clear();
418}
419
420bool LdapSearch::isAvailable() const
421{
422 return !mNoLDAPLookup;
423}
424
425
426
427#include "ldapclient.moc"
KURL
KURL::setUser
void setUser(const TQString &_txt)
KURL::setHost
void setHost(const TQString &_txt)
KURL::setPass
void setPass(const TQString &_txt)
KURL::setPort
void setPort(unsigned short int _p)
KURL::prettyURL
TQString prettyURL(int _trailing=0) const
KURL::setProtocol
void setProtocol(const TQString &_txt)
TDEABC::LDAPUrl
LDAPUrl.
Definition: ldapurl.h:43
TDEABC::LDAPUrl::setScope
void setScope(Scope scope)
Sets the scope part of the LDAP Url.
Definition: ldapurl.h:75
TDEABC::LDAPUrl::setAttributes
void setAttributes(const TQStringList &attributes)
Sets the attributes part of the LDAP Url.
Definition: ldapurl.h:69
TDEABC::LDAPUrl::setDn
void setDn(const TQString &dn)
Sets the the dn part of the LDAP Url.
Definition: ldapurl.cpp:47
TDEABC::LDAPUrl::setFilter
void setFilter(TQString filter)
Sets the filter part of the LDAP Url.
Definition: ldapurl.h:80
TDEABC::LDIF
LDIF.
Definition: ldif.h:41
TDEABC::LDIF::assembleLine
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...
Definition: ldif.cpp:37
TDEABC::LdapClient
This class is internal.
Definition: ldapclient.h:86
TDEABC::LdapClient::error
void error(const TQString &)
TDEABC::LdapClient::setHost
void setHost(const TQString &host)
Definition: ldapclient.cpp:91
TDEABC::LdapClient::setBase
void setBase(const TQString &base)
Definition: ldapclient.cpp:101
TDEABC::LdapClient::setAttrs
void setAttrs(const TQStringList &attrs)
Definition: ldapclient.cpp:116
TDEABC::LdapClient::setPwdBindDN
void setPwdBindDN(const TQString &pwdBindDN)
Definition: ldapclient.cpp:111
TDEABC::LdapClient::result
void result(const TDEABC::LdapObject &)
TDEABC::LdapClient::setPort
void setPort(const TQString &port)
Definition: ldapclient.cpp:96
TDEABC::LdapClient::done
void done()
TDEABC::LdapClient::cancelQuery
void cancelQuery()
Definition: ldapclient.cpp:149
TDEABC::LdapClient::setBindDN
void setBindDN(const TQString &bindDN)
Definition: ldapclient.cpp:106
TDEABC::LdapClient::startQuery
void startQuery(const TQString &filter)
Definition: ldapclient.cpp:121
TDEABC::LdapObject
This class is internal.
Definition: ldapclient.h:50
TDEABC::LdapSearch::searchData
void searchData(const TQStringList &)
Results, assembled as "Full Name <email>" (This signal can be emitted many times)
TDEConfig
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
KStdAction::mail
TDEAction * mail(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48
TDEStdAccel::name
TQString name(StdAccel id)
TDEABC::LdapResult
Structure describing one result returned by a LDAP query.
Definition: ldapclient.h:192
TDEABC::LdapResult::clientNumber
int clientNumber
for sorting
Definition: ldapclient.h:195
TDEABC::LdapResult::email
TQString email
email
Definition: ldapclient.h:194
TDEABC::LdapResult::name
TQString name
full name
Definition: ldapclient.h:193

tdeabc

Skip menu "tdeabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeabc

Skip menu "tdeabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeabc by doxygen 1.9.4
This website is maintained by Timothy Pearson.