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 #include <config.h>
00034
00035 #include "objecttreeparser_p.h"
00036
00037 #include <kleo/decryptverifyjob.h>
00038 #include <kleo/verifydetachedjob.h>
00039 #include <kleo/verifyopaquejob.h>
00040 #include <kleo/keylistjob.h>
00041
00042 #include <gpgmepp/keylistresult.h>
00043
00044 #include <qtimer.h>
00045 #include <qstringlist.h>
00046
00047 #include <cassert>
00048
00049 using namespace KMail;
00050 using namespace Kleo;
00051 using namespace GpgME;
00052
00053 DecryptVerifyBodyPartMemento::DecryptVerifyBodyPartMemento( DecryptVerifyJob * job, const QByteArray & cipherText )
00054 : QObject( 0 ),
00055 Interface::BodyPartMemento(),
00056 ISubject(),
00057 m_cipherText( cipherText ),
00058 m_job( job ),
00059 m_running( false )
00060 {
00061 assert( m_job );
00062 }
00063
00064 DecryptVerifyBodyPartMemento::~DecryptVerifyBodyPartMemento() {
00065 if ( m_job )
00066 m_job->slotCancel();
00067 }
00068
00069 bool DecryptVerifyBodyPartMemento::start() {
00070 assert( m_job );
00071 if ( const Error err = m_job->start( m_cipherText ) ) {
00072 m_dr = DecryptionResult( err );
00073 return false;
00074 }
00075 connect( m_job, SIGNAL(result(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const QByteArray&)),
00076 this, SLOT(slotResult(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const QByteArray&)) );
00077 m_running = true;
00078 return true;
00079 }
00080
00081 void DecryptVerifyBodyPartMemento::exec() {
00082 assert( m_job );
00083 QByteArray plainText;
00084 m_running = true;
00085 const std::pair<DecryptionResult,VerificationResult> p = m_job->exec( m_cipherText, plainText );
00086 saveResult( p.first, p.second, plainText );
00087 m_job->deleteLater();
00088 m_job = 0;
00089 }
00090
00091 void DecryptVerifyBodyPartMemento::saveResult( const DecryptionResult & dr,
00092 const VerificationResult & vr,
00093 const QByteArray & plainText )
00094 {
00095 assert( m_job );
00096 m_running = false;
00097 m_dr = dr;
00098 m_vr = vr;
00099 m_plainText = plainText;
00100 m_auditLog = m_job->auditLogAsHtml();
00101 m_auditLogError = m_job->auditLogError();
00102 }
00103
00104 void DecryptVerifyBodyPartMemento::slotResult( const DecryptionResult & dr,
00105 const VerificationResult & vr,
00106 const QByteArray & plainText )
00107 {
00108 saveResult( dr, vr, plainText );
00109 m_job = 0;
00110 QTimer::singleShot( 100, this, SLOT(notify()) );
00111 }
00112
00113
00114
00115
00116 VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob * job,
00117 KeyListJob * klj,
00118 const QByteArray & signature,
00119 const QByteArray & plainText )
00120 : QObject( 0 ),
00121 Interface::BodyPartMemento(),
00122 ISubject(),
00123 m_signature( signature ),
00124 m_plainText( plainText ),
00125 m_job( job ),
00126 m_keylistjob( klj ),
00127 m_running( false )
00128 {
00129 assert( m_job );
00130 }
00131
00132 VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento() {
00133 if ( m_job )
00134 m_job->slotCancel();
00135 if ( m_keylistjob )
00136 m_keylistjob->slotCancel();
00137 }
00138
00139 bool VerifyDetachedBodyPartMemento::start() {
00140 assert( m_job );
00141 if ( const Error err = m_job->start( m_signature, m_plainText ) ) {
00142 m_vr = VerificationResult( err );
00143 return false;
00144 }
00145 connect( m_job, SIGNAL(result(const GpgME::VerificationResult&)),
00146 this, SLOT(slotResult(const GpgME::VerificationResult&)) );
00147 m_running = true;
00148 return true;
00149 }
00150
00151 void VerifyDetachedBodyPartMemento::exec() {
00152 assert( m_job );
00153 m_running = true;
00154 saveResult( m_job->exec( m_signature, m_plainText ) );
00155 m_job->deleteLater();
00156 m_job = 0;
00157 if ( canStartKeyListJob() ) {
00158 std::vector<GpgME::Key> keys;
00159 m_keylistjob->exec( keyListPattern(), false, keys );
00160 if ( !keys.empty() )
00161 m_key = keys.back();
00162 }
00163 if ( m_keylistjob )
00164 m_keylistjob->deleteLater();
00165 m_keylistjob = 0;
00166 m_running = false;
00167 }
00168
00169 bool VerifyDetachedBodyPartMemento::canStartKeyListJob() const
00170 {
00171 if ( !m_keylistjob )
00172 return false;
00173 const char * const fpr = m_vr.signature( 0 ).fingerprint();
00174 return fpr && *fpr;
00175 }
00176
00177 QStringList VerifyDetachedBodyPartMemento::keyListPattern() const
00178 {
00179 assert( canStartKeyListJob() );
00180 return QStringList( QString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
00181 }
00182
00183 void VerifyDetachedBodyPartMemento::saveResult( const VerificationResult & vr )
00184 {
00185 assert( m_job );
00186 m_vr = vr;
00187 m_auditLog = m_job->auditLogAsHtml();
00188 m_auditLogError = m_job->auditLogError();
00189 }
00190
00191 void VerifyDetachedBodyPartMemento::slotResult( const VerificationResult & vr )
00192 {
00193 saveResult( vr );
00194 m_job = 0;
00195 if ( canStartKeyListJob() && startKeyListJob() )
00196 return;
00197 if ( m_keylistjob )
00198 m_keylistjob->deleteLater();
00199 m_keylistjob = 0;
00200 m_running = false;
00201 QTimer::singleShot( 100, this, SLOT(notify()) );
00202 }
00203
00204 bool VerifyDetachedBodyPartMemento::startKeyListJob()
00205 {
00206 assert( canStartKeyListJob() );
00207 if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
00208 return false;
00209 connect( m_keylistjob, SIGNAL(done()), this, SLOT(slotKeyListJobDone()) );
00210 connect( m_keylistjob, SIGNAL(nextKey(const GpgME::Key&)),
00211 this, SLOT(slotNextKey(const GpgME::Key&)) );
00212 return true;
00213 }
00214
00215 void VerifyDetachedBodyPartMemento::slotNextKey( const GpgME::Key & key )
00216 {
00217 m_key = key;
00218 }
00219
00220 void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
00221 {
00222 m_keylistjob = 0;
00223 m_running = false;
00224 QTimer::singleShot( 100, this, SLOT(notify()) );
00225 }
00226
00227
00228 VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento( VerifyOpaqueJob * job,
00229 KeyListJob * klj,
00230 const QByteArray & signature )
00231 : QObject( 0 ),
00232 Interface::BodyPartMemento(),
00233 ISubject(),
00234 m_signature( signature ),
00235 m_job( job ),
00236 m_keylistjob( klj ),
00237 m_running( false )
00238 {
00239 assert( m_job );
00240 }
00241
00242 VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento() {
00243 if ( m_job )
00244 m_job->slotCancel();
00245 if ( m_keylistjob )
00246 m_keylistjob->slotCancel();
00247 }
00248
00249 bool VerifyOpaqueBodyPartMemento::start() {
00250 assert( m_job );
00251 if ( const Error err = m_job->start( m_signature ) ) {
00252 m_vr = VerificationResult( err );
00253 return false;
00254 }
00255 connect( m_job, SIGNAL(result(const GpgME::VerificationResult&,const QByteArray&)),
00256 this, SLOT(slotResult(const GpgME::VerificationResult&,const QByteArray&)) );
00257 m_running = true;
00258 return true;
00259 }
00260
00261 void VerifyOpaqueBodyPartMemento::exec() {
00262 assert( m_job );
00263 m_running = true;
00264 QByteArray plainText;
00265 saveResult( m_job->exec( m_signature, plainText ), plainText );
00266 m_job->deleteLater();
00267 m_job = 0;
00268 if ( canStartKeyListJob() ) {
00269 std::vector<GpgME::Key> keys;
00270 m_keylistjob->exec( keyListPattern(), false, keys );
00271 if ( !keys.empty() )
00272 m_key = keys.back();
00273 }
00274 if ( m_keylistjob )
00275 m_keylistjob->deleteLater();
00276 m_keylistjob = 0;
00277 m_running = false;
00278 }
00279
00280 bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
00281 {
00282 if ( !m_keylistjob )
00283 return false;
00284 const char * const fpr = m_vr.signature( 0 ).fingerprint();
00285 return fpr && *fpr;
00286 }
00287
00288 QStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
00289 {
00290 assert( canStartKeyListJob() );
00291 return QStringList( QString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
00292 }
00293
00294 void VerifyOpaqueBodyPartMemento::saveResult( const VerificationResult & vr,
00295 const QByteArray & plainText )
00296 {
00297 assert( m_job );
00298 m_vr = vr;
00299 m_plainText = plainText;
00300 m_auditLog = m_job->auditLogAsHtml();
00301 m_auditLogError = m_job->auditLogError();
00302 }
00303
00304 void VerifyOpaqueBodyPartMemento::slotResult( const VerificationResult & vr,
00305 const QByteArray & plainText )
00306 {
00307 saveResult( vr, plainText );
00308 m_job = 0;
00309 if ( canStartKeyListJob() && startKeyListJob() )
00310 return;
00311 if ( m_keylistjob )
00312 m_keylistjob->deleteLater();
00313 m_keylistjob = 0;
00314 m_running = false;
00315 QTimer::singleShot( 100, this, SLOT(notify()) );
00316 }
00317
00318 bool VerifyOpaqueBodyPartMemento::startKeyListJob()
00319 {
00320 assert( canStartKeyListJob() );
00321 if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
00322 return false;
00323 connect( m_keylistjob, SIGNAL(done()), this, SLOT(slotKeyListJobDone()) );
00324 connect( m_keylistjob, SIGNAL(nextKey(const GpgME::Key&)),
00325 this, SLOT(slotNextKey(const GpgME::Key&)) );
00326 return true;
00327 }
00328
00329 void VerifyOpaqueBodyPartMemento::slotNextKey( const GpgME::Key & key )
00330 {
00331 m_key = key;
00332 }
00333
00334 void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
00335 {
00336 m_keylistjob = 0;
00337 m_running = false;
00338 QTimer::singleShot( 100, this, SLOT(notify()) );
00339 }
00340
00341
00342 #include "objecttreeparser_p.moc"