From fa222567170ea09dd1cca9fb7998c601720c2fb8 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 20 Mar 2023 14:49:49 +1000 Subject: [PATCH] Allow temporary folder location to be overridden through QgsProcessingContext --- .../processing/qgsprocessingcontext.sip.in | 24 ++++++++++++++++++ src/core/processing/qgsprocessingcontext.cpp | 10 ++++++++ src/core/processing/qgsprocessingcontext.h | 25 +++++++++++++++++++ src/core/processing/qgsprocessingutils.cpp | 8 +++++- 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/python/core/auto_generated/processing/qgsprocessingcontext.sip.in b/python/core/auto_generated/processing/qgsprocessingcontext.sip.in index 87e2decf09a..48ba8ece037 100644 --- a/python/core/auto_generated/processing/qgsprocessingcontext.sip.in +++ b/python/core/auto_generated/processing/qgsprocessingcontext.sip.in @@ -585,6 +585,30 @@ Sets the logging ``level`` for algorithms to use when pushing feedback messages .. seealso:: :py:func:`logLevel` .. versionadded:: 3.20 +%End + + QString temporaryFolder() const; +%Docstring +Returns the (optional) temporary folder to use when running algorithms. + +If set, this overrides the standard global Processing temporary folder and should be used +for all temporary files created during algorithm execution. + +.. seealso:: :py:func:`setTemporaryFolder` + +.. versionadded:: 3.32 +%End + + void setTemporaryFolder( const QString &folder ); +%Docstring +Sets the (optional) temporary ``folder`` to use when running algorithms. + +If set, this overrides the standard global Processing temporary folder and should be used +for all temporary files created during algorithm execution. + +.. seealso:: :py:func:`temporaryFolder` + +.. versionadded:: 3.32 %End QVariantMap exportToMap() const; diff --git a/src/core/processing/qgsprocessingcontext.cpp b/src/core/processing/qgsprocessingcontext.cpp index be0d7495a9b..50f3ffb093e 100644 --- a/src/core/processing/qgsprocessingcontext.cpp +++ b/src/core/processing/qgsprocessingcontext.cpp @@ -147,6 +147,16 @@ void QgsProcessingContext::setLogLevel( LogLevel level ) mLogLevel = level; } +QString QgsProcessingContext::temporaryFolder() const +{ + return mTemporaryFolderOverride; +} + +void QgsProcessingContext::setTemporaryFolder( const QString &folder ) +{ + mTemporaryFolderOverride = folder; +} + QVariantMap QgsProcessingContext::exportToMap() const { QVariantMap res; diff --git a/src/core/processing/qgsprocessingcontext.h b/src/core/processing/qgsprocessingcontext.h index 3ebff682b4e..235900b8e32 100644 --- a/src/core/processing/qgsprocessingcontext.h +++ b/src/core/processing/qgsprocessingcontext.h @@ -100,6 +100,7 @@ class CORE_EXPORT QgsProcessingContext mDistanceUnit = other.mDistanceUnit; mAreaUnit = other.mAreaUnit; mLogLevel = other.mLogLevel; + mTemporaryFolderOverride = other.mTemporaryFolderOverride; } /** @@ -657,6 +658,28 @@ class CORE_EXPORT QgsProcessingContext */ void setLogLevel( LogLevel level ); + /** + * Returns the (optional) temporary folder to use when running algorithms. + * + * If set, this overrides the standard global Processing temporary folder and should be used + * for all temporary files created during algorithm execution. + * + * \see setTemporaryFolder() + * \since QGIS 3.32 + */ + QString temporaryFolder() const; + + /** + * Sets the (optional) temporary \a folder to use when running algorithms. + * + * If set, this overrides the standard global Processing temporary folder and should be used + * for all temporary files created during algorithm execution. + * + * \see temporaryFolder() + * \since QGIS 3.32 + */ + void setTemporaryFolder( const QString &folder ); + /** * Exports the context's settings to a variant map. * @@ -713,6 +736,8 @@ class CORE_EXPORT QgsProcessingContext LogLevel mLogLevel = DefaultLevel; + QString mTemporaryFolderOverride; + #ifdef SIP_RUN QgsProcessingContext( const QgsProcessingContext &other ); #endif diff --git a/src/core/processing/qgsprocessingutils.cpp b/src/core/processing/qgsprocessingutils.cpp index fdc039dd7b0..cd4a6105bc8 100644 --- a/src/core/processing/qgsprocessingutils.cpp +++ b/src/core/processing/qgsprocessingutils.cpp @@ -1037,7 +1037,13 @@ QString QgsProcessingUtils::tempFolder( const QgsProcessingContext *context ) static QString sFolder; static QMutex sMutex; QMutexLocker locker( &sMutex ); - const QString basePath = QgsProcessing::settingsTempPath->value(); + QString basePath; + + if ( context ) + basePath = context->temporaryFolder(); + if ( basePath.isEmpty() ) + basePath = QgsProcessing::settingsTempPath->value(); + if ( basePath.isEmpty() ) { // default setting -- automatically create a temp folder