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 "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 <tqstringlist.h>
00050
00051 #include <gpg-error.h>
00052
00053 #include <string.h>
00054 #include <assert.h>
00055
00056 Kleo::QGpgMESecretKeyExportJob::QGpgMESecretKeyExportJob( bool armour, const TQString& charset )
00057 : ExportJob( QGpgME::EventLoopInteractor::instance(), "Kleo::QGpgMESecretKeyExportJob" ),
00058 mProcess( 0 ),
00059 mError( 0 ),
00060 mArmour( armour ),
00061 mCharset( charset )
00062 {
00063
00064 }
00065
00066 Kleo::QGpgMESecretKeyExportJob::~QGpgMESecretKeyExportJob() {
00067
00068 }
00069
00070 GpgME::Error Kleo::QGpgMESecretKeyExportJob::start( const TQStringList & patterns ) {
00071 assert( mKeyData.isEmpty() );
00072
00073 if ( patterns.size() != 1 || patterns.front().isEmpty() ) {
00074 deleteLater();
00075 return mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_INV_VALUE );
00076 }
00077
00078
00079 mProcess = new GnuPGProcessBase( this, "gpgsm --export-secret-key-p12" );
00080
00081
00082 *mProcess << "gpgsm" << "--export-secret-key-p12";
00083 if ( mArmour )
00084 *mProcess << "--armor";
00085 if ( !mCharset.isEmpty() )
00086 *mProcess << "--p12-charset" << mCharset;
00087 *mProcess << patterns.front().utf8();
00088
00089 mProcess->setUsetStatusFD( true );
00090
00091 connect( mProcess, TQT_SIGNAL(processExited(KProcess*)),
00092 TQT_SLOT(slotProcessExited(KProcess*)) );
00093 connect( mProcess, TQT_SIGNAL(receivedStdout(KProcess*,char*,int)),
00094 TQT_SLOT(slotStdout(KProcess*,char*,int)) );
00095 connect( mProcess, TQT_SIGNAL(receivedStderr(KProcess*,char*,int)),
00096 TQT_SLOT(slotStderr(KProcess*,char*,int)) );
00097 connect( mProcess, TQT_SIGNAL(status(Kleo::GnuPGProcessBase*,const TQString&,const TQStringList&)),
00098 TQT_SLOT(slotStatus(Kleo::GnuPGProcessBase*,const TQString&,const TQStringList&)) );
00099
00100 if ( !mProcess->start( KProcess::NotifyOnExit, KProcess::AllOutput ) ) {
00101 mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_ENOENT );
00102 deleteLater();
00103 return mError;
00104 } else
00105 return 0;
00106 }
00107
00108 void Kleo::QGpgMESecretKeyExportJob::slotCancel() {
00109 if ( mProcess )
00110 mProcess->kill();
00111 mProcess = 0;
00112 mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_CANCELED );
00113 }
00114
00115 void Kleo::QGpgMESecretKeyExportJob::slotStatus( GnuPGProcessBase * proc, const TQString & type, const TQStringList & args ) {
00116 if ( proc != mProcess )
00117 return;
00118 TQStringList::const_iterator it = args.begin();
00119 bool ok = false;
00120
00121 if ( type == "ERROR" ) {
00122
00123
00124 if ( args.size() < 2 ) {
00125 kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() not recognising ERROR with < 2 args!" << endl;
00126 return;
00127 }
00128 const int source = (*++it).toInt( &ok );
00129 if ( !ok ) {
00130 kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for first ERROR arg, got something else" << endl;
00131 return;
00132 }
00133 ok = false;
00134 const int code = (*++it).toInt( &ok );
00135 if ( !ok ) {
00136 kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for second ERROR arg, got something else" << endl;
00137 return;
00138 }
00139 mError = gpg_err_make( (gpg_err_source_t)source, (gpg_err_code_t)code );
00140
00141
00142 } else if ( type == "PROGRESS" ) {
00143
00144
00145 if ( args.size() < 4 ) {
00146 kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() not recognising PROGRESS with < 4 args!" << endl;
00147 return;
00148 }
00149 const TQString what = *++it;
00150 ++it;
00151 const int cur = (*++it).toInt( &ok );
00152 if ( !ok ) {
00153 kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for \"cur\", got something else" << endl;
00154 return;
00155 }
00156 ok = false;
00157 const int total = (*++it).toInt( &ok );
00158 if ( !ok ) {
00159 kdDebug( 5150 ) << "Kleo::QGpgMESecretKeyExportJob::slotStatus() expected number for \"total\", got something else" << endl;
00160 return;
00161 }
00162 emit progress( QGpgMEProgressTokenMapper::instance()->map( what, 0, cur, total ), cur, total );
00163
00164
00165 }
00166 }
00167
00168 void Kleo::QGpgMESecretKeyExportJob::slotStdout( KProcess * proc, char * buf, int buflen ) {
00169 if ( proc != mProcess )
00170 return;
00171 if ( buflen <= 0 )
00172 return;
00173 if ( !buf )
00174 return;
00175 const unsigned int oldlen = mKeyData.size();
00176 mKeyData.resize( oldlen + buflen );
00177 memcpy( mKeyData.data() + oldlen, buf, buflen );
00178 }
00179
00180 void Kleo::QGpgMESecretKeyExportJob::slotStderr( KProcess *, char *, int ) {
00181
00182 }
00183
00184 void Kleo::QGpgMESecretKeyExportJob::slotProcessExited( KProcess * proc ) {
00185 if ( proc != mProcess )
00186 return;
00187
00188 emit done();
00189 if ( !mError &&
00190 ( !mProcess->normalExit() || mProcess->exitStatus() != 0 ) )
00191 mError = gpg_err_make( GPG_ERR_SOURCE_GPGSM, GPG_ERR_GENERAL );
00192 emit result( mError, mKeyData );
00193 deleteLater();
00194 }
00195
00196 #include "qgpgmesecretkeyexportjob.moc"