53 #include "keyrequester.h"
55 #include "keyselectiondialog.h"
57 #include <kleo/keylistjob.h>
59 #include <kleo/cryptobackendfactory.h>
62 #include <gpgmepp/key.h>
63 #include <gpgmepp/keylistresult.h>
66 #include <tdelocale.h>
67 #include <kiconloader.h>
70 #include <tdemessagebox.h>
71 #include <kpushbutton.h>
74 #include <tqapplication.h>
76 #include <tqtooltip.h>
78 #include <tqstringlist.h>
84 Kleo::KeyRequester::KeyRequester(
unsigned int allowedKeys,
bool multipleKeys,
85 TQWidget * parent,
const char * name )
86 : TQWidget( parent, name ),
89 mMulti( multipleKeys ),
90 mKeyUsage( allowedKeys ),
97 Kleo::KeyRequester::KeyRequester( TQWidget * parent,
const char * name )
98 : TQWidget( parent, name ),
109 void Kleo::KeyRequester::init()
111 TQHBoxLayout * hlay =
new TQHBoxLayout(
this, 0, KDialog::spacingHint() );
114 mLabel =
new TQLabel(
this );
115 mLabel->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
118 mEraseButton =
new KPushButton(
this );
119 mEraseButton->setAutoDefault(
false );
120 mEraseButton->setSizePolicy( TQSizePolicy( TQSizePolicy::Minimum,
121 TQSizePolicy::Minimum ) );
122 mEraseButton->setIconSet( SmallIconSet( TQApplication::reverseLayout() ?
"locationbar_erase" :
"clear_left" ) );
123 TQToolTip::add( mEraseButton, i18n(
"Clear") );
126 mDialogButton =
new TQPushButton( i18n(
"Change..."),
this );
127 mDialogButton->setAutoDefault(
false );
129 hlay->addWidget( mLabel, 1 );
130 hlay->addWidget( mEraseButton );
131 hlay->addWidget( mDialogButton );
133 connect( mEraseButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotEraseButtonClicked()) );
134 connect( mDialogButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotDialogButtonClicked()) );
136 setSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding,
137 TQSizePolicy::Fixed ) );
139 setAllowedKeys( mKeyUsage );
142 Kleo::KeyRequester::~KeyRequester() {
146 const std::vector<GpgME::Key> & Kleo::KeyRequester::keys()
const {
150 const GpgME::Key & Kleo::KeyRequester::key()
const {
152 return GpgME::Key::null;
154 return mKeys.front();
159 for ( std::vector<GpgME::Key>::const_iterator it = keys.begin() ; it != keys.end() ; ++it )
161 mKeys.push_back( *it );
168 mKeys.push_back( key );
172 TQString Kleo::KeyRequester::fingerprint()
const {
176 return mKeys.front().primaryFingerprint();
179 TQStringList Kleo::KeyRequester::fingerprints()
const {
181 for ( std::vector<GpgME::Key>::const_iterator it = mKeys.begin() ; it != mKeys.end() ; ++it )
183 if (
const char * fpr = it->primaryFingerprint() )
184 result.push_back( fpr );
189 startKeyListJob( fingerprint );
193 startKeyListJob( fingerprints );
196 void Kleo::KeyRequester::updateKeys() {
197 if ( mKeys.empty() ) {
201 if ( mKeys.size() > 1 )
202 setMultipleKeysEnabled(
true );
204 TQStringList labelTexts;
205 TQString toolTipText;
206 for ( std::vector<GpgME::Key>::const_iterator it = mKeys.begin() ; it != mKeys.end() ; ++it ) {
209 const TQString fpr = it->primaryFingerprint();
210 labelTexts.push_back( fpr.right(8) );
211 toolTipText += fpr.right(8) +
": ";
212 if (
const char * uid = it->userID(0).id() )
213 if ( it->protocol() == GpgME::Context::OpenPGP )
214 toolTipText += TQString::fromUtf8( uid );
218 toolTipText += i18n(
"<unknown>");
222 mLabel->setText( labelTexts.join(
", ") );
223 TQToolTip::remove( mLabel );
224 TQToolTip::add( mLabel, toolTipText );
227 #ifndef __KLEO_UI_SHOW_KEY_LIST_ERROR_H__
228 #define __KLEO_UI_SHOW_KEY_LIST_ERROR_H__
229 static void showKeyListError( TQWidget * parent,
const GpgME::Error & err ) {
231 const TQString msg = i18n(
"<qt><p>An error occurred while fetching "
232 "the keys from the backend:</p>"
233 "<p><b>%1</b></p></qt>" )
234 .arg( TQString::fromLocal8Bit( err.asString() ) );
236 KMessageBox::error( parent, msg, i18n(
"Key Listing Failed" ) );
240 void Kleo::KeyRequester::startKeyListJob(
const TQStringList & fingerprints ) {
241 if ( !mSMIMEBackend && !mOpenPGPBackend )
247 unsigned int count = 0;
248 for ( TQStringList::const_iterator it = fingerprints.begin() ; it != fingerprints.end() ; ++it )
249 if ( !(*it).stripWhiteSpace().isEmpty() )
255 setKey( GpgME::Key::null );
259 if ( mOpenPGPBackend ) {
260 KeyListJob * job = mOpenPGPBackend->keyListJob(
false );
262 KMessageBox::error(
this,
263 i18n(
"The OpenPGP backend does not support listing keys. "
264 "Check your installation."),
265 i18n(
"Key Listing Failed") );
267 connect( job, TQ_SIGNAL(result(
const GpgME::KeyListResult&)),
268 TQ_SLOT(slotKeyListResult(
const GpgME::KeyListResult&)) );
269 connect( job, TQ_SIGNAL(nextKey(
const GpgME::Key&)),
270 TQ_SLOT(slotNextKey(
const GpgME::Key&)) );
272 const GpgME::Error err = job->start( fingerprints,
273 mKeyUsage & Kleo::KeySelectionDialog::SecretKeys &&
274 !( mKeyUsage & Kleo::KeySelectionDialog::PublicKeys ) );
277 showKeyListError(
this, err );
283 if ( mSMIMEBackend ) {
284 KeyListJob * job = mSMIMEBackend->keyListJob(
false );
286 KMessageBox::error(
this,
287 i18n(
"The S/MIME backend does not support listing keys. "
288 "Check your installation."),
289 i18n(
"Key Listing Failed") );
291 connect( job, TQ_SIGNAL(result(
const GpgME::KeyListResult&)),
292 TQ_SLOT(slotKeyListResult(
const GpgME::KeyListResult&)) );
293 connect( job, TQ_SIGNAL(nextKey(
const GpgME::Key&)),
294 TQ_SLOT(slotNextKey(
const GpgME::Key&)) );
296 const GpgME::Error err = job->start( fingerprints,
297 mKeyUsage & Kleo::KeySelectionDialog::SecretKeys &&
298 !( mKeyUsage & Kleo::KeySelectionDialog::PublicKeys ) );
301 showKeyListError(
this, err );
308 mEraseButton->setEnabled(
false );
309 mDialogButton->setEnabled(
false );
313 void Kleo::KeyRequester::slotNextKey(
const GpgME::Key & key ) {
315 mTmpKeys.push_back( key );
318 void Kleo::KeyRequester::slotKeyListResult(
const GpgME::KeyListResult & res ) {
320 showKeyListError(
this, res.error() );
322 if ( --mJobs <= 0 ) {
323 mEraseButton->setEnabled(
true );
324 mDialogButton->setEnabled(
true );
332 void Kleo::KeyRequester::slotDialogButtonClicked() {
333 KeySelectionDialog * dlg = mKeys.empty()
334 ?
new KeySelectionDialog( mDialogCaption, mDialogMessage, mInitialQuery, mKeyUsage, mMulti,
false,
this )
335 : new KeySelectionDialog( mDialogCaption, mDialogCaption, mKeys, mKeyUsage, mMulti, false, this ) ;
337 if ( dlg->exec() == TQDialog::Accepted ) {
339 setKeys( dlg->selectedKeys() );
341 setKey( dlg->selectedKey() );
348 void Kleo::KeyRequester::slotEraseButtonClicked() {
349 if ( !mKeys.empty() )
355 void Kleo::KeyRequester::setDialogCaption(
const TQString & caption ) {
356 mDialogCaption = caption;
359 void Kleo::KeyRequester::setDialogMessage(
const TQString & msg ) {
360 mDialogMessage = msg;
363 bool Kleo::KeyRequester::isMultipleKeysEnabled()
const {
367 void Kleo::KeyRequester::setMultipleKeysEnabled(
bool multi ) {
368 if ( multi == mMulti )
return;
370 if ( !multi && !mKeys.empty() )
371 mKeys.erase( mKeys.begin() + 1, mKeys.end() );
377 unsigned int Kleo::KeyRequester::allowedKeys()
const {
381 void Kleo::KeyRequester::setAllowedKeys(
unsigned int keyUsage ) {
382 mKeyUsage = keyUsage;
386 if ( mKeyUsage & KeySelectionDialog::OpenPGPKeys )
387 mOpenPGPBackend = Kleo::CryptoBackendFactory::instance()->openpgp();
388 if ( mKeyUsage & KeySelectionDialog::SMIMEKeys )
389 mSMIMEBackend = Kleo::CryptoBackendFactory::instance()->smime();
391 if ( mOpenPGPBackend && !mSMIMEBackend ) {
392 mDialogCaption = i18n(
"OpenPGP Key Selection");
393 mDialogMessage = i18n(
"Please select an OpenPGP key to use.");
394 }
else if ( !mOpenPGPBackend && mSMIMEBackend ) {
395 mDialogCaption = i18n(
"S/MIME Key Selection");
396 mDialogMessage = i18n(
"Please select an S/MIME key to use.");
398 mDialogCaption = i18n(
"Key Selection");
399 mDialogMessage = i18n(
"Please select an (OpenPGP or S/MIME) key to use.");
403 TQPushButton * Kleo::KeyRequester::dialogButton() {
404 return mDialogButton;
407 TQPushButton * Kleo::KeyRequester::eraseButton() {
411 static inline unsigned int foo(
bool openpgp,
bool smime,
bool trusted,
bool valid ) {
412 unsigned int result = 0;
414 result |= Kleo::KeySelectionDialog::OpenPGPKeys;
416 result |= Kleo::KeySelectionDialog::SMIMEKeys;
418 result |= Kleo::KeySelectionDialog::TrustedKeys;
420 result |= Kleo::KeySelectionDialog::ValidKeys;
424 static inline unsigned int encryptionKeyUsage(
bool openpgp,
bool smime,
bool trusted,
bool valid ) {
425 return foo( openpgp, smime, trusted, valid ) | Kleo::KeySelectionDialog::EncryptionKeys | Kleo::KeySelectionDialog::PublicKeys;
428 static inline unsigned int signingKeyUsage(
bool openpgp,
bool smime,
bool trusted,
bool valid ) {
429 return foo( openpgp, smime, trusted, valid ) | Kleo::KeySelectionDialog::SigningKeys | Kleo::KeySelectionDialog::SecretKeys;
432 Kleo::EncryptionKeyRequester::EncryptionKeyRequester(
bool multi,
unsigned int proto,
433 TQWidget * parent,
const char * name,
434 bool onlyTrusted,
bool onlyValid )
435 : KeyRequester( encryptionKeyUsage( proto & OpenPGP, proto & SMIME, onlyTrusted, onlyValid ), multi,
440 Kleo::EncryptionKeyRequester::EncryptionKeyRequester( TQWidget * parent,
const char * name )
441 : KeyRequester( 0, false, parent, name )
445 Kleo::EncryptionKeyRequester::~EncryptionKeyRequester() {}
448 void Kleo::EncryptionKeyRequester::setAllowedKeys(
unsigned int proto,
bool onlyTrusted,
bool onlyValid )
450 KeyRequester::setAllowedKeys( encryptionKeyUsage( proto & OpenPGP, proto & SMIME, onlyTrusted, onlyValid ) );
453 Kleo::SigningKeyRequester::SigningKeyRequester(
bool multi,
unsigned int proto,
454 TQWidget * parent,
const char * name,
455 bool onlyTrusted,
bool onlyValid )
456 : KeyRequester( signingKeyUsage( proto & OpenPGP, proto & SMIME, onlyTrusted, onlyValid ), multi,
461 Kleo::SigningKeyRequester::SigningKeyRequester( TQWidget * parent,
const char * name )
462 : KeyRequester( 0, false, parent, name )
466 Kleo::SigningKeyRequester::~SigningKeyRequester() {}
468 void Kleo::SigningKeyRequester::setAllowedKeys(
unsigned int proto,
bool onlyTrusted,
bool onlyValid )
470 KeyRequester::setAllowedKeys( signingKeyUsage( proto & OpenPGP, proto & SMIME, onlyTrusted, onlyValid ) );
473 void Kleo::KeyRequester::virtual_hook(
int,
void* ) {}
474 void Kleo::EncryptionKeyRequester::virtual_hook(
int id,
void * data ) {
475 KeyRequester::virtual_hook(
id, data );
477 void Kleo::SigningKeyRequester::virtual_hook(
int id,
void * data ) {
478 KeyRequester::virtual_hook(
id, data );
481 #include "keyrequester.moc"
TQString prettyDN() const
void setFingerprints(const TQStringList &fingerprints)
Set the keys by fingerprint.
void setKeys(const std::vector< GpgME::Key > &keys)
Preferred method to set a key for multi-KeyRequesters.
void setKey(const GpgME::Key &key)
Preferred method to set a key for non-multi-KeyRequesters.
void setFingerprint(const TQString &fingerprint)
Set the key by fingerprint.