Messagelib

verifyopaquebodypartmemento.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "verifyopaquebodypartmemento.h"
8 #include "mimetreeparser_debug.h"
9 
10 #include <QGpgME/KeyListJob>
11 #include <QGpgME/VerifyOpaqueJob>
12 
13 #include <gpgme++/keylistresult.h>
14 
15 #include <cassert>
16 
17 using namespace QGpgME;
18 using namespace GpgME;
19 using namespace MimeTreeParser;
20 
21 VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento(VerifyOpaqueJob *job, KeyListJob *klj, const QByteArray &signature)
22  : CryptoBodyPartMemento()
23  , m_signature(signature)
24  , m_job(job)
25  , m_keylistjob(klj)
26 {
27  assert(m_job);
28 }
29 
30 VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento()
31 {
32  if (m_job) {
33  m_job->slotCancel();
34  }
35  if (m_keylistjob) {
36  m_keylistjob->slotCancel();
37  }
38 }
39 
40 bool VerifyOpaqueBodyPartMemento::start()
41 {
42  assert(m_job);
43 #ifdef DEBUG_SIGNATURE
44  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento started";
45 #endif
46  if (const Error err = m_job->start(m_signature)) {
47  m_vr = VerificationResult(err);
48 #ifdef DEBUG_SIGNATURE
49  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento stopped with error";
50 #endif
51  return false;
52  }
53  connect(m_job.data(), &VerifyOpaqueJob::result, this, &VerifyOpaqueBodyPartMemento::slotResult);
54  setRunning(true);
55  return true;
56 }
57 
58 void VerifyOpaqueBodyPartMemento::exec()
59 {
60  assert(m_job);
61  setRunning(true);
62  QByteArray plainText;
63 #ifdef DEBUG_SIGNATURE
64  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento execed";
65 #endif
66  saveResult(m_job->exec(m_signature, plainText), plainText);
67 #ifdef DEBUG_SIGNATURE
68  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento after execed";
69 #endif
70  m_job->deleteLater(); // exec'ed jobs don't delete themselves
71  m_job = nullptr;
72  if (canStartKeyListJob()) {
73  std::vector<GpgME::Key> keys;
74  m_keylistjob->exec(keyListPattern(), /*secretOnly=*/false, keys);
75  if (!keys.empty()) {
76  m_key = keys.back();
77  }
78  }
79  if (m_keylistjob) {
80  m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
81  }
82  m_keylistjob = nullptr;
83  setRunning(false);
84 }
85 
86 bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
87 {
88  if (!m_keylistjob) {
89  return false;
90  }
91  const char *const fpr = m_vr.signature(0).fingerprint();
92  return fpr && *fpr;
93 }
94 
95 QStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
96 {
97  assert(canStartKeyListJob());
98  return QStringList(QString::fromLatin1(m_vr.signature(0).fingerprint()));
99 }
100 
101 void VerifyOpaqueBodyPartMemento::saveResult(const VerificationResult &vr, const QByteArray &plainText)
102 {
103  assert(m_job);
104 #ifdef DEBUG_SIGNATURE
105  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::saveResult called";
106 #endif
107  m_vr = vr;
108  m_plainText = plainText;
109  setAuditLog(m_job->auditLogError(), m_job->auditLogAsHtml());
110 }
111 
112 void VerifyOpaqueBodyPartMemento::slotResult(const VerificationResult &vr, const QByteArray &plainText)
113 {
114 #ifdef DEBUG_SIGNATURE
115  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotResult called";
116 #endif
117  saveResult(vr, plainText);
118  m_job = nullptr;
119  if (canStartKeyListJob() && startKeyListJob()) {
120 #ifdef DEBUG_SIGNATURE
121  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento: canStartKeyListJob && startKeyListJob";
122 #endif
123  return;
124  }
125  if (m_keylistjob) {
126  m_keylistjob->deleteLater();
127  }
128  m_keylistjob = nullptr;
129  setRunning(false);
130  notify();
131 }
132 
133 bool VerifyOpaqueBodyPartMemento::startKeyListJob()
134 {
135  assert(canStartKeyListJob());
136  if (const GpgME::Error err = m_keylistjob->start(keyListPattern())) {
137  return false;
138  }
139  connect(m_keylistjob.data(), &Job::done, this, &VerifyOpaqueBodyPartMemento::slotKeyListJobDone);
140  connect(m_keylistjob.data(), &KeyListJob::nextKey, this, &VerifyOpaqueBodyPartMemento::slotNextKey);
141  return true;
142 }
143 
144 void VerifyOpaqueBodyPartMemento::slotNextKey(const GpgME::Key &key)
145 {
146 #ifdef DEBUG_SIGNATURE
147  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotNextKey called";
148 #endif
149  m_key = key;
150 }
151 
152 void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
153 {
154 #ifdef DEBUG_SIGNATURE
155  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotKeyListJobDone called";
156 #endif
157  m_keylistjob = nullptr;
158  setRunning(false);
159  notify();
160 }
char back() const const
QString fromLatin1(const char *str, int size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.