00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qapplication.h>
00021
00022 #include <qobjectlist.h>
00023 #include <qcheckbox.h>
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qpushbutton.h>
00027 #include <qspinbox.h>
00028 #include <qvgroupbox.h>
00029 #include <qhbuttongroup.h>
00030 #include <qradiobutton.h>
00031
00032 #include <kmessagebox.h>
00033 #include <kaccelmanager.h>
00034 #include <kdialogbase.h>
00035 #include <klocale.h>
00036 #include <klineedit.h>
00037 #include <kcombobox.h>
00038 #include <kprogress.h>
00039
00040 #include <kdebug.h>
00041
00042 #include "ldapconfigwidget.h"
00043 #include "ldapconfigwidget.moc"
00044
00045 using namespace KABC;
00046
00047 LdapConfigWidget::LdapConfigWidget( QWidget* parent,
00048 const char* name, WFlags fl ) : QWidget( parent, name, fl )
00049 {
00050 mProg = 0;
00051 mFlags = 0;
00052 mainLayout = new QGridLayout( this, 12, 4, 0,
00053 KDialog::spacingHint() );
00054 }
00055
00056 LdapConfigWidget::LdapConfigWidget( int flags, QWidget* parent,
00057 const char* name, WFlags fl ) : QWidget( parent, name, fl )
00058 {
00059 mFlags = flags;
00060 mProg = 0;
00061 mainLayout = new QGridLayout( this, 12, 4, 0,
00062 KDialog::spacingHint() );
00063 initWidget();
00064 }
00065
00066 LdapConfigWidget::~LdapConfigWidget()
00067 {
00068 }
00069
00070 void LdapConfigWidget::initWidget()
00071 {
00072 QLabel *label;
00073
00074 mUser = mPassword = mHost = mDn = mBindDN = mRealm = mFilter = 0;
00075 mPort = mVer = mTimeLimit = mSizeLimit = 0;
00076 mAnonymous = mSimple = mSASL = mSecNO = mSecTLS = mSecSSL = 0;
00077 mEditButton = mQueryMech = 0;
00078 mMech = 0;
00079 int row = 0;
00080 int col;
00081
00082 if ( mFlags & W_USER ) {
00083 label = new QLabel( i18n( "User:" ), this );
00084 mUser = new KLineEdit( this, "kcfg_ldapuser" );
00085
00086 mainLayout->addWidget( label, row, 0 );
00087 mainLayout->addMultiCellWidget( mUser, row, row, 1, 3 );
00088 row++;
00089 }
00090
00091 if ( mFlags & W_BINDDN ) {
00092 label = new QLabel( i18n( "Bind DN:" ), this );
00093 mBindDN = new KLineEdit( this, "kcfg_ldapbinddn" );
00094
00095 mainLayout->addWidget( label, row, 0 );
00096 mainLayout->addMultiCellWidget( mBindDN, row, row, 1, 3 );
00097 row++;
00098 }
00099
00100 if ( mFlags & W_REALM ) {
00101 label = new QLabel( i18n( "Realm:" ), this );
00102 mRealm = new KLineEdit( this, "kcfg_ldaprealm" );
00103
00104 mainLayout->addWidget( label, row, 0 );
00105 mainLayout->addMultiCellWidget( mRealm, row, row, 1, 3 );
00106 row++;
00107 }
00108
00109 if ( mFlags & W_PASS ) {
00110 label = new QLabel( i18n( "Password:" ), this );
00111 mPassword = new KLineEdit( this, "kcfg_ldappassword" );
00112 mPassword->setEchoMode( KLineEdit::Password );
00113
00114 mainLayout->addWidget( label, row, 0 );
00115 mainLayout->addMultiCellWidget( mPassword, row, row, 1, 3 );
00116 row++;
00117 }
00118
00119 if ( mFlags & W_HOST ) {
00120 label = new QLabel( i18n( "Host:" ), this );
00121 mHost = new KLineEdit( this, "kcfg_ldaphost" );
00122
00123 mainLayout->addWidget( label, row, 0 );
00124 mainLayout->addMultiCellWidget( mHost, row, row, 1, 3 );
00125 row++;
00126 }
00127
00128 col = 0;
00129 if ( mFlags & W_PORT ) {
00130 label = new QLabel( i18n( "Port:" ), this );
00131 mPort = new QSpinBox( 0, 65535, 1, this, "kcfg_ldapport" );
00132 mPort->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) );
00133 mPort->setValue( 389 );
00134
00135 mainLayout->addWidget( label, row, col );
00136 mainLayout->addWidget( mPort, row, col+1 );
00137 col += 2;
00138 }
00139
00140 if ( mFlags & W_VER ) {
00141 label = new QLabel( i18n( "LDAP version:" ), this );
00142 mVer = new QSpinBox( 2, 3, 1, this, "kcfg_ldapver" );
00143 mVer->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) );
00144 mVer->setValue( 3 );
00145 mainLayout->addWidget( label, row, col );
00146 mainLayout->addWidget( mVer, row, col+1 );
00147 }
00148 if ( mFlags & ( W_PORT | W_VER ) ) row++;
00149
00150 col = 0;
00151 if ( mFlags & W_SIZELIMIT ) {
00152 label = new QLabel( i18n( "Size limit:" ), this );
00153 mSizeLimit = new QSpinBox( 0, 9999999, 1, this, "kcfg_ldapsizelimit" );
00154 mSizeLimit->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) );
00155 mSizeLimit->setValue( 0 );
00156 mSizeLimit->setSpecialValueText( i18n("Default") );
00157 mainLayout->addWidget( label, row, col );
00158 mainLayout->addWidget( mSizeLimit, row, col+1 );
00159 col += 2;
00160 }
00161
00162 if ( mFlags & W_TIMELIMIT ) {
00163 label = new QLabel( i18n( "Time limit:" ), this );
00164 mTimeLimit = new QSpinBox( 0, 9999999, 1, this, "kcfg_ldaptimelimit" );
00165 mTimeLimit->setSizePolicy( QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred ) );
00166 mTimeLimit->setValue( 0 );
00167 mTimeLimit->setSuffix( i18n(" sec") );
00168 mTimeLimit->setSpecialValueText( i18n("Default") );
00169 mainLayout->addWidget( label, row, col );
00170 mainLayout->addWidget( mTimeLimit, row, col+1 );
00171 }
00172 if ( mFlags & ( W_SIZELIMIT | W_TIMELIMIT ) ) row++;
00173
00174 if ( mFlags & W_DN ) {
00175 label = new QLabel( i18n( "Distinguished Name", "DN:" ), this );
00176 mDn = new KLineEdit( this, "kcfg_ldapdn" );
00177
00178 mainLayout->addWidget( label, row, 0 );
00179 mainLayout->addMultiCellWidget( mDn, row, row, 1, 1 );
00180
00181 if ( mHost ) {
00182 QPushButton *dnquery = new QPushButton( i18n( "Query Server" ), this );
00183 connect( dnquery, SIGNAL( clicked() ), SLOT( mQueryDNClicked() ) );
00184 mainLayout->addMultiCellWidget( dnquery, row, row, 2, 3 );
00185 }
00186 row++;
00187 }
00188
00189 if ( mFlags & W_FILTER ) {
00190 label = new QLabel( i18n( "Filter:" ), this );
00191 mFilter = new KLineEdit( this, "kcfg_ldapfilter" );
00192
00193 mainLayout->addWidget( label, row, 0 );
00194 mainLayout->addMultiCellWidget( mFilter, row, row, 1, 3 );
00195 row++;
00196 }
00197
00198 if ( mFlags & W_SECBOX ) {
00199 QHButtonGroup *btgroup = new QHButtonGroup( i18n( "Security" ), this );
00200 mSecNO = new QRadioButton( i18n( "No" ), btgroup, "kcfg_ldapnosec" );
00201 mSecTLS = new QRadioButton( i18n( "TLS" ), btgroup, "kcfg_ldaptls" );
00202 mSecSSL = new QRadioButton( i18n( "SSL" ), btgroup, "kcfg_ldapssl" );
00203 mainLayout->addMultiCellWidget( btgroup, row, row, 0, 3 );
00204
00205 connect( mSecNO, SIGNAL( clicked() ), SLOT( setLDAPPort() ) );
00206 connect( mSecTLS, SIGNAL( clicked() ), SLOT( setLDAPPort() ) );
00207 connect( mSecSSL, SIGNAL( clicked() ), SLOT( setLDAPSPort( ) ) );
00208
00209 mSecNO->setChecked( true );
00210 row++;
00211 }
00212
00213 if ( mFlags & W_AUTHBOX ) {
00214
00215 QButtonGroup *authbox =
00216 new QButtonGroup( 3, Qt::Horizontal, i18n( "Authentication" ), this );
00217
00218 mAnonymous = new QRadioButton( i18n( "Anonymous" ), authbox, "kcfg_ldapanon" );
00219 mSimple = new QRadioButton( i18n( "Simple" ), authbox, "kcfg_ldapsimple" );
00220 mSASL = new QRadioButton( i18n( "SASL" ), authbox, "kcfg_ldapsasl" );
00221
00222 label = new QLabel( i18n( "SASL mechanism:" ), authbox );
00223 mMech = new KComboBox( false, authbox, "kcfg_ldapsaslmech" );
00224 mMech->setEditable( true );
00225 mMech->insertItem( "DIGEST-MD5" );
00226 mMech->insertItem( "GSSAPI" );
00227 mMech->insertItem( "PLAIN" );
00228
00229
00230 if ( mHost ) {
00231 mQueryMech = new QPushButton( i18n( "Query Server" ), authbox );
00232 connect( mQueryMech, SIGNAL( clicked() ), SLOT( mQueryMechClicked() ) );
00233 }
00234
00235 mainLayout->addMultiCellWidget( authbox, row, row+1, 0, 3 );
00236
00237 connect( mAnonymous, SIGNAL( stateChanged(int) ), SLOT( setAnonymous(int) ) );
00238 connect( mSimple, SIGNAL( stateChanged(int) ), SLOT( setSimple(int) ) );
00239 connect( mSASL, SIGNAL( stateChanged(int) ), SLOT( setSASL(int) ) );
00240
00241 mAnonymous->setChecked( true );
00242 }
00243
00244 }
00245
00246 void LdapConfigWidget::loadData( KIO::Job*, const QByteArray& d )
00247 {
00248 LDIF::ParseVal ret;
00249
00250 if ( d.size() ) {
00251 mLdif.setLDIF( d );
00252 } else {
00253 mLdif.endLDIF();
00254 }
00255 do {
00256 ret = mLdif.nextItem();
00257 if ( ret == LDIF::Item && mLdif.attr().lower() == mAttr ) {
00258 mProg->progressBar()->advance( 1 );
00259 mQResult.push_back( QString::fromUtf8( mLdif.val(), mLdif.val().size() ) );
00260 }
00261 } while ( ret != LDIF::MoreData );
00262 }
00263
00264 void LdapConfigWidget::loadResult( KIO::Job* job)
00265 {
00266 int error = job->error();
00267 if ( error && error != KIO::ERR_USER_CANCELED )
00268 mErrorMsg = job->errorString();
00269 else
00270 mErrorMsg = "";
00271
00272 mCancelled = false;
00273 mProg->close();
00274 }
00275
00276 void LdapConfigWidget::sendQuery()
00277 {
00278 LDAPUrl _url;
00279
00280 mQResult.clear();
00281 mCancelled = true;
00282
00283 _url.setProtocol( ( mSecSSL && mSecSSL->isChecked() ) ? "ldaps" : "ldap" );
00284 if ( mHost ) _url.setHost( mHost->text() );
00285 if ( mPort ) _url.setPort( mPort->value() );
00286 _url.setDn( "" );
00287 _url.setAttributes( mAttr );
00288 _url.setScope( LDAPUrl::Base );
00289 if ( mVer ) _url.setExtension( "x-ver", QString::number( mVer->value() ) );
00290 if ( mSecTLS && mSecTLS->isChecked() ) _url.setExtension( "x-tls", "" );
00291
00292 kdDebug(5700) << "sendQuery url: " << _url.prettyURL() << endl;
00293 mLdif.startParsing();
00294 KIO::Job *job = KIO::get( _url, true, false );
00295 job->addMetaData("no-auth-prompt","true");
00296 connect( job, SIGNAL( data( KIO::Job*, const QByteArray& ) ),
00297 this, SLOT( loadData( KIO::Job*, const QByteArray& ) ) );
00298 connect( job, SIGNAL( result( KIO::Job* ) ),
00299 this, SLOT( loadResult( KIO::Job* ) ) );
00300
00301 if ( mProg == NULL )
00302 mProg = new KProgressDialog( this, 0, i18n("LDAP Query"), _url.prettyURL(), true );
00303 else
00304 mProg->setLabel( _url.prettyURL() );
00305 mProg->progressBar()->setValue( 0 );
00306 mProg->progressBar()->setTotalSteps( 1 );
00307 mProg->exec();
00308 if ( mCancelled ) {
00309 kdDebug(5700) << "query cancelled!" << endl;
00310 job->kill( true );
00311 } else {
00312 if ( !mErrorMsg.isEmpty() ) KMessageBox::error( this, mErrorMsg );
00313 }
00314 }
00315
00316 void LdapConfigWidget::mQueryMechClicked()
00317 {
00318 mAttr = "supportedsaslmechanisms";
00319 sendQuery();
00320 if ( !mQResult.isEmpty() ) {
00321 mQResult.sort();
00322 mMech->clear();
00323 mMech->insertStringList( mQResult );
00324 }
00325 }
00326
00327 void LdapConfigWidget::mQueryDNClicked()
00328 {
00329 mAttr = "namingcontexts";
00330 sendQuery();
00331 if ( !mQResult.isEmpty() ) mDn->setText( mQResult.first() );
00332 }
00333
00334 void LdapConfigWidget::setAnonymous( int state )
00335 {
00336 if ( state == QButton::Off ) return;
00337 if ( mUser ) mUser->setEnabled(false);
00338 if ( mPassword ) mPassword->setEnabled(false);
00339 if ( mBindDN ) mBindDN->setEnabled(false);
00340 if ( mRealm ) mRealm->setEnabled(false);
00341 if ( mMech ) mMech->setEnabled(false);
00342 if ( mQueryMech ) mQueryMech->setEnabled(false);
00343 }
00344
00345 void LdapConfigWidget::setSimple( int state )
00346 {
00347 if ( state == QButton::Off ) return;
00348 if ( mUser ) mUser->setEnabled(true);
00349 if ( mPassword ) mPassword->setEnabled(true);
00350 if ( mBindDN ) mBindDN->setEnabled(false);
00351 if ( mRealm ) mRealm->setEnabled(false);
00352 if ( mMech ) mMech->setEnabled(false);
00353 if ( mQueryMech ) mQueryMech->setEnabled(false);
00354 }
00355
00356 void LdapConfigWidget::setSASL( int state )
00357 {
00358 if ( state == QButton::Off ) return;
00359 if ( mUser ) mUser->setEnabled(true);
00360 if ( mPassword ) mPassword->setEnabled(true);
00361 if ( mBindDN ) mBindDN->setEnabled(true);
00362 if ( mRealm ) mRealm->setEnabled(true);
00363 if ( mMech ) mMech->setEnabled(true);
00364 if ( mQueryMech ) mQueryMech->setEnabled(true);
00365 }
00366
00367 void LdapConfigWidget::setLDAPPort()
00368 {
00369 mPort->setValue( 389 );
00370 }
00371
00372 void LdapConfigWidget::setLDAPSPort()
00373 {
00374 mPort->setValue( 636 );
00375 }
00376
00377
00378 LDAPUrl LdapConfigWidget::url() const
00379 {
00380 LDAPUrl _url;
00381 if ( mSecSSL && mSecSSL->isChecked() )
00382 _url.setProtocol( "ldaps" );
00383 else
00384 _url.setProtocol( "ldap" );
00385
00386 if ( mUser ) _url.setUser( mUser->text() );
00387 if ( mPassword ) _url.setPass( mPassword->text() );
00388 if ( mHost ) _url.setHost( mHost->text() );
00389 if ( mPort ) _url.setPort( mPort->value() );
00390 if ( mDn ) _url.setDn( mDn->text() );
00391 if ( mVer ) _url.setExtension( "x-ver", QString::number( mVer->value() ) );
00392 if ( mSizeLimit && mSizeLimit->value() != 0 )
00393 _url.setExtension( "x-sizelimit", QString::number( mSizeLimit->value() ) );
00394 if ( mTimeLimit && mTimeLimit->value() != 0 )
00395 _url.setExtension( "x-timelimit", QString::number( mTimeLimit->value() ) );
00396 if ( mSecTLS && mSecTLS->isChecked() ) _url.setExtension( "x-tls","" );
00397 if ( mFilter && !mFilter->text().isEmpty() )
00398 _url.setFilter( mFilter->text() );
00399 if ( mSASL && mSASL->isChecked() ) {
00400 _url.setExtension( "x-sasl", "" );
00401 _url.setExtension( "x-mech", mMech->currentText() );
00402 if ( mBindDN && !mBindDN->text().isEmpty() )
00403 _url.setExtension( "bindname", mBindDN->text() );
00404 if ( mRealm && !mRealm->text().isEmpty() )
00405 _url.setExtension( "x-realm", mRealm->text() );
00406 }
00407 return ( _url );
00408 }
00409
00410 void LdapConfigWidget::setUser( const QString &user )
00411 {
00412 if ( mUser ) mUser->setText( user );
00413 }
00414
00415 QString LdapConfigWidget::user() const
00416 {
00417 return ( mUser ? mUser->text() : QString::null );
00418 }
00419
00420 void LdapConfigWidget::setPassword( const QString &password )
00421 {
00422 if ( mPassword ) mPassword->setText( password );
00423 }
00424
00425 QString LdapConfigWidget::password() const
00426 {
00427 return ( mPassword ? mPassword->text() : QString::null );
00428 }
00429
00430 void LdapConfigWidget::setBindDN( const QString &binddn )
00431 {
00432 if ( mBindDN ) mBindDN->setText( binddn );
00433 }
00434
00435 QString LdapConfigWidget::bindDN() const
00436 {
00437 return ( mBindDN ? mBindDN->text() : QString::null );
00438 }
00439
00440 void LdapConfigWidget::setRealm( const QString &realm )
00441 {
00442 if ( mRealm ) mRealm->setText( realm );
00443 }
00444
00445 QString LdapConfigWidget::realm() const
00446 {
00447 return ( mRealm ? mRealm->text() : QString::null );
00448 }
00449
00450 void LdapConfigWidget::setHost( const QString &host )
00451 {
00452 if ( mHost ) mHost->setText( host );
00453 }
00454
00455 QString LdapConfigWidget::host() const
00456 {
00457 return ( mHost ? mHost->text() : QString::null );
00458 }
00459
00460 void LdapConfigWidget::setPort( int port )
00461 {
00462 if ( mPort ) mPort->setValue( port );
00463 }
00464
00465 int LdapConfigWidget::port() const
00466 {
00467 return ( mPort ? mPort->value() : 389 );
00468 }
00469
00470 void LdapConfigWidget::setVer( int ver )
00471 {
00472 if ( mVer ) mVer->setValue( ver );
00473 }
00474
00475 int LdapConfigWidget::ver() const
00476 {
00477 return ( mVer ? mVer->value() : 3 );
00478 }
00479
00480 void LdapConfigWidget::setDn( const QString &dn )
00481 {
00482 if ( mDn ) mDn->setText( dn );
00483 }
00484
00485 QString LdapConfigWidget::dn() const
00486 {
00487 return ( mDn ? mDn->text() : QString::null );
00488 }
00489
00490 void LdapConfigWidget::setFilter( const QString &filter )
00491 {
00492 if ( mFilter ) mFilter->setText( filter );
00493 }
00494
00495 QString LdapConfigWidget::filter() const
00496 {
00497 return ( mFilter ? mFilter->text() : QString::null );
00498 }
00499
00500 void LdapConfigWidget::setMech( const QString &mech )
00501 {
00502 if ( mMech == 0 ) return;
00503 if ( !mech.isEmpty() ) {
00504 int i = 0;
00505 while ( i < mMech->count() ) {
00506 if ( mMech->text( i ) == mech ) break;
00507 i++;
00508 }
00509 if ( i == mMech->count() ) mMech->insertItem( mech );
00510 mMech->setCurrentItem( i );
00511 }
00512 }
00513
00514 QString LdapConfigWidget::mech() const
00515 {
00516 return ( mMech ? mMech->currentText() : QString::null );
00517 }
00518
00519 void LdapConfigWidget::setSecNO( bool b )
00520 {
00521 if ( mSecNO ) mSecNO->setChecked( b );
00522 }
00523
00524 bool LdapConfigWidget::isSecNO() const
00525 {
00526 return ( mSecNO ? mSecNO->isChecked() : true );
00527 }
00528
00529 void LdapConfigWidget::setSecTLS( bool b )
00530 {
00531 if ( mSecTLS ) mSecTLS->setChecked( b );
00532 }
00533
00534 bool LdapConfigWidget::isSecTLS() const
00535 {
00536 return ( mSecTLS ? mSecTLS->isChecked() : false );
00537 }
00538
00539 void LdapConfigWidget::setSecSSL( bool b )
00540 {
00541 if ( mSecSSL ) mSecSSL->setChecked( b );
00542 }
00543
00544 bool LdapConfigWidget::isSecSSL() const
00545 {
00546 return ( mSecSSL ? mSecSSL->isChecked() : false );
00547 }
00548
00549 void LdapConfigWidget::setAuthAnon( bool b )
00550 {
00551 if ( mAnonymous ) mAnonymous->setChecked( b );
00552 }
00553
00554 bool LdapConfigWidget::isAuthAnon() const
00555 {
00556 return ( mAnonymous ? mAnonymous->isChecked() : true );
00557 }
00558
00559 void LdapConfigWidget::setAuthSimple( bool b )
00560 {
00561 if ( mSimple ) mSimple->setChecked( b );
00562 }
00563
00564 bool LdapConfigWidget::isAuthSimple() const
00565 {
00566 return ( mSimple ? mSimple->isChecked() : false );
00567 }
00568
00569 void LdapConfigWidget::setAuthSASL( bool b )
00570 {
00571 if ( mSASL ) mSASL->setChecked( b );
00572 }
00573
00574 bool LdapConfigWidget::isAuthSASL() const
00575 {
00576 return ( mSASL ? mSASL->isChecked() : false );
00577 }
00578
00579 void LdapConfigWidget::setSizeLimit( int sizelimit )
00580 {
00581 if ( mSizeLimit ) mSizeLimit->setValue( sizelimit );
00582 }
00583
00584 int LdapConfigWidget::sizeLimit() const
00585 {
00586 return ( mSizeLimit ? mSizeLimit->value() : 0 );
00587 }
00588
00589 void LdapConfigWidget::setTimeLimit( int timelimit )
00590 {
00591 if ( mTimeLimit ) mTimeLimit->setValue( timelimit );
00592 }
00593
00594 int LdapConfigWidget::timeLimit() const
00595 {
00596 return ( mTimeLimit ? mTimeLimit->value() : 0 );
00597 }
00598
00599 int LdapConfigWidget::flags() const
00600 {
00601 return mFlags;
00602 }
00603
00604 void LdapConfigWidget::setFlags( int flags )
00605 {
00606 mFlags = flags;
00607
00608
00609
00610 const QObjectList *ch = children();
00611 QObjectList ch2 = *ch;
00612 QObject *obj;
00613 QWidget *widget;
00614
00615 obj = ch2.first();
00616 while ( obj != 0 ) {
00617 widget = dynamic_cast<QWidget*> (obj);
00618 if ( widget && widget->parent() == this ) {
00619 mainLayout->remove( widget );
00620 delete ( widget );
00621 }
00622 obj = ch2.next();
00623 }
00624
00625 initWidget();
00626 }