00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "resourceldapkioconfig.h"
00022 #include "resourceldapkio.h"
00023
00024 #include <kio/netaccess.h>
00025 #include <kacceleratormanager.h>
00026 #include <kcombobox.h>
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029 #include <klineedit.h>
00030 #include <kmessagebox.h>
00031 #include <kpagewidget.h>
00032 #include <kvbox.h>
00033
00034 #include <QtCore/QPointer>
00035 #include <QtGui/QCheckBox>
00036 #include <QtGui/QLabel>
00037 #include <QtGui/QLayout>
00038 #include <QtGui/QPushButton>
00039 #include <QtGui/QSpinBox>
00040 #include <QtGui/QRadioButton>
00041
00042 #include "resourceldapkioconfig.moc"
00043
00044 using namespace KABC;
00045
00046 ResourceLDAPKIOConfig::ResourceLDAPKIOConfig( QWidget *parent )
00047 : KRES::ConfigWidget( parent )
00048 {
00049 QBoxLayout *mainLayout = new QVBoxLayout( this );
00050 mainLayout->setMargin( 0 );
00051
00052 KPageWidget *pageWidget = new KPageWidget( this );
00053 pageWidget->setFaceType( KPageView::Tabbed );
00054
00055 mCfg = new KLDAP::LdapConfigWidget(
00056 KLDAP::LdapConfigWidget::W_USER |
00057 KLDAP::LdapConfigWidget::W_PASS |
00058 KLDAP::LdapConfigWidget::W_BINDDN |
00059 KLDAP::LdapConfigWidget::W_REALM |
00060 KLDAP::LdapConfigWidget::W_HOST |
00061 KLDAP::LdapConfigWidget::W_PORT |
00062 KLDAP::LdapConfigWidget::W_VER |
00063 KLDAP::LdapConfigWidget::W_DN |
00064 KLDAP::LdapConfigWidget::W_FILTER |
00065 KLDAP::LdapConfigWidget::W_TIMELIMIT |
00066 KLDAP::LdapConfigWidget::W_SIZELIMIT,
00067 this );
00068
00069 mSecurityCfg = new KLDAP::LdapConfigWidget(
00070 KLDAP::LdapConfigWidget::W_SECBOX |
00071 KLDAP::LdapConfigWidget::W_AUTHBOX,
00072 this );
00073
00074 pageWidget->addPage( mCfg,
00075 i18nc( "@title:tab general account settings", "General" ) );
00076
00077 pageWidget->addPage( mSecurityCfg,
00078 i18nc( "@title:tab account security settings", "Security" ) );
00079
00080 mSubTree = new QCheckBox( i18n( "Sub-tree query" ), this );
00081 KHBox *box = new KHBox( this );
00082 box->setSpacing( -1 );
00083 mEditButton = new QPushButton( i18n( "Edit Attributes..." ), box );
00084 mCacheButton = new QPushButton( i18n( "Offline Use..." ), box );
00085
00086 mainLayout->addWidget( pageWidget );
00087 mainLayout->addWidget( mSubTree );
00088 mainLayout->addWidget( box );
00089
00090 connect( mEditButton, SIGNAL( clicked() ), SLOT( editAttributes() ) );
00091 connect( mCacheButton, SIGNAL( clicked() ), SLOT( editCache() ) );
00092 }
00093
00094 void ResourceLDAPKIOConfig::loadSettings( KRES::Resource *res )
00095 {
00096 ResourceLDAPKIO *resource = dynamic_cast<ResourceLDAPKIO*>( res );
00097
00098 if ( !resource ) {
00099 kDebug() << "cast failed";
00100 return;
00101 }
00102
00103 mCfg->setUser( resource->user() );
00104 mCfg->setPassword( resource->password() );
00105 mCfg->setRealm( resource->realm() );
00106 mCfg->setBindDn( resource->bindDN() );
00107 mCfg->setHost( resource->host() );
00108 mCfg->setPort( resource->port() );
00109 mCfg->setVersion( resource->ver() );
00110 mCfg->setTimeLimit( resource->timeLimit() );
00111 mCfg->setSizeLimit( resource->sizeLimit() );
00112 mCfg->setDn( KLDAP::LdapDN( resource->dn() ) );
00113 mCfg->setFilter( resource->filter() );
00114 mSecurityCfg->setMech( resource->mech() );
00115 if ( resource->isTLS() ) {
00116 mSecurityCfg->setSecurity( KLDAP::LdapConfigWidget::TLS );
00117 } else if ( resource->isSSL() ) {
00118 mSecurityCfg->setSecurity( KLDAP::LdapConfigWidget::SSL );
00119 } else {
00120 mSecurityCfg->setSecurity( KLDAP::LdapConfigWidget::None );
00121 }
00122 if ( resource->isAnonymous() ) {
00123 mSecurityCfg->setAuth( KLDAP::LdapConfigWidget::Anonymous );
00124 } else if ( resource->isSASL() ) {
00125 mSecurityCfg->setAuth( KLDAP::LdapConfigWidget::SASL );
00126 } else {
00127 mSecurityCfg->setAuth( KLDAP::LdapConfigWidget::Simple );
00128 }
00129 mSubTree->setChecked( resource->isSubTree() );
00130 mAttributes = resource->attributes();
00131 mRDNPrefix = resource->RDNPrefix();
00132 mCachePolicy = resource->cachePolicy();
00133 mCacheDst = resource->cacheDst();
00134 mAutoCache = resource->autoCache();
00135 }
00136
00137 void ResourceLDAPKIOConfig::saveSettings( KRES::Resource *res )
00138 {
00139 ResourceLDAPKIO *resource = dynamic_cast<ResourceLDAPKIO*>( res );
00140
00141 if ( !resource ) {
00142 kDebug() << "cast failed";
00143 return;
00144 }
00145
00146 resource->setUser( mCfg->user() );
00147 resource->setPassword( mCfg->password() );
00148 resource->setRealm( mCfg->realm() );
00149 resource->setBindDN( mCfg->bindDn() );
00150 resource->setHost( mCfg->host() );
00151 resource->setPort( mCfg->port() );
00152 resource->setVer( mCfg->version() );
00153 resource->setTimeLimit( mCfg->timeLimit() );
00154 resource->setSizeLimit( mCfg->sizeLimit() );
00155 resource->setDn( mCfg->dn().toString() );
00156 resource->setFilter( mCfg->filter() );
00157 resource->setIsAnonymous( mSecurityCfg->auth() ==
00158 KLDAP::LdapConfigWidget::Anonymous );
00159 resource->setIsSASL( mSecurityCfg->auth() == KLDAP::LdapConfigWidget::SASL );
00160 resource->setMech( mSecurityCfg->mech() );
00161 resource->setIsTLS( mSecurityCfg->security() == KLDAP::LdapConfigWidget::TLS );
00162 resource->setIsSSL( mSecurityCfg->security() == KLDAP::LdapConfigWidget::SSL );
00163 resource->setIsSubTree( mSubTree->isChecked() );
00164 resource->setAttributes( mAttributes );
00165 resource->setRDNPrefix( mRDNPrefix );
00166 resource->setCachePolicy( mCachePolicy );
00167 resource->init();
00168
00169 }
00170
00171 void ResourceLDAPKIOConfig::editAttributes()
00172 {
00173 QPointer<AttributesDialog> dlg = new AttributesDialog( mAttributes, mRDNPrefix, this );
00174 if ( dlg->exec() && dlg ) {
00175 mAttributes = dlg->attributes();
00176 mRDNPrefix = dlg->rdnprefix();
00177 }
00178
00179 delete dlg;
00180 }
00181
00182 void ResourceLDAPKIOConfig::editCache()
00183 {
00184 KLDAP::LdapUrl src;
00185 QStringList attr;
00186
00187 src = mCfg->url();
00188 src.setScope( mSubTree->isChecked() ? KLDAP::LdapUrl::Sub : KLDAP::LdapUrl::One );
00189 if ( !mAttributes.empty() ) {
00190 QMap<QString,QString>::Iterator it;
00191 QStringList attr;
00192 for ( it = mAttributes.begin(); it != mAttributes.end(); ++it ) {
00193 if ( !it.value().isEmpty() && it.key() != QLatin1String( "objectClass" ) ) {
00194 attr.append( it.value() );
00195 }
00196 }
00197 src.setAttributes( attr );
00198 }
00199 src.setExtension( QLatin1String( "x-dir" ), QLatin1String( "base" ) );
00200
00201 QPointer<OfflineDialog> dlg = new OfflineDialog( mAutoCache, mCachePolicy, src, mCacheDst, this );
00202 if ( dlg->exec() && dlg ) {
00203 mCachePolicy = dlg->cachePolicy();
00204 mAutoCache = dlg->autoCache();
00205 }
00206
00207 delete dlg;
00208 }
00209
00210 AttributesDialog::AttributesDialog( const QMap<QString, QString> &attributes,
00211 int rdnprefix,
00212 QWidget *parent )
00213 : KDialog( parent )
00214 {
00215 setCaption( i18n( "Attributes Configuration" ) );
00216 setButtons( Ok | Cancel );
00217 setDefaultButton( Ok );
00218 setModal( true );
00219 showButtonSeparator( true );
00220
00221 mNameDict.insert( QLatin1String( "objectClass" ), i18n( "Object classes" ) );
00222 mNameDict.insert( QLatin1String( "commonName" ), i18n( "Common name" ) );
00223 mNameDict.insert( QLatin1String( "formattedName" ), i18n( "Formatted name" ) );
00224 mNameDict.insert( QLatin1String( "familyName" ), i18n( "Family name" ) );
00225 mNameDict.insert( QLatin1String( "givenName" ), i18n( "Given name" ) );
00226 mNameDict.insert( QLatin1String( "organization" ), i18n( "Organization" ) );
00227 mNameDict.insert( QLatin1String( "title" ), i18nc( "job title", "Title" ) );
00228 mNameDict.insert( QLatin1String( "street" ), i18n( "Street" ) );
00229 mNameDict.insert( QLatin1String( "state" ), i18nc( "state/province", "State" ) );
00230 mNameDict.insert( QLatin1String( "city" ), i18n( "City" ) );
00231 mNameDict.insert( QLatin1String( "postalcode" ), i18n( "Postal code" ) );
00232 mNameDict.insert( QLatin1String( "mail" ), i18nc( "email address", "Email" ) );
00233 mNameDict.insert( QLatin1String( "mailAlias" ), i18n( "Email alias" ) );
00234 mNameDict.insert( QLatin1String( "phoneNumber" ), i18n( "Telephone number" ) );
00235 mNameDict.insert( QLatin1String( "telephoneNumber" ), i18n( "Work telephone number" ) );
00236 mNameDict.insert( QLatin1String( "facsimileTelephoneNumber" ), i18n( "Fax number" ) );
00237 mNameDict.insert( QLatin1String( "mobile" ), i18n( "Cell phone number" ) );
00238 mNameDict.insert( QLatin1String( "pager" ), i18n( "Pager" ) );
00239 mNameDict.insert( QLatin1String( "description" ), i18n( "Note" ) );
00240 mNameDict.insert( QLatin1String( "uid" ), i18n( "UID" ) );
00241 mNameDict.insert( QLatin1String( "jpegPhoto" ), i18n( "Photo" ) );
00242
00243
00244 mDefaultMap.insert( QLatin1String( "objectClass" ), QLatin1String( "inetOrgPerson" ) );
00245 mDefaultMap.insert( QLatin1String( "commonName" ), QLatin1String( "cn" ) );
00246 mDefaultMap.insert( QLatin1String( "formattedName" ), QLatin1String( "displayName" ) );
00247 mDefaultMap.insert( QLatin1String( "familyName" ), QLatin1String( "sn" ) );
00248 mDefaultMap.insert( QLatin1String( "givenName" ), QLatin1String( "givenName" ) );
00249 mDefaultMap.insert( QLatin1String( "title" ), QLatin1String( "title" ) );
00250 mDefaultMap.insert( QLatin1String( "street" ), QLatin1String( "street" ) );
00251 mDefaultMap.insert( QLatin1String( "state" ), QLatin1String( "st" ) );
00252 mDefaultMap.insert( QLatin1String( "city" ), QLatin1String( "l" ) );
00253 mDefaultMap.insert( QLatin1String( "organization" ), QLatin1String( "o" ) );
00254 mDefaultMap.insert( QLatin1String( "postalcode" ), QLatin1String( "postalCode" ) );
00255 mDefaultMap.insert( QLatin1String( "mail" ), QLatin1String( "mail" ) );
00256 mDefaultMap.insert( QLatin1String( "mailAlias" ), QString() );
00257 mDefaultMap.insert( QLatin1String( "phoneNumber" ), QLatin1String( "homePhone" ) );
00258 mDefaultMap.insert( QLatin1String( "telephoneNumber" ), QLatin1String( "telephoneNumber" ) );
00259 mDefaultMap.insert( QLatin1String( "facsimileTelephoneNumber" ),
00260 QLatin1String( "facsimileTelephoneNumber" ) );
00261 mDefaultMap.insert( QLatin1String( "mobile" ), QLatin1String( "mobile" ) );
00262 mDefaultMap.insert( QLatin1String( "pager" ), QLatin1String( "pager" ) );
00263 mDefaultMap.insert( QLatin1String( "description" ), QLatin1String( "description" ) );
00264 mDefaultMap.insert( QLatin1String( "uid" ), QLatin1String( "uid" ) );
00265 mDefaultMap.insert( QLatin1String( "jpegPhoto" ), QLatin1String( "jpegPhoto" ) );
00266
00267
00268 QMap<QString, QString> kolabMap, netscapeMap, evolutionMap, outlookMap;
00269
00270
00271 kolabMap.insert( QLatin1String( "formattedName" ), QLatin1String( "display-name" ) );
00272 kolabMap.insert( QLatin1String( "mailAlias" ), QLatin1String( "mailalias" ) );
00273
00274
00275 evolutionMap.insert( QLatin1String( "formattedName" ), QLatin1String( "fileAs" ) );
00276
00277 mMapList.append( attributes );
00278 mMapList.append( kolabMap );
00279 mMapList.append( netscapeMap );
00280 mMapList.append( evolutionMap );
00281 mMapList.append( outlookMap );
00282
00283 QFrame *page = new QFrame( this );
00284 setMainWidget( page );
00285 QGridLayout *layout = new QGridLayout( page );
00286
00287 QLabel *label = new QLabel( i18n( "Template:" ), page );
00288 layout->addWidget( label, 0, 0 );
00289 mMapCombo = new KComboBox( page );
00290 layout->addWidget( mMapCombo, 0, 1 );
00291
00292 mMapCombo->addItem( i18n( "User Defined" ) );
00293 mMapCombo->addItem( i18n( "Kolab" ) );
00294 mMapCombo->addItem( i18n( "Netscape" ) );
00295 mMapCombo->addItem( i18n( "Evolution" ) );
00296 mMapCombo->addItem( i18n( "Outlook" ) );
00297 connect( mMapCombo, SIGNAL( activated( int ) ), SLOT( mapChanged( int ) ) );
00298
00299 label = new QLabel( i18n( "RDN prefix attribute:" ), page );
00300 layout->addWidget( label, 1, 0 );
00301 mRDNCombo = new KComboBox( page );
00302 layout->addWidget( mRDNCombo, 1, 1 );
00303 mRDNCombo->addItem( i18n( "commonName" ) );
00304 mRDNCombo->addItem( i18n( "UID" ) );
00305 mRDNCombo->setCurrentIndex( rdnprefix );
00306
00307 QMap<QString, QString>::ConstIterator it;
00308 int i, j = 0;
00309 for ( i = 2, it = attributes.begin(); it != attributes.end(); ++it, ++i ) {
00310 if ( mNameDict[ it.key() ].isEmpty() ) {
00311 i--;
00312 continue;
00313 }
00314 if ( ( i - 2 ) == ( mNameDict.count() >> 1 ) ) {
00315 i = 0;
00316 j = 2;
00317 }
00318 kDebug() << "itkey:" << it.key() << "i:" << i;
00319 label = new QLabel( mNameDict[ it.key() ] + QLatin1Char( ':' ), page );
00320 KLineEdit *lineedit = new KLineEdit( page );
00321 mLineEditDict.insert( it.key(), lineedit );
00322 lineedit->setText( it.value() );
00323 label->setBuddy( lineedit );
00324 layout->addWidget( label, i, j );
00325 layout->addWidget( lineedit, i, j+1 );
00326 }
00327
00328 for ( i = 1; i < mMapCombo->count(); ++i ) {
00329 QHash<QString,KLineEdit*>::const_iterator it2 = mLineEditDict.constBegin();
00330 while ( it2 != mLineEditDict.constEnd() ) {
00331 if ( mMapList[ i ].contains( it2.key() ) ) {
00332 if ( mMapList[ i ][ it2.key() ] != it2.value()->text() ) {
00333 break;
00334 }
00335 } else {
00336 if ( mDefaultMap[ it2.key() ] != it2.value()->text() ) {
00337 break;
00338 }
00339 }
00340 ++it2;
00341 }
00342 if ( it2 != mLineEditDict.constEnd() ) {
00343 mMapCombo->setCurrentIndex( i );
00344 break;
00345 }
00346 }
00347
00348 KAcceleratorManager::manage( this );
00349 }
00350
00351 AttributesDialog::~AttributesDialog()
00352 {
00353 mNameDict.clear();
00354 }
00355
00356 QMap<QString, QString> AttributesDialog::attributes() const
00357 {
00358 QMap<QString, QString> map;
00359
00360 QHash<QString,KLineEdit*>::const_iterator it = mLineEditDict.constBegin();
00361 while ( it != mLineEditDict.constEnd() ) {
00362 map.insert( it.key(), it.value()->text() );
00363 ++it;
00364 }
00365 return map;
00366 }
00367
00368 int AttributesDialog::rdnprefix() const
00369 {
00370 return mRDNCombo->currentIndex();
00371 }
00372
00373 void AttributesDialog::mapChanged( int pos )
00374 {
00375
00376
00377 QMap<QString, QString>::Iterator it;
00378 for ( it = mDefaultMap.begin(); it != mDefaultMap.end(); ++it ) {
00379 mLineEditDict[ it.key() ]->setText( it.value() );
00380 }
00381
00382 for ( it = mMapList[ pos ].begin(); it != mMapList[ pos ].end(); ++it ) {
00383 if ( !it.value().isEmpty() ) {
00384 KLineEdit *le = mLineEditDict[ it.key() ];
00385 if ( le ) {
00386 le->setText( it.value() );
00387 }
00388 }
00389 }
00390 }
00391
00392 OfflineDialog::OfflineDialog( bool autoCache, int cachePolicy, const KUrl &src,
00393 const QString &dst, QWidget *parent )
00394 : KDialog( parent )
00395 {
00396 setCaption( i18n( "Offline Configuration" ) );
00397 setButtons( Ok | Cancel );
00398 setDefaultButton( Ok );
00399 setModal( true );
00400 showButtonSeparator( true );
00401
00402 QFrame *page = new QFrame( this );
00403 setMainWidget( page );
00404 QVBoxLayout *layout = new QVBoxLayout( page );
00405
00406 mSrc = src;
00407 mDst = dst;
00408 mCacheBox = new QGroupBox( i18n( "Offline Cache Policy" ), page );
00409 QVBoxLayout *cacheBoxLayout = new QVBoxLayout( mCacheBox );
00410
00411 mCacheGroup = new QButtonGroup( this );
00412
00413 QRadioButton *bt;
00414 bt = new QRadioButton( i18n( "Do not use offline cache" ), mCacheBox );
00415 cacheBoxLayout->addWidget( bt );
00416 bt->setDown(true);
00417 mCacheGroup->addButton( bt );
00418
00419 bt = new QRadioButton( i18n( "Use local copy if no connection" ), mCacheBox );
00420 cacheBoxLayout->addWidget( bt );
00421 mCacheGroup->addButton( bt );
00422
00423 bt = new QRadioButton( i18n( "Always use local copy" ), mCacheBox );
00424 cacheBoxLayout->addWidget( bt );
00425 mCacheGroup->addButton( bt );
00426
00427 if ( mCacheGroup->button( cachePolicy ) ) {
00428 mCacheGroup->button( cachePolicy )->setDown( true );
00429 }
00430
00431 mAutoCache = new QCheckBox( i18n( "Refresh offline cache automatically" ),
00432 page );
00433 mAutoCache->setChecked( autoCache );
00434 mAutoCache->setEnabled( bt->isChecked() );
00435
00436 connect( bt, SIGNAL(toggled(bool)), mAutoCache, SLOT(setEnabled(bool)) );
00437
00438 QPushButton *lcache = new QPushButton( i18n( "Load into Cache" ), page );
00439 connect( lcache, SIGNAL( clicked() ), SLOT( loadCache() ) );
00440
00441 layout->addWidget( mCacheBox );
00442 layout->addWidget( mAutoCache );
00443 layout->addWidget( lcache );
00444 }
00445
00446 OfflineDialog::~OfflineDialog()
00447 {
00448 }
00449
00450 bool OfflineDialog::autoCache() const
00451 {
00452 return mAutoCache->isChecked();
00453 }
00454
00455 int OfflineDialog::cachePolicy() const
00456 {
00457 return mCacheGroup->checkedId();
00458 }
00459
00460 void OfflineDialog::loadCache()
00461 {
00462 if ( KIO::NetAccess::download( mSrc, mDst, this ) ) {
00463 KMessageBox::information( this,
00464 i18n( "Successfully downloaded directory server contents." ) );
00465 } else {
00466 KMessageBox::error( this,
00467 i18n( "An error occurred downloading directory server contents into file %1.", mDst ) );
00468 }
00469 }