certmanager Library API Documentation

qgpgmejob.cpp

00001 /*
00002     qgpgmejob.cpp
00003 
00004     This file is part of libkleopatra, the KDE keymanagement library
00005     Copyright (c) 2004 Klarälvdalens Datakonsult AB
00006 
00007     Libkleopatra is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU General Public License as
00009     published by the Free Software Foundation; either version 2 of the
00010     License, or (at your option) any later version.
00011 
00012     Libkleopatra is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
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   // (mmutz) work around a gpgme bug in versions at least <= 0.9.0.
00073   //         These versions will return GPG_ERR_NOT_IMPLEMENTED from
00074   //         a CMS sign operation when a passphrase callback is set.
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   // create a new null-terminated C array of char* from patterns:
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 * /*description*/,
00152                        bool previousWasBad, bool & canceled ) {
00153   // DF: here, description is the key fingerprint, twice, then "17 0". Not really descriptive.
00154   //     So I'm ignoring QString::fromLocal8Bit( description ) )
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" );  // add #gnupg2 if you can make this a real link
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   // gpgme++ free()s it, and we need to copy as long as dlg isn't deleted :o
00171   return strdup( dlg.passphrase() );
00172 }
KDE Logo
This file is part of the documentation for certmanager Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:39:33 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003