diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index ec2e7f495..0302889fd 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -92,6 +92,7 @@ DatabaseWidget::DatabaseWidget(QSharedPointer db, QWidget* parent) , m_tagView(new TagView(this)) , m_saveAttempts(0) , m_remoteSettings(new RemoteSettings(m_db, this)) + , m_remoteHandler(nullptr) , m_entrySearcher(new EntrySearcher(false)) { Q_ASSERT(m_db); @@ -1085,7 +1086,7 @@ void DatabaseWidget::syncWithRemote(const RemoteParams* params) setDisabled(true); emit databaseSyncInProgress(); - QScopedPointer remoteHandler(new RemoteHandler(this)); + m_remoteHandler.reset(new RemoteHandler(this)); RemoteHandler::RemoteResult result; result.success = false; result.errorMessage = tr("Remote Sync did not contain any download or upload commands."); @@ -1094,7 +1095,7 @@ void DatabaseWidget::syncWithRemote(const RemoteParams* params) if (!params->downloadCommand.isEmpty()) { emit updateSyncProgress(25, tr("Downloading...")); // Start a download first then merge and upload in the callback - result = remoteHandler->download(params); + result = m_remoteHandler->download(params); if (result.success) { QString error; QSharedPointer remoteDb = QSharedPointer::create(); @@ -1134,10 +1135,9 @@ void DatabaseWidget::syncDatabaseWithLockedDatabase(const QString& filePath, con void DatabaseWidget::uploadAndFinishSync(const RemoteParams* params, RemoteHandler::RemoteResult result) { - QScopedPointer remoteHandler(new RemoteHandler(this)); if (result.success && !params->uploadCommand.isEmpty()) { emit updateSyncProgress(75, tr("Uploading...")); - result = remoteHandler->upload(result.filePath, params); + result = m_remoteHandler->upload(params); } finishSync(params, result); @@ -1145,8 +1145,7 @@ void DatabaseWidget::uploadAndFinishSync(const RemoteParams* params, RemoteHandl void DatabaseWidget::finishSync(const RemoteParams* params, RemoteHandler::RemoteResult result) { - QScopedPointer remoteHandler(new RemoteHandler(this)); - remoteHandler->cleanup(result.filePath); + m_remoteHandler.reset(); setDisabled(false); emit updateSyncProgress(-1, ""); if (result.success) { diff --git a/src/gui/DatabaseWidget.h b/src/gui/DatabaseWidget.h index 8f71a0676..2a49631f9 100644 --- a/src/gui/DatabaseWidget.h +++ b/src/gui/DatabaseWidget.h @@ -321,6 +321,7 @@ private: int m_saveAttempts; QScopedPointer m_remoteSettings; + QScopedPointer m_remoteHandler; // Search state QScopedPointer m_entrySearcher; diff --git a/src/gui/remote/DatabaseSettingsWidgetRemote.cpp b/src/gui/remote/DatabaseSettingsWidgetRemote.cpp index 5266c5947..33fe5458c 100644 --- a/src/gui/remote/DatabaseSettingsWidgetRemote.cpp +++ b/src/gui/remote/DatabaseSettingsWidgetRemote.cpp @@ -187,17 +187,14 @@ void DatabaseSettingsWidgetRemote::testDownload() if (!result.success) { m_ui->messageWidget->showMessage(tr("Download failed with error: %1").arg(result.errorMessage), MessageWidget::Error); - remoteHandler->cleanup(result.filePath); return; } if (!QFile::exists(result.filePath)) { m_ui->messageWidget->showMessage(tr("Download finished, but file %1 could not be found.").arg(result.filePath), MessageWidget::Error); - remoteHandler->cleanup(result.filePath); return; } - remoteHandler->cleanup(result.filePath); m_ui->messageWidget->showMessage(tr("Download successful."), MessageWidget::Positive); } diff --git a/src/gui/remote/RemoteHandler.cpp b/src/gui/remote/RemoteHandler.cpp index 61753eb78..c69b8d0a7 100644 --- a/src/gui/remote/RemoteHandler.cpp +++ b/src/gui/remote/RemoteHandler.cpp @@ -32,6 +32,14 @@ RemoteHandler::RemoteHandler(QObject* parent) { } +RemoteHandler::~RemoteHandler() +{ + QFileInfo file(m_tempFileLocation); + if (file.absoluteDir().exists() && file.absoluteDir().dirName().startsWith(PREFIX)) { + file.absoluteDir().removeRecursively(); + } +} + void RemoteHandler::setRemoteProcessFunc(std::function(QObject*)> func) { m_createRemoteProcess = std::move(func); @@ -39,7 +47,7 @@ void RemoteHandler::setRemoteProcessFunc(std::functionsetTempFileLocation(filePath); + remoteProcess->setTempFileLocation(m_tempFileLocation); remoteProcess->start(params->downloadCommand); if (!params->downloadInput.isEmpty()) { remoteProcess->write(params->downloadInput + "\n"); @@ -74,7 +82,7 @@ RemoteHandler::RemoteResult RemoteHandler::download(const RemoteParams* params) if (finished && statusCode == 0) { // Check if the file actually downloaded - QFileInfo fileInfo(filePath); + QFileInfo fileInfo(m_tempFileLocation); if (!fileInfo.exists() || fileInfo.size() == 0) { result.success = false; result.errorMessage = tr("Command `%1` failed to download database.").arg(params->downloadCommand); @@ -96,11 +104,11 @@ RemoteHandler::RemoteResult RemoteHandler::download(const RemoteParams* params) }); } -RemoteHandler::RemoteResult RemoteHandler::upload(const QString& filePath, const RemoteParams* params) +RemoteHandler::RemoteResult RemoteHandler::upload(const RemoteParams* params) { - return AsyncTask::runAndWaitForFuture([filePath, params] { + return AsyncTask::runAndWaitForFuture([this, params] { RemoteResult result; - result.filePath = filePath; + result.filePath = m_tempFileLocation; if (!params) { result.success = false; result.errorMessage = tr("Invalid database pointer or upload parameters provided."); @@ -108,7 +116,7 @@ RemoteHandler::RemoteResult RemoteHandler::upload(const QString& filePath, const } auto remoteProcess = m_createRemoteProcess(nullptr); // use nullptr parent, otherwise there is a warning - remoteProcess->setTempFileLocation(filePath); + remoteProcess->setTempFileLocation(m_tempFileLocation); remoteProcess->start(params->uploadCommand); if (!params->uploadInput.isEmpty()) { remoteProcess->write(params->uploadInput + "\n"); @@ -169,11 +177,3 @@ QString RemoteHandler::getTempFileLocation(QString* error) return tempFileLocation; } - -void RemoteHandler::cleanup(QString& tempFileLocation) -{ - QFileInfo file(tempFileLocation); - if (file.absoluteDir().exists() && file.absoluteDir().dirName().startsWith(PREFIX)) { - file.absoluteDir().removeRecursively(); - } -} diff --git a/src/gui/remote/RemoteHandler.h b/src/gui/remote/RemoteHandler.h index ca31a2883..e049e7f39 100644 --- a/src/gui/remote/RemoteHandler.h +++ b/src/gui/remote/RemoteHandler.h @@ -30,7 +30,7 @@ class RemoteHandler : public QObject public: explicit RemoteHandler(QObject* parent = nullptr); - ~RemoteHandler() override = default; + ~RemoteHandler() override; struct RemoteResult { @@ -42,14 +42,14 @@ public: }; RemoteResult download(const RemoteParams* params); - RemoteResult upload(const QString& filePath, const RemoteParams* params); - - void cleanup(QString& tempFileLocation); + RemoteResult upload(const RemoteParams* params); // Used for testing only static void setRemoteProcessFunc(std::function(QObject*)> func); private: + QString m_tempFileLocation; + static QString getTempFileLocation(QString* error); static std::function(QObject*)> m_createRemoteProcess;