certmanager/lib Library API Documentation

cryptoconfigmodule.cpp

00001 /*
00002     cryptoconfigmodule.cpp
00003 
00004     This file is part of kgpgcertmanager
00005     Copyright (c) 2004 Klar�vdalens 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 "cryptoconfigmodule.h"
00033 #include "directoryserviceswidget.h"
00034 #include <kleo/cryptoconfig.h>
00035 
00036 #include <klineedit.h>
00037 #include <klocale.h>
00038 #include <kdialogbase.h>
00039 #include <kdebug.h>
00040 #include <knuminput.h>
00041 #include <kiconloader.h>
00042 #include <kglobal.h>
00043 
00044 #include <qgrid.h>
00045 #include <qlabel.h>
00046 #include <qlayout.h>
00047 #include <qvbox.h>
00048 #include <qpushbutton.h>
00049 #include <qregexp.h>
00050 
00051 using namespace Kleo;
00052 
00053 inline QPixmap loadIcon( QString s ) {
00054   return KGlobal::instance()->iconLoader()
00055     ->loadIcon( s.replace( QRegExp( "[^a-zA-Z0-9_]" ), "_" ), KIcon::NoGroup, KIcon::SizeMedium );
00056 }
00057 
00058 Kleo::CryptoConfigModule::CryptoConfigModule( Kleo::CryptoConfig* config, QWidget * parent, const char * name )
00059   : KJanusWidget( parent, name, KJanusWidget::IconList ), mConfig( config )
00060 {
00061 //  QVBoxLayout *vlay = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00062 //  mTabWidget = new QTabWidget( this );
00063 //  vlay->addWidget( mTabWidget );
00064 
00065   QStringList components = config->componentList();
00066 
00067   for( QStringList::Iterator compit = components.begin(); compit != components.end(); ++compit ) {
00068     //kdDebug(5150) << "Component " << (*compit).local8Bit() << ":" << endl;
00069     Kleo::CryptoConfigComponent* comp = config->component( *compit );
00070     Q_ASSERT( comp );
00071     if ( !comp->groupList().isEmpty() ) {
00072       QVBox* vbox = addVBoxPage( comp->description(), QString::null, loadIcon( *compit ) );
00073       CryptoConfigComponentGUI* compGUI =
00074         new CryptoConfigComponentGUI( this, comp, vbox, (*compit).local8Bit() );
00075       // KJanusWidget doesn't seem to have iterators, so we store a copy...
00076       mComponentGUIs.append( compGUI );
00077     }
00078   }
00079 }
00080 
00081 void Kleo::CryptoConfigModule::save()
00082 {
00083   bool changed = false;
00084   QValueList<CryptoConfigComponentGUI *>::Iterator it = mComponentGUIs.begin();
00085   for( ; it != mComponentGUIs.end(); ++it ) {
00086     if ( (*it)->save() )
00087       changed = true;
00088   }
00089   if ( changed )
00090     mConfig->sync(true /*runtime*/);
00091 }
00092 
00093 void Kleo::CryptoConfigModule::reset()
00094 {
00095   QValueList<CryptoConfigComponentGUI *>::Iterator it = mComponentGUIs.begin();
00096   for( ; it != mComponentGUIs.end(); ++it ) {
00097     (*it)->load();
00098   }
00099 }
00100 
00101 void Kleo::CryptoConfigModule::defaults()
00102 {
00103   QValueList<CryptoConfigComponentGUI *>::Iterator it = mComponentGUIs.begin();
00104   for( ; it != mComponentGUIs.end(); ++it ) {
00105     (*it)->defaults();
00106   }
00107 }
00108 
00109 void Kleo::CryptoConfigModule::cancel()
00110 {
00111   mConfig->clear();
00112 }
00113 
00115 
00116 Kleo::CryptoConfigComponentGUI::CryptoConfigComponentGUI(
00117   CryptoConfigModule* module, Kleo::CryptoConfigComponent* component,
00118   QWidget* parent, const char* name )
00119 #ifdef USE_TABS // Old idea, dead code
00120   : QTabWidget( parent, name ),
00121 #else
00122   : QWidget( parent, name ),
00123 #endif
00124     mComponent( component )
00125 {
00126 #ifndef USE_TABS
00127   QVBoxLayout *vlay = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00128 #endif
00129 
00130   QStringList groups = mComponent->groupList();
00131   for( QStringList::Iterator groupit = groups.begin(); groupit != groups.end(); ++groupit ) {
00132     Kleo::CryptoConfigGroup* group = mComponent->group( *groupit );
00133     Q_ASSERT( group );
00134     CryptoConfigGroupGUI* gg = new CryptoConfigGroupGUI( module, group, this );
00135 #ifdef USE_TABS
00136     addTab( gg, group->description() );
00137 #else
00138     vlay->addWidget( gg );
00139 #endif
00140     mGroupGUIs.append( gg );
00141   }
00142 }
00143 
00144 
00145 bool Kleo::CryptoConfigComponentGUI::save()
00146 {
00147   bool changed = false;
00148   QValueList<CryptoConfigGroupGUI *>::Iterator it = mGroupGUIs.begin();
00149   for( ; it != mGroupGUIs.end(); ++it ) {
00150     if ( (*it)->save() )
00151       changed = true;
00152   }
00153   return changed;
00154 }
00155 
00156 void Kleo::CryptoConfigComponentGUI::load()
00157 {
00158   QValueList<CryptoConfigGroupGUI *>::Iterator it = mGroupGUIs.begin();
00159   for( ; it != mGroupGUIs.end(); ++it )
00160     (*it)->load();
00161 }
00162 
00163 void Kleo::CryptoConfigComponentGUI::defaults()
00164 {
00165   QValueList<CryptoConfigGroupGUI *>::Iterator it = mGroupGUIs.begin();
00166   for( ; it != mGroupGUIs.end(); ++it )
00167     (*it)->defaults();
00168 }
00169 
00171 
00172 Kleo::CryptoConfigGroupGUI::CryptoConfigGroupGUI(
00173   CryptoConfigModule* module, Kleo::CryptoConfigGroup* group,
00174   QWidget* parent, const char* name )
00175   : QGroupBox( 1, Qt::Horizontal, // yeah that means a vertical layout...
00176                group->description(), parent, name ), mGroup( group )
00177 {
00178   QStringList entries = mGroup->entryList();
00179   for( QStringList::Iterator entryit = entries.begin(); entryit != entries.end(); ++entryit ) {
00180     Kleo::CryptoConfigEntry* entry = group->entry( *entryit );
00181     Q_ASSERT( entry );
00182     CryptoConfigEntryGUI* entryGUI =
00183       CryptoConfigEntryGUIFactory::createEntryGUI( module, entry, *entryit, this );
00184     if ( entryGUI ) {
00185       mEntryGUIs.append( entryGUI );
00186       entryGUI->load();
00187     }
00188   }
00189 }
00190 
00191 bool Kleo::CryptoConfigGroupGUI::save()
00192 {
00193   bool changed = false;
00194   QValueList<CryptoConfigEntryGUI *>::Iterator it = mEntryGUIs.begin();
00195   for( ; it != mEntryGUIs.end(); ++it ) {
00196     if ( (*it)->isChanged() ) {
00197       (*it)->save();
00198       changed = true;
00199     }
00200   }
00201   return changed;
00202 }
00203 
00204 void Kleo::CryptoConfigGroupGUI::load()
00205 {
00206   QValueList<CryptoConfigEntryGUI *>::Iterator it = mEntryGUIs.begin();
00207   for( ; it != mEntryGUIs.end(); ++it )
00208     (*it)->load();
00209 }
00210 
00211 void Kleo::CryptoConfigGroupGUI::defaults()
00212 {
00213   QValueList<CryptoConfigEntryGUI *>::Iterator it = mEntryGUIs.begin();
00214   for( ; it != mEntryGUIs.end(); ++it )
00215     (*it)->resetToDefault();
00216 }
00217 
00219 
00220 CryptoConfigEntryGUI* Kleo::CryptoConfigEntryGUIFactory::createEntryGUI( CryptoConfigModule* module, Kleo::CryptoConfigEntry* entry, const QString& entryName, QWidget* parent, const char* name )
00221 {
00222   if ( entry->isList() ) {
00223     switch( entry->argType() ) {
00224     case Kleo::CryptoConfigEntry::ArgType_None:
00225       // A list of options with no arguments (e.g. -v -v -v) is shown as a spinbox
00226       return new CryptoConfigEntrySpinBox( module, entry, entryName, parent, name );
00227     case Kleo::CryptoConfigEntry::ArgType_Int:
00228     case Kleo::CryptoConfigEntry::ArgType_UInt:
00229       // Let people type list of numbers (1,2,3....). Untested.
00230       return new CryptoConfigEntryLineEdit( module, entry, entryName, parent, name );
00231     case Kleo::CryptoConfigEntry::ArgType_URL:
00232     case Kleo::CryptoConfigEntry::ArgType_Path:
00233     case Kleo::CryptoConfigEntry::ArgType_String:
00234       kdWarning(5150) << "No widget implemented for list of type " << entry->argType() << endl;
00235       return 0; // TODO when the need arises :)
00236     case Kleo::CryptoConfigEntry::ArgType_LDAPURL:
00237       return new CryptoConfigEntryLDAPURL( module, entry, entryName, parent, name );
00238     }
00239   }
00240 
00241   switch( entry->argType() ) {
00242   case Kleo::CryptoConfigEntry::ArgType_None:
00243     return new CryptoConfigEntryCheckBox( module, entry, entryName, parent, name );
00244   case Kleo::CryptoConfigEntry::ArgType_Int:
00245     // fallthrough
00246   case Kleo::CryptoConfigEntry::ArgType_UInt:
00247     return new CryptoConfigEntrySpinBox( module, entry, entryName, parent, name );
00248   case Kleo::CryptoConfigEntry::ArgType_LDAPURL:
00249     // TODO when the need arises
00250   case Kleo::CryptoConfigEntry::ArgType_URL:
00251     // fallthrough
00252   case Kleo::CryptoConfigEntry::ArgType_Path:
00253     // fallthrough
00254   case Kleo::CryptoConfigEntry::ArgType_String:
00255     return new CryptoConfigEntryLineEdit( module, entry, entryName, parent, name );
00256   }
00257   kdWarning(5150) << "No widget implemented for list of (unknown) type " << entry->argType() << endl;
00258   return 0;
00259 }
00260 
00262 
00263 Kleo::CryptoConfigEntryGUI::CryptoConfigEntryGUI(
00264   CryptoConfigModule* module,
00265   Kleo::CryptoConfigEntry* entry,
00266   const QString& entryName,
00267   QWidget* parent, const char* name )
00268   : QHBox( parent, name ), mEntry( entry ), mName( entryName ), mChanged( false )
00269 {
00270   connect( this, SIGNAL( changed() ), module, SIGNAL( changed() ) );
00271 }
00272 
00273 QString Kleo::CryptoConfigEntryGUI::description() const
00274 {
00275   QString descr = mEntry->description();
00276   if ( descr.isEmpty() ) // shouldn't happen
00277     descr = QString( "<%1>" ).arg( mName );
00278   return descr;
00279 }
00280 
00281 void Kleo::CryptoConfigEntryGUI::resetToDefault()
00282 {
00283   mEntry->resetToDefault();
00284   load();
00285 }
00286 
00288 
00289 Kleo::CryptoConfigEntryLineEdit::CryptoConfigEntryLineEdit(
00290   CryptoConfigModule* module,
00291   Kleo::CryptoConfigEntry* entry, const QString& entryName,
00292   QWidget* parent, const char* name )
00293   : CryptoConfigEntryGUI( module, entry, entryName, parent, name )
00294 {
00295   setSpacing( KDialog::spacingHint() );
00296   QLabel* label = new QLabel( description(), this );
00297   mLineEdit = new KLineEdit( this );
00298   connect( mLineEdit, SIGNAL( textChanged( const QString& ) ), SLOT( slotChanged() ) );
00299   label->setBuddy( mLineEdit );
00300   QWidget* stretch = new QWidget( this );
00301   setStretchFactor( stretch, 1 );
00302 }
00303 
00304 void Kleo::CryptoConfigEntryLineEdit::doSave()
00305 {
00306   mEntry->setStringValue( mLineEdit->text() );
00307 }
00308 
00309 void Kleo::CryptoConfigEntryLineEdit::doLoad()
00310 {
00311   mLineEdit->setText( mEntry->stringValue() );
00312 }
00313 
00315 
00316 Kleo::CryptoConfigEntrySpinBox::CryptoConfigEntrySpinBox(
00317   CryptoConfigModule* module,
00318   Kleo::CryptoConfigEntry* entry, const QString& entryName,
00319   QWidget* parent, const char* name )
00320   : CryptoConfigEntryGUI( module, entry, entryName, parent, name )
00321 {
00322   setSpacing( KDialog::spacingHint() );
00323   QLabel* label = new QLabel( description(), this );
00324 
00325   if ( entry->argType() == Kleo::CryptoConfigEntry::ArgType_None && entry->isList() ) {
00326     mKind = ListOfNone;
00327   } else if ( entry->argType() == Kleo::CryptoConfigEntry::ArgType_UInt ) {
00328     mKind = UInt;
00329   } else {
00330     Q_ASSERT( entry->argType() == Kleo::CryptoConfigEntry::ArgType_Int );
00331     mKind = Int;
00332   }
00333 
00334   mNumInput = new KIntNumInput( this );
00335   if ( mKind == UInt || mKind == ListOfNone )
00336     mNumInput->setMinValue( 0 );
00337   connect( mNumInput, SIGNAL( valueChanged(int) ), SLOT( slotChanged() ) );
00338   label->setBuddy( mNumInput );
00339 
00340   QWidget* stretch = new QWidget( this );
00341   setStretchFactor( stretch, 1 );
00342 }
00343 
00344 void Kleo::CryptoConfigEntrySpinBox::doSave()
00345 {
00346   int value = mNumInput->value();
00347   switch ( mKind ) {
00348   case ListOfNone:
00349     mEntry->setNumberOfTimesSet( value );
00350     break;
00351   case UInt:
00352     mEntry->setUIntValue( value );
00353     break;
00354   case Int:
00355     mEntry->setIntValue( value );
00356     break;
00357   }
00358 }
00359 
00360 void Kleo::CryptoConfigEntrySpinBox::doLoad()
00361 {
00362   int value = 0;
00363   switch ( mKind ) {
00364   case ListOfNone:
00365     value = mEntry->numberOfTimesSet();
00366     break;
00367   case UInt:
00368     value = mEntry->uintValue();
00369     break;
00370   case Int:
00371     value = mEntry->intValue();
00372     break;
00373   }
00374   mNumInput->setValue( value );
00375 }
00376 
00378 
00379 Kleo::CryptoConfigEntryCheckBox::CryptoConfigEntryCheckBox(
00380   CryptoConfigModule* module,
00381   Kleo::CryptoConfigEntry* entry, const QString& entryName,
00382   QWidget* parent, const char* name )
00383   : CryptoConfigEntryGUI( module, entry, entryName, parent, name )
00384 {
00385   mCheckBox = new QCheckBox( this);
00386   mCheckBox->setText( description() );
00387   connect( mCheckBox, SIGNAL( toggled(bool) ), SLOT( slotChanged() ) );
00388 }
00389 
00390 void Kleo::CryptoConfigEntryCheckBox::doSave()
00391 {
00392   mEntry->setBoolValue( mCheckBox->isChecked() );
00393 }
00394 
00395 void Kleo::CryptoConfigEntryCheckBox::doLoad()
00396 {
00397   mCheckBox->setChecked( mEntry->boolValue() );
00398 }
00399 
00400 Kleo::CryptoConfigEntryLDAPURL::CryptoConfigEntryLDAPURL(
00401   CryptoConfigModule* module,
00402   Kleo::CryptoConfigEntry* entry,
00403   const QString& entryName,
00404   QWidget* parent, const char* name )
00405   : CryptoConfigEntryGUI( module, entry, entryName, parent, name )
00406 {
00407   setSpacing( KDialog::spacingHint() );
00408   QLabel* label = new QLabel( description(), this );
00409   mPushButton = new QPushButton( i18n( "Edit..." ), this );
00410   mLabel = new QLabel( this );
00411   connect( mPushButton, SIGNAL( clicked() ), SLOT( slotOpenDialog() ) );
00412   label->setBuddy( mPushButton );
00413   QWidget* stretch = new QWidget( this );
00414   setStretchFactor( stretch, 1 );
00415 }
00416 
00417 void Kleo::CryptoConfigEntryLDAPURL::doLoad()
00418 {
00419   setURLList( mEntry->urlValueList() );
00420 }
00421 
00422 void Kleo::CryptoConfigEntryLDAPURL::doSave()
00423 {
00424   mEntry->setURLValueList( mURLList );
00425 }
00426 
00427 void Kleo::CryptoConfigEntryLDAPURL::slotOpenDialog()
00428 {
00429   // I'm a bad boy and I do it all on the stack. Enough classes already :)
00430   // This is just a simple dialog around the directory-services-widget
00431   KDialogBase dialog( this, 0, true /*modal*/,
00432                       i18n( "Configure LDAP Servers" ),
00433                       KDialogBase::Default|KDialogBase::Cancel|KDialogBase::Ok,
00434                       KDialogBase::Ok, true /*separator*/ );
00435   DirectoryServicesWidget* dirserv = new DirectoryServicesWidget( mEntry, &dialog );
00436   dirserv->load();
00437   dialog.setMainWidget( dirserv );
00438   connect( &dialog, SIGNAL( defaultClicked() ), dirserv, SLOT( defaults() ) );
00439   if ( dialog.exec() ) {
00440     // Note that we just grab the urls from the dialog, we don't call its save method,
00441     // since the user hasn't confirmed the big config dialog yet.
00442     setURLList( dirserv->urlList() );
00443     slotChanged();
00444   }
00445 }
00446 
00447 void Kleo::CryptoConfigEntryLDAPURL::setURLList( const KURL::List& urlList )
00448 {
00449   mURLList = urlList;
00450   if ( mURLList.isEmpty() )
00451     mLabel->setText( i18n( "No server configured yet" ) );
00452   else
00453     mLabel->setText( i18n( "1 server configured", "%n servers configured", mURLList.count() ) );
00454 }
00455 
00456 #include "cryptoconfigmodule.moc"
KDE Logo
This file is part of the documentation for certmanager/lib Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Apr 4 04:46:15 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003