qgpgmejob.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036
00037 #include "qgpgmejob.h"
00038 #include "qgpgmeprogresstokenmapper.h"
00039
00040 #include <kleo/job.h>
00041 #include <ui/passphrasedialog.h>
00042
00043 #include <qgpgme/eventloopinteractor.h>
00044 #include <qgpgme/dataprovider.h>
00045
00046 #include <gpgmepp/context.h>
00047 #include <gpgmepp/data.h>
00048
00049 #include <klocale.h>
00050
00051 #include <qstring.h>
00052 #include <qstringlist.h>
00053
00054 #include <assert.h>
00055 #include <string.h>
00056
00057 Kleo::QGpgMEJob::QGpgMEJob( Kleo::Job * _this, GpgME::Context * context )
00058 : GpgME::ProgressProvider(),
00059 GpgME::PassphraseProvider(),
00060 mThis( _this ),
00061 mCtx( context ),
00062 mPatterns( 0 ),
00063 mInData( 0 ),
00064 mInDataDataProvider( 0 ),
00065 mOutData( 0 ),
00066 mOutDataDataProvider( 0 )
00067 {
00068 assert( context );
00069 QObject::connect( QGpgME::EventLoopInteractor::instance(), SIGNAL(aboutToDestroy()),
00070 _this, SLOT(slotCancel()) );
00071 context->setProgressProvider( this );
00072
00073
00074
00075 if ( context->protocol() == GpgME::Context::OpenPGP )
00076 context->setPassphraseProvider( this );
00077 }
00078
00079 Kleo::QGpgMEJob::~QGpgMEJob() {
00080 delete mCtx; mCtx = 0;
00081 if ( mPatterns )
00082 for ( const char* * it = mPatterns ; *it ; ++it )
00083 free( (void*)*it );
00084 delete[] mPatterns; mPatterns = 0;
00085 delete mInData; mInData = 0;
00086 delete mInDataDataProvider; mInDataDataProvider = 0;
00087 delete mOutData; mOutData = 0;
00088 delete mOutDataDataProvider; mOutDataDataProvider = 0;
00089 }
00090
00091 void Kleo::QGpgMEJob::hookupContextToEventLoopInteractor() {
00092 mCtx->setManagedByEventLoopInteractor( true );
00093 QObject::connect( QGpgME::EventLoopInteractor::instance(),
00094 SIGNAL(operationDoneEventSignal(GpgME::Context*,const GpgME::Error&)),
00095 mThis, SLOT(slotOperationDoneEvent(GpgME::Context*,const GpgME::Error&)) );
00096 }
00097
00098 void Kleo::QGpgMEJob::setPatterns( const QStringList & sl, bool allowEmpty ) {
00099
00100 mPatterns = new const char*[ sl.size() + 1 ];
00101 const char* * pat_it = mPatterns;
00102 for ( QStringList::const_iterator it = sl.begin() ; it != sl.end() ; ++it ) {
00103 if ( (*it).isNull() )
00104 continue;
00105 if ( (*it).isEmpty() && !allowEmpty )
00106 continue;
00107 *pat_it++ = strdup( (*it).utf8().data() );
00108 }
00109 *pat_it++ = 0;
00110 }
00111
00112 GpgME::Error Kleo::QGpgMEJob::setSigningKeys( const std::vector<GpgME::Key> & signers ) {
00113 mCtx->clearSigningKeys();
00114 for ( std::vector<GpgME::Key>::const_iterator it = signers.begin() ; it != signers.end() ; ++it ) {
00115 if ( (*it).isNull() )
00116 continue;
00117 if ( const GpgME::Error err = mCtx->addSigningKey( *it ) )
00118 return err;
00119 }
00120 return 0;
00121 }
00122
00123 void Kleo::QGpgMEJob::createInData( const QByteArray & in ) {
00124 mInDataDataProvider = new QGpgME::QByteArrayDataProvider( in );
00125 mInData = new GpgME::Data( mInDataDataProvider );
00126 assert( !mInData->isNull() );
00127 }
00128
00129 void Kleo::QGpgMEJob::createOutData() {
00130 mOutDataDataProvider = new QGpgME::QByteArrayDataProvider();
00131 mOutData = new GpgME::Data( mOutDataDataProvider );
00132 assert( !mOutData->isNull() );
00133 }
00134
00135 void Kleo::QGpgMEJob::doSlotOperationDoneEvent( GpgME::Context * context, const GpgME::Error & e ) {
00136 if ( context == mCtx ) {
00137 doEmitDoneSignal();
00138 doOperationDoneEvent( e );
00139 mThis->deleteLater();
00140 }
00141 }
00142
00143 void Kleo::QGpgMEJob::doSlotCancel() {
00144 mCtx->cancelPendingOperation();
00145 }
00146
00147 void Kleo::QGpgMEJob::showProgress( const char * what, int type, int current, int total ) {
00148 doEmitProgressSignal( QGpgMEProgressTokenMapper::instance()->map( what, type, current, total ), current, total );
00149 }
00150
00151 char * Kleo::QGpgMEJob::getPassphrase( const char * useridHint, const char * ,
00152 bool previousWasBad, bool & canceled ) {
00153
00154
00155 QString msg = previousWasBad ?
00156 i18n( "You need a passphrase to unlock the secret key for user:<br/> %1 (retry)" ) :
00157 i18n( "You need a passphrase to unlock the secret key for user:<br/> %1" );
00158 msg = msg.arg( QString::fromUtf8( useridHint ) ) + "<br/><br/>";
00159 msg.prepend( "<qt>" );
00160 msg += i18n( "This dialog will reappear every time the passphrase is needed. For a more secure solution that also allows caching the passphrase, install gpg-agent." );
00161 msg += i18n( "gpg-agent is part of gnupg-%1, which you can download from %2" )
00162 .arg( "1.9" )
00163 .arg( "http://www.gnupg.org/download" );
00164 Kleo::PassphraseDialog dlg( msg, i18n("Passphrase Dialog") );
00165 if ( dlg.exec() != QDialog::Accepted ) {
00166 canceled = true;
00167 return 0;
00168 }
00169 canceled = false;
00170
00171 return strdup( dlg.passphrase() );
00172 }
This file is part of the documentation for certmanager Library Version 3.3.2.