From 8d8985bda1747a18b3b1378ea7c627302a61464a Mon Sep 17 00:00:00 2001 From: Ingo Klöcker Date: Fri, 27 Oct 2023 11:27:29 +0200 Subject: qt: Refactor removal of output file on cancel or error * lang/qt/src/util.h, lang/qt/src/util.cpp (removeFile): New. * lang/qt/src/qgpgmeencryptarchivejob.cpp (encrypt): Move removal of output file from here (encrypt_to_filename): ... to here and use new function. * lang/qt/src/qgpgmesignarchivejob.cpp (sign): Move removal of output file from here (sign_to_filename): ... to here and use new function. * lang/qt/src/qgpgmesignencryptarchivejob.cpp (sign_encrypt): Move removal of output file from here (sign_encrypt_to_filename): ... to here and use new function. -- GnuPG-bug-id: 6721 --- lang/qt/src/qgpgmesignencryptarchivejob.cpp | 31 ++++++++++++----------------- 1 file changed, 13 insertions(+), 18 deletions(-) (limited to 'lang/qt/src/qgpgmesignencryptarchivejob.cpp') 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, -- cgit