kmail

objecttreeparser_p.cpp
1/*
2 objecttreeparser_p.cpp
3
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2009 Klarälvdalens Datakonsult AB
6 Authors: Marc Mutz <marc@kdab.net>
7
8 KMail is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License, version 2, as
10 published by the Free Software Foundation.
11
12 KMail is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the TQt library by Trolltech AS, Norway (or with modified versions
24 of TQt that use the same license as TQt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 TQt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
31*/
32
33#include <config.h>
34
35#include "objecttreeparser_p.h"
36
37#include <kleo/decryptverifyjob.h>
38#include <kleo/verifydetachedjob.h>
39#include <kleo/verifyopaquejob.h>
40#include <kleo/keylistjob.h>
41
42#include <gpgmepp/keylistresult.h>
43
44#include <tqtimer.h>
45#include <tqstringlist.h>
46
47#include <cassert>
48
49using namespace KMail;
50using namespace Kleo;
51using namespace GpgME;
52
53CryptoBodyPartMemento::CryptoBodyPartMemento()
54 : TQObject( 0 ),
55 Interface::BodyPartMemento(),
56 ISubject(),
57 m_running( false )
58{
59
60}
61
62CryptoBodyPartMemento::~CryptoBodyPartMemento() {}
63
64void CryptoBodyPartMemento::setAuditLog( const GpgME::Error & err, const TQString & log ) {
65 m_auditLogError = err;
66 m_auditLog = log;
67}
68
69void CryptoBodyPartMemento::setRunning( bool running ) {
70 m_running = running;
71}
72
73GenericVerifyMemento::GenericVerifyMemento(Kleo::KeyListJob * klj)
74 : m_keylistjob(klj)
75{}
76
77void GenericVerifyMemento::execKeyListJob() {
78 if ( canStartKeyListJob() ) {
79 std::vector<GpgME::Key> keys;
80 m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
81 if ( !keys.empty() )
82 m_key = keys.back();
83 }
84 destroyKeyListJob(); // exec'ed jobs don't delete themselves
85}
86
87bool GenericVerifyMemento::startKeyListJob()
88{
89 if ( !canStartKeyListJob() )
90 return false;
91 if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
92 return false;
93 connect( m_keylistjob, TQ_SIGNAL(done()), this, TQ_SLOT(slotKeyListJobDone()) );
94 connect( m_keylistjob, TQ_SIGNAL(nextKey(const GpgME::Key&)),
95 this, TQ_SLOT(slotNextKey(const GpgME::Key&)) );
96 return true;
97}
98
99void GenericVerifyMemento::destroyKeyListJob() {
100 if ( m_keylistjob )
101 m_keylistjob->deleteLater();
102 m_keylistjob = 0;
103}
104
105bool GenericVerifyMemento::canStartKeyListJob() const {
106 if ( !m_keylistjob )
107 return false;
108 const char * const fpr = m_vr.signature( 0 ).fingerprint();
109 return fpr && *fpr;
110}
111
112void GenericVerifyMemento::slotNextKey( const GpgME::Key & key )
113{
114 m_key = key;
115}
116
117void GenericVerifyMemento::slotKeyListJobDone()
118{
119 m_keylistjob = 0;
120 setRunning( false );
121 notify();
122}
123
124TQStringList GenericVerifyMemento::keyListPattern() const
125{
126 assert( canStartKeyListJob() );
127 return TQStringList( TQString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
128}
129
130
131GenericVerifyMemento::~GenericVerifyMemento() {
132 if ( m_keylistjob )
133 m_keylistjob->slotCancel();
134}
135
136DecryptVerifyBodyPartMemento::DecryptVerifyBodyPartMemento( DecryptVerifyJob * job,
137 Kleo::KeyListJob * klj,
138 const TQByteArray & cipherText )
139 : GenericVerifyMemento(klj),
140 m_cipherText( cipherText ),
141 m_job( job )
142{
143 assert( m_job );
144}
145
146DecryptVerifyBodyPartMemento::~DecryptVerifyBodyPartMemento() {
147 if ( m_job )
148 m_job->slotCancel();
149}
150
151bool DecryptVerifyBodyPartMemento::start() {
152 assert( m_job );
153 if ( const GpgME::Error err = m_job->start( m_cipherText ) ) {
154 m_dr = DecryptionResult( err );
155 return false;
156 }
157 connect( m_job, TQ_SIGNAL(result(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const TQByteArray&)),
158 this, TQ_SLOT(slotResult(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const TQByteArray&)) );
159 setRunning( true );
160 return true;
161}
162
163void DecryptVerifyBodyPartMemento::exec() {
164 assert( m_job );
165 TQByteArray plainText;
166 setRunning( true );
167 const std::pair<DecryptionResult,VerificationResult> p = m_job->exec( m_cipherText, plainText );
168 saveResult( p.first, p.second, plainText );
169 m_job->deleteLater(); // exec'ed jobs don't delete themselves
170 m_job = 0;
171 execKeyListJob();
172 setRunning( false );
173}
174
175void DecryptVerifyBodyPartMemento::saveResult( const DecryptionResult & dr,
176 const VerificationResult & vr,
177 const TQByteArray & plainText )
178{
179 assert( m_job );
180 setRunning( false );
181 m_dr = dr;
182 setVerificationResult( vr );
183 m_plainText = plainText;
184 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
185}
186
187void DecryptVerifyBodyPartMemento::slotResult( const DecryptionResult & dr,
188 const VerificationResult & vr,
189 const TQByteArray & plainText )
190{
191 saveResult( dr, vr, plainText );
192 setRunning( false );
193 m_job = 0;
194 if ( startKeyListJob() )
195 return;
196 destroyKeyListJob();
197 setRunning( false );
198 notify();
199}
200
201
202VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob * job,
203 KeyListJob * klj,
204 const TQByteArray & signature,
205 const TQByteArray & plainText )
206 : GenericVerifyMemento(klj),
207 m_signature( signature ),
208 m_plainText( plainText ),
209 m_job( job )
210{
211 assert( m_job );
212}
213
214VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento() {
215 if ( m_job )
216 m_job->slotCancel();
217}
218
219bool VerifyDetachedBodyPartMemento::start() {
220 assert( m_job );
221 if ( const GpgME::Error err = m_job->start( m_signature, m_plainText ) ) {
222 setVerificationResult( VerificationResult( err ) );
223 return false;
224 }
225 connect( m_job, TQ_SIGNAL(result(const GpgME::VerificationResult&)),
226 this, TQ_SLOT(slotResult(const GpgME::VerificationResult&)) );
227 setRunning( true );
228 return true;
229}
230
231void VerifyDetachedBodyPartMemento::exec() {
232 assert( m_job );
233 setRunning( true );
234 saveResult( m_job->exec( m_signature, m_plainText ) );
235 m_job->deleteLater(); // exec'ed jobs don't delete themselves
236 m_job = 0;
237 execKeyListJob();
238 setRunning( false );
239}
240
241void VerifyDetachedBodyPartMemento::saveResult( const VerificationResult & vr )
242{
243 assert( m_job );
244 setVerificationResult( vr );
245 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
246}
247
248void VerifyDetachedBodyPartMemento::slotResult( const VerificationResult & vr )
249{
250 saveResult( vr );
251 m_job = 0;
252 if ( startKeyListJob() )
253 return;
254 destroyKeyListJob();
255 setRunning( false );
256 notify();
257}
258
259VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento( VerifyOpaqueJob * job,
260 KeyListJob * klj,
261 const TQByteArray & signature )
262 : GenericVerifyMemento(klj),
263 m_signature( signature ),
264 m_job( job )
265{
266 assert( m_job );
267}
268
269VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento() {
270 if ( m_job )
271 m_job->slotCancel();
272}
273
274bool VerifyOpaqueBodyPartMemento::start() {
275 assert( m_job );
276 if ( const GpgME::Error err = m_job->start( m_signature ) ) {
277 setVerificationResult( VerificationResult( err ));
278 return false;
279 }
280 connect( m_job, TQ_SIGNAL(result(const GpgME::VerificationResult&,const TQByteArray&)),
281 this, TQ_SLOT(slotResult(const GpgME::VerificationResult&,const TQByteArray&)) );
282 setRunning( true );
283 return true;
284}
285
286void VerifyOpaqueBodyPartMemento::exec() {
287 assert( m_job );
288 setRunning( true );
289 TQByteArray plainText;
290 saveResult( m_job->exec( m_signature, plainText ), plainText );
291 m_job->deleteLater(); // exec'ed jobs don't delete themselves
292 m_job = 0;
293 execKeyListJob();
294 setRunning( false );
295}
296
297void VerifyOpaqueBodyPartMemento::saveResult( const VerificationResult & vr,
298 const TQByteArray & plainText )
299{
300 assert( m_job );
301 setVerificationResult(vr);
302 m_plainText = plainText;
303 setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
304}
305
306void VerifyOpaqueBodyPartMemento::slotResult( const VerificationResult & vr,
307 const TQByteArray & plainText )
308{
309 saveResult( vr, plainText );
310 m_job = 0;
311 if ( startKeyListJob() )
312 return;
313 destroyKeyListJob();
314 setRunning( false );
315 notify();
316}
317
318
319#include "objecttreeparser_p.moc"
folderdiaquotatab.h
Definition: aboutdata.cpp:40