28 #include <kldap/ldapobject.h>
29 #include <kldap/ldapserver.h>
30 #include <kldap/ldapurl.h>
31 #include <kldap/ldif.h>
34 #include <KConfigGroup>
37 #include <KProtocolInfo>
38 #include <KStandardDirs>
41 #include <QtCore/QPointer>
42 #include <QtCore/QTimer>
44 using namespace KLDAP;
46 class LdapClient::Private
60 #ifdef KDEPIM_INPROCESS_LDAP
61 mSession->disconnectAndDelete();
66 void startParseLDIF();
67 void parseLDIF(
const QByteArray &data );
69 void finishCurrentObject();
71 void slotData( KIO::Job*,
const QByteArray &data );
72 void slotData(
const QByteArray &data );
73 void slotInfoMessage(
KJob*,
const QString &info,
const QString& );
78 KLDAP::LdapServer mServer;
85 KLDAP::LdapObject mCurrentObject;
88 int mCompletionWeight;
94 :
QObject( parent ), d( new Private( this ) )
97 d->mCompletionWeight = 50 - d->mClientNumber;
113 #ifdef KDEPIM_INPROCESS_LDAP
116 d->mSession->connectToServer( server );
128 d->mAttrs << QLatin1String(
"objectClass");
146 url = d->mServer.url();
148 url.setAttributes( d->mAttrs );
149 url.setScope( d->mScope == QLatin1String(
"one") ? KLDAP::LdapUrl::One : KLDAP::LdapUrl::Sub );
150 url.setFilter( QLatin1Char(
'(') + filter + QLatin1Char(
')') );
152 kDebug(5300) <<
"LdapClient: Doing query:" << url.prettyUrl();
156 #ifndef KDEPIM_INPROCESS_LDAP
157 d->mJob = KIO::get( url, KIO::NoReload, KIO::HideProgressInfo );
158 connect( d->mJob, SIGNAL(data(KIO::Job*,QByteArray)),
159 this, SLOT(slotData(KIO::Job*,QByteArray)) );
163 d->mJob = d->mSession->get( url );
164 connect( d->mJob, SIGNAL(data(QByteArray)),
165 this, SLOT(slotData(QByteArray)) );
167 connect( d->mJob, SIGNAL(infoMessage(
KJob*,QString,QString)),
168 this, SLOT(slotInfoMessage(
KJob*,QString,QString)) );
170 this, SLOT(slotDone()) );
183 void LdapClient::Private::slotData( KIO::Job*,
const QByteArray &data )
188 void LdapClient::Private::slotData(
const QByteArray &data )
193 void LdapClient::Private::slotInfoMessage(
KJob*,
const QString&,
const QString& )
198 void LdapClient::Private::slotDone()
204 int err = mJob->error();
205 if ( err && err != KIO::ERR_USER_CANCELED ) {
206 emit q->error( mJob->errorString() );
208 #ifdef KDEPIM_INPROCESS_LDAP
209 QMetaObject::invokeMethod( mJob,
"deleteLater", Qt::QueuedConnection );
214 void LdapClient::Private::startParseLDIF()
216 mCurrentObject.clear();
217 mLdif.startParsing();
220 void LdapClient::Private::endParseLDIF()
224 void LdapClient::Private::finishCurrentObject()
226 mCurrentObject.setDn( mLdif.dn() );
227 KLDAP::LdapAttrValue objectclasses;
228 KLDAP::LdapAttrMap::ConstIterator end = mCurrentObject.attributes().constEnd();
229 for ( KLDAP::LdapAttrMap::ConstIterator it = mCurrentObject.attributes().constBegin();
232 if ( it.key().toLower() == QLatin1String(
"objectclass") ) {
233 objectclasses = it.value();
238 bool groupofnames =
false;
239 KLDAP::LdapAttrValue::ConstIterator endValue(objectclasses.constEnd());
240 for ( KLDAP::LdapAttrValue::ConstIterator it = objectclasses.constBegin();
241 it != endValue; ++it ) {
243 QByteArray sClass = (*it).toLower();
244 if ( sClass ==
"groupofnames" || sClass ==
"kolabgroupofnames" ) {
249 if ( groupofnames ) {
250 KLDAP::LdapAttrMap::ConstIterator it = mCurrentObject.attributes().find( QLatin1String(
"mail") );
251 if ( it == mCurrentObject.attributes().end() ) {
255 QStringList lMail = mCurrentObject.dn().toString().split( QLatin1String(
",dc="), QString::SkipEmptyParts );
256 const int n = lMail.count();
258 if ( lMail.first().toLower().startsWith( QLatin1String(
"cn=" ) ) ) {
259 sMail = lMail.first().simplified().mid( 3 );
261 sMail.append( QLatin1Char(
'@') );
263 for (
int i = 1; i < n; ++i ) {
264 sMail.append( lMail.at(i) );
266 sMail.append( QLatin1Char(
'.') );
269 mCurrentObject.addValue( QLatin1String(
"mail"), sMail.toUtf8() );
274 emit q->result( *q, mCurrentObject );
275 mCurrentObject.clear();
278 void LdapClient::Private::parseLDIF(
const QByteArray &data )
282 mLdif.setLdif( data );
286 KLDAP::Ldif::ParseValue ret;
289 ret = mLdif.nextItem();
291 case KLDAP::Ldif::Item:
294 QByteArray value = mLdif.value();
295 mCurrentObject.addValue( name, value );
298 case KLDAP::Ldif::EndEntry:
299 finishCurrentObject();
304 }
while ( ret != KLDAP::Ldif::MoreData );
309 return d->mClientNumber;
314 return d->mCompletionWeight;
319 d->mCompletionWeight = weight;
323 #include "ldapclient.moc"
QStringList attributes() const
Returns the LDAP attributes that should be returned in the query result.
const KLDAP::LdapServer server() const
Returns the ldap server information that are used by this client.
int completionWeight() const
Returns the completion weight of this client.
virtual ~LdapClient()
Destroys the ldap client.
int clientNumber() const
Returns the number of this client.
bool isActive() const
Returns whether this client is currently running a search query.
void startQuery(const QString &filter)
Starts the query with the given filter.
void setAttributes(const QStringList &attributes)
Sets the LDAP attributes that should be returned in the query result.
void result(const KLDAP::LdapClient &client, const KLDAP::LdapObject &)
This signal is emitted once for each object that is returned from the query.
LdapClient(int clientNumber, QObject *parent=0)
Creates a new ldap client.
void setServer(const KLDAP::LdapServer &server)
Sets the LDAP server information that shall be used by this client.
void setCompletionWeight(int weight)
Sets the completion weight of this client.
An object that represents a configured LDAP server.
void setScope(const QString scope)
Sets the scope of the LDAP query.
void cancelQuery()
Cancels a running query.