kaddressbook

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 
00088     // Iterating over a bitfield means *2 every time
00089     msgFormat *= 2;
00090   }
00091 
00092   QLabel* l = new QLabel( i18n( "Preferred OpenPGP encryption key:" ), this );
00093   topLayout->addWidget( l, 1, 0 );
00094 
00095   mPgpKey = 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 = new Kleo::EncryptionKeyRequester( true, Kleo::EncryptionKeyRequester::SMIME, this );
00102   topLayout->addWidget( mSmimeCert, 2, 1 );
00103 
00104   QGroupBox* box = new QVGroupBox( i18n( "Message Preference" ), this );
00105   topLayout->addMultiCellWidget( box, 3, 3, 0, 1 );
00106 
00107 
00108   // Send preferences/sign (see certmanager/lib/kleo/enum.h)
00109   QHBox* hbox = new QHBox( box );
00110 
00111   l = new QLabel( i18n( "Sign:" ), hbox );
00112 
00113   mSignPref = new QComboBox( false, hbox );
00114   for ( unsigned int i = Kleo::UnknownSigningPreference; i < Kleo::MaxSigningPreference ; ++i )
00115     mSignPref->insertItem( Kleo::signingPreferenceToLabel(
00116                            static_cast<Kleo::SigningPreference>( i ) ) );
00117 
00118   // Send preferences/encrypt (see certmanager/lib/kleo/enum.h)
00119   hbox = new QHBox( box );
00120 
00121   l = new QLabel( i18n("Encrypt:"), hbox );
00122 
00123   mCryptPref = new QComboBox( false, hbox );
00124   for ( unsigned int i = Kleo::UnknownPreference; i < Kleo::MaxEncryptionPreference ; ++i )
00125     mCryptPref->insertItem( Kleo::encryptionPreferenceToLabel(
00126                             static_cast<Kleo::EncryptionPreference>( i ) ) );
00127 
00128   // Emit "changed()" signal
00129   connect( mSignPref, SIGNAL( activated(int) ), this, SLOT( setModified() ) );
00130   connect( mCryptPref, SIGNAL( activated(int) ), this, SLOT( setModified() ) );
00131   // Not optimal, but KeyRequester doesn't emit any signals when the key changes
00132   connect( mPgpKey->eraseButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00133   connect( mPgpKey->dialogButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00134   connect( mSmimeCert->eraseButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00135   connect( mSmimeCert->dialogButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00136 }
00137 
00138 CryptoWidget::~CryptoWidget()
00139 {
00140 }
00141 
00142 void CryptoWidget::loadContact( KABC::Addressee *addr )
00143 {
00144   bool blocked = signalsBlocked();
00145   blockSignals( true );
00146 
00147   QStringList lst = QStringList::split( ',', addr->custom( "KADDRESSBOOK",
00148                                                            "CRYPTOPROTOPREF" ) );
00149   uint cryptoFormats = Kleo::stringListToCryptoMessageFormats( lst );
00150 
00151   uint msgFormat = 1;
00152   for ( uint i = 0 ; i < NumberOfProtocols ; ++i, msgFormat *= 2 ) {
00153     mProtocolCB[i]->setChecked( cryptoFormats & msgFormat );
00154   }
00155 
00156   mSignPref->setCurrentItem( Kleo::stringToSigningPreference( addr->custom( "KADDRESSBOOK",
00157                                                                             "CRYPTOSIGNPREF" ) ) );
00158   mCryptPref->setCurrentItem( Kleo::stringToEncryptionPreference( addr->custom( "KADDRESSBOOK",
00159                                                                                 "CRYPTOENCRYPTPREF" ) ) );
00160 
00161   // We dont use the contents of addr->key(...) because we want just a ref.
00162   // to the key/cert. stored elsewhere.
00163 
00164   mPgpKey->setFingerprints( QStringList::split( ",", addr->custom( "KADDRESSBOOK", "OPENPGPFP" ) ) );
00165   mSmimeCert->setFingerprints( QStringList::split( ",", addr->custom( "KADDRESSBOOK", "SMIMEFP" ) ) );
00166 
00167   blockSignals( blocked );
00168 }
00169 
00170 void CryptoWidget::storeContact( KABC::Addressee *addr )
00171 {
00172   uint cryptoFormats = 0;
00173   uint msgFormat = 1;
00174   for ( uint i = 0 ; i < NumberOfProtocols ; ++i, msgFormat *= 2 ) {
00175     if ( mProtocolCB[ i ]->isChecked() )
00176       cryptoFormats |= msgFormat;
00177   }
00178 
00179   QStringList lst = Kleo::cryptoMessageFormatsToStringList( cryptoFormats );
00180   if ( !lst.isEmpty() )
00181     addr->insertCustom( "KADDRESSBOOK", "CRYPTOPROTOPREF", lst.join( "," ) );
00182   else
00183     addr->removeCustom( "KADDRESSBOOK", "CRYPTOPROTOPREF" );
00184 
00185   Kleo::SigningPreference signPref =
00186       static_cast<Kleo::SigningPreference>( mSignPref->currentItem() );
00187   if ( signPref != Kleo::UnknownSigningPreference )
00188     addr->insertCustom( "KADDRESSBOOK", "CRYPTOSIGNPREF",
00189                         Kleo::signingPreferenceToString( signPref ) );
00190   else
00191     addr->removeCustom( "KADDRESSBOOK", "CRYPTOSIGNPREF" );
00192 
00193   Kleo::EncryptionPreference encryptPref =
00194       static_cast<Kleo::EncryptionPreference>( mCryptPref->currentItem() );
00195   if ( encryptPref != Kleo::UnknownPreference )
00196     addr->insertCustom( "KADDRESSBOOK", "CRYPTOENCRYPTPREF",
00197                         Kleo::encryptionPreferenceToString( encryptPref ) );
00198   else
00199     addr->removeCustom( "KADDRESSBOOK", "CRYPTOENCRYPTPREF" );
00200 
00201   QStringList pfp = mPgpKey->fingerprints();
00202   QStringList sfp = mSmimeCert->fingerprints();
00203 
00204   if ( !pfp.isEmpty() )
00205     addr->insertCustom( "KADDRESSBOOK", "OPENPGPFP", pfp.join( "," ) );
00206   else
00207     addr->removeCustom( "KADDRESSBOOK", "OPENPGPFP" );
00208 
00209   if ( !sfp.isEmpty() )
00210     addr->insertCustom( "KADDRESSBOOK", "SMIMEFP", sfp.join( "," ) );
00211   else
00212     addr->removeCustom( "KADDRESSBOOK", "SMIMEFP" );
00213 }
00214 
00215 void CryptoWidget::setReadOnly( bool readOnly )
00216 {
00217   mReadOnly = readOnly;
00218   for ( uint i = 0 ; i < NumberOfProtocols ; ++i )
00219     mProtocolCB[ i ]->setEnabled( !readOnly );
00220 
00221   mSignPref->setEnabled( !readOnly );
00222   mCryptPref->setEnabled( !readOnly );
00223   mPgpKey->setEnabled( !readOnly );
00224   mSmimeCert->setEnabled( !readOnly );
00225 }
00226 
00227 #include "cryptowidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys