kilobyte is kB not kb
[kdepim.git] / messageviewer / objecttreeparser_p.cpp
blob04d5045ce9a409f5dfbb98739c8d9711488a8c10
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 objecttreeparser_p.cpp
4 This file is part of KMail, the KDE mail client.
5 Copyright (C) 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
6 Copyright (c) 2009 Klarälvdalens Datakonsult AB
7 Authors: Marc Mutz <marc@kdab.net>
8 Copyright (c) 2009 Andras Mantia <andras@kdab.net>
10 KMail is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License, version 2, as
12 published by the Free Software Foundation.
14 KMail is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 In addition, as a special exception, the copyright holders give
24 permission to link the code of this program with any edition of
25 the Qt library by Trolltech AS, Norway (or with modified versions
26 of Qt that use the same license as Qt), and distribute linked
27 combinations including the two. You must obey the GNU General
28 Public License in all respects for all of the code used other than
29 Qt. If you modify this file, you may extend this exception to
30 your version of the file, but you are not obligated to do so. If
31 you do not wish to do so, delete this exception statement from
32 your version.
35 #include <config-messageviewer.h>
37 #include "objecttreeparser_p.h"
39 #include <kdebug.h>
40 #include <kleo/decryptverifyjob.h>
41 #include <kleo/verifydetachedjob.h>
42 #include <kleo/verifyopaquejob.h>
43 #include <kleo/keylistjob.h>
45 #include <gpgme++/keylistresult.h>
47 #include <qtimer.h>
48 #include <qstringlist.h>
50 #include <cassert>
52 using namespace Kleo;
53 using namespace GpgME;
54 using namespace MessageViewer;
56 CryptoBodyPartMemento::CryptoBodyPartMemento()
57 : QObject( 0 ),
58 Interface::BodyPartMemento(),
59 m_running( false )
64 CryptoBodyPartMemento::~CryptoBodyPartMemento() {}
66 void CryptoBodyPartMemento::setAuditLog( const Error & err, const QString & log ) {
67 m_auditLogError = err;
68 m_auditLog = log;
71 void CryptoBodyPartMemento::setRunning( bool running ) {
72 m_running = running;
75 void CryptoBodyPartMemento::detach()
77 disconnect( this, SIGNAL(update(MessageViewer::Viewer::UpdateMode)), 0, 0 );
80 DecryptVerifyBodyPartMemento::DecryptVerifyBodyPartMemento( DecryptVerifyJob * job, const QByteArray & cipherText )
81 : CryptoBodyPartMemento(),
82 m_cipherText( cipherText ),
83 m_job( job )
85 assert( m_job );
88 DecryptVerifyBodyPartMemento::~DecryptVerifyBodyPartMemento() {
89 if ( m_job )
90 m_job->slotCancel();
93 bool DecryptVerifyBodyPartMemento::start() {
94 assert( m_job );
95 if ( const Error err = m_job->start( m_cipherText ) ) {
96 m_dr = DecryptionResult( err );
97 return false;
99 connect( m_job, SIGNAL(result(GpgME::DecryptionResult,GpgME::VerificationResult,QByteArray)),
100 this, SLOT(slotResult(GpgME::DecryptionResult,GpgME::VerificationResult,QByteArray)) );
101 setRunning( true );
102 return true;
105 void DecryptVerifyBodyPartMemento::exec() {
106 assert( m_job );
107 QByteArray plainText;
108 setRunning( true );
109 const std::pair<DecryptionResult,VerificationResult> p = m_job->exec( m_cipherText, plainText );
110 saveResult( p.first, p.second, plainText );
111 m_job->deleteLater(); // exec'ed jobs don't delete themselves
112 m_job = 0;
115 void DecryptVerifyBodyPartMemento::saveResult( const DecryptionResult & dr,
116 const VerificationResult & vr,
117 const QByteArray & plainText )
119 assert( m_job );
120 setRunning( false );
121 m_dr = dr;
122 m_vr = vr;
123 m_plainText = plainText;
124 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
127 void DecryptVerifyBodyPartMemento::slotResult( const DecryptionResult & dr,
128 const VerificationResult & vr,
129 const QByteArray & plainText )
131 saveResult( dr, vr, plainText );
132 m_job = 0;
133 notify();
139 VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob * job,
140 KeyListJob * klj,
141 const QByteArray & signature,
142 const QByteArray & plainText )
143 : CryptoBodyPartMemento(),
144 m_signature( signature ),
145 m_plainText( plainText ),
146 m_job( job ),
147 m_keylistjob( klj )
149 assert( m_job );
152 VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento() {
153 if ( m_job )
154 m_job->slotCancel();
155 if ( m_keylistjob )
156 m_keylistjob->slotCancel();
159 bool VerifyDetachedBodyPartMemento::start() {
160 assert( m_job );
161 #ifdef DEBUG_SIGNATURE
162 kDebug() << "tokoe: VerifyDetachedBodyPartMemento started";
163 #endif
164 connect( m_job, SIGNAL(result(GpgME::VerificationResult)),
165 this, SLOT(slotResult(GpgME::VerificationResult)) );
166 if ( const Error err = m_job->start( m_signature, m_plainText ) ) {
167 m_vr = VerificationResult( err );
168 #ifdef DEBUG_SIGNATURE
169 kDebug() << "tokoe: VerifyDetachedBodyPartMemento stopped with error";
170 #endif
171 return false;
173 setRunning( true );
174 return true;
177 void VerifyDetachedBodyPartMemento::exec() {
178 assert( m_job );
179 setRunning( true );
180 #ifdef DEBUG_SIGNATURE
181 kDebug() << "tokoe: VerifyDetachedBodyPartMemento execed";
182 #endif
183 saveResult( m_job->exec( m_signature, m_plainText ) );
184 m_job->deleteLater(); // exec'ed jobs don't delete themselves
185 m_job = 0;
186 #ifdef DEBUG_SIGNATURE
187 kDebug() << "tokoe: VerifyDetachedBodyPartMemento after execed";
188 #endif
189 if ( canStartKeyListJob() ) {
190 std::vector<GpgME::Key> keys;
191 m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
192 if ( !keys.empty() )
193 m_key = keys.back();
195 if ( m_keylistjob )
196 m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
197 m_keylistjob = 0;
198 setRunning( false );
201 bool VerifyDetachedBodyPartMemento::canStartKeyListJob() const
203 if ( !m_keylistjob )
204 return false;
205 const char * const fpr = m_vr.signature( 0 ).fingerprint();
206 return fpr && *fpr;
209 QStringList VerifyDetachedBodyPartMemento::keyListPattern() const
211 assert( canStartKeyListJob() );
212 return QStringList( QString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
215 void VerifyDetachedBodyPartMemento::saveResult( const VerificationResult & vr )
217 assert( m_job );
218 #ifdef DEBUG_SIGNATURE
219 kDebug() << "tokoe: VerifyDetachedBodyPartMemento::saveResult called";
220 #endif
221 m_vr = vr;
222 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
225 void VerifyDetachedBodyPartMemento::slotResult( const VerificationResult & vr )
227 #ifdef DEBUG_SIGNATURE
228 kDebug() << "tokoe: VerifyDetachedBodyPartMemento::slotResult called";
229 #endif
230 saveResult( vr );
231 m_job = 0;
232 if ( canStartKeyListJob() && startKeyListJob() ) {
233 #ifdef DEBUG_SIGNATURE
234 kDebug() << "tokoe: VerifyDetachedBodyPartMemento: canStartKeyListJob && startKeyListJob";
235 #endif
236 return;
238 if ( m_keylistjob )
239 m_keylistjob->deleteLater();
240 m_keylistjob = 0;
241 setRunning( false );
242 notify();
245 bool VerifyDetachedBodyPartMemento::startKeyListJob()
247 assert( canStartKeyListJob() );
248 if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
249 return false;
250 connect( m_keylistjob, SIGNAL(done()), this, SLOT(slotKeyListJobDone()) );
251 connect( m_keylistjob, SIGNAL(nextKey(GpgME::Key)),
252 this, SLOT(slotNextKey(GpgME::Key)) );
253 return true;
256 void VerifyDetachedBodyPartMemento::slotNextKey( const GpgME::Key & key )
258 #ifdef DEBUG_SIGNATURE
259 kDebug() << "tokoe: VerifyDetachedBodyPartMemento::slotNextKey called";
260 #endif
261 m_key = key;
264 void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
266 #ifdef DEBUG_SIGNATURE
267 kDebug() << "tokoe: VerifyDetachedBodyPartMemento::slotKeyListJobDone called";
268 #endif
269 m_keylistjob = 0;
270 setRunning( false );
271 notify();
274 VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento( VerifyOpaqueJob * job,
275 KeyListJob * klj,
276 const QByteArray & signature )
277 : CryptoBodyPartMemento(),
278 m_signature( signature ),
279 m_job( job ),
280 m_keylistjob( klj )
282 assert( m_job );
285 VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento() {
286 if ( m_job )
287 m_job->slotCancel();
288 if ( m_keylistjob )
289 m_keylistjob->slotCancel();
292 bool VerifyOpaqueBodyPartMemento::start() {
293 assert( m_job );
294 #ifdef DEBUG_SIGNATURE
295 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento started";
296 #endif
297 if ( const Error err = m_job->start( m_signature ) ) {
298 m_vr = VerificationResult( err );
299 #ifdef DEBUG_SIGNATURE
300 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento stopped with error";
301 #endif
302 return false;
304 connect( m_job, SIGNAL(result(GpgME::VerificationResult,QByteArray)),
305 this, SLOT(slotResult(GpgME::VerificationResult,QByteArray)) );
306 setRunning( true );
307 return true;
310 void VerifyOpaqueBodyPartMemento::exec() {
311 assert( m_job );
312 setRunning( true );
313 QByteArray plainText;
314 #ifdef DEBUG_SIGNATURE
315 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento execed";
316 #endif
317 saveResult( m_job->exec( m_signature, plainText ), plainText );
318 #ifdef DEBUG_SIGNATURE
319 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento after execed";
320 #endif
321 m_job->deleteLater(); // exec'ed jobs don't delete themselves
322 m_job = 0;
323 if ( canStartKeyListJob() ) {
324 std::vector<GpgME::Key> keys;
325 m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
326 if ( !keys.empty() )
327 m_key = keys.back();
329 if ( m_keylistjob )
330 m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
331 m_keylistjob = 0;
332 setRunning( false );
335 bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
337 if ( !m_keylistjob )
338 return false;
339 const char * const fpr = m_vr.signature( 0 ).fingerprint();
340 return fpr && *fpr;
343 QStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
345 assert( canStartKeyListJob() );
346 return QStringList( QString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
349 void VerifyOpaqueBodyPartMemento::saveResult( const VerificationResult & vr,
350 const QByteArray & plainText )
352 assert( m_job );
353 #ifdef DEBUG_SIGNATURE
354 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento::saveResult called";
355 #endif
356 m_vr = vr;
357 m_plainText = plainText;
358 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
361 void VerifyOpaqueBodyPartMemento::slotResult( const VerificationResult & vr,
362 const QByteArray & plainText )
364 #ifdef DEBUG_SIGNATURE
365 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento::slotResult called";
366 #endif
367 saveResult( vr, plainText );
368 m_job = 0;
369 if ( canStartKeyListJob() && startKeyListJob() ) {
370 #ifdef DEBUG_SIGNATURE
371 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento: canStartKeyListJob && startKeyListJob";
372 #endif
373 return;
375 if ( m_keylistjob )
376 m_keylistjob->deleteLater();
377 m_keylistjob = 0;
378 setRunning( false );
379 notify();
382 bool VerifyOpaqueBodyPartMemento::startKeyListJob()
384 assert( canStartKeyListJob() );
385 if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
386 return false;
387 connect( m_keylistjob, SIGNAL(done()), this, SLOT(slotKeyListJobDone()) );
388 connect( m_keylistjob, SIGNAL(nextKey(GpgME::Key)),
389 this, SLOT(slotNextKey(GpgME::Key)) );
390 return true;
393 void VerifyOpaqueBodyPartMemento::slotNextKey( const GpgME::Key & key )
395 #ifdef DEBUG_SIGNATURE
396 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento::slotNextKey called";
397 #endif
398 m_key = key;
401 void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
403 #ifdef DEBUG_SIGNATURE
404 kDebug() << "tokoe: VerifyOpaqueBodyPartMemento::slotKeyListJobDone called";
405 #endif
406 m_keylistjob = 0;
407 setRunning( false );
408 notify();
412 #include "objecttreeparser_p.moc"