# HG changeset patch # User Torsten Lilge # Date 1648726203 -7200 # Node ID 2bb53974669765df72ad30cbf089c8b4b980f414 # Parent 6ddc9c9bab502e3433db2102afc4da96c6bbd6d3 consider env variables for location of temp files (bug #62215) * resource-manager.cc: include QStandardPaths; (octave_getenv): new function for using getenv with std::string; (get_temp_dir): new function adapted from mkoctfile.in.cc for determining the most appropriate directory for temporary files; (create_tmp_file): use new function get_temp_dir * resource-manager.h: new functions octave_getenv and get_temp_dir diff -r 6ddc9c9bab50 -r 2bb539746697 libgui/src/resource-manager.cc --- a/libgui/src/resource-manager.cc Wed Mar 30 20:48:06 2022 -0700 +++ b/libgui/src/resource-manager.cc Thu Mar 31 13:30:03 2022 +0200 @@ -40,11 +40,7 @@ #include #include #include -#if defined (HAVE_QSTANDARDPATHS) -# include -#else -# include -#endif +#include #include @@ -731,6 +727,58 @@ combo->setMaxVisibleItems (12); } + + // Extending getenv such taht is gets and returns std::string + std::string resource_manager::octave_getenv (const std::string& name) + { + char *value = ::getenv (name.c_str ()); + return value ? value : ""; + } + + // The following routine for determining the current temp directory + // is taken from mkoctfile.in.cc and updated such that the fallback + // is the location that QStandardPaths::writableLocation returns. + QString resource_manager::get_temp_dir () + { + std::string tempd; + + tempd = octave_getenv ("TMPDIR"); + +#if defined (__MINGW32__) || defined (_MSC_VER) + + if (tempd.empty ()) + tempd = octave_getenv ("TEMP"); + + if (tempd.empty ()) + tempd = octave_getenv ("TMP"); + +#if defined (P_tmpdir) + if (tempd.empty ()) + tempd = P_tmpdir; +#endif + + // Some versions of MinGW and MSVC either don't define P_tmpdir, or + // define it to a single backslash. + if (tempd == R"(\)") + tempd = ""; + +#else + +#if defined (P_tmpdir) + if (tempd.empty ()) + tempd = P_tmpdir; +#endif + +#endif + + if (tempd.empty ()) + return QStandardPaths::writableLocation (QStandardPaths::TempLocation) + + QDir::separator() + "octave"; + else + return QString::fromStdString (tempd); + } + + QPointer resource_manager::create_tmp_file (const QString& extension, const QString& contents) @@ -740,8 +788,7 @@ ext = QString (".") + ext; // Create octave dir within temp. dir - QString tmp_dir = QDir::tempPath () + QDir::separator() + "octave"; - QDir::temp ().mkdir ("octave"); + QString tmp_dir = get_temp_dir (); // Create temp. file QPointer tmp_file diff -r 6ddc9c9bab50 -r 2bb539746697 libgui/src/resource-manager.h --- a/libgui/src/resource-manager.h Wed Mar 30 20:48:06 2022 -0700 +++ b/libgui/src/resource-manager.h Thu Mar 31 13:30:03 2022 +0200 @@ -115,6 +115,10 @@ */ QFont copy_font_attributes (const QFont& attr, const QFont& base) const; + std::string octave_getenv (const std::string& name); + + QString get_temp_dir (void); + QString m_settings_directory; QString m_settings_file;