aboutsummaryrefslogtreecommitdiffstats
path: root/lang/qt/src/qgpgmesignencryptjob.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lang/qt/src/qgpgmesignencryptjob.cpp')
-rw-r--r--lang/qt/src/qgpgmesignencryptjob.cpp86
1 files changed, 75 insertions, 11 deletions
diff --git a/lang/qt/src/qgpgmesignencryptjob.cpp b/lang/qt/src/qgpgmesignencryptjob.cpp
index 0f5642a9..27af7ae9 100644
--- a/lang/qt/src/qgpgmesignencryptjob.cpp
+++ b/lang/qt/src/qgpgmesignencryptjob.cpp
@@ -40,14 +40,14 @@
#include "qgpgmesignencryptjob.h"
-#include "signencryptjob_p.h"
-
#include "dataprovider.h"
+#include "signencryptjob_p.h"
+#include "util.h"
-#include "context.h"
-#include "data.h"
-#include "key.h"
-#include "exception.h"
+#include <context.h>
+#include <data.h>
+#include <exception.h>
+#include <key.h>
#include <QBuffer>
#include <QFileInfo>
@@ -73,11 +73,7 @@ public:
~QGpgMESignEncryptJobPrivate() override = default;
private:
- GpgME::Error startIt() override
- {
- Q_ASSERT(!"Not supported by this Job class.");
- return Error::fromCode(GPG_ERR_NOT_SUPPORTED);
- }
+ GpgME::Error startIt() override;
void startNow() override
{
@@ -171,6 +167,60 @@ static QGpgMESignEncryptJob::result_type sign_encrypt_qba(Context *ctx, const st
return sign_encrypt(ctx, nullptr, signers, recipients, buffer, std::shared_ptr<QIODevice>(), eflags, outputIsBsse64Encoded, fileName);
}
+static QGpgMESignEncryptJob::result_type sign_encrypt_to_filename(Context *ctx,
+ const std::vector<Key> &signers,
+ const std::vector<Key> &recipients,
+ const QString &inputFilePath,
+ const QString &outputFilePath,
+ Context::EncryptionFlags flags)
+{
+ Data indata;
+#ifdef Q_OS_WIN
+ indata.setFileName(inputFilePath().toUtf8().constData());
+#else
+ indata.setFileName(QFile::encodeName(inputFilePath).constData());
+#endif
+
+ PartialFileGuard partFileGuard{outputFilePath};
+ if (partFileGuard.tempFileName().isEmpty()) {
+ return std::make_tuple(SigningResult{Error::fromCode(GPG_ERR_EEXIST)},
+ EncryptionResult{Error::fromCode(GPG_ERR_EEXIST)},
+ QByteArray{},
+ QString{},
+ Error{});
+ }
+
+ Data outdata;
+#ifdef Q_OS_WIN
+ outdata.setFileName(partFileGuard.tempFileName().toUtf8().constData());
+#else
+ outdata.setFileName(QFile::encodeName(partFileGuard.tempFileName()).constData());
+#endif
+
+ ctx->clearSigningKeys();
+ for (const Key &signer : signers) {
+ if (!signer.isNull()) {
+ if (const Error err = ctx->addSigningKey(signer)) {
+ return std::make_tuple(SigningResult{err}, EncryptionResult{}, QByteArray{}, QString{}, Error{});
+ }
+ }
+ }
+
+ flags = static_cast<Context::EncryptionFlags>(flags | Context::EncryptFile);
+ const auto results = ctx->signAndEncrypt(recipients, indata, outdata, flags);
+ const auto &signingResult = results.first;
+ const auto &encryptionResult = results.second;
+
+ if (!signingResult.error().code() && !encryptionResult.error().code()) {
+ // the operation succeeded -> save the result under the requested file name
+ partFileGuard.commit();
+ }
+
+ Error ae;
+ const QString log = _detail::audit_log_as_html(ctx, ae);
+ return std::make_tuple(signingResult, encryptionResult, QByteArray{}, log, ae);
+}
+
Error QGpgMESignEncryptJob::start(const std::vector<Key> &signers, const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust)
{
run(std::bind(&sign_encrypt_qba, std::placeholders::_1, signers, recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, mOutputIsBase64Encoded, fileName()));
@@ -205,4 +255,18 @@ void QGpgMESignEncryptJob::resultHook(const result_type &tuple)
{
mResult = std::make_pair(std::get<0>(tuple), std::get<1>(tuple));
}
+
+GpgME::Error QGpgMESignEncryptJobPrivate::startIt()
+{
+ if (m_inputFilePath.isEmpty() || m_outputFilePath.isEmpty()) {
+ return Error::fromCode(GPG_ERR_INV_VALUE);
+ }
+
+ q->run([=](Context *ctx) {
+ return sign_encrypt_to_filename(ctx, m_signers, m_recipients, m_inputFilePath, m_outputFilePath, m_encryptionFlags);
+ });
+
+ return {};
+}
+
#include "qgpgmesignencryptjob.moc"