38#include "backendconfigwidget.h" 
   39#include "cryptoconfigdialog.h" 
   41#include "kleo/cryptobackendfactory.h" 
   42#include "ui/keylistview.h"  
   44#include <tdelistview.h> 
   48#include <tdemessagebox.h> 
   49#include <tdeapplication.h> 
   50#include <dcopclient.h> 
   52#include <tqpushbutton.h> 
   60  class BackendListView;
 
   63class Kleo::BackendConfigWidget::Private {
 
   65  Kleo::BackendListView * listView;
 
   66  TQPushButton * configureButton;
 
   67  TQPushButton * rescanButton;
 
   68  Kleo::CryptoBackendFactory * backendFactory;
 
   72  class BackendListViewItem;
 
   73  class ProtocolCheckListItem;
 
   76class Kleo::BackendListView : 
public TDEListView
 
   79  BackendListView( BackendConfigWidget* parent, 
const char* name = 0 )
 
   80    : TDEListView( parent, name ) {}
 
   83  const Kleo::CryptoBackend* currentBackend() 
const;
 
   86  const Kleo::CryptoBackend* chosenBackend( 
const char * protocol );
 
   89  void deselectAll( 
const char * protocol, TQCheckListItem* except );
 
   91  void emitChanged() { 
static_cast<BackendConfigWidget *
>( parentWidget() )->emitChanged( 
true ); }
 
   95class Kleo::BackendListViewItem : 
public TQListViewItem
 
   98  BackendListViewItem( TDEListView* lv, TQListViewItem *prev, 
const CryptoBackend *cryptoBackend )
 
   99    : TQListViewItem( lv, prev, cryptoBackend->displayName() ), mCryptoBackend( cryptoBackend )
 
  102  const CryptoBackend *cryptoBackend()
 const { 
return mCryptoBackend; }
 
  103  enum { RTTI = 0x2EAE3BE0, RTTI_MASK = 0xFFFFFFFF };
 
  104  int rtti()
 const { 
return RTTI; }
 
  107  const CryptoBackend *mCryptoBackend;
 
  113class Kleo::ProtocolCheckListItem : 
public TQCheckListItem
 
  116  ProtocolCheckListItem( BackendListViewItem* blvi,
 
  117                         TQListViewItem* prev, 
const char * protocolName,
 
  118                         const CryptoBackend::Protocol* protocol ) 
 
  119    : TQCheckListItem( blvi, prev, itemText( protocolName, protocol ),
 
  120                      TQCheckListItem::CheckBox ),
 
  121      mProtocol( protocol ), mProtocolName( protocolName )
 
  124  enum { RTTI = 0x2EAE3BE1, RTTI_MASK = 0xFFFFFFFF };
 
  125  virtual int rtti()
 const { 
return RTTI; }
 
  128  const CryptoBackend::Protocol* protocol()
 const { 
return mProtocol; }
 
  129  const char * protocolName()
 const { 
return mProtocolName; }
 
  132  virtual void stateChange( 
bool b ) {
 
  133    BackendListView* lv = 
static_cast<BackendListView *
>( listView() );
 
  136      lv->deselectAll( mProtocolName, 
this );
 
  138    TQCheckListItem::stateChange( b );
 
  143  static TQString itemText( 
const char * protocolName, 
const CryptoBackend::Protocol* protocol ) {
 
  145    const TQString protoName = tqstricmp( protocolName, 
"openpgp" ) != 0
 
  146                              ? tqstricmp( protocolName, 
"smime" ) != 0
 
  147                              ? TQString::fromLatin1( protocolName )
 
  151    const TQString impName = protocol ? protocol->displayName() : i18n( 
"failed" );
 
  152    return i18n( 
"Items in Kleo::BackendConfigWidget listview (1: protocol; 2: implementation name)",
 
  153                 "%1 (%2)" ).arg( protoName, impName );
 
  156  const CryptoBackend::Protocol* mProtocol; 
 
  157  const char * mProtocolName;
 
  160const Kleo::CryptoBackend* Kleo::BackendListView::currentBackend()
 const {
 
  161  const TQListViewItem* curItem = currentItem();
 
  164  if ( lvi_cast<Kleo::ProtocolCheckListItem>( curItem ) )
 
  165    curItem = curItem->parent();
 
  166  if ( 
const Kleo::BackendListViewItem * blvi = lvi_cast<Kleo::BackendListViewItem>( curItem ) )
 
  167    return blvi->cryptoBackend();
 
  172const Kleo::CryptoBackend* Kleo::BackendListView::chosenBackend( 
const char * protocolName )
 
  174  for ( TQListViewItemIterator it( 
this  ) ;
 
  175        it.current() ; ++it )
 
  176    if ( ProtocolCheckListItem * p = lvi_cast<ProtocolCheckListItem>( it.current() ) )
 
  177      if ( p->isOn() && tqstricmp( p->protocolName(), protocolName ) == 0 ) {
 
  180        if ( 
const BackendListViewItem * parItem = lvi_cast<BackendListViewItem>( it.current()->parent() ) )
 
  181          return parItem->cryptoBackend();
 
  186void Kleo::BackendListView::deselectAll( 
const char * protocolName, TQCheckListItem* except )
 
  188  for ( TQListViewItemIterator it( 
this  ) ;
 
  189        it.current() ; ++it ) {
 
  190    if ( it.current() == except ) 
continue;
 
  191    if ( ProtocolCheckListItem * p = lvi_cast<ProtocolCheckListItem>( it.current() ) )
 
  192      if ( p->isOn() && tqstricmp( p->protocolName(), protocolName ) == 0 )
 
  199Kleo::BackendConfigWidget::BackendConfigWidget( CryptoBackendFactory * factory, TQWidget * parent, 
const char * name, WFlags f )
 
  200  : TQWidget( parent, name, f ), d( 0 )
 
  204  d->backendFactory = factory;
 
  206  TQHBoxLayout * hlay =
 
  207    new TQHBoxLayout( 
this, 0, KDialog::spacingHint() );
 
  209  d->listView = 
new BackendListView( 
this, 
"d->listView" );
 
  210  d->listView->addColumn( i18n(
"Available Backends") );
 
  211  d->listView->setAllColumnsShowFocus( 
true );
 
  212  d->listView->setSorting( -1 );
 
  213  d->listView->header()->setClickEnabled( 
false );
 
  214  d->listView->setFullWidth( 
true );
 
  216  hlay->addWidget( d->listView, 1 );
 
  218  connect( d->listView, TQ_SIGNAL(selectionChanged(TQListViewItem*)),
 
  219       TQ_SLOT(slotSelectionChanged(TQListViewItem*)) );
 
  221  TQVBoxLayout * vlay = 
new TQVBoxLayout( hlay ); 
 
  223  d->configureButton = 
new TQPushButton( i18n(
"Confi&gure..."), 
this );
 
  224  d->configureButton->setAutoDefault( 
false );
 
  225  vlay->addWidget( d->configureButton );
 
  227  connect( d->configureButton, TQ_SIGNAL(clicked()),
 
  228       TQ_SLOT(slotConfigureButtonClicked()) );
 
  230  d->rescanButton = 
new TQPushButton( i18n(
"Rescan"), 
this );
 
  231  d->rescanButton->setAutoDefault( 
false );
 
  232  vlay->addWidget( d->rescanButton );
 
  234  connect( d->rescanButton, TQ_SIGNAL(clicked()),
 
  235       TQ_SLOT(slotRescanButtonClicked()) );
 
  237  vlay->addStretch( 1 );
 
  240Kleo::BackendConfigWidget::~BackendConfigWidget() {
 
  244void Kleo::BackendConfigWidget::load() {
 
  245  d->listView->clear();
 
  247  unsigned int backendCount = 0;
 
  250  BackendListViewItem * top = 0;
 
  251  for ( 
unsigned int i = 0 ; 
const CryptoBackend * b = d->backendFactory->backend( i ) ; ++i ) {
 
  253    top = 
new Kleo::BackendListViewItem( d->listView, top, b );
 
  255    ProtocolCheckListItem * last = 0;
 
  256    for ( 
int i = 0 ; 
const char * name = b->enumerateProtocols( i ) ; ++i ) {
 
  257      const CryptoBackend::Protocol * protocol = b->protocol( name );
 
  260        last = 
new ProtocolCheckListItem( top, last, name, protocol );
 
  261        last->setOn( protocol == d->backendFactory->protocol( name ) );
 
  262      } 
else if ( b->supportsProtocol( name ) ) {
 
  263        last = 
new ProtocolCheckListItem( top, last, name, 0 );
 
  264        last->setOn( 
false );
 
  265        last->setEnabled( 
false );
 
  269    top->setOpen( 
true );
 
  273  if ( backendCount ) {
 
  274    d->listView->setCurrentItem( d->listView->firstChild() );
 
  275    d->listView->setSelected( d->listView->firstChild(), 
true );
 
  278  slotSelectionChanged( d->listView->firstChild() );
 
  281void Kleo::BackendConfigWidget::slotSelectionChanged( TQListViewItem * ) {
 
  282  const CryptoBackend* backend = d->listView->currentBackend();
 
  283  if ( backend && !backend->config() )
 
  284    kdDebug(5150) << 
"Backend w/o config object!" << endl;
 
  285  d->configureButton->setEnabled( backend && backend->config() );
 
  289void Kleo::BackendConfigWidget::slotRescanButtonClicked() {
 
  290  TQStringList reasons;
 
  291  d->backendFactory->scanForBackends( &reasons );
 
  292  if ( !reasons.empty() )
 
  293    KMessageBox::informationList( 
this,
 
  294                  i18n(
"The following problems where encountered during scanning:"),
 
  295                  reasons, i18n(
"Scan Results") );
 
  297  emit changed( 
true );
 
  300void Kleo::BackendConfigWidget::slotConfigureButtonClicked() {
 
  301  const CryptoBackend* backend = d->listView->currentBackend();
 
  302  if ( backend && backend->config() ) {
 
  304    int result = dlg.exec();
 
  305    if ( result == TQDialog::Accepted ) {
 
  307      tdeApp->dcopClient()->emitDCOPSignal( 
"KPIM::CryptoConfig", 
"changed()", TQByteArray() );
 
  309      TQTimer::singleShot( 0, 
this, TQ_SLOT(slotRescanButtonClicked()) );
 
  313    kdWarning(5150) << 
"Can't configure backend, no config object available" << endl;
 
  316void Kleo::BackendConfigWidget::save()
 const {
 
  317  for ( 
int i = 0 ; 
const char * name = d->backendFactory->enumerateProtocols( i ) ; ++i )
 
  318    d->backendFactory->setProtocolBackend( name,  d->listView->chosenBackend( name ) );
 
  321void Kleo::BackendConfigWidget::virtual_hook( 
int, 
void* ) {}
 
  323#include "backendconfigwidget.moc" 
Simple KDialogBase wrapper around CryptoConfigModule.