kaddressbook

cryptowidget.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2004 Klarälvdalens Datakonsult AB
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <config.h>
25#include "certmanager/lib/ui/keyrequester.h"
26#include "certmanager/lib/cryptplugfactory.h"
27#include "certmanager/lib/cryptplugwrapper.h"
28#include "certmanager/lib/kleo/enum.h"
29
30#include "gpgmepp/data.h"
31#include "gpgmepp/key.h"
32
33#include <kdebug.h>
34#include <kdialog.h>
35#include <kiconloader.h>
36#include <tdelocale.h>
37
38#include <tqlayout.h>
39#include <tqlabel.h>
40#include <tqcheckbox.h>
41#include <tqcombobox.h>
42#include <tqpushbutton.h>
43#include <tqvgroupbox.h>
44#include <tqhbox.h>
45
46#include "cryptowidget.h"
47
48extern "C" {
49 void *init_libkaddrbk_cryptosettings()
50 {
51 return ( new CryptoWidgetFactory );
52 }
53}
54
55CryptoWidgetFactory::CryptoWidgetFactory()
56{
57 TDEGlobal::locale()->insertCatalogue( "libkleopatra" );
58 TDEGlobal::iconLoader()->addAppDir( "libkleopatra" );
59}
60
61TQString CryptoWidgetFactory::pageTitle() const
62{
63 return i18n( "Crypto Settings" );
64}
65
66TQString CryptoWidgetFactory::pageIdentifier() const
67{
68 return "crypto";
69}
70
71CryptoWidget::CryptoWidget( TDEABC::AddressBook *ab, TQWidget *parent, const char *name )
72 : KAB::ContactEditorWidget( ab, parent, name ), mReadOnly( false )
73{
74 TQGridLayout *topLayout = new TQGridLayout( this, 2, 5, KDialog::marginHint(),
75 KDialog::spacingHint() );
76 topLayout->setColStretch( 1, 1 );
77 topLayout->setRowStretch( 4, 1 );
78
79 TQVGroupBox* protGB = new TQVGroupBox( i18n( "Allowed Protocols" ), this );
80 topLayout->addMultiCellWidget( protGB, 0, 0, 0, 1 );
81
82 uint msgFormat = 1;
83 for ( uint i = 0 ; i < NumberOfProtocols ; ++i ) {
84 Kleo::CryptoMessageFormat f = static_cast<Kleo::CryptoMessageFormat>( msgFormat );
85 mProtocolCB[ i ] = new TQCheckBox( Kleo::cryptoMessageFormatToLabel( f ), protGB );
86 connect( mProtocolCB[i], TQ_SIGNAL( clicked() ), this, TQ_SLOT( setModified() ) );
87
88 // Iterating over a bitfield means *2 every time
89 msgFormat *= 2;
90 }
91
92 TQLabel* l = new TQLabel( i18n( "Preferred OpenPGP encryption key:" ), this );
93 topLayout->addWidget( l, 1, 0 );
94
95 mPgpKey = new Kleo::EncryptionKeyRequester( true, Kleo::EncryptionKeyRequester::OpenPGP, this );
96 topLayout->addWidget( mPgpKey, 1, 1 );
97
98 l = new TQLabel( i18n( "Preferred S/MIME encryption certificate:" ), this );
99 topLayout->addWidget( l, 2, 0 );
100
101 mSmimeCert = new Kleo::EncryptionKeyRequester( true, Kleo::EncryptionKeyRequester::SMIME, this );
102 topLayout->addWidget( mSmimeCert, 2, 1 );
103
104 TQGroupBox* box = new TQVGroupBox( i18n( "Message Preference" ), this );
105 topLayout->addMultiCellWidget( box, 3, 3, 0, 1 );
106
107
108 // Send preferences/sign (see certmanager/lib/kleo/enum.h)
109 TQHBox* hbox = new TQHBox( box );
110
111 l = new TQLabel( i18n( "Sign:" ), hbox );
112
113 mSignPref = new TQComboBox( false, hbox );
114 for ( unsigned int i = Kleo::UnknownSigningPreference; i < Kleo::MaxSigningPreference ; ++i )
115 mSignPref->insertItem( Kleo::signingPreferenceToLabel(
116 static_cast<Kleo::SigningPreference>( i ) ) );
117
118 // Send preferences/encrypt (see certmanager/lib/kleo/enum.h)
119 hbox = new TQHBox( box );
120
121 l = new TQLabel( i18n("Encrypt:"), hbox );
122
123 mCryptPref = new TQComboBox( false, hbox );
124 for ( unsigned int i = Kleo::UnknownPreference; i < Kleo::MaxEncryptionPreference ; ++i )
125 mCryptPref->insertItem( Kleo::encryptionPreferenceToLabel(
126 static_cast<Kleo::EncryptionPreference>( i ) ) );
127
128 // Emit "changed()" signal
129 connect( mSignPref, TQ_SIGNAL( activated(int) ), this, TQ_SLOT( setModified() ) );
130 connect( mCryptPref, TQ_SIGNAL( activated(int) ), this, TQ_SLOT( setModified() ) );
131 // Not optimal, but KeyRequester doesn't emit any signals when the key changes
132 connect( mPgpKey->eraseButton(), TQ_SIGNAL( clicked() ), this, TQ_SLOT( setModified() ) );
133 connect( mPgpKey->dialogButton(), TQ_SIGNAL( clicked() ), this, TQ_SLOT( setModified() ) );
134 connect( mSmimeCert->eraseButton(), TQ_SIGNAL( clicked() ), this, TQ_SLOT( setModified() ) );
135 connect( mSmimeCert->dialogButton(), TQ_SIGNAL( clicked() ), this, TQ_SLOT( setModified() ) );
136}
137
138CryptoWidget::~CryptoWidget()
139{
140}
141
142void CryptoWidget::loadContact( TDEABC::Addressee *addr )
143{
144 bool blocked = signalsBlocked();
145 blockSignals( true );
146
147 TQStringList lst = TQStringList::split( ',', addr->custom( "KADDRESSBOOK",
148 "CRYPTOPROTOPREF" ) );
149 uint cryptoFormats = Kleo::stringListToCryptoMessageFormats( lst );
150
151 uint msgFormat = 1;
152 for ( uint i = 0 ; i < NumberOfProtocols ; ++i, msgFormat *= 2 ) {
153 mProtocolCB[i]->setChecked( cryptoFormats & msgFormat );
154 }
155
156 mSignPref->setCurrentItem( Kleo::stringToSigningPreference( addr->custom( "KADDRESSBOOK",
157 "CRYPTOSIGNPREF" ) ) );
158 mCryptPref->setCurrentItem( Kleo::stringToEncryptionPreference( addr->custom( "KADDRESSBOOK",
159 "CRYPTOENCRYPTPREF" ) ) );
160
161 // We dont use the contents of addr->key(...) because we want just a ref.
162 // to the key/cert. stored elsewhere.
163
164 mPgpKey->setFingerprints( TQStringList::split( ",", addr->custom( "KADDRESSBOOK", "OPENPGPFP" ) ) );
165 mSmimeCert->setFingerprints( TQStringList::split( ",", addr->custom( "KADDRESSBOOK", "SMIMEFP" ) ) );
166
167 blockSignals( blocked );
168}
169
170void CryptoWidget::storeContact( TDEABC::Addressee *addr )
171{
172 uint cryptoFormats = 0;
173 uint msgFormat = 1;
174 for ( uint i = 0 ; i < NumberOfProtocols ; ++i, msgFormat *= 2 ) {
175 if ( mProtocolCB[ i ]->isChecked() )
176 cryptoFormats |= msgFormat;
177 }
178
179 TQStringList lst = Kleo::cryptoMessageFormatsToStringList( cryptoFormats );
180 if ( !lst.isEmpty() )
181 addr->insertCustom( "KADDRESSBOOK", "CRYPTOPROTOPREF", lst.join( "," ) );
182 else
183 addr->removeCustom( "KADDRESSBOOK", "CRYPTOPROTOPREF" );
184
185 Kleo::SigningPreference signPref =
186 static_cast<Kleo::SigningPreference>( mSignPref->currentItem() );
187 if ( signPref != Kleo::UnknownSigningPreference )
188 addr->insertCustom( "KADDRESSBOOK", "CRYPTOSIGNPREF",
189 Kleo::signingPreferenceToString( signPref ) );
190 else
191 addr->removeCustom( "KADDRESSBOOK", "CRYPTOSIGNPREF" );
192
193 Kleo::EncryptionPreference encryptPref =
194 static_cast<Kleo::EncryptionPreference>( mCryptPref->currentItem() );
195 if ( encryptPref != Kleo::UnknownPreference )
196 addr->insertCustom( "KADDRESSBOOK", "CRYPTOENCRYPTPREF",
197 Kleo::encryptionPreferenceToString( encryptPref ) );
198 else
199 addr->removeCustom( "KADDRESSBOOK", "CRYPTOENCRYPTPREF" );
200
201 TQStringList pfp = mPgpKey->fingerprints();
202 TQStringList sfp = mSmimeCert->fingerprints();
203
204 if ( !pfp.isEmpty() )
205 addr->insertCustom( "KADDRESSBOOK", "OPENPGPFP", pfp.join( "," ) );
206 else
207 addr->removeCustom( "KADDRESSBOOK", "OPENPGPFP" );
208
209 if ( !sfp.isEmpty() )
210 addr->insertCustom( "KADDRESSBOOK", "SMIMEFP", sfp.join( "," ) );
211 else
212 addr->removeCustom( "KADDRESSBOOK", "SMIMEFP" );
213}
214
215void CryptoWidget::setReadOnly( bool readOnly )
216{
217 mReadOnly = readOnly;
218 for ( uint i = 0 ; i < NumberOfProtocols ; ++i )
219 mProtocolCB[ i ]->setEnabled( !readOnly );
220
221 mSignPref->setEnabled( !readOnly );
222 mCryptPref->setEnabled( !readOnly );
223 mPgpKey->setEnabled( !readOnly );
224 mSmimeCert->setEnabled( !readOnly );
225}
226
227#include "cryptowidget.moc"