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