2016-03-19 06:57:51 +13:00
|
|
|
#include "tenv.h"
|
|
|
|
#include "tsystem.h"
|
|
|
|
#include "tconvert.h"
|
|
|
|
#include "tfilepath_io.h"
|
2018-12-28 15:15:09 +13:00
|
|
|
#include "tversion.h"
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-04-24 18:26:55 +12:00
|
|
|
#include <QDir>
|
2016-03-19 06:57:51 +13:00
|
|
|
#include <QSettings>
|
2020-10-11 07:06:49 +13:00
|
|
|
#include <QCoreApplication>
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifdef LEVO_MACOSX
|
|
|
|
|
|
|
|
#include "macofflinegl.h"
|
|
|
|
#include "tofflinegl.h"
|
|
|
|
|
|
|
|
// Imposto l'offlineGL usando AGL (per togliere la dipendenza da X)
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TOfflineGL::Imp *MacOfflineGenerator1(const TDimension &dim) {
|
|
|
|
return new MacImplementation(dim);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <map>
|
2016-06-14 00:50:05 +12:00
|
|
|
#include <sstream>
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
using namespace TEnv;
|
2018-12-28 15:15:09 +13:00
|
|
|
using namespace TVER;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
//
|
|
|
|
// root dir
|
|
|
|
//
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
2018-07-10 17:42:57 +12:00
|
|
|
const std::map<std::string, std::string> systemPathMap{
|
2019-09-17 19:26:56 +12:00
|
|
|
{"LIBRARY", "library"}, {"STUDIOPALETTE", "studiopalette"},
|
|
|
|
{"FXPRESETS", "fxs"}, {"PROFILES", "profiles"},
|
|
|
|
{"CONFIG", "config"}, {"PROJECTS", "projects"}};
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class EnvGlobals { // singleton
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2018-12-28 15:15:09 +13:00
|
|
|
ToonzVersion m_version;
|
2019-06-03 00:39:40 +12:00
|
|
|
std::string m_applicationFileName; // May differ from application name
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string m_applicationVersion;
|
|
|
|
std::string m_applicationFullName;
|
|
|
|
std::string m_moduleName;
|
|
|
|
std::string m_rootVarName;
|
|
|
|
std::string m_systemVarPrefix;
|
2017-09-04 18:13:56 +12:00
|
|
|
std::string m_workingDirectory;
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath m_registryRoot;
|
|
|
|
TFilePath m_envFile;
|
|
|
|
TFilePath *m_stuffDir;
|
|
|
|
TFilePath *m_dllRelativeDir;
|
2017-09-04 18:13:56 +12:00
|
|
|
bool m_isPortable = false;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2018-07-10 17:42:57 +12:00
|
|
|
// path values specified with command line arguments
|
|
|
|
std::map<std::string, std::string> m_argPathValues;
|
|
|
|
|
2018-12-28 15:15:09 +13:00
|
|
|
EnvGlobals() : m_stuffDir(0) {
|
|
|
|
setWorkingDirectory();
|
|
|
|
init();
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
~EnvGlobals() { delete m_stuffDir; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
static EnvGlobals *instance() {
|
|
|
|
static EnvGlobals _instance;
|
|
|
|
return &_instance;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath getSystemVarPath(std::string varName) {
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
return m_registryRoot + varName;
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
QString settingsPath;
|
2016-04-24 18:26:55 +12:00
|
|
|
|
|
|
|
#ifdef MACOSX
|
2019-06-03 00:39:40 +12:00
|
|
|
settingsPath = QString::fromStdString(getApplicationFileName()) +
|
2018-12-28 15:15:09 +13:00
|
|
|
QString(".app") +
|
|
|
|
QString("/Contents/Resources/SystemVar.ini");
|
2016-06-15 18:43:10 +12:00
|
|
|
#else /* Generic Unix */
|
|
|
|
// TODO: use QStandardPaths::ConfigLocation when we drop Qt4
|
|
|
|
settingsPath = QDir::homePath();
|
|
|
|
settingsPath.append("/.config/");
|
|
|
|
settingsPath.append(getApplicationName().c_str());
|
|
|
|
settingsPath.append("/SystemVar.ini");
|
2016-04-24 18:26:55 +12:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QSettings settings(settingsPath, QSettings::IniFormat);
|
|
|
|
QString qStr = QString::fromStdString(varName);
|
|
|
|
QString systemVar = settings.value(qStr).toString();
|
|
|
|
// printf("getSystemVarPath: path:%s key:%s var:%s\n",
|
|
|
|
// settingsPath.toStdString().data(), varName.data(),
|
|
|
|
// systemVar.toStdString().data());
|
|
|
|
return TFilePath(systemVar.toStdWString());
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath getRootVarPath() { return getSystemVarPath(m_rootVarName); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string getSystemVarValue(std::string varName) {
|
2018-07-10 17:42:57 +12:00
|
|
|
if (getIsPortable()) return "";
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
return TSystem::getSystemValue(getSystemVarPath(varName)).toStdString();
|
2016-03-19 06:57:51 +13:00
|
|
|
#else
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath systemVarPath = getSystemVarPath(varName);
|
|
|
|
if (systemVarPath.isEmpty()) {
|
|
|
|
std::cout << "varName:" << varName << " TOONZROOT not set..."
|
|
|
|
<< std::endl;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return ::to_string(systemVarPath);
|
2016-03-19 06:57:51 +13:00
|
|
|
/*
|
2016-06-15 18:43:10 +12:00
|
|
|
char *value = getenv(varName.c_str());
|
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
std::cout << varName << " not set, returning
|
|
|
|
TOONZROOT" << std::endl;
|
2016-03-19 06:57:51 +13:00
|
|
|
//value = getenv("TOONZROOT");
|
|
|
|
value="";
|
|
|
|
std::cout << "!!!value= "<< value << std::endl;
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
std::cout << varName << "TOONZROOT not
|
|
|
|
set..." << std::endl;
|
|
|
|
//exit(-1);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
return string(value);
|
2016-06-15 18:43:10 +12:00
|
|
|
*/
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:15:09 +13:00
|
|
|
TFilePath getSystemVarPathValue(std::string varName) {
|
|
|
|
// return if the path is registered by command line argument
|
|
|
|
std::string argVar = getArgPathValue(varName);
|
|
|
|
if (argVar != "") return TFilePath(argVar);
|
|
|
|
return TFilePath(getSystemVarValue(varName));
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath getStuffDir() {
|
|
|
|
if (m_stuffDir) return *m_stuffDir;
|
2017-09-04 18:13:56 +12:00
|
|
|
if (m_isPortable)
|
2020-06-09 16:42:51 +12:00
|
|
|
return TFilePath((getWorkingDirectory() + "\\tahomastuff\\"));
|
2017-09-04 18:13:56 +12:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return TFilePath(getSystemVarValue(m_rootVarName));
|
|
|
|
}
|
|
|
|
void setStuffDir(const TFilePath &stuffDir) {
|
|
|
|
delete m_stuffDir;
|
|
|
|
m_stuffDir = new TFilePath(stuffDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateEnvFile() {
|
|
|
|
TFilePath profilesDir =
|
|
|
|
getSystemVarPathValue(getSystemVarPrefix() + "PROFILES");
|
2018-07-10 17:42:57 +12:00
|
|
|
if (profilesDir == TFilePath())
|
|
|
|
profilesDir = getStuffDir() + systemPathMap.at("PROFILES");
|
2020-10-22 03:59:24 +13:00
|
|
|
m_envFile = profilesDir + "users" + TSystem::getUserName().toStdString() +
|
|
|
|
"env.ini";
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
|
|
|
|
2018-12-28 15:15:09 +13:00
|
|
|
void init() {
|
|
|
|
if (m_version.getAppRevision() != 0) {
|
|
|
|
m_applicationVersion = m_version.getAppVersionString() + "." +
|
|
|
|
m_version.getAppRevisionString();
|
2016-06-15 18:43:10 +12:00
|
|
|
} else {
|
2018-12-28 15:15:09 +13:00
|
|
|
m_applicationVersion = m_version.getAppVersionString();
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
2018-12-28 15:15:09 +13:00
|
|
|
|
|
|
|
m_applicationFullName = m_version.getAppName() + " " + m_applicationVersion;
|
2019-07-02 17:59:37 +12:00
|
|
|
if (m_version.hasAppNote())
|
|
|
|
m_applicationFullName += " " + m_version.getAppNote();
|
2020-05-23 16:20:51 +12:00
|
|
|
|
|
|
|
m_moduleName = m_version.getAppName();
|
|
|
|
m_rootVarName = toUpper(m_version.getAppName()) + "ROOT";
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2020-05-23 16:20:51 +12:00
|
|
|
// from v1.3, registry root is moved to SOFTWARE\\Tahoma\\Tahoma
|
2020-10-02 19:20:33 +13:00
|
|
|
m_registryRoot = TFilePath("SOFTWARE\\Tahoma2D\\") + m_version.getAppName();
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2018-12-28 15:15:09 +13:00
|
|
|
m_systemVarPrefix = m_version.getAppName();
|
2016-06-15 18:43:10 +12:00
|
|
|
updateEnvFile();
|
|
|
|
}
|
|
|
|
|
2019-06-03 00:39:40 +12:00
|
|
|
void setApplicationFileName(std::string appFileName) {
|
|
|
|
m_applicationFileName = appFileName;
|
2019-06-03 11:21:05 +12:00
|
|
|
setWorkingDirectory();
|
2019-06-03 00:39:40 +12:00
|
|
|
}
|
|
|
|
std::string getApplicationFileName() { return m_applicationFileName; }
|
2018-12-28 15:15:09 +13:00
|
|
|
std::string getApplicationName() { return m_version.getAppName(); }
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string getApplicationVersion() { return m_applicationVersion; }
|
|
|
|
std::string getApplicationVersionWithoutRevision() {
|
2018-12-28 15:15:09 +13:00
|
|
|
return m_version.getAppVersionString();
|
2016-06-15 18:43:10 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
TFilePath getEnvFile() { return m_envFile; }
|
2017-10-21 02:23:54 +13:00
|
|
|
TFilePath getTemplateEnvFile() {
|
|
|
|
return m_envFile.getParentDir() + TFilePath("template.env");
|
|
|
|
}
|
2016-06-15 18:43:10 +12:00
|
|
|
|
|
|
|
void setApplicationFullName(std::string applicationFullName) {
|
|
|
|
m_applicationFullName = applicationFullName;
|
|
|
|
}
|
|
|
|
std::string getApplicationFullName() { return m_applicationFullName; }
|
|
|
|
|
|
|
|
void setModuleName(std::string moduleName) { m_moduleName = moduleName; }
|
|
|
|
std::string getModuleName() { return m_moduleName; }
|
|
|
|
|
|
|
|
void setRootVarName(std::string varName) {
|
|
|
|
m_rootVarName = varName;
|
|
|
|
updateEnvFile();
|
|
|
|
}
|
|
|
|
std::string getRootVarName() { return m_rootVarName; }
|
|
|
|
|
2017-09-07 20:13:23 +12:00
|
|
|
void setSystemVarPrefix(std::string prefix) {
|
|
|
|
m_systemVarPrefix = prefix;
|
|
|
|
updateEnvFile();
|
|
|
|
}
|
2018-07-10 17:42:57 +12:00
|
|
|
std::string getSystemVarPrefix() { return m_systemVarPrefix; }
|
2017-09-04 18:13:56 +12:00
|
|
|
|
|
|
|
void setWorkingDirectory() {
|
2020-10-11 07:06:49 +13:00
|
|
|
QString workingDirectoryTmp = QDir::currentPath();
|
|
|
|
|
2020-05-23 02:17:41 +12:00
|
|
|
#if defined(LINUX) || defined(FREEBSD)
|
2020-10-11 07:06:49 +13:00
|
|
|
QString appPath =
|
|
|
|
workingDirectoryTmp + "/" + QCoreApplication::applicationName();
|
|
|
|
QDir appDir(appPath);
|
|
|
|
appPath = appDir.canonicalPath();
|
|
|
|
if (!appPath.isEmpty())
|
|
|
|
workingDirectoryTmp = TFilePath(appPath).getParentDir().getQString();
|
|
|
|
#endif
|
|
|
|
|
2017-09-04 18:13:56 +12:00
|
|
|
QByteArray ba = workingDirectoryTmp.toLatin1();
|
|
|
|
const char *workingDirectory = ba.data();
|
|
|
|
m_workingDirectory = workingDirectory;
|
|
|
|
|
|
|
|
// check if portable
|
2020-06-09 16:42:51 +12:00
|
|
|
TFilePath portableCheck = TFilePath(m_workingDirectory + "\\tahomastuff\\");
|
2017-09-04 18:13:56 +12:00
|
|
|
TFileStatus portableStatus(portableCheck);
|
|
|
|
m_isPortable = portableStatus.doesExist();
|
2019-05-26 03:14:07 +12:00
|
|
|
|
|
|
|
#ifdef MACOSX
|
|
|
|
// macOS 10.12 (Sierra) translocates applications before running them
|
|
|
|
// depending on how it was installed. This separates the app from the
|
2020-06-09 16:42:51 +12:00
|
|
|
// tahomastuff folder and we don't know where it is so we stop treating it
|
|
|
|
// as a portable. Placing stuff inside Tahoma.app will keep
|
2019-05-26 03:14:07 +12:00
|
|
|
// everything together when it translocates.
|
|
|
|
if (!m_isPortable) {
|
2019-06-03 00:39:40 +12:00
|
|
|
portableCheck =
|
|
|
|
TFilePath(m_workingDirectory + "\\" + getApplicationFileName() +
|
2020-06-09 16:42:51 +12:00
|
|
|
".app\\tahomastuff\\");
|
2019-05-26 03:14:07 +12:00
|
|
|
portableStatus = TFileStatus(portableCheck);
|
|
|
|
m_isPortable = portableStatus.doesExist();
|
|
|
|
if (m_isPortable)
|
2019-06-03 11:21:05 +12:00
|
|
|
m_workingDirectory =
|
|
|
|
portableCheck.getParentDir().getQString().toStdString();
|
2019-05-26 03:14:07 +12:00
|
|
|
}
|
|
|
|
#endif
|
2017-09-04 18:13:56 +12:00
|
|
|
}
|
|
|
|
std::string getWorkingDirectory() { return m_workingDirectory; }
|
|
|
|
|
|
|
|
bool getIsPortable() { return m_isPortable; }
|
2016-06-15 18:43:10 +12:00
|
|
|
|
|
|
|
void setDllRelativeDir(const TFilePath &dllRelativeDir) {
|
|
|
|
delete m_dllRelativeDir;
|
|
|
|
m_dllRelativeDir = new TFilePath(dllRelativeDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
TFilePath getDllRelativeDir() {
|
|
|
|
if (m_dllRelativeDir) return *m_dllRelativeDir;
|
|
|
|
return TFilePath(".");
|
|
|
|
}
|
2018-07-10 17:42:57 +12:00
|
|
|
|
|
|
|
void setArgPathValue(std::string key, std::string value) {
|
|
|
|
m_argPathValues.emplace(key, value);
|
|
|
|
if (key == m_systemVarPrefix + "PROFILES") updateEnvFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getArgPathValue(std::string key) {
|
|
|
|
decltype(m_argPathValues)::iterator it = m_argPathValues.find(key);
|
|
|
|
if (it != m_argPathValues.end())
|
|
|
|
return it->second;
|
|
|
|
else
|
|
|
|
return "";
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
TFilePath EnvGlobals::getSystemPath(int id)
|
|
|
|
{
|
|
|
|
std::map<int, TFilePath>::iterator it = m_systemPaths.find(id);
|
|
|
|
if(it != m_systemPaths.end()) return it->second;
|
|
|
|
switch(id)
|
|
|
|
{
|
2016-06-14 00:50:05 +12:00
|
|
|
case StuffDir: return TFilePath();
|
2016-03-19 06:57:51 +13:00
|
|
|
case ConfigDir: return getSystemPath(StuffDir) + "config";
|
|
|
|
case ProfilesDir: return getSystemPath(StuffDir) + "profiles";
|
2016-06-14 00:50:05 +12:00
|
|
|
default: return TFilePath();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnvGlobals::setSystemPath(int id, const TFilePath &fp)
|
|
|
|
{
|
|
|
|
m_systemPaths[id] = fp;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
//
|
|
|
|
// Variable::Imp
|
|
|
|
//
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class Variable::Imp {
|
2016-03-19 06:57:51 +13:00
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string m_name;
|
|
|
|
std::string m_value;
|
|
|
|
bool m_loaded, m_defaultDefined, m_assigned;
|
|
|
|
|
|
|
|
Imp(std::string name)
|
|
|
|
: m_name(name)
|
|
|
|
, m_value("")
|
|
|
|
, m_loaded(false)
|
|
|
|
, m_defaultDefined(false)
|
|
|
|
, m_assigned(false) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
//
|
2021-07-08 13:39:28 +12:00
|
|
|
// variable manager (singleton)
|
2016-03-19 06:57:51 +13:00
|
|
|
//
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
class VariableSet {
|
|
|
|
std::map<std::string, Variable::Imp *> m_variables;
|
|
|
|
bool m_loaded;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
VariableSet() : m_loaded(false) {}
|
|
|
|
|
|
|
|
~VariableSet() {
|
|
|
|
std::map<std::string, Variable::Imp *>::iterator it;
|
|
|
|
for (it = m_variables.begin(); it != m_variables.end(); ++it)
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VariableSet *instance() {
|
|
|
|
static VariableSet instance;
|
|
|
|
return &instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
Variable::Imp *getImp(std::string name) {
|
|
|
|
std::map<std::string, Variable::Imp *>::iterator it;
|
|
|
|
it = m_variables.find(name);
|
|
|
|
if (it == m_variables.end()) {
|
|
|
|
Variable::Imp *imp = new Variable::Imp(name);
|
|
|
|
m_variables[name] = imp;
|
|
|
|
return imp;
|
|
|
|
} else
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit() {
|
|
|
|
// save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadIfNeeded() {
|
|
|
|
if (m_loaded) return;
|
|
|
|
m_loaded = true;
|
|
|
|
try {
|
|
|
|
load();
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void load();
|
|
|
|
void save();
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void VariableSet::load() {
|
2016-04-24 18:26:55 +12:00
|
|
|
#ifndef WIN32
|
2016-06-15 18:43:10 +12:00
|
|
|
EnvGlobals::instance()->updateEnvFile();
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath fp = EnvGlobals::instance()->getEnvFile();
|
|
|
|
if (fp == TFilePath()) return;
|
2017-10-21 02:23:54 +13:00
|
|
|
// if the personal env is not found, then try to find the template
|
|
|
|
if (!TFileStatus(fp).doesExist())
|
|
|
|
fp = EnvGlobals::instance()->getTemplateEnvFile();
|
2016-06-15 18:43:10 +12:00
|
|
|
Tifstream is(fp);
|
2017-10-21 02:23:54 +13:00
|
|
|
if (!is.isOpen()) return;
|
2016-06-15 18:43:10 +12:00
|
|
|
char buffer[1024];
|
|
|
|
while (is.getline(buffer, sizeof(buffer))) {
|
|
|
|
char *s = buffer;
|
|
|
|
while (*s == ' ') s++;
|
|
|
|
char *t = s;
|
2020-04-12 17:19:20 +12:00
|
|
|
while (('a' <= *s && *s <= 'z') || ('A' <= *s && *s <= 'Z') ||
|
|
|
|
('0' <= *s && *s <= '9') || *s == '_')
|
2016-06-15 18:43:10 +12:00
|
|
|
s++;
|
|
|
|
std::string name(t, s - t);
|
|
|
|
if (name.size() == 0) continue;
|
|
|
|
while (*s == ' ') s++;
|
|
|
|
if (*s != '\"') continue;
|
|
|
|
s++;
|
|
|
|
std::string value;
|
|
|
|
while (*s != '\n' && *s != '\0' && *s != '\"') {
|
|
|
|
if (*s != '\\')
|
|
|
|
value.push_back(*s);
|
|
|
|
else {
|
|
|
|
s++;
|
|
|
|
if (*s == '\\')
|
|
|
|
value.push_back('\\');
|
|
|
|
else if (*s == '"')
|
|
|
|
value.push_back('"');
|
|
|
|
else if (*s == 'n')
|
|
|
|
value.push_back('\n');
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
Variable::Imp *imp = getImp(name);
|
|
|
|
imp->m_value = value;
|
|
|
|
imp->m_loaded = true;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void VariableSet::save() {
|
|
|
|
TFilePath fp = EnvGlobals::instance()->getEnvFile();
|
|
|
|
if (fp == TFilePath()) return;
|
|
|
|
bool exists = TFileStatus(fp.getParentDir()).doesExist();
|
|
|
|
if (!exists) {
|
|
|
|
try {
|
|
|
|
TSystem::mkDir(fp.getParentDir());
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Tofstream os(fp);
|
|
|
|
if (!os) return;
|
|
|
|
std::map<std::string, Variable::Imp *>::iterator it;
|
|
|
|
for (it = m_variables.begin(); it != m_variables.end(); ++it) {
|
|
|
|
os << it->first << " \"";
|
|
|
|
std::string s = it->second->m_value;
|
|
|
|
for (int i = 0; i < (int)s.size(); i++)
|
|
|
|
if (s[i] == '\"')
|
|
|
|
os << "\\\"";
|
|
|
|
else if (s[i] == '\\')
|
|
|
|
os << "\\\\";
|
|
|
|
else if (s[i] == '\n')
|
|
|
|
os << "\\n";
|
|
|
|
else
|
|
|
|
os.put(s[i]);
|
|
|
|
os << "\"" << std::endl;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
|
2016-04-19 19:32:17 +12:00
|
|
|
Variable::Variable(std::string name)
|
2016-06-15 18:43:10 +12:00
|
|
|
: m_imp(VariableSet::instance()->getImp(name)) {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-04-19 19:32:17 +12:00
|
|
|
Variable::Variable(std::string name, std::string defaultValue)
|
2016-06-15 18:43:10 +12:00
|
|
|
: m_imp(VariableSet::instance()->getImp(name)) {
|
|
|
|
// assert(!m_imp->m_defaultDefined);
|
2020-05-23 16:20:51 +12:00
|
|
|
m_imp->m_defaultDefined = true;
|
2016-06-15 18:43:10 +12:00
|
|
|
if (!m_imp->m_loaded) m_imp->m_value = defaultValue;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Variable::~Variable() {}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string Variable::getName() const { return m_imp->m_name; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string Variable::getValue() const {
|
|
|
|
VariableSet::instance()->loadIfNeeded();
|
|
|
|
return m_imp->m_value;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void Variable::assignValue(std::string value) {
|
|
|
|
VariableSet *vs = VariableSet::instance();
|
|
|
|
vs->loadIfNeeded();
|
|
|
|
m_imp->m_value = value;
|
|
|
|
try {
|
|
|
|
vs->commit();
|
|
|
|
} catch (...) {
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//===================================================================
|
|
|
|
|
2019-06-03 00:39:40 +12:00
|
|
|
void TEnv::setApplicationFileName(std::string appFileName) {
|
|
|
|
TFilePath fp(appFileName);
|
2019-06-03 05:41:10 +12:00
|
|
|
#ifdef MACOSX
|
|
|
|
if (fp.getWideName().find(L".app"))
|
|
|
|
for (int i = 0; i < 3; i++) fp = fp.getParentDir();
|
2020-05-23 02:17:41 +12:00
|
|
|
#elif defined(LINUX) || defined(FREEBSD)
|
2019-06-03 05:41:10 +12:00
|
|
|
if (fp.getWideName().find(L".appimage"))
|
|
|
|
for (int i = 0; i < 2; i++) fp = fp.getParentDir();
|
|
|
|
#endif
|
2019-06-03 00:39:40 +12:00
|
|
|
EnvGlobals::instance()->setApplicationFileName(fp.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TEnv::getApplicationFileName() {
|
|
|
|
return EnvGlobals::instance()->getApplicationFileName();
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string TEnv::getApplicationName() {
|
|
|
|
return EnvGlobals::instance()->getApplicationName();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string TEnv::getApplicationVersion() {
|
|
|
|
return EnvGlobals::instance()->getApplicationVersion();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TEnv::setApplicationFullName(std::string applicationFullName) {
|
|
|
|
EnvGlobals::instance()->setApplicationFullName(applicationFullName);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string TEnv::getApplicationFullName() {
|
|
|
|
return EnvGlobals::instance()->getApplicationFullName();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TEnv::setModuleName(std::string moduleName) {
|
|
|
|
EnvGlobals::instance()->setModuleName(moduleName);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string TEnv::getModuleName() {
|
|
|
|
return EnvGlobals::instance()->getModuleName();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TEnv::setRootVarName(std::string varName) {
|
|
|
|
EnvGlobals::instance()->setRootVarName(varName);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string TEnv::getRootVarName() {
|
|
|
|
return EnvGlobals::instance()->getRootVarName();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TEnv::getRootVarPath() {
|
|
|
|
return EnvGlobals::instance()->getRootVarPath();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string TEnv::getSystemVarStringValue(std::string varName) {
|
|
|
|
return EnvGlobals::instance()->getSystemVarValue(varName);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TEnv::getSystemVarPathValue(std::string varName) {
|
2018-12-28 15:15:09 +13:00
|
|
|
return EnvGlobals::instance()->getSystemVarPathValue(varName);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePathSet TEnv::getSystemVarPathSetValue(std::string varName) {
|
|
|
|
TFilePathSet lst;
|
2018-07-10 17:42:57 +12:00
|
|
|
EnvGlobals *eg = EnvGlobals::instance();
|
|
|
|
// if the path is registered by command line argument, then use it
|
2020-05-23 16:20:51 +12:00
|
|
|
std::string value = eg->getArgPathValue(varName);
|
2018-07-10 17:42:57 +12:00
|
|
|
if (value == "") value = eg->getSystemVarValue(varName);
|
2020-05-23 16:20:51 +12:00
|
|
|
int len = (int)value.size();
|
|
|
|
int i = 0;
|
|
|
|
int j = value.find(';');
|
2016-06-15 18:43:10 +12:00
|
|
|
while (j != std::string::npos) {
|
|
|
|
std::string s = value.substr(i, j - i);
|
|
|
|
lst.push_back(TFilePath(s));
|
|
|
|
i = j + 1;
|
|
|
|
if (i >= len) return lst;
|
|
|
|
j = value.find(';', i);
|
|
|
|
}
|
|
|
|
if (i < len) lst.push_back(TFilePath(value.substr(i)));
|
|
|
|
return lst;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TEnv::setSystemVarPrefix(std::string varName) {
|
|
|
|
EnvGlobals::instance()->setSystemVarPrefix(varName);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string TEnv::getSystemVarPrefix() {
|
|
|
|
return EnvGlobals::instance()->getSystemVarPrefix();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TEnv::getStuffDir() {
|
|
|
|
//#ifdef MACOSX
|
|
|
|
// return TFilePath("/Applications/Toonz 5.0/Toonz 5.0 stuff");
|
|
|
|
//#else
|
|
|
|
return EnvGlobals::instance()->getStuffDir();
|
|
|
|
//#endif
|
|
|
|
}
|
|
|
|
|
2017-09-04 18:13:56 +12:00
|
|
|
bool TEnv::getIsPortable() { return EnvGlobals::instance()->getIsPortable(); }
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
TFilePath TEnv::getConfigDir() {
|
|
|
|
TFilePath configDir = getSystemVarPathValue(getSystemVarPrefix() + "CONFIG");
|
2018-07-10 17:42:57 +12:00
|
|
|
if (configDir == TFilePath())
|
|
|
|
configDir = getStuffDir() + systemPathMap.at("CONFIG");
|
2016-06-15 18:43:10 +12:00
|
|
|
return configDir;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/*TFilePath TEnv::getProfilesDir()
|
|
|
|
{
|
|
|
|
TFilePath fp(getStuffDir());
|
|
|
|
return fp != TFilePath() ? fp + "profiles" : fp;
|
|
|
|
}
|
|
|
|
*/
|
2020-10-11 07:06:49 +13:00
|
|
|
TFilePath TEnv::getWorkingDirectory() {
|
|
|
|
TFilePath workingDir(EnvGlobals::instance()->getWorkingDirectory());
|
|
|
|
if (workingDir == TFilePath()) workingDir = TFilePath(QDir::currentPath());
|
|
|
|
return workingDir;
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TEnv::setStuffDir(const TFilePath &stuffDir) {
|
|
|
|
EnvGlobals::instance()->setStuffDir(stuffDir);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void TEnv::saveAllEnvVariables() { VariableSet::instance()->save(); }
|
2020-11-11 06:44:05 +13:00
|
|
|
void TEnv::loadAllEnvVariables() { VariableSet::instance()->load(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2018-07-10 17:42:57 +12:00
|
|
|
bool TEnv::setArgPathValue(std::string key, std::string value) {
|
|
|
|
EnvGlobals *eg = EnvGlobals::instance();
|
|
|
|
// in case of "-TOONZROOT" , set the all unregistered paths
|
|
|
|
if (key == getRootVarName()) {
|
|
|
|
TFilePath rootPath(value);
|
|
|
|
eg->setStuffDir(rootPath);
|
|
|
|
for (auto itr = systemPathMap.begin(); itr != systemPathMap.end(); ++itr) {
|
|
|
|
std::string k = getSystemVarPrefix() + (*itr).first;
|
|
|
|
std::string val = value + "\\" + (*itr).second;
|
|
|
|
// set all unregistered values
|
|
|
|
if (eg->getArgPathValue(k) == "") eg->setArgPathValue(k, val);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
for (auto itr = systemPathMap.begin(); itr != systemPathMap.end(); ++itr) {
|
|
|
|
// found the corresponding registry key
|
|
|
|
if (key == getSystemVarPrefix() + (*itr).first) {
|
|
|
|
eg->setArgPathValue(key, value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// registry key not found. failed to register
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::map<std::string, std::string> &TEnv::getSystemPathMap() {
|
|
|
|
return systemPathMap;
|
|
|
|
}
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
/*
|
|
|
|
void TEnv::defineSystemPath(SystemFileId id, const TFilePath ®istryName)
|
|
|
|
{
|
|
|
|
string s = TSystem::getSystemValue(registryName);
|
|
|
|
if(s=="") return;
|
|
|
|
EnvGlobals::instance()->setSystemPath(id, TFilePath(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
TFilePath TEnv::getSystemPath(SystemFileId id)
|
|
|
|
{
|
|
|
|
return EnvGlobals::instance()->getSystemPath(id);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
//=========================================================
|
|
|
|
//
|
|
|
|
// Variabili tipizzate
|
|
|
|
//
|
|
|
|
//=========================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::istream &operator>>(std::istream &is, TFilePath &path) {
|
|
|
|
std::string s;
|
|
|
|
is >> s;
|
|
|
|
return is;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::istream &operator>>(std::istream &is, TRect &rect) {
|
|
|
|
return is >> rect.x0 >> rect.y0 >> rect.x1 >> rect.y1;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string toString2(T value) {
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << value << '\0';
|
|
|
|
return ss.str();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string toString2(TRect value) {
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << value.x0 << " " << value.y0 << " " << value.x1 << " " << value.y1
|
|
|
|
<< '\0';
|
|
|
|
return ss.str();
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2016-06-15 18:43:10 +12:00
|
|
|
void fromString(std::string s, T &value) {
|
|
|
|
if (s.empty()) return;
|
|
|
|
std::istringstream is(s);
|
|
|
|
is >> value;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void fromString(std::string s, std::string &value) { value = s; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
IntVar::IntVar(std::string name, int defValue)
|
|
|
|
: Variable(name, std::to_string(defValue)) {}
|
2016-04-19 19:32:17 +12:00
|
|
|
IntVar::IntVar(std::string name) : Variable(name) {}
|
2016-06-15 18:43:10 +12:00
|
|
|
IntVar::operator int() const {
|
|
|
|
int v;
|
|
|
|
fromString(getValue(), v);
|
|
|
|
return v;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
2016-06-07 19:06:00 +12:00
|
|
|
void IntVar::operator=(int v) { assignValue(std::to_string(v)); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
DoubleVar::DoubleVar(std::string name, double defValue)
|
|
|
|
: Variable(name, std::to_string(defValue)) {}
|
2016-04-19 19:32:17 +12:00
|
|
|
DoubleVar::DoubleVar(std::string name) : Variable(name) {}
|
2016-06-15 18:43:10 +12:00
|
|
|
DoubleVar::operator double() const {
|
|
|
|
double v;
|
|
|
|
fromString(getValue(), v);
|
|
|
|
return v;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
2016-06-07 19:06:00 +12:00
|
|
|
void DoubleVar::operator=(double v) { assignValue(std::to_string(v)); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
StringVar::StringVar(std::string name, const std::string &defValue)
|
|
|
|
: Variable(name, defValue) {}
|
2016-04-19 19:32:17 +12:00
|
|
|
StringVar::StringVar(std::string name) : Variable(name) {}
|
2016-06-15 18:43:10 +12:00
|
|
|
StringVar::operator std::string() const {
|
|
|
|
std::string v;
|
|
|
|
fromString(getValue(), v);
|
|
|
|
return v;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
2016-04-19 19:32:17 +12:00
|
|
|
void StringVar::operator=(const std::string &v) { assignValue(v); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
FilePathVar::FilePathVar(std::string name, const TFilePath &defValue)
|
|
|
|
: Variable(name, ::to_string(defValue)) {}
|
2016-04-19 19:32:17 +12:00
|
|
|
FilePathVar::FilePathVar(std::string name) : Variable(name) {}
|
2016-06-15 18:43:10 +12:00
|
|
|
FilePathVar::operator TFilePath() const {
|
|
|
|
std::string v;
|
|
|
|
fromString(getValue(), v);
|
|
|
|
return TFilePath(v);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
2016-06-07 19:06:00 +12:00
|
|
|
void FilePathVar::operator=(const TFilePath &v) { assignValue(::to_string(v)); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
RectVar::RectVar(std::string name, const TRect &defValue)
|
|
|
|
: Variable(name, toString2(defValue)) {}
|
2016-04-19 19:32:17 +12:00
|
|
|
RectVar::RectVar(std::string name) : Variable(name) {}
|
2016-06-15 18:43:10 +12:00
|
|
|
RectVar::operator TRect() const {
|
|
|
|
TRect v;
|
|
|
|
fromString(getValue(), v);
|
|
|
|
return v;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
void RectVar::operator=(const TRect &v) { assignValue(toString2(v)); }
|
|
|
|
|
|
|
|
//=========================================================
|