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