Messagelib

qgpgmejobexecutor.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "qgpgmejobexecutor.h"
8 #include "mimetreeparser_debug.h"
9 
10 #include <QGpgME/DecryptVerifyJob>
11 #include <QGpgME/ImportJob>
12 #include <QGpgME/VerifyDetachedJob>
13 #include <QGpgME/VerifyOpaqueJob>
14 
15 #include <QEventLoop>
16 
17 #include <cassert>
18 
19 using namespace GpgME;
20 using namespace MimeTreeParser;
21 
22 QGpgMEJobExecutor::QGpgMEJobExecutor(QObject *parent)
23  : QObject(parent)
24  , mEventLoop(new QEventLoop(this))
25 {
26  setObjectName(QStringLiteral("KleoJobExecutor"));
27 }
28 
29 GpgME::VerificationResult QGpgMEJobExecutor::exec(QGpgME::VerifyDetachedJob *job, const QByteArray &signature, const QByteArray &signedData)
30 {
31  qCDebug(MIMETREEPARSER_LOG) << "Starting detached verification job";
32  connect(job, &QGpgME::VerifyDetachedJob::result, this, qOverload<const GpgME::VerificationResult &>(&QGpgMEJobExecutor::verificationResult));
33  GpgME::Error err = job->start(signature, signedData);
34  if (err) {
35  return VerificationResult(err);
36  }
37  mEventLoop->exec(QEventLoop::ExcludeUserInputEvents);
38  return mVerificationResult;
39 }
40 
41 GpgME::VerificationResult QGpgMEJobExecutor::exec(QGpgME::VerifyOpaqueJob *job, const QByteArray &signedData, QByteArray &plainText)
42 {
43  qCDebug(MIMETREEPARSER_LOG) << "Starting opaque verification job";
44  connect(job,
45  &QGpgME::VerifyOpaqueJob::result,
46  this,
47  qOverload<const GpgME::VerificationResult &, const QByteArray &>(&QGpgMEJobExecutor::verificationResult));
48  GpgME::Error err = job->start(signedData);
49  if (err) {
50  plainText.clear();
51  return VerificationResult(err);
52  }
53  mEventLoop->exec(QEventLoop::ExcludeUserInputEvents);
54  plainText = mData;
55  return mVerificationResult;
56 }
57 
58 std::pair<GpgME::DecryptionResult, GpgME::VerificationResult>
59 QGpgMEJobExecutor::exec(QGpgME::DecryptVerifyJob *job, const QByteArray &cipherText, QByteArray &plainText)
60 {
61  qCDebug(MIMETREEPARSER_LOG) << "Starting decryption job";
62  connect(job, &QGpgME::DecryptVerifyJob::result, this, &QGpgMEJobExecutor::decryptResult);
63  GpgME::Error err = job->start(cipherText);
64  if (err) {
65  plainText.clear();
66  return std::make_pair(DecryptionResult(err), VerificationResult(err));
67  }
68  mEventLoop->exec(QEventLoop::ExcludeUserInputEvents);
69  plainText = mData;
70  return std::make_pair(mDecryptResult, mVerificationResult);
71 }
72 
73 GpgME::ImportResult QGpgMEJobExecutor::exec(QGpgME::ImportJob *job, const QByteArray &certData)
74 {
75  connect(job, &QGpgME::AbstractImportJob::result, this, &QGpgMEJobExecutor::importResult);
76  GpgME::Error err = job->start(certData);
77  if (err) {
78  return ImportResult(err);
79  }
80  mEventLoop->exec(QEventLoop::ExcludeUserInputEvents);
81  return mImportResult;
82 }
83 
84 Error QGpgMEJobExecutor::auditLogError() const
85 {
86  return mAuditLogError;
87 }
88 
89 void QGpgMEJobExecutor::verificationResult(const GpgME::VerificationResult &result)
90 {
91  qCDebug(MIMETREEPARSER_LOG) << "Detached verification job finished";
92  auto job = qobject_cast<QGpgME::Job *>(sender());
93  assert(job);
94  mVerificationResult = result;
95  mAuditLogError = job->auditLogError();
96  mAuditLog = job->auditLogAsHtml();
97  mEventLoop->quit();
98 }
99 
100 void QGpgMEJobExecutor::verificationResult(const GpgME::VerificationResult &result, const QByteArray &plainText)
101 {
102  qCDebug(MIMETREEPARSER_LOG) << "Opaque verification job finished";
103  auto job = qobject_cast<QGpgME::Job *>(sender());
104  assert(job);
105  mVerificationResult = result;
106  mData = plainText;
107  mAuditLogError = job->auditLogError();
108  mAuditLog = job->auditLogAsHtml();
109  mEventLoop->quit();
110 }
111 
112 void QGpgMEJobExecutor::decryptResult(const GpgME::DecryptionResult &decryptionresult,
113  const GpgME::VerificationResult &verificationresult,
114  const QByteArray &plainText)
115 {
116  qCDebug(MIMETREEPARSER_LOG) << "Decryption job finished";
117  auto job = qobject_cast<QGpgME::Job *>(sender());
118  assert(job);
119  mVerificationResult = verificationresult;
120  mDecryptResult = decryptionresult;
121  mData = plainText;
122  mAuditLogError = job->auditLogError();
123  mAuditLog = job->auditLogAsHtml();
124  mEventLoop->quit();
125 }
126 
127 void QGpgMEJobExecutor::importResult(const GpgME::ImportResult &result)
128 {
129  auto job = qobject_cast<QGpgME::Job *>(sender());
130  assert(job);
131  mImportResult = result;
132  mAuditLogError = job->auditLogError();
133  mAuditLog = job->auditLogAsHtml();
134  mEventLoop->quit();
135 }
136 
137 QString QGpgMEJobExecutor::auditLogAsHtml() const
138 {
139  return mAuditLog;
140 }
void clear()
QObject * sender() const const
void setObjectName(const QString &name)
void result(KJob *job)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun May 16 2021 23:14:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.