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
This commit is contained in:
parent
0a0a5906d5
commit
8d8985bda1
@ -44,6 +44,7 @@
|
|||||||
#include "encryptarchivejob_p.h"
|
#include "encryptarchivejob_p.h"
|
||||||
#include "filelistdataprovider.h"
|
#include "filelistdataprovider.h"
|
||||||
#include "qgpgme_debug.h"
|
#include "qgpgme_debug.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
@ -104,20 +105,6 @@ static QGpgMEEncryptArchiveJob::result_type encrypt(Context *ctx,
|
|||||||
flags = static_cast<Context::EncryptionFlags>(flags | Context::EncryptArchive);
|
flags = static_cast<Context::EncryptionFlags>(flags | Context::EncryptArchive);
|
||||||
const auto encryptionResult = ctx->encrypt(recipients, indata, outdata, flags);
|
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;
|
Error ae;
|
||||||
const QString log = _detail::audit_log_as_html(ctx, ae);
|
const QString log = _detail::audit_log_as_html(ctx, ae);
|
||||||
return std::make_tuple(encryptionResult, log, 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,
|
static QGpgMEEncryptArchiveJob::result_type encrypt_to_filename(Context *ctx,
|
||||||
const std::vector<Key> &recipients,
|
const std::vector<Key> &recipients,
|
||||||
const std::vector<QString> &paths,
|
const std::vector<QString> &paths,
|
||||||
const QString &outputFile,
|
const QString &outputFileName,
|
||||||
Context::EncryptionFlags flags,
|
Context::EncryptionFlags flags,
|
||||||
const QString &baseDirectory)
|
const QString &baseDirectory)
|
||||||
{
|
{
|
||||||
Data outdata;
|
Data outdata;
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
outdata.setFileName(outputFile.toUtf8().constData());
|
outdata.setFileName(outputFileName.toUtf8().constData());
|
||||||
#else
|
#else
|
||||||
outdata.setFileName(QFile::encodeName(outputFile).constData());
|
outdata.setFileName(QFile::encodeName(outputFileName).constData());
|
||||||
#endif
|
#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<GpgME::Key> &recipients,
|
GpgME::Error QGpgMEEncryptArchiveJob::start(const std::vector<GpgME::Key> &recipients,
|
||||||
|
@ -44,6 +44,7 @@
|
|||||||
#include "signarchivejob_p.h"
|
#include "signarchivejob_p.h"
|
||||||
#include "filelistdataprovider.h"
|
#include "filelistdataprovider.h"
|
||||||
#include "qgpgme_debug.h"
|
#include "qgpgme_debug.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
@ -111,20 +112,6 @@ static QGpgMESignArchiveJob::result_type sign(Context *ctx,
|
|||||||
|
|
||||||
const auto signingResult = ctx->sign(indata, outdata, GpgME::SignArchive);
|
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;
|
Error ae;
|
||||||
const QString log = _detail::audit_log_as_html(ctx, ae);
|
const QString log = _detail::audit_log_as_html(ctx, ae);
|
||||||
return std::make_tuple(signingResult, log, 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,
|
static QGpgMESignArchiveJob::result_type sign_to_filename(Context *ctx,
|
||||||
const std::vector<Key> &signers,
|
const std::vector<Key> &signers,
|
||||||
const std::vector<QString> &paths,
|
const std::vector<QString> &paths,
|
||||||
const QString &outputFile,
|
const QString &outputFileName,
|
||||||
const QString &baseDirectory)
|
const QString &baseDirectory)
|
||||||
{
|
{
|
||||||
Data outdata;
|
Data outdata;
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
outdata.setFileName(outputFile.toUtf8().constData());
|
outdata.setFileName(outputFileName.toUtf8().constData());
|
||||||
#else
|
#else
|
||||||
outdata.setFileName(QFile::encodeName(outputFile).constData());
|
outdata.setFileName(QFile::encodeName(outputFileName).constData());
|
||||||
#endif
|
#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<GpgME::Key> &signers,
|
GpgME::Error QGpgMESignArchiveJob::start(const std::vector<GpgME::Key> &signers,
|
||||||
|
@ -44,6 +44,7 @@
|
|||||||
#include "signencryptarchivejob_p.h"
|
#include "signencryptarchivejob_p.h"
|
||||||
#include "filelistdataprovider.h"
|
#include "filelistdataprovider.h"
|
||||||
#include "qgpgme_debug.h"
|
#include "qgpgme_debug.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
@ -116,20 +117,6 @@ static QGpgMESignEncryptArchiveJob::result_type sign_encrypt(Context *ctx,
|
|||||||
const auto &signingResult = res.first;
|
const auto &signingResult = res.first;
|
||||||
const auto &encryptionResult = res.second;
|
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;
|
Error ae;
|
||||||
const QString log = _detail::audit_log_as_html(ctx, ae);
|
const QString log = _detail::audit_log_as_html(ctx, ae);
|
||||||
return std::make_tuple(signingResult, encryptionResult, log, 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<GpgME::Key> &signers,
|
const std::vector<GpgME::Key> &signers,
|
||||||
const std::vector<Key> &recipients,
|
const std::vector<Key> &recipients,
|
||||||
const std::vector<QString> &paths,
|
const std::vector<QString> &paths,
|
||||||
const QString &outputFile,
|
const QString &outputFileName,
|
||||||
Context::EncryptionFlags encryptionFlags,
|
Context::EncryptionFlags encryptionFlags,
|
||||||
const QString &baseDirectory)
|
const QString &baseDirectory)
|
||||||
{
|
{
|
||||||
Data outdata;
|
Data outdata;
|
||||||
#ifdef Q_OS_WIN
|
#ifdef Q_OS_WIN
|
||||||
outdata.setFileName(outputFile.toUtf8().constData());
|
outdata.setFileName(outputFileName.toUtf8().constData());
|
||||||
#else
|
#else
|
||||||
outdata.setFileName(QFile::encodeName(outputFile).constData());
|
outdata.setFileName(QFile::encodeName(outputFileName).constData());
|
||||||
#endif
|
#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<GpgME::Key> &signers,
|
GpgME::Error QGpgMESignEncryptArchiveJob::start(const std::vector<GpgME::Key> &signers,
|
||||||
|
@ -37,6 +37,10 @@
|
|||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
#include "qgpgme_debug.h"
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
#include <key.h>
|
#include <key.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -61,3 +65,14 @@ QStringList toFingerprints(const std::vector<GpgME::Key> &keys)
|
|||||||
});
|
});
|
||||||
return fprs;
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -55,4 +55,6 @@ std::vector<std::string> toStrings(const QStringList &l);
|
|||||||
|
|
||||||
QStringList toFingerprints(const std::vector<GpgME::Key> &keys);
|
QStringList toFingerprints(const std::vector<GpgME::Key> &keys);
|
||||||
|
|
||||||
|
void removeFile(const QString &fileName);
|
||||||
|
|
||||||
#endif // __QGPGME_UTIL_H__
|
#endif // __QGPGME_UTIL_H__
|
||||||
|
Loading…
Reference in New Issue
Block a user