kmail

objecttreeparser_p.cpp

00001 /*  -*- mode: C++; c-file-style: "gnu" -*-
00002     objecttreeparser_p.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2009 Klarälvdalens Datakonsult AB
00006     Authors: Marc Mutz <marc@kdab.net>
00007 
00008     KMail is free software; you can redistribute it and/or modify it
00009     under the terms of the GNU General Public License, version 2, as
00010     published by the Free Software Foundation.
00011 
00012     KMail is distributed in the hope that it will be useful, but
00013     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 #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(); // exec'ed jobs don't delete themselves
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(); // exec'ed jobs don't delete themselves
00156   m_job = 0;
00157   if ( canStartKeyListJob() ) {
00158     std::vector<GpgME::Key> keys;
00159     m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
00160     if ( !keys.empty() )
00161       m_key = keys.back();
00162   }
00163   if ( m_keylistjob )
00164     m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
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(); // exec'ed jobs don't delete themselves
00267   m_job = 0;
00268   if ( canStartKeyListJob() ) {
00269     std::vector<GpgME::Key> keys;
00270     m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
00271     if ( !keys.empty() )
00272       m_key = keys.back();
00273   }
00274   if ( m_keylistjob )
00275     m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
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"
KDE Home | KDE Accessibility Home | Description of Access Keys