21 #include "ldapsearch.h"
25 #include <QtCore/QEventLoop>
26 #include <QtCore/QTimer>
30 using namespace KLDAP;
33 #define LDAPSEARCH_BLOCKING_TIMEOUT 10
35 class LdapSearch::Private
45 void closeConnection();
47 const QString &filter,
const QStringList &attributes,
48 int pagesize,
int count );
53 bool mOwnConnection, mAbandoned;
57 QStringList mAttributes;
62 int mCount, mMaxCount;
66 void LdapSearch::Private::result()
72 int res = mOp.waitForResult( mId, LDAPSEARCH_BLOCKING_TIMEOUT );
74 kDebug() <<
"LDAP result:" << res;
78 ( mConn->ldapErrorCode() != KLDAP_SUCCESS &&
79 mConn->ldapErrorCode() != KLDAP_SASL_BIND_IN_PROGRESS ) ) ) {
81 mError = mConn->ldapErrorCode();
82 mErrorString = mConn->ldapErrorString();
83 emit mParent->result( mParent );
88 if ( res == LdapOperation::RES_BIND ) {
91 servercc = mOp.serverCred();
93 kDebug() <<
"LdapSearch RES_BIND";
94 if ( mConn->ldapErrorCode() == KLDAP_SUCCESS ) {
95 kDebug() <<
"bind succeeded";
96 LdapControls savedctrls = mOp.serverControls();
98 LdapControls ctrls = savedctrls;
100 mOp.setServerControls( ctrls );
103 mId = mOp.search( mBase, mScope, mFilter, mAttributes );
104 mOp.setServerControls( savedctrls );
106 kDebug() <<
"bind next step";
107 mId = mOp.bind( servercc );
110 if ( mId == KLDAP_SASL_ERROR ) {
112 mErrorString = mConn->saslErrorString();
114 mError = mConn->ldapErrorCode();
115 mErrorString = mConn->ldapErrorString();
117 emit mParent->result( mParent );
120 QTimer::singleShot( 0, mParent, SLOT(result()) );
125 if ( res == LdapOperation::RES_SEARCH_RESULT ) {
129 const int numberOfControls( mOp.controls().count() );
130 for (
int i = 0; i < numberOfControls; ++i ) {
131 estsize = mOp.controls()[i].parsePageControl( cookie );
132 if ( estsize != -1 ) {
136 kDebug() <<
" estimated size:" << estsize;
137 if ( estsize != -1 && !cookie.isEmpty() ) {
138 LdapControls ctrls, savedctrls;
139 savedctrls = mOp.serverControls();
142 mOp.setServerControls( ctrls );
143 mId = mOp.search( mBase, mScope, mFilter, mAttributes );
144 mOp.setServerControls( savedctrls );
146 mError = mConn->ldapErrorCode();
147 mErrorString = mConn->ldapErrorString();
148 emit mParent->result( mParent );
152 QTimer::singleShot( 0, mParent, SLOT(result()) );
157 emit mParent->result( mParent );
162 if ( res == LdapOperation::RES_SEARCH_ENTRY ) {
163 emit mParent->data( mParent, mOp.object() );
168 if ( mMaxCount <= 0 || mCount < mMaxCount ) {
169 QTimer::singleShot( 0, mParent, SLOT(result()) );
172 if ( mMaxCount > 0 && mCount == mMaxCount ) {
173 kDebug() << mCount <<
" entries reached";
174 emit mParent->result( mParent );
178 bool LdapSearch::Private::connect()
180 int ret = mConn->connect();
181 if ( ret != KLDAP_SUCCESS ) {
183 mErrorString = mConn->connectionError();
190 void LdapSearch::Private::closeConnection()
192 if ( mOwnConnection && mConn ) {
199 bool LdapSearch::Private::startSearch(
const LdapDN &base,
LdapUrl::Scope scope,
200 const QString &filter,
201 const QStringList &attributes,
int pagesize,
int count )
203 kDebug() <<
"search: base=" << base.toString() <<
"scope=" << (int)scope
204 <<
"filter=" << filter <<
"attributes=" << attributes
205 <<
"pagesize=" << pagesize;
208 mErrorString.clear();
209 mOp.setConnection( *mConn );
210 mPageSize = pagesize;
214 mAttributes = attributes;
219 LdapControls savedctrls = mOp.serverControls();
221 LdapControls ctrls = savedctrls;
222 mConn->setOption( 0x0008, NULL );
224 mOp.setServerControls( ctrls );
229 if ( mId == KLDAP_SASL_ERROR ) {
231 mErrorString = mConn->saslErrorString();
233 mError = mConn->ldapErrorCode();
234 mErrorString = mConn->ldapErrorString();
235 if ( mError == -1 && mErrorString.isEmpty() ) {
236 mErrorString = i18n(
"Cannot access to server. Please reconfigure it." );
241 kDebug() <<
"startSearch msg id=" << mId;
244 QTimer::singleShot( 0, mParent, SLOT(result()) );
252 : d( new Private( this ) )
254 d->mOwnConnection =
true;
259 : d( new Private( this ) )
261 d->mOwnConnection =
false;
262 d->mConn = &connection;
265 LdapSearch::~LdapSearch()
267 d->closeConnection();
273 d->closeConnection();
274 d->mOwnConnection =
false;
275 d->mConn = &connection;
280 d->mOp.setClientControls( ctrls );
285 d->mOp.setServerControls( ctrls );
289 const QStringList &attributes,
int count )
291 if ( d->mOwnConnection ) {
292 d->closeConnection();
294 if ( !d->connect() ) {
299 attributes, server.
pageSize(), count );
304 if ( d->mOwnConnection ) {
305 d->closeConnection();
307 if ( !d->connect() ) {
311 bool critical =
true;
312 int pagesize = url.
extension( QLatin1String(
"x-pagesize" ), critical ).toInt();
318 const QString &filter,
const QStringList &attributes,
319 int pagesize,
int count )
321 Q_ASSERT( !d->mOwnConnection );
322 return d->startSearch( base, scope, filter, attributes, pagesize, count );
327 Q_ASSERT( !d->mFinished );
329 QTimer::singleShot( 0,
this, SLOT(
result()) );
339 d->mAbandoned =
true;
349 return d->mErrorString;
352 #include "moc_ldapsearch.cpp"