certmanager/lib

qgpgmesecretkeyexportjob.cpp

00001 /*
00002     qgpgmesecretexportjob.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 "qgpgmesecretkeyexportjob.h"
00038 
00039 #include "gnupgprocessbase.h"
00040 #include "qgpgmeprogresstokenmapper.h"
00041 
00042 #include <kdebug.h>
00043 
00044 #include <gpgmepp/context.h>
00045 #include <gpgmepp/data.h>
00046 
00047 #include <qgpgme/eventloopinteractor.h>
00048 
00049 #include <qstringlist.h>
00050 
00051 #include <gpg-error.h>
00052 
00053 #include <string.h>
00054 #include <assert.h>
00055 
00056 Kleo::QGpgMESecretKeyExportJob::QGpgMESecretKeyExportJob( bool armour )
00057   : ExportJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMESecretKeyExportJob" ),
00058     mProcess( 0 ),
00059     mError( 0 ),
00060     mArmour( armour )
00061 {
00062 
00063 }
00064 
00065 Kleo::QGpgMESecretKeyExportJob::~QGpgMESecretKeyExportJob() {
00066 
00067 }
00068 
00069 GpgME::Error Kleo::QGpgMESecretKeyExportJob::start( const QStringList & patterns ) {
00070   assert( mKeyData.isEmpty() );
00071 
00072   if ( patterns.size() != 1 || patterns.front().isEmpty() ) {
00073     deleteLater();
00074     return mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_INV_VALUE );
00075   }
00076 
00077   // create and start gpgsm process:
00078   mProcess = new GnuPGProcessBase( this, "gpgsm --export-secret-key-p12" );
00079 
00080   // FIXME: obbtain the path to gpgsm from gpgme, so we use the same instance.
00081   *mProcess << "gpgsm" << "--export-secret-key-p12";
00082   if ( mArmour )
00083     *mProcess << "--armor";
00084   *mProcess << patterns.front().utf8();
00085 
00086   mProcess->setUseStatusFD( true );
00087 
00088   connect( mProcess, SIGNAL(processExited(KProcess*)),
00089        SLOT(slotProcessExited(KProcess*)) );
00090   connect( mProcess, SIGNAL(receivedStdout(KProcess*,char*,int)),
00091        SLOT(slotStdout(KProcess*,char*,int)) );
00092   connect( mProcess, SIGNAL(receivedStderr(KProcess*,char*,int)),
00093        SLOT(slotStderr(KProcess*,char*,int)) );
00094   connect( mProcess, SIGNAL(status(Kleo::GnuPGProcessBase*,const QString&,const QStringList&)),
00095        SLOT(slotStatus(Kleo::GnuPGProcessBase*,const QString&,const QStringList&)) );
00096 
00097   if ( !mProcess->start( KProcess::NotifyOnExit, KProcess::AllOutput ) ) {
00098     mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_ENOENT ); // what else?
00099     deleteLater();
00100     return mError;
00101   } else
00102     return 0;
00103 }
00104 
00105 void Kleo::QGpgMESecretKeyExportJob::slotCancel() {
00106   if ( mProcess )
00107     mProcess->kill();
00108   mProcess = 0;
00109   mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_CANCELED );
00110 }
00111 
00112 void Kleo::QGpgMESecretKeyExportJob::slotStatus( GnuPGProcessBase * proc, const QString & type, const QStringList & args ) {
00113   if ( proc != mProcess )
00114     return;
00115   QStringList::const_iterator it = args.begin();
00116   bool ok = false;
00117 
00118   if ( type == "ERROR" ) {
00119 
00120 
00121     if ( args.size() < 2 ) {
00122       kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() not recognising ERROR with < 2 args!" << endl;
00123       return;
00124     }
00125     const int source = (*++it).toInt( &ok );
00126     if ( !ok ) {
00127       kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for first ERROR arg, got something else" << endl;
00128       return;
00129     }
00130     ok = false;
00131     const int code = (*++it).toInt( &ok );
00132     if ( !ok ) {
00133       kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for second ERROR arg, got something else" << endl;
00134       return;
00135     }
00136     mError = gpg_err_make( (gpg_err_source_t)source, (gpg_err_code_t)code );
00137 
00138 
00139   } else if ( type == "PROGRESS" ) {
00140 
00141 
00142     if ( args.size() < 4 ) {
00143       kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() not recognising PROGRESS with < 4 args!" << endl;
00144       return;
00145     }
00146     const QString what = *++it;
00147     ++it; // don't use "type"...
00148     const int cur = (*++it).toInt( &ok );
00149     if ( !ok ) {
00150       kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for \"cur\", got something else" << endl;
00151       return;
00152     }
00153     ok = false;
00154     const int total = (*++it).toInt( &ok );
00155     if ( !ok ) {
00156       kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for \"total\", got something else" << endl;
00157       return;
00158     }
00159     emit progress( QGpgMEProgressTokenMapper::instance()->map( what, 0, cur, total ), cur, total );
00160 
00161 
00162   }
00163 }
00164 
00165 void Kleo::QGpgMESecretKeyExportJob::slotStdout( KProcess * proc, char * buf, int buflen ) {
00166   if ( proc != mProcess )
00167     return;
00168   if ( buflen <= 0 )
00169     return;
00170   if ( !buf )
00171     return;
00172   const unsigned int oldlen = mKeyData.size();
00173   mKeyData.resize( oldlen + buflen );
00174   memcpy( mKeyData.data() + oldlen, buf, buflen );
00175 }
00176 
00177 void Kleo::QGpgMESecretKeyExportJob::slotStderr( KProcess *, char *, int ) {
00178   // implement? or not?
00179 }
00180 
00181 void Kleo::QGpgMESecretKeyExportJob::slotProcessExited( KProcess * proc ) {
00182   if ( proc != mProcess )
00183     return;
00184 
00185   emit done();
00186   if ( !mError &&
00187        ( !mProcess->normalExit() || mProcess->exitStatus() != 0 ) )
00188     mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_GENERAL );
00189   emit result( mError, mKeyData );
00190   deleteLater();
00191 }
00192 
00193 #include "qgpgmesecretkeyexportjob.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys