diff options
author | Ingo Klöcker <[email protected]> | 2023-12-21 08:38:52 +0000 |
---|---|---|
committer | Ingo Klöcker <[email protected]> | 2023-12-21 10:49:23 +0000 |
commit | a44d84772d6197dfb977b411f6eb7cb29b08471b (patch) | |
tree | d3b679026236ab83faabc36b94a466b6787f593d /lang/qt/src/qgpgmeencryptjob.cpp | |
parent | qt: Remove dead code (diff) | |
download | gpgme-a44d84772d6197dfb977b411f6eb7cb29b08471b.tar.gz gpgme-a44d84772d6197dfb977b411f6eb7cb29b08471b.zip |
qt: Support writing/reading signed/encrypted files directly to/from file
* lang/qt/src/Makefile.am: Add new files.
* lang/qt/src/job.cpp (EncryptJob, SignJob, SignEncryptJob): Move
definition of constructor and destructor and inclusion of the moc file
to the corresponding .cpp files.
* lang/qt/src/encryptjob.cpp (EncryptJob): Define constructor. Define
destructor as default. Include moc file.
* lang/qt/src/encryptjob.cpp, lang/qt/src/encryptjob.h (EncryptJob):
Add member functions setRecipients, recipients, setInputFile, inputFile,
setOutputFile, outputFile, setEncryptionFlags, encryptionFlags.
* lang/qt/src/encryptjob_p.h (EncryptJobPrivate): Add members
m_recipients, m_inputFilePath, m_outputFilePath, m_encryptionFlags.
* lang/qt/src/qgpgmeencryptjob.cpp (encrypt_to_filename): New.
(QGpgMEEncryptJobPrivate::startIt): Start the job with the values
from the member variables.
* lang/qt/src/qgpgmesignencryptjob.cpp (sign_encrypt_to_filename): New.
(QGpgMESignEncryptJobPrivate::startIt): Start the job with the values
from the member variables.
* lang/qt/src/qgpgmesignjob.cpp (class QGpgMESignJobPrivate): New.
(QGpgMESignJob::QGpgMESignJob): Instantiate private job class.
(sign_to_filename): New.
* lang/qt/src/signencryptjob.cpp (SignEncryptJob): Define constructor.
Define destructor as default. Include moc file.
* lang/qt/src/signencryptjob.cpp, lang/qt/src/signencryptjob.h
(SignEncryptJob): Add member functions setSigners, signers,
setRecipients, recipients, setInputFile, inputFile, setOutputFile,
outputFile, setEncryptionFlags, encryptionFlags.
* lang/qt/src/signencryptjob_p.h (SignEncryptJobPrivate): Add members
m_signers, m_recipients, m_inputFilePath, m_outputFilePath,
m_encryptionFlags.
* lang/qt/src/signjob.cpp: New.
* lang/qt/src/signjob.h (SignJob): Add member functions setSigners,
signers, setInputFile, inputFile, setOutputFile, outputFile,
setSigningFlags, signingFlags.
* lang/qt/src/signjob_p.h: New.
* lang/qt/tests/Makefile.am: Add new test programs.
* lang/qt/tests/run-encryptjob.cpp: New.
* lang/qt/tests/run-signjob.cpp: New.
--
This makes it possible to tell gpg to read the input and write the
output directly to a specified file bypassing GpgME's Data IO when
signing and/or encrypting a file.
GnuPG-bug-id: 6550
Diffstat (limited to 'lang/qt/src/qgpgmeencryptjob.cpp')
-rw-r--r-- | lang/qt/src/qgpgmeencryptjob.cpp | 67 |
1 files changed, 57 insertions, 10 deletions
diff --git a/lang/qt/src/qgpgmeencryptjob.cpp b/lang/qt/src/qgpgmeencryptjob.cpp index c11089ff..d254d8a4 100644 --- a/lang/qt/src/qgpgmeencryptjob.cpp +++ b/lang/qt/src/qgpgmeencryptjob.cpp @@ -40,13 +40,13 @@ #include "qgpgmeencryptjob.h" -#include "encryptjob_p.h" - #include "dataprovider.h" +#include "encryptjob_p.h" +#include "util.h" -#include "context.h" -#include "encryptionresult.h" -#include "data.h" +#include <context.h> +#include <data.h> +#include <encryptionresult.h> #include <QBuffer> #include <QFileInfo> @@ -72,11 +72,7 @@ public: ~QGpgMEEncryptJobPrivate() 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 { @@ -168,6 +164,44 @@ static QGpgMEEncryptJob::result_type encrypt_qba(Context *ctx, const std::vector return encrypt(ctx, nullptr, recipients, buffer, std::shared_ptr<QIODevice>(), eflags, outputIsBsse64Encoded, inputEncoding, fileName); } +static QGpgMEEncryptJob::result_type encrypt_to_filename(Context *ctx, + 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(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 + + flags = static_cast<Context::EncryptionFlags>(flags | Context::EncryptFile); + const auto encryptionResult = ctx->encrypt(recipients, indata, outdata, flags); + + if (!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(encryptionResult, QByteArray{}, log, ae); +} + Error QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust) { run(std::bind(&encrypt_qba, std::placeholders::_1, recipients, plainText, @@ -213,4 +247,17 @@ void QGpgMEEncryptJob::resultHook(const result_type &tuple) mResult = std::get<0>(tuple); } +GpgME::Error QGpgMEEncryptJobPrivate::startIt() +{ + if (m_inputFilePath.isEmpty() || m_outputFilePath.isEmpty()) { + return Error::fromCode(GPG_ERR_INV_VALUE); + } + + q->run([=](Context *ctx) { + return encrypt_to_filename(ctx, m_recipients, m_inputFilePath, m_outputFilePath, m_encryptionFlags); + }); + + return {}; +} + #include "qgpgmeencryptjob.moc" |