certmanager Library API Documentation

dirservconfigpage.cpp

00001 /*
00002     dirservconfigpage.cpp
00003 
00004     This file is part of kleopatra
00005     Copyright (c) 2004 Klarälvdalens Datakonsult AB
00006 
00007     Libkleopatra is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License,
00009     version 2, as published by the Free Software Foundation.
00010 
00011     Libkleopatra is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this program with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
00030 */
00031 
00032 #include "dirservconfigpage.h"
00033 #include "directoryserviceswidget.h"
00034 
00035 #include <kleo/cryptobackendfactory.h>
00036 
00037 #include <kmessagebox.h>
00038 #include <klocale.h>
00039 #include <kdebug.h>
00040 #include <kconfig.h>
00041 #include <knuminput.h>
00042 #include <kdialog.h>
00043 
00044 #include <qhbox.h>
00045 #include <qlabel.h>
00046 #include <qdatetimeedit.h>
00047 #include <qcheckbox.h>
00048 #include <qlayout.h>
00049 
00050 
00051 #if 0 // disabled, since it is apparently confusing
00052 // For sync'ing kabldaprc
00053 class KABSynchronizer
00054 {
00055 public:
00056   KABSynchronizer()
00057     : mConfig( "kabldaprc" ) {
00058     mConfig.setGroup( "LDAP" );
00059   }
00060 
00061   KURL::List readCurrentList() const {
00062 
00063     KURL::List lst;
00064     // stolen from kabc/ldapclient.cpp
00065     const uint numHosts = mConfig.readUnsignedNumEntry( "NumSelectedHosts" );
00066     for ( uint j = 0; j < numHosts; j++ ) {
00067       const QString num = QString::number( j );
00068 
00069       KURL url;
00070       url.setProtocol( "ldap" );
00071       url.setPath( "/" ); // workaround KURL parsing bug
00072       const QString host = mConfig.readEntry( QString( "SelectedHost" ) + num ).stripWhiteSpace();
00073       url.setHost( host );
00074 
00075       const int port = mConfig.readUnsignedNumEntry( QString( "SelectedPort" ) + num );
00076       if ( port != 0 )
00077         url.setPort( port );
00078 
00079       const QString base = mConfig.readEntry( QString( "SelectedBase" ) + num ).stripWhiteSpace();
00080       url.setQuery( base );
00081 
00082       const QString bindDN = mConfig.readEntry( QString( "SelectedBind" ) + num ).stripWhiteSpace();
00083       url.setUser( bindDN.isEmpty()?QString::null:bindDN ); // otherwise KURL::operator== fails
00084 
00085       const QString pwdBindDN = mConfig.readEntry( QString( "SelectedPwdBind" ) + num ).stripWhiteSpace();
00086       url.setPass( pwdBindDN.isEmpty()?QString::null:pwdBindDN ); // otherwise KURL::operator== fails
00087 
00088       lst.append( url );
00089     }
00090     return lst;
00091   }
00092 
00093   void writeList( const KURL::List& lst ) {
00094 
00095     mConfig.writeEntry( "NumSelectedHosts", lst.count() );
00096 
00097     KURL::List::const_iterator it = lst.begin();
00098     KURL::List::const_iterator end = lst.end();
00099     unsigned j = 0;
00100     for( ; it != end; ++it, ++j ) {
00101       const QString num = QString::number( j );
00102       KURL url = *it;
00103 
00104       Q_ASSERT( url.protocol() == "ldap" );
00105       mConfig.writeEntry( QString( "SelectedHost" ) + num, url.host() );
00106       mConfig.writeEntry( QString( "SelectedPort" ) + num, url.port() );
00107 
00108       // KURL automatically encoded the query (e.g. for spaces inside it),
00109       // so decode it before writing it out
00110       const QString base = KURL::decode_string( url.query().mid(1) );
00111       mConfig.writeEntry( QString( "SelectedBase" ) + num, base );
00112       mConfig.writeEntry( QString( "SelectedBind" ) + num, url.user() );
00113       mConfig.writeEntry( QString( "SelectedPwdBind" ) + num, url.pass() );
00114     }
00115     mConfig.sync();
00116   }
00117 
00118 private:
00119   KConfig mConfig;
00120 };
00121 
00122 #endif
00123 
00124 static const char s_dirserv_componentName[] = "dirmngr";
00125 static const char s_dirserv_groupName[] = "LDAP";
00126 static const char s_dirserv_entryName[] = "LDAP Server";
00127 
00128 static const char s_timeout_componentName[] = "dirmngr";
00129 static const char s_timeout_groupName[] = "LDAP";
00130 static const char s_timeout_entryName[] = "ldaptimeout";
00131 
00132 static const char s_maxitems_componentName[] = "dirmngr";
00133 static const char s_maxitems_groupName[] = "LDAP";
00134 static const char s_maxitems_entryName[] = "max-replies";
00135 
00136 static const char s_addnewservers_componentName[] = "dirmngr";
00137 static const char s_addnewservers_groupName[] = "LDAP";
00138 static const char s_addnewservers_entryName[] = "add-servers";
00139 
00140 DirectoryServicesConfigurationPage::DirectoryServicesConfigurationPage( QWidget * parent, const char * name )
00141     : KCModule( parent, name )
00142 {
00143   mConfig = Kleo::CryptoBackendFactory::instance()->config();
00144   QVBoxLayout* lay = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00145   Kleo::CryptoConfigEntry* entry = configEntry( s_dirserv_componentName, s_dirserv_groupName, s_dirserv_entryName,
00146                                                 Kleo::CryptoConfigEntry::ArgType_LDAPURL, true );
00147   mWidget = new Kleo::DirectoryServicesWidget( entry, this );
00148   lay->addWidget( mWidget );
00149   connect( mWidget, SIGNAL( changed() ), this, SLOT( slotChanged() ) );
00150 
00151   // LDAP timeout
00152   QHBox* box = new QHBox( this );
00153   box->setSpacing( KDialog::spacingHint() );
00154   lay->addWidget( box );
00155   QLabel* label = new QLabel( i18n( "LDAP &timeout (minutes:seconds)" ), box );
00156   mTimeout = new QTimeEdit( box );
00157   mTimeout->setDisplay( QTimeEdit::Minutes | QTimeEdit::Seconds );
00158   connect( mTimeout, SIGNAL( valueChanged( const QTime& ) ), this, SLOT( slotChanged() ) );
00159   label->setBuddy( mTimeout );
00160   QWidget* stretch = new QWidget( box );
00161   box->setStretchFactor( stretch, 2 );
00162 
00163   // Max number of items returned by queries
00164   box = new QHBox( this );
00165   box->setSpacing( KDialog::spacingHint() );
00166   lay->addWidget( box );
00167   mMaxItems = new KIntNumInput( box );
00168   mMaxItems->setLabel( i18n( "&Maximum number of items returned by query" ), Qt::AlignLeft | Qt::AlignVCenter );
00169   mMaxItems->setMinValue( 0 );
00170   connect( mMaxItems, SIGNAL( valueChanged(int) ), this, SLOT( slotChanged() ) );
00171   stretch = new QWidget( box );
00172   box->setStretchFactor( stretch, 2 );
00173 
00174 #ifdef NOT_USEFUL_CURRENTLY
00175   mAddNewServersCB = new QCheckBox( i18n( "Automatically add &new servers discovered in CRL distribution points" ), this );
00176   connect( mAddNewServersCB, SIGNAL( clicked() ), this, SLOT( slotChanged() ) );
00177   lay->addWidget( mAddNewServersCB );
00178 #endif
00179 
00180 #ifndef HAVE_UNBROKEN_KCMULTIDIALOG
00181   load();
00182 #endif
00183 }
00184 
00185 void DirectoryServicesConfigurationPage::load()
00186 {
00187   mWidget->load();
00188 
00189   mTimeoutConfigEntry = configEntry( s_timeout_componentName, s_timeout_groupName, s_timeout_entryName, Kleo::CryptoConfigEntry::ArgType_UInt, false );
00190   if ( mTimeoutConfigEntry ) {
00191     QTime time = QTime().addSecs( mTimeoutConfigEntry->uintValue() );
00192     //kdDebug() << "timeout:" << mTimeoutConfigEntry->uintValue() << "  -> " << time << endl;
00193     mTimeout->setTime( time );
00194   }
00195 
00196   mMaxItemsConfigEntry = configEntry( s_maxitems_componentName, s_maxitems_groupName, s_maxitems_entryName, Kleo::CryptoConfigEntry::ArgType_UInt, false );
00197   if ( mMaxItemsConfigEntry ) {
00198     mMaxItems->blockSignals( true ); // KNumInput emits valueChanged from setValue!
00199     mMaxItems->setValue( mMaxItemsConfigEntry->uintValue() );
00200     mMaxItems->blockSignals( false );
00201   }
00202 
00203 #ifdef NOT_USEFUL_CURRENTLY
00204   mAddNewServersConfigEntry = configEntry( s_addnewservers_componentName, s_addnewservers_groupName, s_addnewservers_entryName, Kleo::CryptoConfigEntry::ArgType_None, false );
00205   if ( mAddNewServersConfigEntry ) {
00206     mAddNewServersCB->setChecked( mAddNewServersConfigEntry->boolValue() );
00207   }
00208 #endif
00209 }
00210 
00211 void DirectoryServicesConfigurationPage::save()
00212 {
00213   mWidget->save();
00214 
00215   QTime time( mTimeout->time() );
00216   unsigned int timeout = time.minute() * 60 + time.second();
00217   if ( mTimeoutConfigEntry && mTimeoutConfigEntry->uintValue() != timeout )
00218     mTimeoutConfigEntry->setUIntValue( timeout );
00219   if ( mMaxItemsConfigEntry && mMaxItemsConfigEntry->uintValue() != (uint)mMaxItems->value() )
00220     mMaxItemsConfigEntry->setUIntValue( mMaxItems->value() );
00221 #ifdef NOT_USEFUL_CURRENTLY
00222   if ( mAddNewServersConfigEntry && mAddNewServersConfigEntry->boolValue() != mAddNewServersCB->isChecked() )
00223     mAddNewServersConfigEntry->setBoolValue( mAddNewServersCB->isChecked() );
00224 #endif
00225 
00226   mConfig->sync( true );
00227 
00228 #if 0
00229   // Also write the LDAP URLs to kabldaprc so that they are used by kaddressbook
00230   KABSynchronizer sync;
00231   const KURL::List toAdd = mWidget->urlList();
00232   KURL::List currentList = sync.readCurrentList();
00233 
00234   KURL::List::const_iterator it = toAdd.begin();
00235   KURL::List::const_iterator end = toAdd.end();
00236   for( ; it != end; ++it ) {
00237     // check if the URL is already in currentList
00238     if ( currentList.find( *it ) == currentList.end() )
00239       // if not, add it
00240       currentList.append( *it );
00241   }
00242   sync.writeList( currentList );
00243 #endif
00244 }
00245 
00246 void DirectoryServicesConfigurationPage::defaults()
00247 {
00248   mWidget->defaults();
00249   if ( mTimeoutConfigEntry )
00250     mTimeoutConfigEntry->resetToDefault();
00251   if ( mMaxItemsConfigEntry )
00252     mMaxItemsConfigEntry->resetToDefault();
00253 #ifdef NOT_USEFUL_CURRENTLY
00254   if ( mAddNewServersConfigEntry )
00255     mAddNewServersConfigEntry->resetToDefault();
00256 #endif
00257   load();
00258 }
00259 
00260 extern "C"
00261 {
00262   KCModule *create_kleopatra_config_dirserv( QWidget *parent, const char * )
00263   {
00264     DirectoryServicesConfigurationPage *page =
00265       new DirectoryServicesConfigurationPage( parent, "kleopatra_config_dirserv" );
00266     return page;
00267   }
00268 }
00269 
00270 // kdelibs-3.2 didn't have the changed signal in KCModule...
00271 void DirectoryServicesConfigurationPage::slotChanged()
00272 {
00273   emit changed(true);
00274 }
00275 
00276 
00277 // Find config entry for ldap servers. Implements runtime checks on the configuration option.
00278 Kleo::CryptoConfigEntry* DirectoryServicesConfigurationPage::configEntry( const char* componentName,
00279                                                                           const char* groupName,
00280                                                                           const char* entryName,
00281                                                                           Kleo::CryptoConfigEntry::ArgType argType,
00282                                                                           bool isList )
00283 {
00284     Kleo::CryptoConfigEntry* entry = mConfig->entry( componentName, groupName, entryName );
00285     if ( !entry ) {
00286         KMessageBox::error( this, i18n( "Backend error: gpgconf doesn't seem to know the entry for %1/%2/%3" ).arg( componentName, groupName, entryName ) );
00287         return 0;
00288     }
00289     if( entry->argType() != argType || entry->isList() != isList ) {
00290         KMessageBox::error( this, i18n( "Backend error: gpgconf has wrong type for %1/%2/%3: %4 %5" ).arg( componentName, groupName, entryName ).arg( entry->argType() ).arg( entry->isList() ) );
00291         return 0;
00292     }
00293     return entry;
00294 }
00295 
00296 #include "dirservconfigpage.moc"
KDE Logo
This file is part of the documentation for certmanager Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Dec 21 14:22:38 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003