summaryrefslogtreecommitdiffstats
path: root/certmanager/lib/ui/keyrequester.cpp
blob: 16efbd46f4331aca8df1f3f2d939b81303f86266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*  -*- c++ -*-
    keyrequester.cpp

    This file is part of libkleopatra, the KDE keymanagement library
    Copyright (c) 2004 Klar�vdalens Datakonsult AB

    Libkleopatra is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    Libkleopatra is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.


    Based on kpgpui.cpp
    Copyright (C) 2001,2002 the KPGP authors
    See file libkdenetwork/AUTHORS.kpgp for details

    This file is part of KPGP, the KDE PGP/GnuPG support library.

    KPGP is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "keyrequester.h"

#include "keyselectiondialog.h"

#include <kleo/keylistjob.h>
#include <kleo/dn.h>
#include <kleo/cryptobackendfactory.h>

// gpgme++
#include <gpgmepp/key.h>
#include <gpgmepp/keylistresult.h>

// KDE
#include <klocale.h>
#include <kiconloader.h>
#include <kdialog.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <kpushbutton.h>

// TQt
#include <tqapplication.h>
#include <tqlayout.h>
#include <tqtooltip.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqlabel.h>
#include <tqregexp.h>

#include <assert.h>

Kleo::KeyRequester::KeyRequester( unsigned int allowedKeys, bool multipleKeys,
				  TQWidget * tqparent, const char * name )
  : TQWidget( tqparent, name ),
    mOpenPGPBackend( 0 ),
    mSMIMEBackend( 0 ),
    mMulti( multipleKeys ),
    mKeyUsage( allowedKeys ),
    mJobs( 0 ),
    d( 0 )
{
  init();
}

Kleo::KeyRequester::KeyRequester( TQWidget * tqparent, const char * name )
  : TQWidget( tqparent, name ),
    mOpenPGPBackend( 0 ),
    mSMIMEBackend( 0 ),
    mMulti( false ),
    mKeyUsage( 0 ),
    mJobs( 0 ),
    d( 0 )
{
  init();
}

void Kleo::KeyRequester::init()
{
  TQHBoxLayout * hlay = new TQHBoxLayout( this, 0, KDialog::spacingHint() );

  // the label where the key id is to be displayed:
  mLabel = new TQLabel( this );
  mLabel->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );

  // the button to unset any key:
  mEraseButton = new KPushButton( this );
  mEraseButton->setAutoDefault( false );
  mEraseButton->tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Minimum,
					    TQSizePolicy::Minimum ) );
  mEraseButton->setIconSet( SmallIconSet( TQApplication::reverseLayout() ? "locationbar_erase" : "clear_left" ) );
  TQToolTip::add( mEraseButton, i18n("Clear") );

  // the button to call the KeySelectionDialog:
  mDialogButton = new TQPushButton( i18n("Change..."), this );
  mDialogButton->setAutoDefault( false );

  hlay->addWidget( mLabel, 1 );
  hlay->addWidget( mEraseButton );
  hlay->addWidget( mDialogButton );

  connect( mEraseButton,  TQT_SIGNAL(clicked()), TQT_SLOT(slotEraseButtonClicked()) );
  connect( mDialogButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotDialogButtonClicked()) );

  tqsetSizePolicy( TQSizePolicy( TQSizePolicy::MinimumExpanding,
			      TQSizePolicy::Fixed ) );

  setAllowedKeys( mKeyUsage );
}

Kleo::KeyRequester::~KeyRequester() {

}

const std::vector<GpgME::Key> & Kleo::KeyRequester::keys() const {
  return mKeys;
}

const GpgME::Key & Kleo::KeyRequester::key() const {
  if ( mKeys.empty() )
    return GpgME::Key::null;
  else
    return mKeys.front();
}

void Kleo::KeyRequester::setKeys( const std::vector<GpgME::Key> & keys ) {
  mKeys.clear();
  for ( std::vector<GpgME::Key>::const_iterator it = keys.begin() ; it != keys.end() ; ++it )
    if ( !it->isNull() )
      mKeys.push_back( *it );
  updateKeys();
}

void Kleo::KeyRequester::setKey( const GpgME::Key & key ) {
  mKeys.clear();
  if ( !key.isNull() )
    mKeys.push_back( key );
  updateKeys();
}

TQString Kleo::KeyRequester::fingerprint() const {
  if ( mKeys.empty() )
    return TQString();
  else
    return mKeys.front().primaryFingerprint();
}

TQStringList Kleo::KeyRequester::fingerprints() const {
  TQStringList result;
  for ( std::vector<GpgME::Key>::const_iterator it = mKeys.begin() ; it != mKeys.end() ; ++it )
    if ( !it->isNull() )
      if ( const char * fpr = it->primaryFingerprint() )
	result.push_back( fpr );
  return result;
}

void Kleo::KeyRequester::setFingerprint( const TQString & fingerprint ) {
  startKeyListJob( fingerprint );
}

void Kleo::KeyRequester::setFingerprints( const TQStringList & fingerprints ) {
  startKeyListJob( fingerprints );
}

void Kleo::KeyRequester::updateKeys() {
  if ( mKeys.empty() ) {
    mLabel->clear();
    return;
  }
  if ( mKeys.size() > 1 )
    setMultipleKeysEnabled( true );

  TQStringList labelTexts;
  TQString toolTipText;
  for ( std::vector<GpgME::Key>::const_iterator it = mKeys.begin() ; it != mKeys.end() ; ++it ) {
    if ( it->isNull() )
      continue;
    const TQString fpr = it->primaryFingerprint();
    labelTexts.push_back( fpr.right(8) );
    toolTipText += fpr.right(8) + ": ";
    if ( const char * uid = it->userID(0).id() )
      if ( it->protocol() == GpgME::Context::OpenPGP )
	toolTipText += TQString::fromUtf8( uid );
      else
	toolTipText += Kleo::DN( uid ).prettyDN();
    else
      toolTipText += i18n("<unknown>");
    toolTipText += '\n';
  }

  mLabel->setText( labelTexts.join(", ") );
  TQToolTip::remove( mLabel );
  TQToolTip::add( mLabel, toolTipText );
}

#ifndef __KLEO_UI_SHOW_KEY_LIST_ERROR_H__
#define __KLEO_UI_SHOW_KEY_LIST_ERROR_H__
static void showKeyListError( TQWidget * tqparent, const GpgME::Error & err ) {
  assert( err );
  const TQString msg = i18n( "<qt><p>An error occurred while fetching "
			    "the keys from the backend:</p>"
			    "<p><b>%1</b></p></qt>" )
    .tqarg( TQString::fromLocal8Bit( err.asString() ) );

  KMessageBox::error( tqparent, msg, i18n( "Key Listing Failed" ) );
}
#endif // __KLEO_UI_SHOW_KEY_LIST_ERROR_H__

void Kleo::KeyRequester::startKeyListJob( const TQStringList & fingerprints ) {
  if ( !mSMIMEBackend && !mOpenPGPBackend )
    return;

  mTmpKeys.clear();
  mJobs = 0;

  unsigned int count = 0;
  for ( TQStringList::const_iterator it = fingerprints.begin() ; it != fingerprints.end() ; ++it )
    if ( !(*it).stripWhiteSpace().isEmpty() )
      ++count;

  if ( !count ) {
    // don't fall into the trap that an empty pattern means
    // "return all keys" :)
    setKey( GpgME::Key::null );
    return;
  }

  if ( mOpenPGPBackend ) {
    KeyListJob * job = mOpenPGPBackend->keyListJob( false ); // local, no sigs
    if ( !job ) {
      KMessageBox::error( this,
			  i18n("The OpenPGP backend does not support listing keys. "
			       "Check your installation."),
			  i18n("Key Listing Failed") );
    } else {
      connect( job, TQT_SIGNAL(result(const GpgME::KeyListResult&)),
	       TQT_SLOT(slotKeyListResult(const GpgME::KeyListResult&)) );
      connect( job, TQT_SIGNAL(nextKey(const GpgME::Key&)),
	       TQT_SLOT(slotNextKey(const GpgME::Key&)) );

      const GpgME::Error err = job->start( fingerprints,
        mKeyUsage & Kleo::KeySelectionDialog::SecretKeys &&
        !( mKeyUsage & Kleo::KeySelectionDialog::PublicKeys ) );

      if ( err )
	showKeyListError( this, err );
      else
	++mJobs;
    }
  }

  if ( mSMIMEBackend ) {
    KeyListJob * job = mSMIMEBackend->keyListJob( false ); // local, no sigs
    if ( !job ) {
      KMessageBox::error( this,
			  i18n("The S/MIME backend does not support listing keys. "
			       "Check your installation."),
			  i18n("Key Listing Failed") );
    } else {
      connect( job, TQT_SIGNAL(result(const GpgME::KeyListResult&)),
	       TQT_SLOT(slotKeyListResult(const GpgME::KeyListResult&)) );
      connect( job, TQT_SIGNAL(nextKey(const GpgME::Key&)),
	       TQT_SLOT(slotNextKey(const GpgME::Key&)) );

      const GpgME::Error err = job->start( fingerprints,
        mKeyUsage & Kleo::KeySelectionDialog::SecretKeys &&
        !( mKeyUsage & Kleo::KeySelectionDialog::PublicKeys ) );

      if ( err )
	showKeyListError( this, err );
      else
	++mJobs;
    }
  }

  if ( mJobs > 0 ) {
    mEraseButton->setEnabled( false );
    mDialogButton->setEnabled( false );
  }
}

void Kleo::KeyRequester::slotNextKey( const GpgME::Key & key ) {
  if ( !key.isNull() )
    mTmpKeys.push_back( key );
}

void Kleo::KeyRequester::slotKeyListResult( const GpgME::KeyListResult & res ) {
  if ( res.error() )
    showKeyListError( this, res.error() );

  if ( --mJobs <= 0 ) {
    mEraseButton->setEnabled( true );
    mDialogButton->setEnabled( true );

    setKeys( mTmpKeys );
    mTmpKeys.clear();
  }
}


void Kleo::KeyRequester::slotDialogButtonClicked() {
  KeySelectionDialog * dlg = mKeys.empty()
    ? new KeySelectionDialog( mDialogCaption, mDialogMessage, mInitialQuery, mKeyUsage, mMulti, false, this )
    : new KeySelectionDialog( mDialogCaption, mDialogCaption, mKeys, mKeyUsage, mMulti, false, this ) ;

  if ( dlg->exec() == TQDialog::Accepted ) {
    if ( mMulti )
      setKeys( dlg->selectedKeys() );
    else
      setKey( dlg->selectedKey() );
    emit changed();
  }

  delete dlg;
}

void Kleo::KeyRequester::slotEraseButtonClicked() {
  if ( !mKeys.empty() )
    emit changed();
  mKeys.clear();
  updateKeys();
}

void Kleo::KeyRequester::setDialogCaption( const TQString & caption ) {
  mDialogCaption = caption;
}

void Kleo::KeyRequester::setDialogMessage( const TQString & msg ) {
  mDialogMessage = msg;
}

bool Kleo::KeyRequester::isMultipleKeysEnabled() const {
  return mMulti;
}

void Kleo::KeyRequester::setMultipleKeysEnabled( bool multi ) {
  if ( multi == mMulti ) return;

  if ( !multi && !mKeys.empty() )
    mKeys.erase( mKeys.begin() + 1, mKeys.end() );

  mMulti = multi;
  updateKeys();
}

unsigned int Kleo::KeyRequester::allowedKeys() const {
  return mKeyUsage;
}

void Kleo::KeyRequester::setAllowedKeys( unsigned int keyUsage ) {
  mKeyUsage = keyUsage;
  mOpenPGPBackend = 0;
  mSMIMEBackend = 0;

  if ( mKeyUsage & KeySelectionDialog::OpenPGPKeys )
    mOpenPGPBackend = Kleo::CryptoBackendFactory::instance()->openpgp();
  if ( mKeyUsage & KeySelectionDialog::SMIMEKeys )
    mSMIMEBackend = Kleo::CryptoBackendFactory::instance()->smime();

  if ( mOpenPGPBackend && !mSMIMEBackend ) {
    mDialogCaption = i18n("OpenPGP Key Selection");
    mDialogMessage = i18n("Please select an OpenPGP key to use.");
  } else if ( !mOpenPGPBackend && mSMIMEBackend ) {
    mDialogCaption = i18n("S/MIME Key Selection");
    mDialogMessage = i18n("Please select an S/MIME key to use.");
  } else {
    mDialogCaption = i18n("Key Selection");
    mDialogMessage = i18n("Please select an (OpenPGP or S/MIME) key to use.");
  }
}

TQPushButton * Kleo::KeyRequester::dialogButton() {
  return mDialogButton;
}

TQPushButton * Kleo::KeyRequester::eraseButton() {
  return mEraseButton;
}

static inline unsigned int foo( bool openpgp, bool smime, bool trusted, bool valid ) {
  unsigned int result = 0;
  if ( openpgp )
    result |= Kleo::KeySelectionDialog::OpenPGPKeys;
  if ( smime )
    result |= Kleo::KeySelectionDialog::SMIMEKeys;
  if ( trusted )
    result |= Kleo::KeySelectionDialog::TrustedKeys;
  if ( valid )
    result |= Kleo::KeySelectionDialog::ValidKeys;
  return result;
}

static inline unsigned int encryptionKeyUsage( bool openpgp, bool smime, bool trusted, bool valid ) {
  return foo( openpgp, smime, trusted, valid ) | Kleo::KeySelectionDialog::EncryptionKeys | Kleo::KeySelectionDialog::PublicKeys;
}

static inline unsigned int signingKeyUsage( bool openpgp, bool smime, bool trusted, bool valid ) {
  return foo( openpgp, smime, trusted, valid ) | Kleo::KeySelectionDialog::SigningKeys | Kleo::KeySelectionDialog::SecretKeys;
}

Kleo::EncryptionKeyRequester::EncryptionKeyRequester( bool multi, unsigned int proto,
						      TQWidget * tqparent, const char * name,
						      bool onlyTrusted, bool onlyValid )
  : KeyRequester( encryptionKeyUsage( proto & OpenPGP, proto & SMIME, onlyTrusted, onlyValid ), multi,
		  tqparent, name )
{
}

Kleo::EncryptionKeyRequester::EncryptionKeyRequester( TQWidget * tqparent, const char * name )
  : KeyRequester( 0, false, tqparent, name )
{
}

Kleo::EncryptionKeyRequester::~EncryptionKeyRequester() {}


void Kleo::EncryptionKeyRequester::setAllowedKeys( unsigned int proto, bool onlyTrusted, bool onlyValid )
{
  KeyRequester::setAllowedKeys( encryptionKeyUsage( proto & OpenPGP, proto & SMIME, onlyTrusted, onlyValid ) );
}

Kleo::SigningKeyRequester::SigningKeyRequester( bool multi, unsigned int proto,
						TQWidget * tqparent, const char * name,
						bool onlyTrusted, bool onlyValid )
  : KeyRequester( signingKeyUsage( proto & OpenPGP, proto & SMIME, onlyTrusted, onlyValid ), multi,
		  tqparent, name )
{
}

Kleo::SigningKeyRequester::SigningKeyRequester( TQWidget * tqparent, const char * name )
  : KeyRequester( 0, false, tqparent, name )
{
}

Kleo::SigningKeyRequester::~SigningKeyRequester() {}

void Kleo::SigningKeyRequester::setAllowedKeys( unsigned int proto, bool onlyTrusted, bool onlyValid )
{
  KeyRequester::setAllowedKeys( signingKeyUsage( proto & OpenPGP, proto & SMIME, onlyTrusted, onlyValid ) );
}

void Kleo::KeyRequester::virtual_hook( int, void* ) {}
void Kleo::EncryptionKeyRequester::virtual_hook( int id, void * data ) {
  KeyRequester::virtual_hook( id, data );
}
void Kleo::SigningKeyRequester::virtual_hook( int id, void * data ) {
  KeyRequester::virtual_hook( id, data );
}

#include "keyrequester.moc"