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