diff --git a/lang/qt/src/qgpgmeencryptarchivejob.cpp b/lang/qt/src/qgpgmeencryptarchivejob.cpp index 07095ea1..b8bb4c42 100644 --- a/lang/qt/src/qgpgmeencryptarchivejob.cpp +++ b/lang/qt/src/qgpgmeencryptarchivejob.cpp @@ -44,6 +44,7 @@ #include "encryptarchivejob_p.h" #include "filelistdataprovider.h" #include "qgpgme_debug.h" +#include "util.h" #include @@ -104,20 +105,6 @@ static QGpgMEEncryptArchiveJob::result_type encrypt(Context *ctx, flags = static_cast(flags | Context::EncryptArchive); const auto encryptionResult = ctx->encrypt(recipients, indata, outdata, flags); -#ifdef Q_OS_WIN - const auto outputFileName = QString::fromUtf8(outdata.fileName()); -#else - const auto outputFileName = QFile::decodeName(outdata.fileName()); -#endif - if (!outputFileName.isEmpty() && encryptionResult.error().code()) { - // ensure that the output file is removed if the operation was canceled or failed - if (QFile::exists(outputFileName)) { - qCDebug(QGPGME_LOG) << __func__ << "Removing output file" << outputFileName << "after error or cancel"; - if (!QFile::remove(outputFileName)) { - qCDebug(QGPGME_LOG) << __func__ << "Removing output file" << outputFileName << "failed"; - } - } - } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(encryptionResult, log, ae); @@ -142,18 +129,25 @@ static QGpgMEEncryptArchiveJob::result_type encrypt_to_io_device(Context *ctx, static QGpgMEEncryptArchiveJob::result_type encrypt_to_filename(Context *ctx, const std::vector &recipients, const std::vector &paths, - const QString &outputFile, + const QString &outputFileName, Context::EncryptionFlags flags, const QString &baseDirectory) { Data outdata; #ifdef Q_OS_WIN - outdata.setFileName(outputFile.toUtf8().constData()); + outdata.setFileName(outputFileName.toUtf8().constData()); #else - outdata.setFileName(QFile::encodeName(outputFile).constData()); + outdata.setFileName(QFile::encodeName(outputFileName).constData()); #endif - return encrypt(ctx, recipients, paths, outdata, flags, baseDirectory); + const auto result = encrypt(ctx, recipients, paths, outdata, flags, baseDirectory); + const auto &encryptionResult = std::get<0>(result); + if (encryptionResult.error().code()) { + // ensure that the output file is removed if the operation was canceled or failed + removeFile(outputFileName); + } + + return result; } GpgME::Error QGpgMEEncryptArchiveJob::start(const std::vector &recipients, diff --git a/lang/qt/src/qgpgmesignarchivejob.cpp b/lang/qt/src/qgpgmesignarchivejob.cpp index ba82be77..fc36d886 100644 --- a/lang/qt/src/qgpgmesignarchivejob.cpp +++ b/lang/qt/src/qgpgmesignarchivejob.cpp @@ -44,6 +44,7 @@ #include "signarchivejob_p.h" #include "filelistdataprovider.h" #include "qgpgme_debug.h" +#include "util.h" #include @@ -111,20 +112,6 @@ static QGpgMESignArchiveJob::result_type sign(Context *ctx, const auto signingResult = ctx->sign(indata, outdata, GpgME::SignArchive); -#ifdef Q_OS_WIN - const auto outputFileName = QString::fromUtf8(outdata.fileName()); -#else - const auto outputFileName = QFile::decodeName(outdata.fileName()); -#endif - if (!outputFileName.isEmpty() && signingResult.error().code()) { - // ensure that the output file is removed if the operation was canceled or failed - if (QFile::exists(outputFileName)) { - qCDebug(QGPGME_LOG) << __func__ << "Removing output file" << outputFileName << "after error or cancel"; - if (!QFile::remove(outputFileName)) { - qCDebug(QGPGME_LOG) << __func__ << "Removing output file" << outputFileName << "failed"; - } - } - } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(signingResult, log, ae); @@ -148,17 +135,24 @@ static QGpgMESignArchiveJob::result_type sign_to_io_device(Context *ctx, static QGpgMESignArchiveJob::result_type sign_to_filename(Context *ctx, const std::vector &signers, const std::vector &paths, - const QString &outputFile, + const QString &outputFileName, const QString &baseDirectory) { Data outdata; #ifdef Q_OS_WIN - outdata.setFileName(outputFile.toUtf8().constData()); + outdata.setFileName(outputFileName.toUtf8().constData()); #else - outdata.setFileName(QFile::encodeName(outputFile).constData()); + outdata.setFileName(QFile::encodeName(outputFileName).constData()); #endif - return sign(ctx, signers, paths, outdata, baseDirectory); + const auto result = sign(ctx, signers, paths, outdata, baseDirectory); + const auto &signingResult = std::get<0>(result); + if (signingResult.error().code()) { + // ensure that the output file is removed if the operation was canceled or failed + removeFile(outputFileName); + } + + return result; } GpgME::Error QGpgMESignArchiveJob::start(const std::vector &signers, diff --git a/lang/qt/src/qgpgmesignencryptarchivejob.cpp b/lang/qt/src/qgpgmesignencryptarchivejob.cpp index 6c28b7ec..3403ad57 100644 --- a/lang/qt/src/qgpgmesignencryptarchivejob.cpp +++ b/lang/qt/src/qgpgmesignencryptarchivejob.cpp @@ -44,6 +44,7 @@ #include "signencryptarchivejob_p.h" #include "filelistdataprovider.h" #include "qgpgme_debug.h" +#include "util.h" #include @@ -116,20 +117,6 @@ static QGpgMESignEncryptArchiveJob::result_type sign_encrypt(Context *ctx, const auto &signingResult = res.first; const auto &encryptionResult = res.second; -#ifdef Q_OS_WIN - const auto outputFileName = QString::fromUtf8(outdata.fileName()); -#else - const auto outputFileName = QFile::decodeName(outdata.fileName()); -#endif - if (!outputFileName.isEmpty() && (signingResult.error().code() || encryptionResult.error().code())) { - // ensure that the output file is removed if the operation was canceled or failed - if (QFile::exists(outputFileName)) { - qCDebug(QGPGME_LOG) << __func__ << "Removing output file" << outputFileName << "after error or cancel"; - if (!QFile::remove(outputFileName)) { - qCDebug(QGPGME_LOG) << __func__ << "Removing output file" << outputFileName << "failed"; - } - } - } Error ae; const QString log = _detail::audit_log_as_html(ctx, ae); return std::make_tuple(signingResult, encryptionResult, log, ae); @@ -156,18 +143,26 @@ static QGpgMESignEncryptArchiveJob::result_type sign_encrypt_to_filename(Context const std::vector &signers, const std::vector &recipients, const std::vector &paths, - const QString &outputFile, + const QString &outputFileName, Context::EncryptionFlags encryptionFlags, const QString &baseDirectory) { Data outdata; #ifdef Q_OS_WIN - outdata.setFileName(outputFile.toUtf8().constData()); + outdata.setFileName(outputFileName.toUtf8().constData()); #else - outdata.setFileName(QFile::encodeName(outputFile).constData()); + outdata.setFileName(QFile::encodeName(outputFileName).constData()); #endif - return sign_encrypt(ctx, signers, recipients, paths, outdata, encryptionFlags, baseDirectory); + const auto result = sign_encrypt(ctx, signers, recipients, paths, outdata, encryptionFlags, baseDirectory); + const auto &signingResult = std::get<0>(result); + const auto &encryptionResult = std::get<1>(result); + if (signingResult.error().code() || encryptionResult.error().code()) { + // ensure that the output file is removed if the operation was canceled or failed + removeFile(outputFileName); + } + + return result; } GpgME::Error QGpgMESignEncryptArchiveJob::start(const std::vector &signers, diff --git a/lang/qt/src/util.cpp b/lang/qt/src/util.cpp index d4190eb8..a9a70290 100644 --- a/lang/qt/src/util.cpp +++ b/lang/qt/src/util.cpp @@ -37,6 +37,10 @@ #include "util.h" +#include "qgpgme_debug.h" + +#include + #include #include @@ -61,3 +65,14 @@ QStringList toFingerprints(const std::vector &keys) }); return fprs; } + +void removeFile(const QString &fileName) +{ + if (QFile::exists(fileName)) { + if (QFile::remove(fileName)) { + qCDebug(QGPGME_LOG) << __func__ << "- Removed file" << fileName; + } else { + qCDebug(QGPGME_LOG) << __func__ << "- Removing file" << fileName << "failed"; + } + } +} diff --git a/lang/qt/src/util.h b/lang/qt/src/util.h index 626de3df..9128e4bf 100644 --- a/lang/qt/src/util.h +++ b/lang/qt/src/util.h @@ -55,4 +55,6 @@ std::vector toStrings(const QStringList &l); QStringList toFingerprints(const std::vector &keys); +void removeFile(const QString &fileName); + #endif // __QGPGME_UTIL_H__