Messagelib

verifyopaquebodypartmemento.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 "verifyopaquebodypartmemento.h"
8
9#include <QGpgME/KeyListJob>
10#include <QGpgME/VerifyOpaqueJob>
11
12#include <gpgme++/keylistresult.h>
13
14#include <cassert>
15
16using namespace QGpgME;
17using namespace GpgME;
18using namespace MimeTreeParser;
19
20VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento(VerifyOpaqueJob *job, KeyListJob *klj, const QByteArray &signature)
21 : CryptoBodyPartMemento()
22 , m_signature(signature)
23 , m_job(job)
24 , m_keylistjob(klj)
25{
26 assert(m_job);
27}
28
29VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento()
30{
31 if (m_job) {
32 m_job->slotCancel();
33 }
34 if (m_keylistjob) {
35 m_keylistjob->slotCancel();
36 }
37}
38
39bool VerifyOpaqueBodyPartMemento::start()
40{
41 assert(m_job);
42#ifdef DEBUG_SIGNATURE
43 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento started";
44#endif
45 if (const Error err = m_job->start(m_signature)) {
46 m_vr = VerificationResult(err);
47#ifdef DEBUG_SIGNATURE
48 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento stopped with error";
49#endif
50 return false;
51 }
52 connect(m_job.data(), &VerifyOpaqueJob::result, this, &VerifyOpaqueBodyPartMemento::slotResult);
53 setRunning(true);
54 return true;
55}
56
57void VerifyOpaqueBodyPartMemento::exec()
58{
59 assert(m_job);
60 setRunning(true);
61 QByteArray plainText;
62#ifdef DEBUG_SIGNATURE
63 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento execed";
64#endif
65 saveResult(m_job->exec(m_signature, plainText), plainText);
66#ifdef DEBUG_SIGNATURE
67 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento after execed";
68#endif
69 m_job->deleteLater(); // exec'ed jobs don't delete themselves
70 m_job = nullptr;
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
85bool VerifyOpaqueBodyPartMemento::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
94QStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
95{
96 assert(canStartKeyListJob());
97 return QStringList(QString::fromLatin1(m_vr.signature(0).fingerprint()));
98}
99
100void VerifyOpaqueBodyPartMemento::saveResult(const VerificationResult &vr, const QByteArray &plainText)
101{
102 assert(m_job);
103#ifdef DEBUG_SIGNATURE
104 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::saveResult called";
105#endif
106 m_vr = vr;
107 m_plainText = plainText;
108 setAuditLog(m_job->auditLogError(), m_job->auditLogAsHtml());
109}
110
111void VerifyOpaqueBodyPartMemento::slotResult(const VerificationResult &vr, const QByteArray &plainText)
112{
113#ifdef DEBUG_SIGNATURE
114 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotResult called";
115#endif
116 saveResult(vr, plainText);
117 m_job = nullptr;
118 if (canStartKeyListJob() && startKeyListJob()) {
119#ifdef DEBUG_SIGNATURE
120 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento: 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
132bool VerifyOpaqueBodyPartMemento::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, &VerifyOpaqueBodyPartMemento::slotKeyListJobDone);
139 connect(m_keylistjob.data(), &KeyListJob::nextKey, this, &VerifyOpaqueBodyPartMemento::slotNextKey);
140 return true;
141}
142
143void VerifyOpaqueBodyPartMemento::slotNextKey(const GpgME::Key &key)
144{
145#ifdef DEBUG_SIGNATURE
146 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotNextKey called";
147#endif
148 m_key = key;
149}
150
151void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
152{
153#ifdef DEBUG_SIGNATURE
154 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotKeyListJobDone called";
155#endif
156 m_keylistjob = nullptr;
157 setRunning(false);
158 notify();
159}
160
161#include "moc_verifyopaquebodypartmemento.cpp"
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T * data() const const
QString fromLatin1(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.