certmanager Library API Documentation

backendconfigwidget.cpp

00001 /* -*- c++ -*- 00002 backendconfigwidget.cpp 00003 00004 This file is part of libkleopatra, the KDE keymanagement library 00005 Copyright (c) 2002,2004 Klarälvdalens Datakonsult AB 00006 Copyright (c) 2002,2003 Marc Mutz <mutz@kde.org> 00007 00008 Libkleopatra is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU General Public License as 00010 published by the Free Software Foundation; either version 2 of the 00011 License, or (at your option) any later version. 00012 00013 Libkleopatra is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 00022 In addition, as a special exception, the copyright holders give 00023 permission to link the code of this program with any edition of 00024 the Qt library by Trolltech AS, Norway (or with modified versions 00025 of Qt that use the same license as Qt), and distribute linked 00026 combinations including the two. You must obey the GNU General 00027 Public License in all respects for all of the code used other than 00028 Qt. If you modify this file, you may extend this exception to 00029 your version of the file, but you are not obligated to do so. If 00030 you do not wish to do so, delete this exception statement from 00031 your version. 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 // Toplevel listviewitem for a given backend (e.g. "GpgME", "Kgpg/gpg v2") 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 // Checklist item under a BackendListViewItem 00109 // (e.g. "GpgME supports protocol OpenPGP") 00110 class Kleo::ProtocolCheckListItem : public QCheckListItem 00111 { 00112 public: 00113 ProtocolCheckListItem( BackendListViewItem* blvi, 00114 QListViewItem* prev, 00115 ProtocolType protocolType, 00116 const CryptoBackend::Protocol* protocol ) // can be 0 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 // can be 0 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 // "radio-button-like" behavior for the protocol checkboxes 00133 if ( b ) 00134 lv->deselectAll( mProtocolType, this ); 00135 lv->emitChanged(); 00136 QCheckListItem::stateChange( b ); 00137 } 00138 00139 private: 00140 // Helper for the constructor. 00141 static QString itemText( ProtocolType protocolType, const CryptoBackend::Protocol* protocol ) { 00142 // First one is the generic name (OpenPGP, SMIME) 00143 QString protoTypeName = protocolType == OpenPGP ? i18n( "OpenPGP" ) : i18n( "S/MIME" ); 00144 // second one is implementation name (gpg, gpgsm...) 00145 QString impName = protocol ? protocol->displayName() : i18n( "failed" ); 00146 return QString( "%1 (%2)" ).arg( protoTypeName ).arg( impName ); 00147 } 00148 00149 const CryptoBackend::Protocol* mProtocol; // can be 0 00150 ProtocolType mProtocolType; 00151 }; 00152 00153 const Kleo::CryptoBackend* Kleo::BackendListView::currentBackend() const { 00154 QListViewItem* curItem = currentItem(); 00155 if ( !curItem ) // can't happen 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 // can't be const method due to QListViewItemIterator (why?) 00165 const Kleo::CryptoBackend* Kleo::BackendListView::chosenBackend( ProtocolType protocolType ) 00166 { 00167 QListViewItemIterator it( this /*, QListViewItemIterator::Checked doesn't work*/ ); 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 // OK that's the one. Now go up to the parent backend 00173 // (need to do that in the listview since Protocol doesn't know it) 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 /*, QListViewItemIterator::Checked doesn't work*/ ); 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 ); // inherits spacing 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 // populate the plugin list: 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 // shouldn't happen, button is disabled 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"
KDE Logo
This file is part of the documentation for certmanager Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 21 19:46:22 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003