summaryrefslogtreecommitdiffstats
path: root/kmail/objecttreeparser_p.cpp
blob: a645b39899795d0a5814a22d980da0239f895fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*  -*- mode: C++; c-file-style: "gnu" -*-
    objecttreeparser_p.cpp

    This file is part of KMail, the KDE mail client.
    Copyright (c) 2009 Klarälvdalens Datakonsult AB
    Authors: Marc Mutz <marc@kdab.net>

    KMail is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    KMail is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#include <config.h>

#include "objecttreeparser_p.h"

#include <kleo/decryptverifyjob.h>
#include <kleo/verifydetachedjob.h>
#include <kleo/verifyopaquejob.h>
#include <kleo/keylistjob.h>

#include <gpgmepp/keylistresult.h>

#include <tqtimer.h>
#include <tqstringlist.h>

#include <cassert>

using namespace KMail;
using namespace Kleo;
using namespace GpgME;

CryptoBodyPartMemento::CryptoBodyPartMemento()
  : TQObject( 0 ),
    Interface::BodyPartMemento(),
    ISubject(),
    m_running( false )
{

}

CryptoBodyPartMemento::~CryptoBodyPartMemento() {}

void CryptoBodyPartMemento::setAuditLog( const GpgME::Error & err, const TQString & log ) {
  m_auditLogError = err;
  m_auditLog = log;
}

void CryptoBodyPartMemento::setRunning( bool running ) {
  m_running = running;
}

DecryptVerifyBodyPartMemento::DecryptVerifyBodyPartMemento( DecryptVerifyJob * job, const TQByteArray & cipherText )
  : CryptoBodyPartMemento(),
    m_cipherText( cipherText ),
    m_job( job )
{
  assert( m_job );
}

DecryptVerifyBodyPartMemento::~DecryptVerifyBodyPartMemento() {
  if ( m_job )
    m_job->slotCancel();
}

bool DecryptVerifyBodyPartMemento::start() {
  assert( m_job );
  if ( const GpgME::Error err = m_job->start( m_cipherText ) ) {
    m_dr = DecryptionResult( err );
    return false;
  }
  connect( m_job, TQT_SIGNAL(result(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const TQByteArray&)),
           this, TQT_SLOT(slotResult(const GpgME::DecryptionResult&,const GpgME::VerificationResult&,const TQByteArray&)) );
  setRunning( true );
  return true;
}

void DecryptVerifyBodyPartMemento::exec() {
  assert( m_job );
  TQByteArray plainText;
  setRunning( true );
  const std::pair<DecryptionResult,VerificationResult> p = m_job->exec( m_cipherText, plainText );
  saveResult( p.first, p.second, plainText );
  m_job->deleteLater(); // exec'ed jobs don't delete themselves
  m_job = 0;
}

void DecryptVerifyBodyPartMemento::saveResult( const DecryptionResult & dr,
                                               const VerificationResult & vr,
                                               const TQByteArray & plainText )
{
  assert( m_job );
  setRunning( false );
  m_dr = dr;
  m_vr = vr;
  m_plainText = plainText;
  setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
}

void DecryptVerifyBodyPartMemento::slotResult( const DecryptionResult & dr,
                                               const VerificationResult & vr,
                                               const TQByteArray & plainText )
{
  saveResult( dr, vr, plainText );
  m_job = 0;
  notify();
}




VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento( VerifyDetachedJob * job,
                                                              KeyListJob * klj,
                                                              const TQByteArray & signature,
                                                              const TQByteArray & plainText )
  : CryptoBodyPartMemento(),
    m_signature( signature ),
    m_plainText( plainText ),
    m_job( job ),
    m_keylistjob( klj )
{
  assert( m_job );
}

VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento() {
  if ( m_job )
    m_job->slotCancel();
  if ( m_keylistjob )
    m_keylistjob->slotCancel();
}

bool VerifyDetachedBodyPartMemento::start() {
  assert( m_job );
  if ( const GpgME::Error err = m_job->start( m_signature, m_plainText ) ) {
    m_vr = VerificationResult( err );
    return false;
  }
  connect( m_job, TQT_SIGNAL(result(const GpgME::VerificationResult&)),
           this, TQT_SLOT(slotResult(const GpgME::VerificationResult&)) );
  setRunning( true );
  return true;
}

void VerifyDetachedBodyPartMemento::exec() {
  assert( m_job );
  setRunning( true );
  saveResult( m_job->exec( m_signature, m_plainText ) );
  m_job->deleteLater(); // exec'ed jobs don't delete themselves
  m_job = 0;
  if ( canStartKeyListJob() ) {
    std::vector<GpgME::Key> keys;
    m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
    if ( !keys.empty() )
      m_key = keys.back();
  }
  if ( m_keylistjob )
    m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
  m_keylistjob = 0;
  setRunning( false );
}

bool VerifyDetachedBodyPartMemento::canStartKeyListJob() const
{
  if ( !m_keylistjob )
    return false;
  const char * const fpr = m_vr.signature( 0 ).fingerprint();
  return fpr && *fpr;
}

TQStringList VerifyDetachedBodyPartMemento::keyListPattern() const
{
  assert( canStartKeyListJob() );
  return TQStringList( TQString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
}

void VerifyDetachedBodyPartMemento::saveResult( const VerificationResult & vr )
{
  assert( m_job );
  m_vr = vr;
  setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
}

void VerifyDetachedBodyPartMemento::slotResult( const VerificationResult & vr )
{
  saveResult( vr );
  m_job = 0;
  if ( canStartKeyListJob() && startKeyListJob() )
    return;
  if ( m_keylistjob )
    m_keylistjob->deleteLater();
  m_keylistjob = 0;
  setRunning( false );
  notify();
}

bool VerifyDetachedBodyPartMemento::startKeyListJob()
{
  assert( canStartKeyListJob() );
  if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
    return false;
  connect( m_keylistjob, TQT_SIGNAL(done()), this, TQT_SLOT(slotKeyListJobDone()) );
  connect( m_keylistjob, TQT_SIGNAL(nextKey(const GpgME::Key&)),
           this, TQT_SLOT(slotNextKey(const GpgME::Key&)) );
  return true;
}

void VerifyDetachedBodyPartMemento::slotNextKey( const GpgME::Key & key )
{
  m_key = key;
}

void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
{
  m_keylistjob = 0;
  setRunning( false );
  notify();
}


VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento( VerifyOpaqueJob * job,
                                                          KeyListJob *  klj,
                                                          const TQByteArray & signature )
  : CryptoBodyPartMemento(),
    m_signature( signature ),
    m_job( job ),
    m_keylistjob( klj )
{
  assert( m_job );
}

VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento() {
  if ( m_job )
    m_job->slotCancel();
  if ( m_keylistjob )
    m_keylistjob->slotCancel();
}

bool VerifyOpaqueBodyPartMemento::start() {
  assert( m_job );
  if ( const GpgME::Error err = m_job->start( m_signature ) ) {
    m_vr = VerificationResult( err );
    return false;
  }
  connect( m_job, TQT_SIGNAL(result(const GpgME::VerificationResult&,const TQByteArray&)),
           this, TQT_SLOT(slotResult(const GpgME::VerificationResult&,const TQByteArray&)) );
  setRunning( true );
  return true;
}

void VerifyOpaqueBodyPartMemento::exec() {
  assert( m_job );
  setRunning( true );
  TQByteArray plainText;
  saveResult( m_job->exec( m_signature, plainText ), plainText );
  m_job->deleteLater(); // exec'ed jobs don't delete themselves
  m_job = 0;
  if ( canStartKeyListJob() ) {
    std::vector<GpgME::Key> keys;
    m_keylistjob->exec( keyListPattern(), /*secretOnly=*/false, keys );
    if ( !keys.empty() )
      m_key = keys.back();
  }
  if ( m_keylistjob )
    m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
  m_keylistjob = 0;
  setRunning( false );
}

bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
{
  if ( !m_keylistjob )
    return false;
  const char * const fpr = m_vr.signature( 0 ).fingerprint();
  return fpr && *fpr;
}

TQStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
{
  assert( canStartKeyListJob() );
  return TQStringList( TQString::fromLatin1( m_vr.signature( 0 ).fingerprint() ) );
}

void VerifyOpaqueBodyPartMemento::saveResult( const VerificationResult & vr,
                                              const TQByteArray & plainText )
{
  assert( m_job );
  m_vr = vr;
  m_plainText = plainText;
  setAuditLog( m_job->auditLogError(), m_job->auditLogAsHtml() );
}

void VerifyOpaqueBodyPartMemento::slotResult( const VerificationResult & vr,
                                              const TQByteArray & plainText )
{
  saveResult( vr, plainText );
  m_job = 0;
  if ( canStartKeyListJob() && startKeyListJob() )
    return;
  if ( m_keylistjob )
    m_keylistjob->deleteLater();
  m_keylistjob = 0;
  setRunning( false );
  notify();
}

bool VerifyOpaqueBodyPartMemento::startKeyListJob()
{
  assert( canStartKeyListJob() );
  if ( const GpgME::Error err = m_keylistjob->start( keyListPattern() ) )
    return false;
  connect( m_keylistjob, TQT_SIGNAL(done()), this, TQT_SLOT(slotKeyListJobDone()) );
  connect( m_keylistjob, TQT_SIGNAL(nextKey(const GpgME::Key&)),
           this, TQT_SLOT(slotNextKey(const GpgME::Key&)) );
  return true;
}

void VerifyOpaqueBodyPartMemento::slotNextKey( const GpgME::Key & key )
{
  m_key = key;
}

void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
{
  m_keylistjob = 0;
  setRunning( false );
  notify();
}


#include "objecttreeparser_p.moc"