37 #include "qgpgmejob.h"
38 #include "qgpgmeprogresstokenmapper.h"
41 #include <ui/passphrasedialog.h>
43 #include <qgpgme/eventloopinteractor.h>
44 #include <qgpgme/dataprovider.h>
46 #include <gpgmepp/context.h>
47 #include <gpgmepp/data.h>
50 #include <kstandarddirs.h>
53 #include <tqstringlist.h>
61 class InvarianceChecker {
70 _this->checkInvariants();
72 ~InvarianceChecker() {
73 _this->checkInvariants();
81 Kleo::QGpgMEJob::QGpgMEJob(
Kleo::Job * _this, GpgME::Context * context )
82 : GpgME::ProgressProvider(),
83 GpgME::PassphraseProvider(),
87 mInDataDataProvider( 0 ),
89 mOutDataDataProvider( 0 ),
91 mReplacedPattern( 0 ),
94 mPatternStartIndex( 0 ), mPatternEndIndex( 0 )
96 InvarianceChecker check(
this );
98 TQObject::connect( QGpgME::EventLoopInteractor::instance(), TQT_SIGNAL(aboutToDestroy()),
99 _this, TQT_SLOT(slotCancel()) );
100 context->setProgressProvider(
this );
104 if ( context->protocol() == GpgME::Context::OpenPGP )
105 context->setPassphraseProvider(
this );
108 void Kleo::QGpgMEJob::checkInvariants()
const {
111 assert( mPatterns[mNumPatterns] == 0 );
112 if ( mPatternEndIndex > 0 ) {
113 assert( mPatternEndIndex > mPatternStartIndex );
114 assert( mPatternEndIndex - mPatternStartIndex == mChunkSize );
116 assert( mPatternEndIndex == mPatternStartIndex );
118 if ( mPatternEndIndex < mNumPatterns ) {
119 assert( mPatterns[mPatternEndIndex] == 0 );
120 assert( mReplacedPattern != 0 );
122 assert( mReplacedPattern == 0 );
125 assert( mNumPatterns == 0 );
126 assert( mPatternStartIndex == 0 );
127 assert( mPatternEndIndex == 0 );
128 assert( mReplacedPattern == 0 );
133 Kleo::QGpgMEJob::~QGpgMEJob() {
134 InvarianceChecker check(
this );
135 delete mCtx; mCtx = 0;
136 delete mInData; mInData = 0;
137 delete mInDataDataProvider; mInDataDataProvider = 0;
138 delete mOutData; mOutData = 0;
139 delete mOutDataDataProvider; mOutDataDataProvider = 0;
143 void Kleo::QGpgMEJob::deleteAllPatterns() {
145 for (
unsigned int i = 0 ; i < mNumPatterns ; ++i )
146 free( (
void*)mPatterns[i] );
147 free( (
void*)mReplacedPattern ); mReplacedPattern = 0;
148 delete[] mPatterns; mPatterns = 0;
149 mPatternEndIndex = mPatternStartIndex = mNumPatterns = 0;
153 mCtx->setManagedByEventLoopInteractor(
true );
154 TQObject::connect( QGpgME::EventLoopInteractor::instance(),
155 TQT_SIGNAL(operationDoneEventSignal(GpgME::Context*,
const GpgME::Error&)),
156 mThis, TQT_SLOT(slotOperationDoneEvent(GpgME::Context*,
const GpgME::Error&)) );
160 InvarianceChecker check(
this );
163 mPatterns =
new const char*[ sl.size() + 1 ];
164 const char* * pat_it = mPatterns;
166 for ( TQStringList::const_iterator it = sl.begin() ; it != sl.end() ; ++it ) {
167 if ( (*it).isNull() )
169 if ( (*it).isEmpty() && !allowEmpty )
171 *pat_it++ = strdup( (*it).utf8().data() );
175 mReplacedPattern = 0;
176 mPatternEndIndex = mChunkSize = mNumPatterns;
180 InvarianceChecker check(
this );
181 if ( mReplacedPattern ) {
182 mPatterns[mPatternEndIndex] = mReplacedPattern;
183 mReplacedPattern = 0;
185 mChunkSize = std::min( chunksize, mNumPatterns );
186 mPatternStartIndex = 0;
187 mPatternEndIndex = mChunkSize;
188 mReplacedPattern = mPatterns[mPatternEndIndex];
189 mPatterns[mPatternEndIndex] = 0;
193 InvarianceChecker check(
this );
194 if ( mReplacedPattern ) {
195 mPatterns[mPatternEndIndex] = mReplacedPattern;
196 mReplacedPattern = 0;
198 mPatternStartIndex += mChunkSize;
199 mPatternEndIndex += mChunkSize;
200 if ( mPatternEndIndex < mNumPatterns ) {
201 mReplacedPattern = mPatterns[mPatternEndIndex];
202 mPatterns[mPatternEndIndex] = 0;
208 InvarianceChecker check(
this );
209 if ( mPatternStartIndex < mNumPatterns )
210 return mPatterns + mPatternStartIndex;
215 mCtx->clearSigningKeys();
216 for ( std::vector<GpgME::Key>::const_iterator it = signers.begin() ; it != signers.end() ; ++it ) {
217 if ( (*it).isNull() )
219 if (
const GpgME::Error err = mCtx->addSigningKey( *it ) )
226 mInDataDataProvider =
new QGpgME::TQByteArrayDataProvider( in );
227 mInData =
new GpgME::Data( mInDataDataProvider );
228 assert( !mInData->isNull() );
232 mOutDataDataProvider =
new QGpgME::TQByteArrayDataProvider();
233 mOutData =
new GpgME::Data( mOutDataDataProvider );
234 assert( !mOutData->isNull() );
237 static const unsigned int GetAuditLogFlags = GpgME::Context::AuditLogWithHelp|GpgME::Context::HtmlAuditLog;
239 static TQString audit_log_as_html( GpgME::Context * ctx, GpgME::Error & err ) {
241 QGpgME::TQByteArrayDataProvider dp;
242 GpgME::Data data( &dp );
243 assert( !data.isNull() );
244 if ( ( err = ctx->getAuditLog( data, GetAuditLogFlags ) ) )
246 const TQByteArray ba = dp.data();
247 return TQString::fromUtf8( ba.data(), ba.size() );
251 if ( context == mCtx ) {
253 doOperationDoneEvent( e );
254 mThis->deleteLater();
261 mAuditLogAsHtml = audit_log_as_html( mCtx, mAuditLogError );
264 void Kleo::QGpgMEJob::doSlotCancel() {
265 mCtx->cancelPendingOperation();
268 void Kleo::QGpgMEJob::showProgress(
const char * what,
int type,
int current,
int total ) {
269 doEmitProgressSignal( QGpgMEProgressTokenMapper::instance()->map( what, type, current, total ), current, total );
272 char * Kleo::QGpgMEJob::getPassphrase(
const char * useridHint,
const char * ,
273 bool previousWasBad,
bool & canceled ) {
276 TQString msg = previousWasBad ?
277 i18n(
"You need a passphrase to unlock the secret key for user:<br/> %1 (retry)" ) :
278 i18n(
"You need a passphrase to unlock the secret key for user:<br/> %1" );
279 msg = msg.arg( TQString::fromUtf8( useridHint ) ) +
"<br/><br/>";
280 msg.prepend(
"<qt>" );
281 msg += i18n(
"This dialog will reappear every time the passphrase is needed. For a more secure solution that also allows caching the passphrase, use gpg-agent." ) +
"<br/>";
282 const TQString gpgAgent = KStandardDirs::findExe(
"gpg-agent" );
283 if ( !gpgAgent.isEmpty() ) {
284 msg += i18n(
"gpg-agent was found in %1, but does not appear to be running." )
287 msg += i18n(
"gpg-agent is part of gnupg-%1, which you can download from %2" )
289 .arg(
"http://www.gnupg.org/download" );
292 msg += i18n(
"For information on how to set up gpg-agent, see %1" )
293 .arg(
"http://kmail.kde.org/kmail-pgpmime-howto.html" );
295 msg += i18n(
"Enter passphrase:" );
296 Kleo::PassphraseDialog dlg( msg, i18n(
"Passphrase Dialog") );
297 if ( dlg.exec() != TQDialog::Accepted ) {
303 return strdup( dlg.passphrase() );