00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "cryptoconfigmodule.h"
00033 #include "cryptoconfigmodule_p.h"
00034 #include "directoryserviceswidget.h"
00035 #include "kdhorizontalline.h"
00036
00037 #include <kleo/cryptoconfig.h>
00038
00039 #include <klineedit.h>
00040 #include <klocale.h>
00041 #include <kdialogbase.h>
00042 #include <kdebug.h>
00043 #include <knuminput.h>
00044 #include <kiconloader.h>
00045 #include <kglobal.h>
00046 #include <kurlrequester.h>
00047
00048 #include <qgrid.h>
00049 #include <qlabel.h>
00050 #include <qlayout.h>
00051 #include <qvbox.h>
00052 #include <qhbox.h>
00053 #include <qpushbutton.h>
00054 #include <qregexp.h>
00055
00056 using namespace Kleo;
00057
00058 inline QPixmap loadIcon( QString s ) {
00059 return KGlobal::instance()->iconLoader()
00060 ->loadIcon( s.replace( QRegExp( "[^a-zA-Z0-9_]" ), "_" ), KIcon::NoGroup, KIcon::SizeMedium );
00061 }
00062
00063 static const KJanusWidget::Face determineJanusFace( const Kleo::CryptoConfig * config ) {
00064 return config && config->componentList().size() < 2
00065 ? KJanusWidget::Plain
00066 : KJanusWidget::IconList ;
00067 }
00068
00069 Kleo::CryptoConfigModule::CryptoConfigModule( Kleo::CryptoConfig* config, QWidget * parent, const char * name )
00070 : KJanusWidget( parent, name, determineJanusFace( config ) ), mConfig( config )
00071 {
00072 QWidget * vbox = 0;
00073 if ( face() == Plain ) {
00074 vbox = plainPage();
00075 QVBoxLayout * vlay = new QVBoxLayout( vbox, 0, KDialog::spacingHint() );
00076 vlay->setAutoAdd( true );
00077 }
00078
00079 const QStringList components = config->componentList();
00080 for ( QStringList::const_iterator it = components.begin(); it != components.end(); ++it ) {
00081
00082 Kleo::CryptoConfigComponent* comp = config->component( *it );
00083 Q_ASSERT( comp );
00084 if ( comp->groupList().empty() )
00085 continue;
00086 if ( face() != Plain ) {
00087 vbox = addVBoxPage( comp->description(), QString::null, loadIcon( comp->iconName() ) );
00088 }
00089 CryptoConfigComponentGUI* compGUI =
00090 new CryptoConfigComponentGUI( this, comp, vbox, (*it).local8Bit() );
00091
00092 mComponentGUIs.append( compGUI );
00093 }
00094 }
00095
00096 void Kleo::CryptoConfigModule::save()
00097 {
00098 bool changed = false;
00099 QValueList<CryptoConfigComponentGUI *>::Iterator it = mComponentGUIs.begin();
00100 for( ; it != mComponentGUIs.end(); ++it ) {
00101 if ( (*it)->save() )
00102 changed = true;
00103 }
00104 if ( changed )
00105 mConfig->sync(true );
00106 }
00107
00108 void Kleo::CryptoConfigModule::reset()
00109 {
00110 QValueList<CryptoConfigComponentGUI *>::Iterator it = mComponentGUIs.begin();
00111 for( ; it != mComponentGUIs.end(); ++it ) {
00112 (*it)->load();
00113 }
00114 }
00115
00116 void Kleo::CryptoConfigModule::defaults()
00117 {
00118 QValueList<CryptoConfigComponentGUI *>::Iterator it = mComponentGUIs.begin();
00119 for( ; it != mComponentGUIs.end(); ++it ) {
00120 (*it)->defaults();
00121 }
00122 }
00123
00124 void Kleo::CryptoConfigModule::cancel()
00125 {
00126 mConfig->clear();
00127 }
00128
00130
00131 Kleo::CryptoConfigComponentGUI::CryptoConfigComponentGUI(
00132 CryptoConfigModule* module, Kleo::CryptoConfigComponent* component,
00133 QWidget* parent, const char* name )
00134 : QWidget( parent, name ),
00135 mComponent( component )
00136 {
00137 QGridLayout * glay = new QGridLayout( this, 1, 3, 0, KDialog::spacingHint() );
00138 const QStringList groups = mComponent->groupList();
00139 if ( groups.size() > 1 ) {
00140 glay->setColSpacing( 0, KDHorizontalLine::indentHint() );
00141 for ( QStringList::const_iterator it = groups.begin(), end = groups.end() ; it != end; ++it ) {
00142 Kleo::CryptoConfigGroup* group = mComponent->group( *it );
00143 Q_ASSERT( group );
00144 if ( !group )
00145 continue;
00146 KDHorizontalLine * hl = new KDHorizontalLine( group->description(), this );
00147 const int row = glay->numRows();
00148 glay->addMultiCellWidget( hl, row, row, 0, 2 );
00149 mGroupGUIs.append( new CryptoConfigGroupGUI( module, group, glay, this ) );
00150 }
00151 } else if ( !groups.empty() ) {
00152 mGroupGUIs.append( new CryptoConfigGroupGUI( module, mComponent->group( groups.front() ), glay, this ) );
00153 }
00154 glay->setRowStretch( glay->numRows(), 1 );
00155 }
00156
00157
00158 bool Kleo::CryptoConfigComponentGUI::save()
00159 {
00160 bool changed = false;
00161 QValueList<CryptoConfigGroupGUI *>::Iterator it = mGroupGUIs.begin();
00162 for( ; it != mGroupGUIs.end(); ++it ) {
00163 if ( (*it)->save() )
00164 changed = true;
00165 }
00166 return changed;
00167 }
00168
00169 void Kleo::CryptoConfigComponentGUI::load()
00170 {
00171 QValueList<CryptoConfigGroupGUI *>::Iterator it = mGroupGUIs.begin();
00172 for( ; it != mGroupGUIs.end(); ++it )
00173 (*it)->load();
00174 }
00175
00176 void Kleo::CryptoConfigComponentGUI::defaults()
00177 {
00178 QValueList<CryptoConfigGroupGUI *>::Iterator it = mGroupGUIs.begin();
00179 for( ; it != mGroupGUIs.end(); ++it )
00180 (*it)->defaults();
00181 }
00182
00184
00185 Kleo::CryptoConfigGroupGUI::CryptoConfigGroupGUI(
00186 CryptoConfigModule* module, Kleo::CryptoConfigGroup* group,
00187 QGridLayout * glay, QWidget* widget, const char* name )
00188 : QObject( module, name ), mGroup( group )
00189 {
00190 const int startRow = glay->numRows();
00191 const QStringList entries = mGroup->entryList();
00192 for( QStringList::const_iterator it = entries.begin(), end = entries.end() ; it != end; ++it ) {
00193 Kleo::CryptoConfigEntry* entry = group->entry( *it );
00194 Q_ASSERT( entry );
00195 if ( entry->level() > CryptoConfigEntry::Level_Advanced ) continue;
00196 CryptoConfigEntryGUI* entryGUI =
00197 CryptoConfigEntryGUIFactory::createEntryGUI( module, entry, *it, glay, widget );
00198 if ( entryGUI ) {
00199 mEntryGUIs.append( entryGUI );
00200 entryGUI->load();
00201 }
00202 }
00203 const int endRow = glay->numRows() - 1;
00204 if ( endRow < startRow )
00205 return;
00206
00207 const QString iconName = group->iconName();
00208 if ( iconName.isEmpty() )
00209 return;
00210
00211 QLabel * l = new QLabel( widget );
00212 l->setPixmap( loadIcon( iconName ) );
00213 glay->addMultiCellWidget( l, startRow, endRow, 0, 0, Qt::AlignTop );
00214 }
00215
00216 bool Kleo::CryptoConfigGroupGUI::save()
00217 {
00218 bool changed = false;
00219 QValueList<CryptoConfigEntryGUI *>::Iterator it = mEntryGUIs.begin();
00220 for( ; it != mEntryGUIs.end(); ++it ) {
00221 if ( (*it)->isChanged() ) {
00222 (*it)->save();
00223 changed = true;
00224 }
00225 }
00226 return changed;
00227 }
00228
00229 void Kleo::CryptoConfigGroupGUI::load()
00230 {
00231 QValueList<CryptoConfigEntryGUI *>::Iterator it = mEntryGUIs.begin();
00232 for( ; it != mEntryGUIs.end(); ++it )
00233 (*it)->load();
00234 }
00235
00236 void Kleo::CryptoConfigGroupGUI::defaults()
00237 {
00238 QValueList<CryptoConfigEntryGUI *>::Iterator it = mEntryGUIs.begin();
00239 for( ; it != mEntryGUIs.end(); ++it )
00240 (*it)->resetToDefault();
00241 }
00242
00244
00245 CryptoConfigEntryGUI* Kleo::CryptoConfigEntryGUIFactory::createEntryGUI( CryptoConfigModule* module, Kleo::CryptoConfigEntry* entry, const QString& entryName, QGridLayout * glay, QWidget* widget, const char* name )
00246 {
00247 if ( entry->isList() ) {
00248 switch( entry->argType() ) {
00249 case Kleo::CryptoConfigEntry::ArgType_None:
00250
00251 return new CryptoConfigEntrySpinBox( module, entry, entryName, glay, widget, name );
00252 case Kleo::CryptoConfigEntry::ArgType_Int:
00253 case Kleo::CryptoConfigEntry::ArgType_UInt:
00254
00255 return new CryptoConfigEntryLineEdit( module, entry, entryName, glay, widget, name );
00256 case Kleo::CryptoConfigEntry::ArgType_URL:
00257 case Kleo::CryptoConfigEntry::ArgType_Path:
00258 case Kleo::CryptoConfigEntry::ArgType_DirPath:
00259 case Kleo::CryptoConfigEntry::ArgType_String:
00260 kdWarning(5150) << "No widget implemented for list of type " << entry->argType() << endl;
00261 return 0;
00262 case Kleo::CryptoConfigEntry::ArgType_LDAPURL:
00263 return new CryptoConfigEntryLDAPURL( module, entry, entryName, glay, widget, name );
00264 }
00265 kdWarning(5150) << "No widget implemented for list of (unknown) type " << entry->argType() << endl;
00266 return 0;
00267 }
00268
00269 switch( entry->argType() ) {
00270 case Kleo::CryptoConfigEntry::ArgType_None:
00271 return new CryptoConfigEntryCheckBox( module, entry, entryName, glay, widget, name );
00272 case Kleo::CryptoConfigEntry::ArgType_Int:
00273 case Kleo::CryptoConfigEntry::ArgType_UInt:
00274 return new CryptoConfigEntrySpinBox( module, entry, entryName, glay, widget, name );
00275 case Kleo::CryptoConfigEntry::ArgType_URL:
00276 return new CryptoConfigEntryURL( module, entry, entryName, glay, widget, name );
00277 case Kleo::CryptoConfigEntry::ArgType_Path:
00278 return new CryptoConfigEntryPath( module, entry, entryName, glay, widget, name );
00279 case Kleo::CryptoConfigEntry::ArgType_DirPath:
00280 return new CryptoConfigEntryDirPath( module, entry, entryName, glay, widget, name );
00281 case Kleo::CryptoConfigEntry::ArgType_LDAPURL:
00282 kdWarning(5150) << "No widget implemented for type " << entry->argType() << endl;
00283 return 0;
00284 case Kleo::CryptoConfigEntry::ArgType_String:
00285 return new CryptoConfigEntryLineEdit( module, entry, entryName, glay, widget, name );
00286 }
00287 kdWarning(5150) << "No widget implemented for (unknown) type " << entry->argType() << endl;
00288 return 0;
00289 }
00290
00292
00293 Kleo::CryptoConfigEntryGUI::CryptoConfigEntryGUI(
00294 CryptoConfigModule* module,
00295 Kleo::CryptoConfigEntry* entry,
00296 const QString& entryName,
00297 const char* name )
00298 : QObject( module, name ), mEntry( entry ), mName( entryName ), mChanged( false )
00299 {
00300 connect( this, SIGNAL( changed() ), module, SIGNAL( changed() ) );
00301 }
00302
00303 QString Kleo::CryptoConfigEntryGUI::description() const
00304 {
00305 QString descr = mEntry->description();
00306 if ( descr.isEmpty() )
00307 descr = QString( "<%1>" ).arg( mName );
00308 return descr;
00309 }
00310
00311 void Kleo::CryptoConfigEntryGUI::resetToDefault()
00312 {
00313 mEntry->resetToDefault();
00314 load();
00315 }
00316
00318
00319 Kleo::CryptoConfigEntryLineEdit::CryptoConfigEntryLineEdit(
00320 CryptoConfigModule* module,
00321 Kleo::CryptoConfigEntry* entry, const QString& entryName,
00322 QGridLayout * glay, QWidget* widget, const char* name )
00323 : CryptoConfigEntryGUI( module, entry, entryName, name )
00324 {
00325 const int row = glay->numRows();
00326 mLineEdit = new KLineEdit( widget );
00327 glay->addWidget( new QLabel( mLineEdit, description(), widget ), row, 1 );
00328 glay->addWidget( mLineEdit, row, 2 );
00329 connect( mLineEdit, SIGNAL( textChanged( const QString& ) ), SLOT( slotChanged() ) );
00330 }
00331
00332 void Kleo::CryptoConfigEntryLineEdit::doSave()
00333 {
00334 mEntry->setStringValue( mLineEdit->text() );
00335 }
00336
00337 void Kleo::CryptoConfigEntryLineEdit::doLoad()
00338 {
00339 mLineEdit->setText( mEntry->stringValue() );
00340 }
00341
00343
00344 Kleo::CryptoConfigEntryPath::CryptoConfigEntryPath(
00345 CryptoConfigModule* module,
00346 Kleo::CryptoConfigEntry* entry, const QString& entryName,
00347 QGridLayout * glay, QWidget* widget, const char* name )
00348 : CryptoConfigEntryGUI( module, entry, entryName, name )
00349 {
00350 const int row = glay->numRows();
00351 mUrlRequester = new KURLRequester( widget );
00352 mUrlRequester->setMode( KFile::File | KFile::ExistingOnly | KFile::LocalOnly );
00353 glay->addWidget( new QLabel( mUrlRequester, description(), widget ), row, 1 );
00354 glay->addWidget( mUrlRequester, row, 2 );
00355 connect( mUrlRequester, SIGNAL( textChanged( const QString& ) ), SLOT( slotChanged() ) );
00356 }
00357
00358 void Kleo::CryptoConfigEntryPath::doSave()
00359 {
00360 KURL url;
00361 url.setPath( mUrlRequester->url() );
00362 mEntry->setURLValue( url );
00363 }
00364
00365 void Kleo::CryptoConfigEntryPath::doLoad()
00366 {
00367 mUrlRequester->setURL( mEntry->urlValue().path() );
00368 }
00369
00371
00372 Kleo::CryptoConfigEntryDirPath::CryptoConfigEntryDirPath(
00373 CryptoConfigModule* module,
00374 Kleo::CryptoConfigEntry* entry, const QString& entryName,
00375 QGridLayout * glay, QWidget* widget, const char* name )
00376 : CryptoConfigEntryGUI( module, entry, entryName, name )
00377 {
00378 const int row = glay->numRows();
00379 mUrlRequester = new KURLRequester( widget );
00380 mUrlRequester->setMode( KFile::Directory | KFile::ExistingOnly | KFile::LocalOnly );
00381 glay->addWidget( new QLabel( mUrlRequester, description(), widget ), row, 1 );
00382 glay->addWidget( mUrlRequester, row, 2 );
00383 connect( mUrlRequester, SIGNAL( textChanged( const QString& ) ), SLOT( slotChanged() ) );
00384 }
00385
00386 void Kleo::CryptoConfigEntryDirPath::doSave()
00387 {
00388 KURL url;
00389 url.setPath( mUrlRequester->url() );
00390 mEntry->setURLValue( url );
00391
00392 }
00393
00394 void Kleo::CryptoConfigEntryDirPath::doLoad()
00395 {
00396 mUrlRequester->setURL( mEntry->urlValue().path() );
00397 }
00398
00400
00401 Kleo::CryptoConfigEntryURL::CryptoConfigEntryURL(
00402 CryptoConfigModule* module,
00403 Kleo::CryptoConfigEntry* entry, const QString& entryName,
00404 QGridLayout * glay, QWidget* widget, const char* name )
00405 : CryptoConfigEntryGUI( module, entry, entryName, name )
00406 {
00407 const int row = glay->numRows();
00408 mUrlRequester = new KURLRequester( widget );
00409 mUrlRequester->setMode( KFile::File | KFile::ExistingOnly );
00410 glay->addWidget( new QLabel( mUrlRequester, description(), widget ), row, 1 );
00411 glay->addWidget( mUrlRequester, row, 2 );
00412 connect( mUrlRequester, SIGNAL( textChanged( const QString& ) ), SLOT( slotChanged() ) );
00413 }
00414
00415 void Kleo::CryptoConfigEntryURL::doSave()
00416 {
00417 mEntry->setURLValue( mUrlRequester->url() );
00418 }
00419
00420 void Kleo::CryptoConfigEntryURL::doLoad()
00421 {
00422 mUrlRequester->setURL( mEntry->urlValue().url() );
00423 }
00424
00426
00427 Kleo::CryptoConfigEntrySpinBox::CryptoConfigEntrySpinBox(
00428 CryptoConfigModule* module,
00429 Kleo::CryptoConfigEntry* entry, const QString& entryName,
00430 QGridLayout * glay, QWidget* widget, const char* name )
00431 : CryptoConfigEntryGUI( module, entry, entryName, name )
00432 {
00433
00434 if ( entry->argType() == Kleo::CryptoConfigEntry::ArgType_None && entry->isList() ) {
00435 mKind = ListOfNone;
00436 } else if ( entry->argType() == Kleo::CryptoConfigEntry::ArgType_UInt ) {
00437 mKind = UInt;
00438 } else {
00439 Q_ASSERT( entry->argType() == Kleo::CryptoConfigEntry::ArgType_Int );
00440 mKind = Int;
00441 }
00442
00443 const int row = glay->numRows();
00444 mNumInput = new KIntNumInput( widget );
00445 glay->addWidget( new QLabel( mNumInput, description(), widget ), row, 1 );
00446 glay->addWidget( mNumInput, row, 2 );
00447
00448 if ( mKind == UInt || mKind == ListOfNone )
00449 mNumInput->setMinValue( 0 );
00450 connect( mNumInput, SIGNAL( valueChanged(int) ), SLOT( slotChanged() ) );
00451 }
00452
00453 void Kleo::CryptoConfigEntrySpinBox::doSave()
00454 {
00455 int value = mNumInput->value();
00456 switch ( mKind ) {
00457 case ListOfNone:
00458 mEntry->setNumberOfTimesSet( value );
00459 break;
00460 case UInt:
00461 mEntry->setUIntValue( value );
00462 break;
00463 case Int:
00464 mEntry->setIntValue( value );
00465 break;
00466 }
00467 }
00468
00469 void Kleo::CryptoConfigEntrySpinBox::doLoad()
00470 {
00471 int value = 0;
00472 switch ( mKind ) {
00473 case ListOfNone:
00474 value = mEntry->numberOfTimesSet();
00475 break;
00476 case UInt:
00477 value = mEntry->uintValue();
00478 break;
00479 case Int:
00480 value = mEntry->intValue();
00481 break;
00482 }
00483 mNumInput->setValue( value );
00484 }
00485
00487
00488 Kleo::CryptoConfigEntryCheckBox::CryptoConfigEntryCheckBox(
00489 CryptoConfigModule* module,
00490 Kleo::CryptoConfigEntry* entry, const QString& entryName,
00491 QGridLayout * glay, QWidget* widget, const char* name )
00492 : CryptoConfigEntryGUI( module, entry, entryName, name )
00493 {
00494 const int row = glay->numRows();
00495 mCheckBox = new QCheckBox( widget );
00496 glay->addMultiCellWidget( mCheckBox, row, row, 1, 2 );
00497 mCheckBox->setText( description() );
00498 connect( mCheckBox, SIGNAL( toggled(bool) ), SLOT( slotChanged() ) );
00499 }
00500
00501 void Kleo::CryptoConfigEntryCheckBox::doSave()
00502 {
00503 mEntry->setBoolValue( mCheckBox->isChecked() );
00504 }
00505
00506 void Kleo::CryptoConfigEntryCheckBox::doLoad()
00507 {
00508 mCheckBox->setChecked( mEntry->boolValue() );
00509 }
00510
00511 Kleo::CryptoConfigEntryLDAPURL::CryptoConfigEntryLDAPURL(
00512 CryptoConfigModule* module,
00513 Kleo::CryptoConfigEntry* entry,
00514 const QString& entryName,
00515 QGridLayout * glay, QWidget* widget, const char* name )
00516 : CryptoConfigEntryGUI( module, entry, entryName, name )
00517 {
00518 mLabel = new QLabel( widget );
00519 mPushButton = new QPushButton( i18n( "Edit..." ), widget );
00520
00521
00522 const int row = glay->numRows();
00523 glay->addWidget( new QLabel( mPushButton, description(), widget ), row, 1 );
00524 QHBoxLayout * hlay = new QHBoxLayout;
00525 glay->addLayout( hlay, row, 2 );
00526 hlay->addWidget( mLabel, 1 );
00527 hlay->addWidget( mPushButton );
00528
00529 connect( mPushButton, SIGNAL( clicked() ), SLOT( slotOpenDialog() ) );
00530 }
00531
00532 void Kleo::CryptoConfigEntryLDAPURL::doLoad()
00533 {
00534 setURLList( mEntry->urlValueList() );
00535 }
00536
00537 void Kleo::CryptoConfigEntryLDAPURL::doSave()
00538 {
00539 mEntry->setURLValueList( mURLList );
00540 }
00541
00542 void Kleo::CryptoConfigEntryLDAPURL::slotOpenDialog()
00543 {
00544
00545
00546 KDialogBase dialog( mPushButton->parentWidget(), 0, true ,
00547 i18n( "Configure LDAP Servers" ),
00548 KDialogBase::Default|KDialogBase::Cancel|KDialogBase::Ok,
00549 KDialogBase::Ok, true );
00550 DirectoryServicesWidget* dirserv = new DirectoryServicesWidget( mEntry, &dialog );
00551 dirserv->load();
00552 dialog.setMainWidget( dirserv );
00553 connect( &dialog, SIGNAL( defaultClicked() ), dirserv, SLOT( defaults() ) );
00554 if ( dialog.exec() ) {
00555
00556
00557 setURLList( dirserv->urlList() );
00558 slotChanged();
00559 }
00560 }
00561
00562 void Kleo::CryptoConfigEntryLDAPURL::setURLList( const KURL::List& urlList )
00563 {
00564 mURLList = urlList;
00565 if ( mURLList.isEmpty() )
00566 mLabel->setText( i18n( "No server configured yet" ) );
00567 else
00568 mLabel->setText( i18n( "1 server configured", "%n servers configured", mURLList.count() ) );
00569 }
00570
00571 #include "cryptoconfigmodule.moc"
00572 #include "cryptoconfigmodule_p.moc"