kaddressbook Library API Documentation

cryptowidget.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2004 Klar�vdalens Datakonsult AB
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <config.h>
00025 #include "certmanager/lib/ui/keyrequester.h"
00026 #include "certmanager/lib/cryptplugfactory.h"
00027 #include "certmanager/lib/cryptplugwrapper.h"
00028 #include "certmanager/lib/kleo/enum.h"
00029 
00030 #include "gpgmepp/data.h"
00031 #include "gpgmepp/key.h"
00032 
00033 #include <kdebug.h>
00034 #include <kdialog.h>
00035 #include <kiconloader.h>
00036 #include <klocale.h>
00037 
00038 #include <qlayout.h>
00039 #include <qlabel.h>
00040 #include <qcheckbox.h>
00041 #include <qcombobox.h>
00042 #include <qpushbutton.h>
00043 #include <qvgroupbox.h>
00044 #include <qhbox.h>
00045 
00046 #include "cryptowidget.h"
00047 
00048 extern "C" {
00049   void *init_libkaddrbk_cryptosettings()
00050   {
00051     return ( new CryptoWidgetFactory );
00052   }
00053 }
00054 
00055 CryptoWidgetFactory::CryptoWidgetFactory()
00056 {
00057   KGlobal::locale()->insertCatalogue( "libkleopatra" );
00058   KGlobal::iconLoader()->addAppDir( "libkleopatra" );
00059 }
00060 
00061 QString CryptoWidgetFactory::pageTitle() const
00062 {
00063   return i18n( "Crypto Settings" );
00064 }
00065 
00066 QString CryptoWidgetFactory::pageIdentifier() const
00067 {
00068   return "crypto";
00069 }
00070 
00071 CryptoWidget::CryptoWidget( KABC::AddressBook *ab, QWidget *parent, const char *name )
00072   : KAB::ContactEditorWidget( ab, parent, name ), mReadOnly( false )
00073 {
00074   QGridLayout *topLayout = new QGridLayout( this, 2, 5, KDialog::marginHint(),
00075                                             KDialog::spacingHint() );
00076   topLayout->setColStretch( 1, 1 );
00077   topLayout->setRowStretch( 4, 1 );
00078 
00079   QVGroupBox* protGB = new QVGroupBox( i18n("Allowed Protocols"), this );
00080   topLayout->addMultiCellWidget( protGB,0,0,0,1 );
00081 
00082   uint msgFormat = 1;
00083   for ( uint i = 0 ; i < NumberOfProtocols ; ++i ) {
00084       Kleo::CryptoMessageFormat f = static_cast<Kleo::CryptoMessageFormat>( msgFormat );
00085       mProtocolCB[i] = new QCheckBox( Kleo::cryptoMessageFormatToLabel( f ), protGB );
00086       connect( mProtocolCB[i], SIGNAL( clicked() ), this, SLOT( setModified() ) );
00087       // Iterating over a bitfield means *2 every time
00088       msgFormat *= 2;
00089   }
00090 
00091   QLabel* l = new QLabel( i18n("Preferred OpenPGP encryption key:"), this );
00092   topLayout->addWidget( l,1,0 );
00093 
00094   mPgpKey =
00095     new Kleo::EncryptionKeyRequester( true, Kleo::EncryptionKeyRequester::OpenPGP, this );
00096   topLayout->addWidget( mPgpKey,1,1 );
00097 
00098   l = new QLabel( i18n("Preferred S/MIME encryption certificate:"), this );
00099   topLayout->addWidget( l,2,0 );
00100 
00101   mSmimeCert =
00102     new Kleo::EncryptionKeyRequester( true, Kleo::EncryptionKeyRequester::SMIME, this );
00103   topLayout->addWidget( mSmimeCert,2,1 );
00104 
00105   QGroupBox* box = new QVGroupBox( i18n("Message Preference"), this );
00106   topLayout->addMultiCellWidget( box, 3,3,0,1 );
00107 
00108 
00109   //send preferences/sign (see certmanager/lib/kleo/enum.h)
00110   QHBox* hbox = new QHBox(box);
00111 
00112   l = new QLabel( i18n("Sign:"), hbox );
00113 
00114   mSignPref = new QComboBox( false, hbox );
00115   for ( unsigned int i = Kleo::UnknownSigningPreference ;
00116         i < Kleo::MaxSigningPreference ; ++i )
00117       mSignPref->insertItem( Kleo::signingPreferenceToLabel(
00118                                  static_cast<Kleo::SigningPreference>( i ) ) );
00119 
00120   //send preferences/encrypt (see certmanager/lib/kleo/enum.h)
00121   hbox = new QHBox(box);
00122 
00123   l = new QLabel( i18n("Encrypt:"), hbox );
00124 
00125   mCryptPref = new QComboBox( false, hbox );
00126   for ( unsigned int i = Kleo::UnknownPreference ;
00127         i < Kleo::MaxEncryptionPreference ; ++i )
00128       mCryptPref->insertItem(
00129           Kleo::encryptionPreferenceToLabel( static_cast<Kleo::EncryptionPreference>( i ) ) );
00130 
00131   // Emit "changed()" signal
00132   connect( mSignPref, SIGNAL( activated(int) ), this, SLOT( setModified() ) );
00133   connect( mCryptPref, SIGNAL( activated(int) ), this, SLOT( setModified() ) );
00134   // Not optimal, but KeyRequester doesn't emit any signals when the key changes
00135   connect( mPgpKey->eraseButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00136   connect( mPgpKey->dialogButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00137   connect( mSmimeCert->eraseButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00138   connect( mSmimeCert->dialogButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00139 }
00140 
00141 CryptoWidget::~CryptoWidget()
00142 {
00143 }
00144 
00145 void CryptoWidget::loadContact( KABC::Addressee *addr )
00146 {
00147   bool blocked = signalsBlocked();
00148   blockSignals( true );
00149 
00150   QStringList lst = QStringList::split( ',', addr->custom( "KADDRESSBOOK",
00151                                                            "CRYPTOPROTOPREF" ) );
00152   uint cryptoFormats = Kleo::stringListToCryptoMessageFormats( lst );
00153 
00154   uint msgFormat = 1;
00155   for ( uint i = 0 ; i < NumberOfProtocols ; ++i, msgFormat *= 2 ) {
00156       mProtocolCB[i]->setChecked( cryptoFormats & msgFormat );
00157   }
00158 
00159   mSignPref->setCurrentItem( Kleo::stringToSigningPreference(addr->custom( "KADDRESSBOOK",
00160                                                                            "CRYPTOSIGNPREF" )) );
00161   mCryptPref->setCurrentItem( Kleo::stringToEncryptionPreference(addr->custom( "KADDRESSBOOK",
00162                                                                                "CRYPTOENCRYPTPREF" )) );
00163 
00164   // We dont use the contents of addr->key(...) because we want just a ref.
00165   // to the key/cert. stored elsewhere.
00166 
00167   mPgpKey->setFingerprints( QStringList::split( ",", addr->custom( "KADDRESSBOOK", "OPENPGPFP" ) ) );
00168   mSmimeCert->setFingerprints( QStringList::split( ",", addr->custom( "KADDRESSBOOK", "SMIMEFP" ) ) );
00169 
00170   blockSignals( blocked );
00171 }
00172 
00173 void CryptoWidget::storeContact( KABC::Addressee *addr )
00174 {
00175   uint cryptoFormats = 0;
00176   uint msgFormat = 1;
00177   for ( uint i = 0 ; i < NumberOfProtocols ; ++i, msgFormat *= 2 ) {
00178       if ( mProtocolCB[i]->isChecked() )
00179           cryptoFormats |= msgFormat;
00180   }
00181   QStringList lst = Kleo::cryptoMessageFormatsToStringList(cryptoFormats);
00182 
00183   if ( !lst.isEmpty() )
00184       addr->insertCustom( "KADDRESSBOOK", "CRYPTOPROTOPREF", lst.join( "," ) );
00185   else
00186       addr->removeCustom( "KADDRESSBOOK", "CRYPTOPROTOPREF" );
00187 
00188   Kleo::SigningPreference signPref =
00189       static_cast<Kleo::SigningPreference>( mSignPref->currentItem() );
00190   if ( signPref != Kleo::UnknownSigningPreference )
00191       addr->insertCustom( "KADDRESSBOOK", "CRYPTOSIGNPREF",
00192                           Kleo::signingPreferenceToString( signPref ) );
00193   else
00194       addr->removeCustom( "KADDRESSBOOK", "CRYPTOSIGNPREF" );
00195 
00196   Kleo::EncryptionPreference encryptPref =
00197       static_cast<Kleo::EncryptionPreference>( mCryptPref->currentItem() );
00198   if ( encryptPref != Kleo::UnknownPreference )
00199       addr->insertCustom( "KADDRESSBOOK", "CRYPTOENCRYPTPREF",
00200                           Kleo::encryptionPreferenceToString( encryptPref ) );
00201   else
00202       addr->removeCustom( "KADDRESSBOOK", "CRYPTOENCRYPTPREF" );
00203 
00204   QStringList pfp = mPgpKey->fingerprints();
00205   QStringList sfp = mSmimeCert->fingerprints();
00206 
00207   if( !pfp.isEmpty() ) {
00208     addr->insertCustom( "KADDRESSBOOK", "OPENPGPFP", pfp.join( "," ) );
00209   } else {
00210     addr->removeCustom( "KADDRESSBOOK", "OPENPGPFP" );
00211   }
00212 
00213   if( !sfp.isEmpty() ) {
00214     addr->insertCustom( "KADDRESSBOOK", "SMIMEFP", sfp.join( "," ) );
00215   } else {
00216     addr->removeCustom( "KADDRESSBOOK", "SMIMEFP" );
00217   }
00218 
00219 }
00220 
00221 void CryptoWidget::setReadOnly( bool readOnly )
00222 {
00223   mReadOnly = readOnly;
00224   for ( uint i = 0 ; i < NumberOfProtocols ; ++i )
00225       mProtocolCB[i]->setEnabled( !readOnly );
00226   mSignPref->setEnabled( !readOnly );
00227   mCryptPref->setEnabled( !readOnly );
00228   mPgpKey->setEnabled( !readOnly );
00229   mSmimeCert->setEnabled( !readOnly );
00230 }
00231 
00232 #include "cryptowidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:42:46 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003