TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qgpgmekeylistjob.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. qgpgmekeylistjob.cpp
  3. This file is part of libkleopatra, the KDE keymanagement library
  4. Copyright (c) 2004 Klarälvdalens Datakonsult AB
  5. Libkleopatra is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. Libkleopatra is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. In addition, as a special exception, the copyright holders give
  17. permission to link the code of this program with any edition of
  18. the Qt library by Trolltech AS, Norway (or with modified versions
  19. of Qt that use the same license as Qt), and distribute linked
  20. combinations including the two. You must obey the GNU General
  21. Public License in all respects for all of the code used other than
  22. Qt. If you modify this file, you may extend this exception to
  23. your version of the file, but you are not obligated to do so. If
  24. you do not wish to do so, delete this exception statement from
  25. your version.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include "qgpgmekeylistjob.h"
  31. #include <qgpgme/eventloopinteractor.h>
  32. #include <gpgmepp/key.h>
  33. #include <gpgmepp/context.h>
  34. #include <gpgmepp/keylistresult.h>
  35. #include <gpg-error.h>
  36. #include <kmessagebox.h>
  37. #include <klocale.h>
  38. #include <kdebug.h>
  39. #include <tqstringlist.h>
  40. #include <tqtimer.h>
  41. #include <algorithm>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <assert.h>
  45. Kleo::QGpgMEKeyListJob::QGpgMEKeyListJob( GpgME::Context * context )
  46. : KeyListJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMEKeyListJob" ),
  47. QGpgMEJob( this, context ),
  48. mResult(), mSecretOnly( false )
  49. {
  50. assert( context );
  51. }
  52. Kleo::QGpgMEKeyListJob::~QGpgMEKeyListJob() {
  53. }
  54. void Kleo::QGpgMEKeyListJob::setup( const TQStringList & pats, bool secretOnly ) {
  55. assert( !patterns() );
  56. mSecretOnly = secretOnly;
  57. setPatterns( pats );
  58. }
  59. GpgME::Error Kleo::QGpgMEKeyListJob::start( const TQStringList & pats, bool secretOnly ) {
  60. setup( pats, secretOnly );
  61. hookupContextToEventLoopInteractor();
  62. connect( QGpgME::EventLoopInteractor::instance(),
  63. TQT_SIGNAL(nextKeyEventSignal(GpgME::Context*,const GpgME::Key&)),
  64. TQT_SLOT(slotNextKeyEvent(GpgME::Context*,const GpgME::Key&)) );
  65. // The communication channel between gpgme and gpgsm is limited in
  66. // the number of patterns that can be transported, but they won't
  67. // say to how much, so we need to find out ourselves if we get a
  68. // LINE_TOO_LONG error back...
  69. // We could of course just feed them single patterns, and that would
  70. // probably be easier, but the performance penalty would currently
  71. // be noticable.
  72. while ( const GpgME::Error err = mCtx->startKeyListing( patterns(), mSecretOnly ) ) {
  73. if ( err.code() == GPG_ERR_LINE_TOO_LONG ) {
  74. setChunkSize( chunkSize()/2 );
  75. if ( chunkSize() >= 1 ) {
  76. kdDebug(5150) << "QGpgMEKeyListJob::start(): retrying keylisting with chunksize " << chunkSize() << endl;
  77. continue;
  78. }
  79. } else if ( err.code() == GPG_ERR_EOF ) {
  80. kdDebug(5150) << "QGpgMEKeyListJob::start(): early end of keylisting, trying to fake an empty result" << endl;
  81. TQTimer::singleShot( 10, this, TQT_SLOT(slotFakeOperationDoneEvent()) );
  82. return GpgME::Error();
  83. }
  84. deleteLater();
  85. mResult = GpgME::KeyListResult( 0, err );
  86. return err;
  87. }
  88. mResult = GpgME::KeyListResult( 0, 0 );
  89. return 0;
  90. }
  91. GpgME::KeyListResult Kleo::QGpgMEKeyListJob::exec( const TQStringList & pats, bool secretOnly, std::vector<GpgME::Key> & keys ) {
  92. setup( pats, secretOnly );
  93. // The communication channel between gpgme and gpgsm is limited in
  94. // the number of patterns that can be transported, but they won't
  95. // say to how much, so we need to find out ourselves if we get a
  96. // LINE_TOO_LONG error back...
  97. // We could of course just feed them single patterns, and that would
  98. // probably be easier, but the performance penalty would currently
  99. // be noticable.
  100. for (;;) {
  101. keys.clear();
  102. mResult = attemptSyncKeyListing( keys );
  103. if ( !mResult.error() || mResult.error().code() != GPG_ERR_LINE_TOO_LONG )
  104. return mResult;
  105. // got LINE_TOO_LONG, try a smaller chunksize:
  106. setChunkSize( chunkSize()/2 );
  107. if ( chunkSize() < 1 )
  108. // chunks smaller than one can't be -> return the error.
  109. return mResult;
  110. kdDebug(5150) << "QGpgMEKeyListJob::exec(): retrying keylisting with chunksize " << chunkSize() << endl;
  111. }
  112. kdFatal(5150) << "QGpgMEKeyListJob::exec(): Oops, this is not supposed to happen!" << endl;
  113. return GpgME::KeyListResult();
  114. }
  115. GpgME::KeyListResult Kleo::QGpgMEKeyListJob::attemptSyncKeyListing( std::vector<GpgME::Key> & keys ) {
  116. GpgME::KeyListResult result;
  117. for ( const char* * chunk = patterns() ; chunk ; chunk = nextChunk() ) {
  118. if ( const GpgME::Error err = mCtx->startKeyListing( chunk, mSecretOnly ) )
  119. return GpgME::KeyListResult( 0, err );
  120. GpgME::Error err;
  121. do
  122. keys.push_back( mCtx->nextKey( err ) );
  123. while ( !err );
  124. keys.pop_back();
  125. result.mergeWith( mCtx->endKeyListing() );
  126. if ( result.error() )
  127. break;
  128. }
  129. return result;
  130. }
  131. void Kleo::QGpgMEKeyListJob::slotNextKeyEvent( GpgME::Context * context, const GpgME::Key & key ) {
  132. if ( context == mCtx )
  133. emit nextKey( key );
  134. }
  135. void Kleo::QGpgMEKeyListJob::slotFakeOperationDoneEvent() {
  136. const GpgME::KeyListResult res = mCtx->keyListResult();
  137. if ( !res.error().code() == GPG_ERR_EOF )
  138. kdDebug(5150) << "QGpgMEKeyListJob::slotFakeOperationDoneEvent: expected EOF, got "
  139. << res.error().asString() << endl;
  140. mResult = GpgME::KeyListResult();
  141. emit done();
  142. emit result( mResult );
  143. deleteLater();
  144. }
  145. void Kleo::QGpgMEKeyListJob::slotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & ) {
  146. if ( context != mCtx )
  147. return;
  148. mResult.mergeWith( mCtx->keyListResult() );
  149. if ( !mResult.error() )
  150. if ( const char* * chunk = nextChunk() ) {
  151. if ( const GpgME::Error err = mCtx->startKeyListing( chunk, mSecretOnly ) )
  152. mResult.mergeWith( GpgME::KeyListResult( 0, err ) );
  153. else
  154. return;
  155. }
  156. emit done();
  157. emit result( mResult );
  158. deleteLater();
  159. }
  160. void Kleo::QGpgMEKeyListJob::showErrorDialog( TQWidget * parent, const TQString & caption ) const {
  161. if ( !mResult.error() || mResult.error().isCanceled() )
  162. return;
  163. const TQString msg = i18n( "<qt><p>An error occurred while fetching "
  164. "the keys from the backend:</p>"
  165. "<p><b>%1</b></p></qt>" )
  166. .arg( TQString::fromLocal8Bit( mResult.error().asString() ) );
  167. KMessageBox::error( parent, msg, caption );
  168. }
  169. #include "qgpgmekeylistjob.moc"