37 #include "qgpgmekeylistjob.h"
39 #include <qgpgme/eventloopinteractor.h>
41 #include <gpgmepp/key.h>
42 #include <gpgmepp/context.h>
43 #include <gpgmepp/keylistresult.h>
44 #include <gpg-error.h>
46 #include <tdemessagebox.h>
47 #include <tdelocale.h>
50 #include <tqstringlist.h>
59 Kleo::QGpgMEKeyListJob::QGpgMEKeyListJob( GpgME::Context * context )
60 : KeyListJob( QGpgME::EventLoopInteractor::instance(),
"Kleo::QGpgMEKeyListJob" ),
61 QGpgMEJob( this, context ),
62 mResult(), mSecretOnly( false )
67 Kleo::QGpgMEKeyListJob::~QGpgMEKeyListJob() {
70 void Kleo::QGpgMEKeyListJob::setup(
const TQStringList & pats,
bool secretOnly ) {
71 assert( !patterns() );
73 mSecretOnly = secretOnly;
77 GpgME::Error Kleo::QGpgMEKeyListJob::start(
const TQStringList & pats,
bool secretOnly ) {
78 setup( pats, secretOnly );
80 hookupContextToEventLoopInteractor();
81 connect( QGpgME::EventLoopInteractor::instance(),
82 TQ_SIGNAL(nextKeyEventSignal(GpgME::Context*,
const GpgME::Key&)),
83 TQ_SLOT(slotNextKeyEvent(GpgME::Context*,
const GpgME::Key&)) );
94 while (
const GpgME::Error err = mCtx->startKeyListing( patterns(), mSecretOnly ) ) {
95 if ( err.code() == GPG_ERR_LINE_TOO_LONG ) {
96 setChunkSize( chunkSize()/2 );
97 if ( chunkSize() >= 1 ) {
98 kdDebug(5150) <<
"QGpgMEKeyListJob::start(): retrying keylisting with chunksize " << chunkSize() << endl;
101 }
else if ( err.code() == GPG_ERR_EOF ) {
102 kdDebug(5150) <<
"QGpgMEKeyListJob::start(): early end of keylisting, trying to fake an empty result" << endl;
103 TQTimer::singleShot( 10,
this, TQ_SLOT(slotFakeOperationDoneEvent()) );
104 return GpgME::Error();
107 mResult = GpgME::KeyListResult( 0, err );
110 mResult = GpgME::KeyListResult( 0, 0 );
114 GpgME::KeyListResult Kleo::QGpgMEKeyListJob::exec(
const TQStringList & pats,
bool secretOnly, std::vector<GpgME::Key> & keys ) {
115 setup( pats, secretOnly );
128 mResult = attemptSyncKeyListing( keys );
129 if ( !mResult.error() || mResult.error().code() != GPG_ERR_LINE_TOO_LONG )
132 setChunkSize( chunkSize()/2 );
133 if ( chunkSize() < 1 )
136 kdDebug(5150) <<
"QGpgMEKeyListJob::exec(): retrying keylisting with chunksize " << chunkSize() << endl;
138 kdFatal(5150) <<
"QGpgMEKeyListJob::exec(): Oops, this is not supposed to happen!" << endl;
139 return GpgME::KeyListResult();
142 GpgME::KeyListResult Kleo::QGpgMEKeyListJob::attemptSyncKeyListing( std::vector<GpgME::Key> & keys ) {
143 GpgME::KeyListResult result;
144 for (
const char* * chunk = patterns() ; chunk ; chunk = nextChunk() ) {
146 if (
const GpgME::Error err = mCtx->startKeyListing( chunk, mSecretOnly ) )
147 return GpgME::KeyListResult( 0, err );
151 keys.push_back( mCtx->nextKey( err ) );
154 result.mergeWith( mCtx->endKeyListing() );
155 if ( result.error() )
161 void Kleo::QGpgMEKeyListJob::slotNextKeyEvent( GpgME::Context * context,
const GpgME::Key & key ) {
162 if ( context == mCtx )
166 void Kleo::QGpgMEKeyListJob::slotFakeOperationDoneEvent() {
167 const GpgME::KeyListResult res = mCtx->keyListResult();
168 if ( !res.error().code() == GPG_ERR_EOF )
169 kdDebug(5150) <<
"QGpgMEKeyListJob::slotFakeOperationDoneEvent: expected EOF, got "
170 << res.error().asString() << endl;
171 mResult = GpgME::KeyListResult();
173 emit result( mResult );
177 void Kleo::QGpgMEKeyListJob::slotOperationDoneEvent( GpgME::Context * context,
const GpgME::Error & ) {
178 if ( context != mCtx )
180 mResult.mergeWith( mCtx->keyListResult() );
181 if ( !mResult.error() )
182 if (
const char* * chunk = nextChunk() ) {
183 if (
const GpgME::Error err = mCtx->startKeyListing( chunk, mSecretOnly ) )
184 mResult.mergeWith( GpgME::KeyListResult( 0, err ) );
189 emit result( mResult );
193 void Kleo::QGpgMEKeyListJob::showErrorDialog( TQWidget * parent,
const TQString & caption )
const {
194 if ( !mResult.error() || mResult.error().isCanceled() )
196 const TQString msg = i18n(
"<qt><p>An error occurred while fetching "
197 "the keys from the backend:</p>"
198 "<p><b>%1</b></p></qt>" )
199 .arg( TQString::fromLocal8Bit( mResult.error().asString() ) );
200 KMessageBox::error( parent, msg, caption );
203 #include "qgpgmekeylistjob.moc"