Skip to content

Commit 9c3f9b7

Browse files
committed
App: improve support of portable version of FreeCAD
1 parent e813c5a commit 9c3f9b7

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

src/App/Application.cpp

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
#include <boost/version.hpp>
127127
#include <QDir>
128128
#include <QFileInfo>
129+
#include <QProcessEnvironment>
129130

130131
using namespace App;
131132
using namespace std;
@@ -2018,8 +2019,7 @@ void Application::initConfig(int argc, char ** argv)
20182019
#endif
20192020
}
20202021

2021-
// Set application tmp. directory
2022-
mConfig["AppTempPath"] = Base::FileInfo::getTempPath();
2022+
// Change application tmp. directory
20232023
std::string tmpPath = _pcUserParamMngr->GetGroup("BaseApp/Preferences/General")->GetASCII("TempPath");
20242024
Base::FileInfo di(tmpPath);
20252025
if (di.exists() && di.isDir()) {
@@ -2696,20 +2696,70 @@ void Application::ExtractUserPath()
26962696
mConfig["BinPath"] = mConfig["AppHomePath"] + "bin" + PATHSEP;
26972697
mConfig["DocPath"] = mConfig["AppHomePath"] + "doc" + PATHSEP;
26982698

2699+
// Set application tmp. directory
2700+
mConfig["AppTempPath"] = Base::FileInfo::getTempPath();
2701+
2702+
// this is to support a portable version of FreeCAD
2703+
QProcessEnvironment env(QProcessEnvironment::systemEnvironment());
2704+
QString userHome = env.value(QString::fromLatin1("FREECAD_USER_HOME"));
2705+
QString userData = env.value(QString::fromLatin1("FREECAD_USER_DATA"));
2706+
QString userTemp = env.value(QString::fromLatin1("FREECAD_USER_TEMP"));
2707+
2708+
// verify env. variables
2709+
if (!userHome.isEmpty()) {
2710+
QDir dir(userHome);
2711+
if (dir.exists())
2712+
userHome = QDir::toNativeSeparators(dir.canonicalPath());
2713+
else
2714+
userHome.clear();
2715+
}
2716+
2717+
if (!userData.isEmpty()) {
2718+
QDir dir(userData);
2719+
if (dir.exists())
2720+
userData = QDir::toNativeSeparators(dir.canonicalPath());
2721+
else
2722+
userData.clear();
2723+
}
2724+
else if (!userHome.isEmpty()) {
2725+
// if FREECAD_USER_HOME is set but not FREECAD_USER_DATA
2726+
userData = userHome;
2727+
}
2728+
2729+
// override temp directory if set by env. variable
2730+
if (!userTemp.isEmpty()) {
2731+
QDir dir(userTemp);
2732+
if (dir.exists()) {
2733+
userTemp = dir.canonicalPath();
2734+
userTemp += QDir::separator();
2735+
userTemp = QDir::toNativeSeparators(userTemp);
2736+
mConfig["AppTempPath"] = userTemp.toUtf8().data();
2737+
}
2738+
}
2739+
else if (!userHome.isEmpty()) {
2740+
// if FREECAD_USER_HOME is set but not FREECAD_USER_TEMP
2741+
QDir dir(userHome);
2742+
dir.mkdir(QString::fromLatin1("temp"));
2743+
QFileInfo fi(dir, QString::fromLatin1("temp"));
2744+
QString tmp(fi.absoluteFilePath());
2745+
tmp += QDir::separator();
2746+
tmp = QDir::toNativeSeparators(tmp);
2747+
mConfig["AppTempPath"] = tmp.toUtf8().data();
2748+
}
2749+
26992750
#if defined(FC_OS_LINUX) || defined(FC_OS_CYGWIN) || defined(FC_OS_BSD)
27002751
// Default paths for the user specific stuff
27012752
struct passwd *pwd = getpwuid(getuid());
27022753
if (pwd == NULL)
27032754
throw Base::RuntimeError("Getting HOME path from system failed!");
27042755
mConfig["UserHomePath"] = pwd->pw_dir;
2756+
if (!userHome.isEmpty()) {
2757+
mConfig["UserHomePath"] = userHome.toUtf8().data();
2758+
}
27052759

2706-
char *path = pwd->pw_dir;
2707-
char *fc_user_data;
2708-
if ((fc_user_data = getenv("FREECAD_USER_DATA"))) {
2709-
QString env = QString::fromUtf8(fc_user_data);
2710-
QDir dir(env);
2711-
if (!env.isEmpty() && dir.exists())
2712-
path = fc_user_data;
2760+
std::string path = pwd->pw_dir;
2761+
if (!userData.isEmpty()) {
2762+
path = userData.toUtf8().data();
27132763
}
27142764

27152765
std::string appData(path);
@@ -2764,7 +2814,14 @@ void Application::ExtractUserPath()
27642814
if (pwd == NULL)
27652815
throw Base::RuntimeError("Getting HOME path from system failed!");
27662816
mConfig["UserHomePath"] = pwd->pw_dir;
2817+
if (!userHome.isEmpty()) {
2818+
mConfig["UserHomePath"] = userHome.toUtf8().data();
2819+
}
2820+
27672821
std::string appData = pwd->pw_dir;
2822+
if (!userData.isEmpty()) {
2823+
appData = userData.toUtf8().data();
2824+
}
27682825
appData += PATHSEP;
27692826
appData += "Library";
27702827
appData += PATHSEP;
@@ -2823,8 +2880,13 @@ void Application::ExtractUserPath()
28232880
WideCharToMultiByte(CP_UTF8, 0, szPath, -1,dest, 256, NULL, NULL);
28242881
mConfig["UserHomePath"] = dest;
28252882
}
2826-
else
2883+
else {
28272884
mConfig["UserHomePath"] = mConfig["AppHomePath"];
2885+
}
2886+
2887+
if (!userHome.isEmpty()) {
2888+
mConfig["UserHomePath"] = userHome.toUtf8().data();
2889+
}
28282890

28292891
// In the second step we want the directory where user settings of the application can be
28302892
// kept. There we create a directory with name of the vendor and a sub-directory with name
@@ -2834,6 +2896,9 @@ void Application::ExtractUserPath()
28342896
WideCharToMultiByte(CP_UTF8, 0, szPath, -1,dest, 256, NULL, NULL);
28352897

28362898
std::string appData = dest;
2899+
if (!userData.isEmpty()) {
2900+
appData = userData.toUtf8().data();
2901+
}
28372902
Base::FileInfo fi(appData.c_str());
28382903
if (!fi.exists()) {
28392904
// This should never ever happen

0 commit comments

Comments
 (0)