Messagelib

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

KDE's Doxygen guidelines are available online.