Messagelib

verifyopaquebodypartmemento.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2025 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 : m_signature(signature)
22 , m_job(job)
23 , m_keylistjob(klj)
24{
25 assert(m_job);
26}
27
28VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento()
29{
30 if (m_job) {
31 m_job->slotCancel();
32 }
33 if (m_keylistjob) {
34 m_keylistjob->slotCancel();
35 }
36}
37
38bool VerifyOpaqueBodyPartMemento::start()
39{
40 assert(m_job);
41#ifdef DEBUG_SIGNATURE
42 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento started";
43#endif
44 if (const Error err = m_job->start(m_signature)) {
45 m_vr = VerificationResult(err);
46#ifdef DEBUG_SIGNATURE
47 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento stopped with error";
48#endif
49 return false;
50 }
51 connect(m_job.data(), &VerifyOpaqueJob::result, this, &VerifyOpaqueBodyPartMemento::slotResult);
52 setRunning(true);
53 return true;
54}
55
56void VerifyOpaqueBodyPartMemento::exec()
57{
58 assert(m_job);
59 setRunning(true);
60 QByteArray plainText;
61#ifdef DEBUG_SIGNATURE
62 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento execed";
63#endif
64 saveResult(m_job->exec(m_signature, plainText), plainText);
65#ifdef DEBUG_SIGNATURE
66 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento after execed";
67#endif
68 m_job->deleteLater(); // exec'ed jobs don't delete themselves
69 m_job = nullptr;
70 if (canStartKeyListJob()) {
71 std::vector<GpgME::Key> keys;
72 m_keylistjob->exec(keyListPattern(), /*secretOnly=*/false, keys);
73 if (!keys.empty()) {
74 m_key = keys.back();
75 }
76 }
77 if (m_keylistjob) {
78 m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
79 }
80 m_keylistjob = nullptr;
81 setRunning(false);
82}
83
84bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
85{
86 if (!m_keylistjob) {
87 return false;
88 }
89 const char *const fpr = m_vr.signature(0).fingerprint();
90 return fpr && *fpr;
91}
92
93QStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
94{
95 assert(canStartKeyListJob());
96 return QStringList(QString::fromLatin1(m_vr.signature(0).fingerprint()));
97}
98
99void VerifyOpaqueBodyPartMemento::saveResult(const VerificationResult &vr, const QByteArray &plainText)
100{
101 assert(m_job);
102#ifdef DEBUG_SIGNATURE
103 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::saveResult called";
104#endif
105 m_vr = vr;
106 m_plainText = plainText;
107 setAuditLog(m_job->auditLogError(), m_job->auditLogAsHtml());
108}
109
110void VerifyOpaqueBodyPartMemento::slotResult(const VerificationResult &vr, const QByteArray &plainText)
111{
112#ifdef DEBUG_SIGNATURE
113 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotResult called";
114#endif
115 saveResult(vr, plainText);
116 m_job = nullptr;
117 if (canStartKeyListJob() && startKeyListJob()) {
118#ifdef DEBUG_SIGNATURE
119 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento: 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
131bool VerifyOpaqueBodyPartMemento::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, &VerifyOpaqueBodyPartMemento::slotKeyListJobDone);
138 connect(m_keylistjob.data(), &KeyListJob::nextKey, this, &VerifyOpaqueBodyPartMemento::slotNextKey);
139 return true;
140}
141
142void VerifyOpaqueBodyPartMemento::slotNextKey(const GpgME::Key &key)
143{
144#ifdef DEBUG_SIGNATURE
145 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotNextKey called";
146#endif
147 m_key = key;
148}
149
150void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
151{
152#ifdef DEBUG_SIGNATURE
153 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotKeyListJobDone called";
154#endif
155 m_keylistjob = nullptr;
156 setRunning(false);
157 notify();
158}
159
160#include "moc_verifyopaquebodypartmemento.cpp"
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromLatin1(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 25 2025 11:46:37 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.