• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kaddressbook

cryptowidget.cpp

Go to the documentation of this file.
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 "libkleo/ui/keyrequester.h"
00025 #include "libkleo/kleo/cryptplugfactory.h"
00026 #include "libkleo/kleo/cryptplugwrapper.h"
00027 #include "libkleo/kleo/enum.h"
00028 
00029 #include "gpgme++/data.h"
00030 #include "gpgme++/key.h"
00031 
00032 #include <kdebug.h>
00033 #include <kdialog.h>
00034 #include <khbox.h>
00035 #include <kiconloader.h>
00036 #include <klocale.h>
00037 
00038 #include <QtGui/QCheckBox>
00039 #include <QtGui/QComboBox>
00040 #include <QtGui/QGridLayout>
00041 #include <QtGui/QGroupBox>
00042 #include <QtGui/QLabel>
00043 #include <QtGui/QLayout>
00044 #include <QtGui/QPushButton>
00045 
00046 #include "cryptowidget.h"
00047 
00048 K_EXPORT_PLUGIN(CryptoWidgetFactory)
00049 
00050 CryptoWidgetFactory::CryptoWidgetFactory()
00051 {
00052   KGlobal::locale()->insertCatalog( "libkleopatra" );
00053   KIconLoader::global()->addAppDir( "libkleopatra" );
00054   KIconLoader::global()->addAppDir( "kdepim" );
00055 }
00056 
00057 QString CryptoWidgetFactory::pageTitle() const
00058 {
00059   return i18n( "Crypto Settings" );
00060 }
00061 
00062 QString CryptoWidgetFactory::pageIdentifier() const
00063 {
00064   return "crypto";
00065 }
00066 
00067 CryptoWidget::CryptoWidget( KABC::AddressBook *ab, QWidget *parent )
00068   : KAB::ContactEditorWidget( ab, parent ), mReadOnly( false )
00069 {
00070   QGridLayout *topLayout = new QGridLayout( this );
00071   topLayout->setSpacing( KDialog::spacingHint() );
00072   topLayout->setMargin( KDialog::marginHint() );
00073   topLayout->setColumnStretch( 1, 1 );
00074   topLayout->setRowStretch( 4, 1 );
00075 
00076   QGroupBox* protGB = new QGroupBox( i18n( "Allowed Protocols" ), this );
00077   QLayout* protGBLayout = new QVBoxLayout;
00078   topLayout->addWidget( protGB, 0, 0, 1, 2 );
00079 
00080   uint msgFormat = 1;
00081   for ( uint i = 0 ; i < NumberOfProtocols ; ++i ) {
00082     Kleo::CryptoMessageFormat f = static_cast<Kleo::CryptoMessageFormat>( msgFormat );
00083     mProtocolCB[ i ] = new QCheckBox( Kleo::cryptoMessageFormatToLabel( f ), protGB );
00084     protGBLayout->addWidget( mProtocolCB[i] );
00085     connect( mProtocolCB[i], SIGNAL( clicked() ), this, SLOT( setModified() ) );
00086 
00087     // Iterating over a bitfield means *2 every time
00088     msgFormat *= 2;
00089   }
00090   protGB->setLayout( protGBLayout );
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 QGroupBox( i18n( "Message Preference" ), this );
00105   QLayout* boxLayout = new QVBoxLayout;
00106   topLayout->addWidget( box, 3, 0, 1, 2 );
00107 
00108 
00109   // Send preferences/sign (see kleo/kleo/enum.h)
00110   KHBox* hbox = new KHBox( box );
00111 
00112   l = new QLabel( i18n( "Sign:" ), hbox );
00113 
00114   mSignPref = new QComboBox( hbox );
00115   mSignPref->setEditable( false );
00116   for ( unsigned int i = Kleo::UnknownSigningPreference; i < Kleo::MaxSigningPreference ; ++i )
00117     mSignPref->addItem( Kleo::signingPreferenceToLabel(
00118                            static_cast<Kleo::SigningPreference>( i ) ) );
00119   boxLayout->addWidget( hbox );
00120 
00121   // Send preferences/encrypt (see kleo/kleo/enum.h)
00122   hbox = new KHBox( box );
00123 
00124   l = new QLabel( i18n("Encrypt:"), hbox );
00125 
00126   mCryptPref = new QComboBox( hbox );
00127   mCryptPref->setEditable( false );
00128   for ( unsigned int i = Kleo::UnknownPreference; i < Kleo::MaxEncryptionPreference ; ++i )
00129     mCryptPref->addItem( Kleo::encryptionPreferenceToLabel(
00130                             static_cast<Kleo::EncryptionPreference>( i ) ) );
00131   boxLayout->addWidget( hbox );
00132   box->setLayout( boxLayout );
00133 
00134   // Emit "changed()" signal
00135   connect( mSignPref, SIGNAL( activated(int) ), this, SLOT( setModified() ) );
00136   connect( mCryptPref, SIGNAL( activated(int) ), this, SLOT( setModified() ) );
00137   // Not optimal, but KeyRequester doesn't emit any signals when the key changes
00138   connect( mPgpKey->eraseButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00139   connect( mPgpKey->dialogButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00140   connect( mSmimeCert->eraseButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00141   connect( mSmimeCert->dialogButton(), SIGNAL( clicked() ), this, SLOT( setModified() ) );
00142 }
00143 
00144 CryptoWidget::~CryptoWidget()
00145 {
00146 }
00147 
00148 void CryptoWidget::loadContact( KABC::Addressee *addr )
00149 {
00150   bool blocked = signalsBlocked();
00151   blockSignals( true );
00152 
00153   QStringList lst = addr->custom( "KADDRESSBOOK", "CRYPTOPROTOPREF" ).split( ",", QString::SkipEmptyParts );
00154   uint cryptoFormats = Kleo::stringListToCryptoMessageFormats( lst );
00155 
00156   uint msgFormat = 1;
00157   for ( uint i = 0 ; i < NumberOfProtocols ; ++i, msgFormat *= 2 ) {
00158     mProtocolCB[i]->setChecked( cryptoFormats & msgFormat );
00159   }
00160 
00161   mSignPref->setCurrentIndex( Kleo::stringToSigningPreference( addr->custom( "KADDRESSBOOK",
00162                                                                             "CRYPTOSIGNPREF" ) ) );
00163   mCryptPref->setCurrentIndex( Kleo::stringToEncryptionPreference( addr->custom( "KADDRESSBOOK",
00164                                                                                 "CRYPTOENCRYPTPREF" ) ) );
00165 
00166   // We don't use the contents of addr->key(...) because we want just a ref.
00167   // to the key/cert. stored elsewhere.
00168 
00169   mPgpKey->setFingerprints( addr->custom( "KADDRESSBOOK", "OPENPGPFP" ).split( ",", QString::SkipEmptyParts ) );
00170   mSmimeCert->setFingerprints( addr->custom( "KADDRESSBOOK", "SMIMEFP" ).split( ",", QString::SkipEmptyParts ) );
00171 
00172   blockSignals( blocked );
00173 }
00174 
00175 void CryptoWidget::storeContact( KABC::Addressee *addr )
00176 {
00177   uint cryptoFormats = 0;
00178   uint msgFormat = 1;
00179   for ( uint i = 0 ; i < NumberOfProtocols ; ++i, msgFormat *= 2 ) {
00180     if ( mProtocolCB[ i ]->isChecked() )
00181       cryptoFormats |= msgFormat;
00182   }
00183 
00184   QStringList lst = Kleo::cryptoMessageFormatsToStringList( cryptoFormats );
00185   if ( !lst.isEmpty() )
00186     addr->insertCustom( "KADDRESSBOOK", "CRYPTOPROTOPREF", lst.join( "," ) );
00187   else
00188     addr->removeCustom( "KADDRESSBOOK", "CRYPTOPROTOPREF" );
00189 
00190   Kleo::SigningPreference signPref =
00191       static_cast<Kleo::SigningPreference>( mSignPref->currentIndex() );
00192   if ( signPref != Kleo::UnknownSigningPreference )
00193     addr->insertCustom( "KADDRESSBOOK", "CRYPTOSIGNPREF",
00194                         Kleo::signingPreferenceToString( signPref ) );
00195   else
00196     addr->removeCustom( "KADDRESSBOOK", "CRYPTOSIGNPREF" );
00197 
00198   Kleo::EncryptionPreference encryptPref =
00199       static_cast<Kleo::EncryptionPreference>( mCryptPref->currentIndex() );
00200   if ( encryptPref != Kleo::UnknownPreference )
00201     addr->insertCustom( "KADDRESSBOOK", "CRYPTOENCRYPTPREF",
00202                         Kleo::encryptionPreferenceToString( encryptPref ) );
00203   else
00204     addr->removeCustom( "KADDRESSBOOK", "CRYPTOENCRYPTPREF" );
00205 
00206   QStringList pfp = mPgpKey->fingerprints();
00207   QStringList sfp = mSmimeCert->fingerprints();
00208 
00209   if ( !pfp.isEmpty() )
00210     addr->insertCustom( "KADDRESSBOOK", "OPENPGPFP", pfp.join( "," ) );
00211   else
00212     addr->removeCustom( "KADDRESSBOOK", "OPENPGPFP" );
00213 
00214   if ( !sfp.isEmpty() )
00215     addr->insertCustom( "KADDRESSBOOK", "SMIMEFP", sfp.join( "," ) );
00216   else
00217     addr->removeCustom( "KADDRESSBOOK", "SMIMEFP" );
00218 }
00219 
00220 void CryptoWidget::setReadOnly( bool readOnly )
00221 {
00222   mReadOnly = readOnly;
00223   for ( uint i = 0 ; i < NumberOfProtocols ; ++i )
00224     mProtocolCB[ i ]->setEnabled( !readOnly );
00225 
00226   mSignPref->setEnabled( !readOnly );
00227   mCryptPref->setEnabled( !readOnly );
00228   mPgpKey->setEnabled( !readOnly );
00229   mSmimeCert->setEnabled( !readOnly );
00230 }
00231 
00232 #include "cryptowidget.moc"

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal