changeset 30877:2bb539746697

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
author Torsten Lilge <ttl-octave@mailbox.org>
date Thu, 31 Mar 2022 13:30:03 +0200
parents 6ddc9c9bab50
children a1c2ea6deaa4
files libgui/src/resource-manager.cc libgui/src/resource-manager.h
diffstat 2 files changed, 58 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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 <QLibraryInfo>
 #include <QMessageBox>
 #include <QNetworkProxy>
-#if defined (HAVE_QSTANDARDPATHS)
-#  include <QStandardPaths>
-#else
-#  include <QDesktopServices>
-#endif
+#include <QStandardPaths>
 
 #include <QTextCodec>
 
@@ -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<QTemporaryFile>
   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<QTemporaryFile> tmp_file
--- 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;