00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
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
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
00135 connect( mSignPref, SIGNAL( activated(int) ), this, SLOT( setModified() ) );
00136 connect( mCryptPref, SIGNAL( activated(int) ), this, SLOT( setModified() ) );
00137
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
00167
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"