diff --git a/include/MUtils/Global.h b/include/MUtils/Global.h index 8e57887..fe30b4e 100644 --- a/include/MUtils/Global.h +++ b/include/MUtils/Global.h @@ -23,18 +23,24 @@ #include +//Forward Declarations +class QProcess; + /////////////////////////////////////////////////////////////////////////////// namespace MUtils { - //Random + //Temp Folder + const QString &temp_folder(void); + + //Process Utils + void init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true); + + //Random void seed_rand(void); QString rand_str(const bool &bLong = false); quint32 next_rand32(void); quint64 next_rand64(void); - - //Temp Folder - const QString &temp_folder(void); } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Global.cpp b/src/Global.cpp index 4b3b6f6..81614d2 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -29,6 +29,7 @@ //Qt #include #include +#include //CRT #include @@ -229,3 +230,52 @@ const QString &MUtils::temp_folder(void) return (*g_temp_folder_path); } + +/////////////////////////////////////////////////////////////////////////////// +// PROCESS UTILS +/////////////////////////////////////////////////////////////////////////////// + +void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir) +{ + //Environment variable names + static const char *const s_envvar_names_temp[] = + { + "TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL + }; + static const char *const s_envvar_names_remove[] = + { + "WGETRC", "SYSTEM_WGETRC", "HTTP_PROXY", "FTP_PROXY", "NO_PROXY", "GNUPGHOME", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LANG", NULL + }; + + //Initialize environment + QProcessEnvironment env = process.processEnvironment(); + if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment(); + + //Clean a number of enviroment variables that might affect our tools + for(size_t i = 0; s_envvar_names_remove[i]; i++) + { + env.remove(QString::fromLatin1(s_envvar_names_remove[i])); + env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower()); + } + + const QString tempDir = QDir::toNativeSeparators(temp_folder()); + + //Replace TEMP directory in environment + if(bReplaceTempDir) + { + for(size_t i = 0; s_envvar_names_temp[i]; i++) + { + env.insert(s_envvar_names_temp[i], tempDir); + } + } + + //Setup PATH variable + const QString path = env.value("PATH", QString()).trimmed(); + env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path)); + + //Setup QPorcess object + process.setWorkingDirectory(wokringDir); + process.setProcessChannelMode(QProcess::MergedChannels); + process.setReadChannel(QProcess::StandardOutput); + process.setProcessEnvironment(env); +}