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 
49 using namespace KMail;
50 using namespace Kleo;
51 using namespace GpgME;
52 
53 CryptoBodyPartMemento::CryptoBodyPartMemento()
54  : TQObject( 0 ),
55  Interface::BodyPartMemento(),
56  ISubject(),
57  m_running( false )
58 {
59 
60 }
61 
62 CryptoBodyPartMemento::~CryptoBodyPartMemento() {}
63 
64 void CryptoBodyPartMemento::setAuditLog( const GpgME::Error & err, const TQString & log ) {
65  m_auditLogError = err;
66  m_auditLog = log;
67 }
68 
69 void CryptoBodyPartMemento::setRunning( bool running ) {
70  m_running = running;
71 }
72 
73 DecryptVerifyBodyPartMemento::DecryptVerifyBodyPartMemento( DecryptVerifyJob * job, const TQByteArray & cipherText )
74  : CryptoBodyPartMemento(),
75  m_cipherText( cipherText ),
76  m_job( job )
77 {
78  assert( m_job );
79 }
80 
81 DecryptVerifyBodyPartMemento::~DecryptVerifyBodyPartMemento() {
82  if ( m_job )
83  m_job->slotCancel();
84 }
85 
86 bool DecryptVerifyBodyPartMemento::start() {
87  assert( m_job );
88  if ( const GpgME::Error err = m_job->start( m_cipherText ) ) {
89  m_dr = DecryptionResult( err );
90  return false;
91  }
92  connect( m_job, TQ_SIGNAL(result(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const TQByteArray&)),
93  this, TQ_SLOT(slotResult(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const TQByteArray&)) );
94  setRunning( true );
95  return true;
96 }
97 
98 void DecryptVerifyBodyPartMemento::exec() {
99  assert( m_job );
100  TQByteArray plainText;
101  setRunning( true );
102  const std::pair<DecryptionResult,VerificationResult> p = m_job->exec( m_cipherText, plainText );
103  saveResult( p.first, p.second, plainText );
104  m_job->deleteLater(); // exec'ed jobs don't delete themselves
105  m_job = 0;
106 }
107 
108 void DecryptVerifyBodyPartMemento::saveResult( const DecryptionResult & dr,
109  const VerificationResult & vr,
110  const TQByteArray & plainText )
111 {
112  assert( m_job );
113  setRunning( false );
114  m_dr = dr;
115  m_vr = vr;
116  m_plainText = plainText;
117  setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
118 }
119 
120 void DecryptVerifyBodyPartMemento::slotResult( const DecryptionResult & dr,
121  const VerificationResult & vr,
122  const TQByteArray & plainText )
123 {
124  saveResult( dr, vr, plainText );
125  m_job = 0;
126  notify();
127 }
128 
129 
130 
131 
132 VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob * job,
133  KeyListJob * klj,
134  const TQByteArray & signature,
135  const TQByteArray & plainText )
136  : CryptoBodyPartMemento(),
137  m_signature( signature ),
138  m_plainText( plainText ),
139  m_job( job ),
140  m_keylistjob( klj )
141 {
142  assert( m_job );
143 }
144 
145 VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento() {
146  if ( m_job )
147  m_job->slotCancel();
148  if ( m_keylistjob )
149  m_keylistjob->slotCancel();
150 }
151 
152 bool VerifyDetachedBodyPartMemento::start() {
153  assert( m_job );
154  if ( const GpgME::Error err = m_job->start( m_signature, m_plainText ) ) {
155  m_vr = VerificationResult( err );
156  return false;
157  }
158  connect( m_job, TQ_SIGNAL(result(const GpgME::VerificationResult&)),
159  this, TQ_SLOT(slotResult(const GpgME::VerificationResult&)) );
160  setRunning( true );
161  return true;
162 }
163 
164 void VerifyDetachedBodyPartMemento::exec() {
165  assert( m_job );
166  setRunning( true );
167  saveResult( m_job->exec( m_signature, m_plainText ) );
168  m_job->deleteLater(); // exec'ed jobs don't delete themselves
169  m_job = 0;
170  if ( canStartKeyListJob() ) {
171  std::vector<GpgME::Key> keys;
172  m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
173  if ( !keys.empty() )
174  m_key = keys.back();
175  }
176  if ( m_keylistjob )
177  m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
178  m_keylistjob = 0;
179  setRunning( false );
180 }
181 
182 bool VerifyDetachedBodyPartMemento::canStartKeyListJob() const
183 {
184  if ( !m_keylistjob )
185  return false;
186  const char * const fpr = m_vr.signature( 0 ).fingerprint();
187  return fpr && *fpr;
188 }
189 
190 TQStringList VerifyDetachedBodyPartMemento::keyListPattern() const
191 {
192  assert( canStartKeyListJob() );
193  return TQStringList( TQString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
194 }
195 
196 void VerifyDetachedBodyPartMemento::saveResult( const VerificationResult & vr )
197 {
198  assert( m_job );
199  m_vr = vr;
200  setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
201 }
202 
203 void VerifyDetachedBodyPartMemento::slotResult( const VerificationResult & vr )
204 {
205  saveResult( vr );
206  m_job = 0;
207  if ( canStartKeyListJob() && startKeyListJob() )
208  return;
209  if ( m_keylistjob )
210  m_keylistjob->deleteLater();
211  m_keylistjob = 0;
212  setRunning( false );
213  notify();
214 }
215 
216 bool VerifyDetachedBodyPartMemento::startKeyListJob()
217 {
218  assert( canStartKeyListJob() );
219  if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
220  return false;
221  connect( m_keylistjob, TQ_SIGNAL(done()), this, TQ_SLOT(slotKeyListJobDone()) );
222  connect( m_keylistjob, TQ_SIGNAL(nextKey(const GpgME::Key&)),
223  this, TQ_SLOT(slotNextKey(const GpgME::Key&)) );
224  return true;
225 }
226 
227 void VerifyDetachedBodyPartMemento::slotNextKey( const GpgME::Key & key )
228 {
229  m_key = key;
230 }
231 
232 void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
233 {
234  m_keylistjob = 0;
235  setRunning( false );
236  notify();
237 }
238 
239 
240 VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento( VerifyOpaqueJob * job,
241  KeyListJob * klj,
242  const TQByteArray & signature )
243  : CryptoBodyPartMemento(),
244  m_signature( signature ),
245  m_job( job ),
246  m_keylistjob( klj )
247 {
248  assert( m_job );
249 }
250 
251 VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento() {
252  if ( m_job )
253  m_job->slotCancel();
254  if ( m_keylistjob )
255  m_keylistjob->slotCancel();
256 }
257 
258 bool VerifyOpaqueBodyPartMemento::start() {
259  assert( m_job );
260  if ( const GpgME::Error err = m_job->start( m_signature ) ) {
261  m_vr = VerificationResult( err );
262  return false;
263  }
264  connect( m_job, TQ_SIGNAL(result(const GpgME::VerificationResult&,const TQByteArray&)),
265  this, TQ_SLOT(slotResult(const GpgME::VerificationResult&,const TQByteArray&)) );
266  setRunning( true );
267  return true;
268 }
269 
270 void VerifyOpaqueBodyPartMemento::exec() {
271  assert( m_job );
272  setRunning( true );
273  TQByteArray plainText;
274  saveResult( m_job->exec( m_signature, plainText ), plainText );
275  m_job->deleteLater(); // exec'ed jobs don't delete themselves
276  m_job = 0;
277  if ( canStartKeyListJob() ) {
278  std::vector<GpgME::Key> keys;
279  m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
280  if ( !keys.empty() )
281  m_key = keys.back();
282  }
283  if ( m_keylistjob )
284  m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
285  m_keylistjob = 0;
286  setRunning( false );
287 }
288 
289 bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
290 {
291  if ( !m_keylistjob )
292  return false;
293  const char * const fpr = m_vr.signature( 0 ).fingerprint();
294  return fpr && *fpr;
295 }
296 
297 TQStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
298 {
299  assert( canStartKeyListJob() );
300  return TQStringList( TQString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
301 }
302 
303 void VerifyOpaqueBodyPartMemento::saveResult( const VerificationResult & vr,
304  const TQByteArray & plainText )
305 {
306  assert( m_job );
307  m_vr = vr;
308  m_plainText = plainText;
309  setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
310 }
311 
312 void VerifyOpaqueBodyPartMemento::slotResult( const VerificationResult & vr,
313  const TQByteArray & plainText )
314 {
315  saveResult( vr, plainText );
316  m_job = 0;
317  if ( canStartKeyListJob() && startKeyListJob() )
318  return;
319  if ( m_keylistjob )
320  m_keylistjob->deleteLater();
321  m_keylistjob = 0;
322  setRunning( false );
323  notify();
324 }
325 
326 bool VerifyOpaqueBodyPartMemento::startKeyListJob()
327 {
328  assert( canStartKeyListJob() );
329  if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
330  return false;
331  connect( m_keylistjob, TQ_SIGNAL(done()), this, TQ_SLOT(slotKeyListJobDone()) );
332  connect( m_keylistjob, TQ_SIGNAL(nextKey(const GpgME::Key&)),
333  this, TQ_SLOT(slotNextKey(const GpgME::Key&)) );
334  return true;
335 }
336 
337 void VerifyOpaqueBodyPartMemento::slotNextKey( const GpgME::Key & key )
338 {
339  m_key = key;
340 }
341 
342 void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
343 {
344  m_keylistjob = 0;
345  setRunning( false );
346  notify();
347 }
348 
349 
350 #include "objecttreeparser_p.moc"
folderdiaquotatab.h
Definition: aboutdata.cpp:40