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
00033
00034
#ifdef HAVE_CONFIG_H
00035
#include <config.h>
00036
#endif
00037
00038
#include "backendconfigwidget.h"
00039
#include "cryptoconfigdialog.h"
00040
00041
#include "kleo/cryptobackendfactory.h"
00042
00043
#include <klistview.h>
00044
#include <kdialog.h>
00045
#include <klocale.h>
00046
#include <kdebug.h>
00047
#include <kmessagebox.h>
00048
00049
#include <qpushbutton.h>
00050
#include <qlayout.h>
00051
#include <qheader.h>
00052
00053
#include <assert.h>
00054
00055
namespace Kleo {
00056
class BackendListView;
00057 }
00058
00059
class Kleo::BackendConfigWidget::Private {
00060
public:
00061 Kleo::BackendListView * listView;
00062
QPushButton * configureButton;
00063
QPushButton * rescanButton;
00064 Kleo::CryptoBackendFactory * backendFactory;
00065 };
00066
00067
namespace Kleo {
00068
class BackendListViewItem;
00069
class ProtocolCheckListItem;
00070
enum ProtocolType { OpenPGP, SMIME };
00071 }
00072
00073
class Kleo::BackendListView :
public KListView
00074 {
00075
public:
00076 BackendListView( BackendConfigWidget* parent,
const char* name = 0 )
00077 : KListView( parent, name ) {}
00078
00080
const Kleo::CryptoBackend* currentBackend() const;
00081
00083 const Kleo::CryptoBackend* chosenBackend( ProtocolType protocolType );
00084
00086
void deselectAll( ProtocolType protocolType,
QCheckListItem* except );
00087
00088
void emitChanged() { static_cast<BackendConfigWidget *>( parentWidget() )->emitChanged(
true ); }
00089 };
00090
00091
00092
class Kleo::BackendListViewItem :
public QListViewItem
00093 {
00094
public:
00095 BackendListViewItem( KListView* lv,
QListViewItem *prev,
const CryptoBackend *cryptoBackend )
00096 :
QListViewItem( lv, prev, cryptoBackend->displayName() ), mCryptoBackend( cryptoBackend )
00097 {}
00098
00099
const CryptoBackend *cryptoBackend()
const {
return mCryptoBackend; }
00100
static const int RTTI = 20001;
00101
virtual int rtti()
const {
return RTTI; }
00102
00103
private:
00104
const CryptoBackend *mCryptoBackend;
00105 };
00106
00107
00108
00109
00110
class Kleo::ProtocolCheckListItem :
public QCheckListItem
00111 {
00112
public:
00113 ProtocolCheckListItem( BackendListViewItem* blvi,
00114
QListViewItem* prev,
00115 ProtocolType protocolType,
00116
const CryptoBackend::Protocol* protocol )
00117 :
QCheckListItem( blvi, prev, itemText( protocolType, protocol ),
00118
QCheckListItem::CheckBox ),
00119 mProtocol( protocol ), mProtocolType( protocolType )
00120 {}
00121
00122
static const int RTTI = 20002;
00123
virtual int rtti()
const {
return RTTI; }
00124
00125
00126
const CryptoBackend::Protocol* protocol()
const {
return mProtocol; }
00127 ProtocolType protocolType()
const {
return mProtocolType; }
00128
00129
protected:
00130
virtual void stateChange(
bool b ) {
00131 BackendListView* lv = static_cast<BackendListView *>( listView() );
00132
00133
if ( b )
00134 lv->deselectAll( mProtocolType,
this );
00135 lv->emitChanged();
00136 QCheckListItem::stateChange( b );
00137 }
00138
00139
private:
00140
00141
static QString itemText( ProtocolType protocolType,
const CryptoBackend::Protocol* protocol ) {
00142
00143
QString protoTypeName = protocolType == OpenPGP ? i18n(
"OpenPGP" ) : i18n(
"S/MIME" );
00144
00145
QString impName = protocol ? protocol->displayName() : i18n(
"failed" );
00146
return QString(
"%1 (%2)" ).arg( protoTypeName ).arg( impName );
00147 }
00148
00149
const CryptoBackend::Protocol* mProtocol;
00150 ProtocolType mProtocolType;
00151 };
00152
00153
const Kleo::CryptoBackend* Kleo::BackendListView::currentBackend()
const {
00154
QListViewItem* curItem = currentItem();
00155
if ( !curItem )
00156
return 0;
00157
if ( curItem->rtti() == Kleo::ProtocolCheckListItem::RTTI )
00158 curItem = curItem->parent();
00159
if ( curItem && curItem->rtti() == Kleo::BackendListViewItem::RTTI )
00160
return static_cast<Kleo::BackendListViewItem *>( curItem )->cryptoBackend();
00161
return 0;
00162 }
00163
00164
00165
const Kleo::CryptoBackend* Kleo::BackendListView::chosenBackend( ProtocolType protocolType )
00166 {
00167
QListViewItemIterator it(
this );
00168
for ( ; it.current() ; ++it ) {
00169
if( it.current()->rtti() == Kleo::ProtocolCheckListItem::RTTI ) {
00170 Kleo::ProtocolCheckListItem* p = static_cast<Kleo::ProtocolCheckListItem *>( it.current() );
00171
if ( p->isOn() && p->protocolType() == protocolType ) {
00172
00173
00174
QListViewItem* parItem = it.current()->parent();
00175
if ( parItem && parItem->rtti() == Kleo::BackendListViewItem::RTTI )
00176
return static_cast<Kleo::BackendListViewItem *>( parItem )->cryptoBackend();
00177 }
00178 }
00179 }
00180
return 0;
00181 }
00182
00183
void Kleo::BackendListView::deselectAll( ProtocolType protocolType,
QCheckListItem* except )
00184 {
00185
QListViewItemIterator it(
this );
00186
for ( ; it.current() ; ++it ) {
00187
if( it.current() != except &&
00188 it.current()->rtti() == Kleo::ProtocolCheckListItem::RTTI ) {
00189 Kleo::ProtocolCheckListItem* p = static_cast<Kleo::ProtocolCheckListItem *>( it.current() );
00190
if ( p->isOn() && p->protocolType() == protocolType )
00191 p->setOn(
false );
00192 }
00193 }
00194 }
00195
00197
00198 Kleo::BackendConfigWidget::BackendConfigWidget( CryptoBackendFactory * factory,
QWidget * parent,
const char * name, WFlags f )
00199 :
QWidget( parent, name, f ), d( 0 )
00200 {
00201 assert( factory );
00202 d =
new Private();
00203 d->backendFactory = factory;
00204
00205
QHBoxLayout * hlay =
00206
new QHBoxLayout(
this, 0, KDialog::spacingHint() );
00207
00208 d->listView =
new BackendListView(
this,
"d->listView" );
00209 d->listView->addColumn( i18n(
"Available Backends") );
00210 d->listView->setAllColumnsShowFocus(
true );
00211 d->listView->setSorting( -1 );
00212 d->listView->header()->setClickEnabled(
false );
00213 d->listView->setFullWidth(
true );
00214
00215 hlay->addWidget( d->listView, 1 );
00216
00217 connect( d->listView, SIGNAL(selectionChanged(
QListViewItem*)),
00218 SLOT(slotSelectionChanged(
QListViewItem*)) );
00219
00220
QVBoxLayout * vlay =
new QVBoxLayout( hlay );
00221
00222 d->configureButton =
new QPushButton( i18n(
"Confi&gure..."),
this );
00223 d->configureButton->setAutoDefault(
false );
00224 vlay->addWidget( d->configureButton );
00225
00226 connect( d->configureButton, SIGNAL(clicked()),
00227 SLOT(slotConfigureButtonClicked()) );
00228
00229 d->rescanButton =
new QPushButton( i18n(
"Rescan"),
this );
00230 d->rescanButton->setAutoDefault(
false );
00231 vlay->addWidget( d->rescanButton );
00232
00233 connect( d->rescanButton, SIGNAL(clicked()),
00234 SLOT(slotRescanButtonClicked()) );
00235
00236 vlay->addStretch( 1 );
00237 }
00238
00239 Kleo::BackendConfigWidget::~BackendConfigWidget() {
00240
delete d; d = 0;
00241 }
00242
00243
void Kleo::BackendConfigWidget::load() {
00244 d->listView->clear();
00245
00246
unsigned int backendCount = 0;
00247
00248
00249 BackendListViewItem * top = 0;
00250
for (
unsigned int i = 0 ;
const CryptoBackend * b = d->backendFactory->backend( i ) ; ++i ) {
00251
const CryptoBackend::Protocol * openpgp = b->openpgp();
00252
const CryptoBackend::Protocol * smime = b->smime();
00253
00254 top =
new Kleo::BackendListViewItem( d->listView, top, b );
00255 ProtocolCheckListItem * last = 0;
00256
if ( openpgp ) {
00257 last =
new ProtocolCheckListItem( top, last, Kleo::OpenPGP, openpgp );
00258 last->setOn( openpgp == d->backendFactory->openpgp() );
00259 }
else if ( b->supportsOpenPGP() ) {
00260 last =
new ProtocolCheckListItem( top, last, Kleo::OpenPGP, 0 );
00261 last->setOn(
false );
00262 last->setEnabled(
false );
00263 }
00264
if ( smime ) {
00265 last =
new ProtocolCheckListItem( top, last, Kleo::SMIME, smime );
00266 last->setOn( smime == d->backendFactory->smime() );
00267 }
else if ( b->supportsSMIME() ) {
00268 last =
new ProtocolCheckListItem( top, last, Kleo::SMIME, 0 );
00269 last->setOn(
false );
00270 last->setEnabled(
false );
00271 }
00272 top->setOpen(
true );
00273
00274 ++backendCount;
00275 }
00276
00277
if ( backendCount ) {
00278 d->listView->setCurrentItem( d->listView->firstChild() );
00279 d->listView->setSelected( d->listView->firstChild(),
true );
00280 }
00281
00282 slotSelectionChanged( d->listView->firstChild() );
00283 }
00284
00285
void Kleo::BackendConfigWidget::slotSelectionChanged(
QListViewItem * ) {
00286
const CryptoBackend* backend = d->listView->currentBackend();
00287 d->configureButton->setEnabled( backend && backend->config() );
00288 }
00289
00290
00291
void Kleo::BackendConfigWidget::slotRescanButtonClicked() {
00292
QStringList reasons;
00293 d->backendFactory->scanForBackends( &reasons );
00294
if ( !reasons.empty() )
00295 KMessageBox::informationList(
this,
00296 i18n(
"The following problems where encountered during scanning:"),
00297 reasons, i18n(
"Scan Results") );
00298 load();
00299 emit changed(
true );
00300 }
00301
00302
void Kleo::BackendConfigWidget::slotConfigureButtonClicked() {
00303
const CryptoBackend* backend = d->listView->currentBackend();
00304
if ( backend && backend->config() ) {
00305
Kleo::CryptoConfigDialog dlg( backend->config() );
00306 dlg.exec();
00307 }
00308
else
00309 kdWarning(5150) <<
"Can't configure backend, no config object available" << endl;
00310 }
00311
00312
void Kleo::BackendConfigWidget::save()
const {
00313 d->backendFactory->setSMIMEBackend( d->listView->chosenBackend( Kleo::SMIME ) );
00314 d->backendFactory->setOpenPGPBackend( d->listView->chosenBackend( Kleo::OpenPGP ) );
00315 }
00316
00317
void Kleo::BackendConfigWidget::virtual_hook(
int,
void* ) {}
00318
00319
#include "backendconfigwidget.moc"