typo fix spotted by Freek de Kruijf
[kdepim.git] / messageviewer / objecttreeparser_p.cpp
blobda53b04b4b50cc145a2b6892332f89eb5d75f718
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 "objecttreeparser_p.h"
37 #include <kleo/decryptverifyjob.h>
38 #include <kleo/verifydetachedjob.h>
39 #include <kleo/verifyopaquejob.h>
40 #include <kleo/keylistjob.h>
42 #include <gpgme++/keylistresult.h>
44 #include <qtimer.h>
45 #include <qstringlist.h>
47 #include <cassert>
49 using namespace Kleo;
50 using namespace GpgME;
51 using namespace MessageViewer;
53 CryptoBodyPartMemento::CryptoBodyPartMemento()
54 : QObject( 0 ),
55 Interface::BodyPartMemento(),
56 m_running( false )
61 CryptoBodyPartMemento::~CryptoBodyPartMemento() {}
63 void CryptoBodyPartMemento::setAuditLog( const Error & err, const QString & log ) {
64 m_auditLogError = err;
65 m_auditLog = log;
68 void CryptoBodyPartMemento::setRunning( bool running ) {
69 m_running = running;
72 void CryptoBodyPartMemento::detach()
74 disconnect( this, SIGNAL(update(Viewer::UpdateMode)), 0, 0 );
77 DecryptVerifyBodyPartMemento::DecryptVerifyBodyPartMemento( DecryptVerifyJob * job, const QByteArray & cipherText )
78 : CryptoBodyPartMemento(),
79 m_cipherText( cipherText ),
80 m_job( job )
82 assert( m_job );
85 DecryptVerifyBodyPartMemento::~DecryptVerifyBodyPartMemento() {
86 if ( m_job )
87 m_job->slotCancel();
90 bool DecryptVerifyBodyPartMemento::start() {
91 assert( m_job );
92 if ( const Error err = m_job->start( m_cipherText ) ) {
93 m_dr = DecryptionResult( err );
94 return false;
96 connect( m_job, SIGNAL(result(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const QByteArray&)),
97 this, SLOT(slotResult(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const QByteArray&)) );
98 setRunning( true );
99 return true;
102 void DecryptVerifyBodyPartMemento::exec() {
103 assert( m_job );
104 QByteArray plainText;
105 setRunning( true );
106 const std::pair<DecryptionResult,VerificationResult> p = m_job->exec( m_cipherText, plainText );
107 saveResult( p.first, p.second, plainText );
108 m_job->deleteLater(); // exec'ed jobs don't delete themselves
109 m_job = 0;
112 void DecryptVerifyBodyPartMemento::saveResult( const DecryptionResult & dr,
113 const VerificationResult & vr,
114 const QByteArray & plainText )
116 assert( m_job );
117 setRunning( false );
118 m_dr = dr;
119 m_vr = vr;
120 m_plainText = plainText;
121 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
124 void DecryptVerifyBodyPartMemento::slotResult( const DecryptionResult & dr,
125 const VerificationResult & vr,
126 const QByteArray & plainText )
128 saveResult( dr, vr, plainText );
129 m_job = 0;
130 notify();
136 VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob * job,
137 KeyListJob * klj,
138 const QByteArray & signature,
139 const QByteArray & plainText )
140 : CryptoBodyPartMemento(),
141 m_signature( signature ),
142 m_plainText( plainText ),
143 m_job( job ),
144 m_keylistjob( klj )
146 assert( m_job );
149 VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento() {
150 if ( m_job )
151 m_job->slotCancel();
152 if ( m_keylistjob )
153 m_keylistjob->slotCancel();
156 bool VerifyDetachedBodyPartMemento::start() {
157 assert( m_job );
158 if ( const Error err = m_job->start( m_signature, m_plainText ) ) {
159 m_vr = VerificationResult( err );
160 return false;
162 connect( m_job, SIGNAL(result(const GpgME::VerificationResult&)),
163 this, SLOT(slotResult(const GpgME::VerificationResult&)) );
164 setRunning( true );
165 return true;
168 void VerifyDetachedBodyPartMemento::exec() {
169 assert( m_job );
170 setRunning( true );
171 saveResult( m_job->exec( m_signature, m_plainText ) );
172 m_job->deleteLater(); // exec'ed jobs don't delete themselves
173 m_job = 0;
174 if ( canStartKeyListJob() ) {
175 std::vector<GpgME::Key> keys;
176 m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
177 if ( !keys.empty() )
178 m_key = keys.back();
180 if ( m_keylistjob )
181 m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
182 m_keylistjob = 0;
183 setRunning( false );
186 bool VerifyDetachedBodyPartMemento::canStartKeyListJob() const
188 if ( !m_keylistjob )
189 return false;
190 const char * const fpr = m_vr.signature( 0 ).fingerprint();
191 return fpr && *fpr;
194 QStringList VerifyDetachedBodyPartMemento::keyListPattern() const
196 assert( canStartKeyListJob() );
197 return QStringList( QString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
200 void VerifyDetachedBodyPartMemento::saveResult( const VerificationResult & vr )
202 assert( m_job );
203 m_vr = vr;
204 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
207 void VerifyDetachedBodyPartMemento::slotResult( const VerificationResult & vr )
209 saveResult( vr );
210 m_job = 0;
211 if ( canStartKeyListJob() && startKeyListJob() )
212 return;
213 if ( m_keylistjob )
214 m_keylistjob->deleteLater();
215 m_keylistjob = 0;
216 setRunning( false );
217 notify();
220 bool VerifyDetachedBodyPartMemento::startKeyListJob()
222 assert( canStartKeyListJob() );
223 if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
224 return false;
225 connect( m_keylistjob, SIGNAL(done()), this, SLOT(slotKeyListJobDone()) );
226 connect( m_keylistjob, SIGNAL(nextKey(const GpgME::Key&)),
227 this, SLOT(slotNextKey(const GpgME::Key&)) );
228 return true;
231 void VerifyDetachedBodyPartMemento::slotNextKey( const GpgME::Key & key )
233 m_key = key;
236 void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
238 m_keylistjob = 0;
239 setRunning( false );
240 notify();
243 VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento( VerifyOpaqueJob * job,
244 KeyListJob * klj,
245 const QByteArray & signature )
246 : CryptoBodyPartMemento(),
247 m_signature( signature ),
248 m_job( job ),
249 m_keylistjob( klj )
251 assert( m_job );
254 VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento() {
255 if ( m_job )
256 m_job->slotCancel();
257 if ( m_keylistjob )
258 m_keylistjob->slotCancel();
261 bool VerifyOpaqueBodyPartMemento::start() {
262 assert( m_job );
263 if ( const Error err = m_job->start( m_signature ) ) {
264 m_vr = VerificationResult( err );
265 return false;
267 connect( m_job, SIGNAL(result(const GpgME::VerificationResult&,const QByteArray&)),
268 this, SLOT(slotResult(const GpgME::VerificationResult&,const QByteArray&)) );
269 setRunning( true );
270 return true;
273 void VerifyOpaqueBodyPartMemento::exec() {
274 assert( m_job );
275 setRunning( true );
276 QByteArray plainText;
277 saveResult( m_job->exec( m_signature, plainText ), plainText );
278 m_job->deleteLater(); // exec'ed jobs don't delete themselves
279 m_job = 0;
280 if ( canStartKeyListJob() ) {
281 std::vector<GpgME::Key> keys;
282 m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
283 if ( !keys.empty() )
284 m_key = keys.back();
286 if ( m_keylistjob )
287 m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
288 m_keylistjob = 0;
289 setRunning( false );
292 bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
294 if ( !m_keylistjob )
295 return false;
296 const char * const fpr = m_vr.signature( 0 ).fingerprint();
297 return fpr && *fpr;
300 QStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
302 assert( canStartKeyListJob() );
303 return QStringList( QString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
306 void VerifyOpaqueBodyPartMemento::saveResult( const VerificationResult & vr,
307 const QByteArray & plainText )
309 assert( m_job );
310 m_vr = vr;
311 m_plainText = plainText;
312 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
315 void VerifyOpaqueBodyPartMemento::slotResult( const VerificationResult & vr,
316 const QByteArray & plainText )
318 saveResult( vr, plainText );
319 m_job = 0;
320 if ( canStartKeyListJob() && startKeyListJob() )
321 return;
322 if ( m_keylistjob )
323 m_keylistjob->deleteLater();
324 m_keylistjob = 0;
325 setRunning( false );
326 notify();
329 bool VerifyOpaqueBodyPartMemento::startKeyListJob()
331 assert( canStartKeyListJob() );
332 if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
333 return false;
334 connect( m_keylistjob, SIGNAL(done()), this, SLOT(slotKeyListJobDone()) );
335 connect( m_keylistjob, SIGNAL(nextKey(const GpgME::Key&)),
336 this, SLOT(slotNextKey(const GpgME::Key&)) );
337 return true;
340 void VerifyOpaqueBodyPartMemento::slotNextKey( const GpgME::Key & key )
342 m_key = key;
345 void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
347 m_keylistjob = 0;
348 setRunning( false );
349 notify();
353 #include "objecttreeparser_p.moc"