comparison libgui/src/resource-manager.cc @ 30882:b77b321f1ac5

use common function for getting temp dir in GUI (bug #62215) * resource-manager.cc (octave_getenv): removed; (get_temp_dir): removed; (create_tmp_file): use sys::env::get_temp_directory () * resource-manager.h: removed octave_getenv and get_temp_dir
author Torsten Lilge <ttl-octave@mailbox.org>
date Sun, 03 Apr 2022 14:28:45 +0200
parents 2bb539746697
children
comparison
equal deleted inserted replaced
30881:1921d9d0e62b 30882:b77b321f1ac5
726 726
727 combo->setMaxVisibleItems (12); 727 combo->setMaxVisibleItems (12);
728 } 728 }
729 729
730 730
731 // Extending getenv such taht is gets and returns std::string
732 std::string resource_manager::octave_getenv (const std::string& name)
733 {
734 char *value = ::getenv (name.c_str ());
735 return value ? value : "";
736 }
737
738 // The following routine for determining the current temp directory
739 // is taken from mkoctfile.in.cc and updated such that the fallback
740 // is the location that QStandardPaths::writableLocation returns.
741 QString resource_manager::get_temp_dir ()
742 {
743 std::string tempd;
744
745 tempd = octave_getenv ("TMPDIR");
746
747 #if defined (__MINGW32__) || defined (_MSC_VER)
748
749 if (tempd.empty ())
750 tempd = octave_getenv ("TEMP");
751
752 if (tempd.empty ())
753 tempd = octave_getenv ("TMP");
754
755 #if defined (P_tmpdir)
756 if (tempd.empty ())
757 tempd = P_tmpdir;
758 #endif
759
760 // Some versions of MinGW and MSVC either don't define P_tmpdir, or
761 // define it to a single backslash.
762 if (tempd == R"(\)")
763 tempd = "";
764
765 #else
766
767 #if defined (P_tmpdir)
768 if (tempd.empty ())
769 tempd = P_tmpdir;
770 #endif
771
772 #endif
773
774 if (tempd.empty ())
775 return QStandardPaths::writableLocation (QStandardPaths::TempLocation) +
776 QDir::separator() + "octave";
777 else
778 return QString::fromStdString (tempd);
779 }
780
781
782 QPointer<QTemporaryFile> 731 QPointer<QTemporaryFile>
783 resource_manager::create_tmp_file (const QString& extension, 732 resource_manager::create_tmp_file (const QString& extension,
784 const QString& contents) 733 const QString& contents)
785 { 734 {
786 QString ext = extension; 735 QString ext = extension;
787 if ((! ext.isEmpty ()) && (! ext.startsWith ('.'))) 736 if ((! ext.isEmpty ()) && (! ext.startsWith ('.')))
788 ext = QString (".") + ext; 737 ext = QString (".") + ext;
789 738
790 // Create octave dir within temp. dir 739 // Create octave dir within temp. dir
791 QString tmp_dir = get_temp_dir (); 740 QString tmp_dir = QString::fromStdString (sys::env::get_temp_directory ());
792 741
793 // Create temp. file 742 // Create temp. file
794 QPointer<QTemporaryFile> tmp_file 743 QPointer<QTemporaryFile> tmp_file
795 = new QTemporaryFile (tmp_dir + QDir::separator() + 744 = new QTemporaryFile (tmp_dir + QDir::separator() +
796 "octave_XXXXXX" + ext, this); 745 "octave_XXXXXX" + ext, this);