Messagelib

verifydetachedbodypartmemento.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 "verifydetachedbodypartmemento.h"
8
9#include <QGpgME/KeyListJob>
10#include <QGpgME/VerifyDetachedJob>
11
12#include <gpgme++/keylistresult.h>
13
14#include <cassert>
15
16using namespace QGpgME;
17using namespace GpgME;
18using namespace MimeTreeParser;
19
20VerifyDetachedBodyPartMemento::VerifyDetachedBodyPartMemento(VerifyDetachedJob *job, KeyListJob *klj, const QByteArray &signature, const QByteArray &plainText)
21 : m_signature(signature)
22 , m_plainText(plainText)
23 , m_job(job)
24 , m_keylistjob(klj)
25{
26 assert(m_job);
27}
28
29VerifyDetachedBodyPartMemento::~VerifyDetachedBodyPartMemento()
30{
31 if (m_job) {
32 m_job->slotCancel();
33 }
34 if (m_keylistjob) {
35 m_keylistjob->slotCancel();
36 }
37}
38
39bool VerifyDetachedBodyPartMemento::start()
40{
41 assert(m_job);
42#ifdef DEBUG_SIGNATURE
43 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento started";
44#endif
45 connect(m_job.data(), &VerifyDetachedJob::result, this, &VerifyDetachedBodyPartMemento::slotResult);
46 if (const Error err = m_job->start(m_signature, m_plainText)) {
47 m_vr = VerificationResult(err);
48#ifdef DEBUG_SIGNATURE
49 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento stopped with error";
50#endif
51 return false;
52 }
53 setRunning(true);
54 return true;
55}
56
57void VerifyDetachedBodyPartMemento::exec()
58{
59 assert(m_job);
60 setRunning(true);
61#ifdef DEBUG_SIGNATURE
62 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento execed";
63#endif
64 saveResult(m_job->exec(m_signature, m_plainText));
65 m_job->deleteLater(); // exec'ed jobs don't delete themselves
66 m_job = nullptr;
67#ifdef DEBUG_SIGNATURE
68 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento after execed";
69#endif
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 VerifyDetachedBodyPartMemento::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 VerifyDetachedBodyPartMemento::keyListPattern() const
94{
95 assert(canStartKeyListJob());
96 return QStringList(QString::fromLatin1(m_vr.signature(0).fingerprint()));
97}
98
99void VerifyDetachedBodyPartMemento::saveResult(const VerificationResult &vr)
100{
101 assert(m_job);
102#ifdef DEBUG_SIGNATURE
103 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento::saveResult called";
104#endif
105 m_vr = vr;
106 setAuditLog(m_job->auditLogError(), m_job->auditLogAsHtml());
107}
108
109void VerifyDetachedBodyPartMemento::slotResult(const VerificationResult &vr)
110{
111#ifdef DEBUG_SIGNATURE
112 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento::slotResult called";
113#endif
114 saveResult(vr);
115 m_job = nullptr;
116 if (canStartKeyListJob() && startKeyListJob()) {
117#ifdef DEBUG_SIGNATURE
118 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento: canStartKeyListJob && startKeyListJob";
119#endif
120 return;
121 }
122 if (m_keylistjob) {
123 m_keylistjob->deleteLater();
124 }
125 m_keylistjob = nullptr;
126 setRunning(false);
127 notify();
128}
129
130bool VerifyDetachedBodyPartMemento::startKeyListJob()
131{
132 assert(canStartKeyListJob());
133 if (const GpgME::Error err = m_keylistjob->start(keyListPattern())) {
134 return false;
135 }
136 connect(m_keylistjob.data(), &Job::done, this, &VerifyDetachedBodyPartMemento::slotKeyListJobDone);
137 connect(m_keylistjob.data(), &KeyListJob::nextKey, this, &VerifyDetachedBodyPartMemento::slotNextKey);
138 return true;
139}
140
141void VerifyDetachedBodyPartMemento::slotNextKey(const GpgME::Key &key)
142{
143#ifdef DEBUG_SIGNATURE
144 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento::slotNextKey called";
145#endif
146 m_key = key;
147}
148
149void VerifyDetachedBodyPartMemento::slotKeyListJobDone()
150{
151#ifdef DEBUG_SIGNATURE
152 qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyDetachedBodyPartMemento::slotKeyListJobDone called";
153#endif
154 m_keylistjob = nullptr;
155 setRunning(false);
156 notify();
157}
158
159#include "moc_verifydetachedbodypartmemento.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.