Messagelib

verifydetachedbodypartmemento.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 "verifydetachedbodypartmemento.h"
8 #include "mimetreeparser_debug.h"
9 
10 #include <QGpgME/KeyListJob>
11 #include <QGpgME/VerifyDetachedJob>
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 VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento(VerifyDetachedJob *job, KeyListJob *klj, const QByteArray &signature, const QByteArray &plainText)
22  : CryptoBodyPartMemento()
23  , m_signature(signature)
24  , m_plainText(plainText)
25  , m_job(job)
26  , m_keylistjob(klj)
27 {
28  assert(m_job);
29 }
30 
31 VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento()
32 {
33  if (m_job) {
34  m_job->slotCancel();
35  }
36  if (m_keylistjob) {
37  m_keylistjob->slotCancel();
38  }
39 }
40 
41 bool VerifyDetachedBodyPartMemento::start()
42 {
43  assert(m_job);
44 #ifdef DEBUG_SIGNATURE
45  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento started";
46 #endif
47  connect(m_job.data(), &VerifyDetachedJob::result, this, &VerifyDetachedBodyPartMemento::slotResult);
48  if (const Error err = m_job->start(m_signature, m_plainText)) {
49  m_vr = VerificationResult(err);
50 #ifdef DEBUG_SIGNATURE
51  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento stopped with error";
52 #endif
53  return false;
54  }
55  setRunning(true);
56  return true;
57 }
58 
59 void VerifyDetachedBodyPartMemento::exec()
60 {
61  assert(m_job);
62  setRunning(true);
63 #ifdef DEBUG_SIGNATURE
64  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento execed";
65 #endif
66  saveResult(m_job->exec(m_signature, m_plainText));
67  m_job->deleteLater(); // exec'ed jobs don't delete themselves
68  m_job = nullptr;
69 #ifdef DEBUG_SIGNATURE
70  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento after execed";
71 #endif
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 VerifyDetachedBodyPartMemento::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 VerifyDetachedBodyPartMemento::keyListPattern() const
96 {
97  assert(canStartKeyListJob());
98  return QStringList(QString::fromLatin1(m_vr.signature(0).fingerprint()));
99 }
100 
101 void VerifyDetachedBodyPartMemento::saveResult(const VerificationResult &vr)
102 {
103  assert(m_job);
104 #ifdef DEBUG_SIGNATURE
105  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento::saveResult called";
106 #endif
107  m_vr = vr;
108  setAuditLog(m_job->auditLogError(), m_job->auditLogAsHtml());
109 }
110 
111 void VerifyDetachedBodyPartMemento::slotResult(const VerificationResult &vr)
112 {
113 #ifdef DEBUG_SIGNATURE
114  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento::slotResult called";
115 #endif
116  saveResult(vr);
117  m_job = nullptr;
118  if (canStartKeyListJob() && startKeyListJob()) {
119 #ifdef DEBUG_SIGNATURE
120  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento: canStartKeyListJob && startKeyListJob";
121 #endif
122  return;
123  }
124  if (m_keylistjob) {
125  m_keylistjob->deleteLater();
126  }
127  m_keylistjob = nullptr;
128  setRunning(false);
129  notify();
130 }
131 
132 bool VerifyDetachedBodyPartMemento::startKeyListJob()
133 {
134  assert(canStartKeyListJob());
135  if (const GpgME::Error err = m_keylistjob->start(keyListPattern())) {
136  return false;
137  }
138  connect(m_keylistjob.data(), &Job::done, this, &VerifyDetachedBodyPartMemento::slotKeyListJobDone);
139  connect(m_keylistjob.data(), &KeyListJob::nextKey, this, &VerifyDetachedBodyPartMemento::slotNextKey);
140  return true;
141 }
142 
143 void VerifyDetachedBodyPartMemento::slotNextKey(const GpgME::Key &key)
144 {
145 #ifdef DEBUG_SIGNATURE
146  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento::slotNextKey called";
147 #endif
148  m_key = key;
149 }
150 
151 void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
152 {
153 #ifdef DEBUG_SIGNATURE
154  qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento::slotKeyListJobDone called";
155 #endif
156  m_keylistjob = nullptr;
157  setRunning(false);
158  notify();
159 }
QString fromLatin1(const char *str, int size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.