parent
4e57686976
commit
d879569fce
@ -0,0 +1,5 @@
|
||||
install(
|
||||
FILES
|
||||
UiGuiSyntaxHighlightConfig.ini
|
||||
DESTINATION ${SHARE_INSTALL_PREFIX}/universal-indent-gui-tqt/config
|
||||
)
|
@ -0,0 +1,229 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2006-2012 by Thomas Schweitzer *
|
||||
* thomas-schweitzer(at)arcor.de *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License version 2.0 as *
|
||||
* published by the Free Software Foundation. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program in the file LICENSE.GPL; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "config.h"
|
||||
#include "SettingsPaths.h"
|
||||
|
||||
#include <tqapplication.h>
|
||||
#include <tqdir.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
\class SettingsPaths
|
||||
\brief SettingsPaths is a pure static functions class from which info
|
||||
about the paths needed for settings can be retrieved.
|
||||
*/
|
||||
|
||||
bool SettingsPaths::m_alreadyInitialized = false;
|
||||
bool SettingsPaths::m_portableMode = false;
|
||||
TQString SettingsPaths::m_applicationBinaryPath = "";
|
||||
TQString SettingsPaths::m_globalFilesPath = "";
|
||||
TQString SettingsPaths::m_indenterPath = "";
|
||||
TQString SettingsPaths::m_settingsPath = "";
|
||||
TQString SettingsPaths::m_tempPath = "";
|
||||
|
||||
/*
|
||||
\brief Initializes all available information about the paths.
|
||||
|
||||
Mainly during this init it is detected whether to start in portable mode or not. This is
|
||||
done by testing whether the directory "config" is in the same directory as this
|
||||
applications executable file.
|
||||
In portable mode all data is ONLY written to subdirectories of the applications executable file.
|
||||
Means also that the directory "indenters" has to be there.
|
||||
In not portable mode (multiuser mode) only users home directory is used for writing config data.
|
||||
*/
|
||||
void SettingsPaths::init()
|
||||
{
|
||||
m_alreadyInitialized = true;
|
||||
|
||||
tqDebug("Initializing application paths.");
|
||||
|
||||
// Get the applications binary path, with respect to MacOSXs use of the .app folder.
|
||||
m_applicationBinaryPath = tqApp->applicationDirPath();
|
||||
// Remove any trailing slashes
|
||||
while (m_applicationBinaryPath.right(1) == "/")
|
||||
{
|
||||
m_applicationBinaryPath.truncate(m_applicationBinaryPath.length() - 1);
|
||||
}
|
||||
|
||||
#ifdef UNIVERSALINDENTGUI_NPP_EXPORTS
|
||||
m_applicationBinaryPath += "/plugins/uigui";
|
||||
#endif
|
||||
|
||||
// If the "config" directory is a subdir of the applications binary path, use this one
|
||||
// (portable mode)
|
||||
m_settingsPath = m_applicationBinaryPath + "/config";
|
||||
if (TQFile::exists(m_settingsPath))
|
||||
{
|
||||
m_portableMode = true;
|
||||
TQDir dirCreator;
|
||||
m_globalFilesPath = m_applicationBinaryPath;
|
||||
m_indenterPath = m_applicationBinaryPath + "/indenters";
|
||||
dirCreator.mkdir(m_settingsPath);
|
||||
m_tempPath = m_applicationBinaryPath + "/temp";
|
||||
//TODO: If the portable drive has write protection, use local temp path and clean it up on exit.
|
||||
dirCreator.mkdir(m_tempPath);
|
||||
}
|
||||
// ... otherwise use the system specific global application data path.
|
||||
else
|
||||
{
|
||||
m_portableMode = false;
|
||||
TQDir dirCreator;
|
||||
// Remove any trailing slashes.
|
||||
m_settingsPath = TQDir::homeDirPath();
|
||||
while (m_settingsPath.right(1) == "/")
|
||||
{
|
||||
m_settingsPath.truncate(m_settingsPath.length() - 1);
|
||||
}
|
||||
m_settingsPath = m_settingsPath + "/.universalindentgui";
|
||||
m_globalFilesPath = APP_SHARE_PATH;
|
||||
dirCreator.mkdir(m_settingsPath);
|
||||
// If a highlighter config file does not exist in the users home config dir
|
||||
// copy the default config file over there.
|
||||
if (!TQFile::exists(m_settingsPath + "/UiGuiSyntaxHighlightConfig.ini"))
|
||||
{
|
||||
TQString copyCmd("cp " + m_globalFilesPath + "/config/UiGuiSyntaxHighlightConfig.ini " +
|
||||
m_settingsPath + "/UiGuiSyntaxHighlightConfig.ini");
|
||||
int res = system(copyCmd.local8Bit().data());
|
||||
if (res == -1)
|
||||
{
|
||||
tqWarning("Unable to copy default highlighter config to the user home config folder.");
|
||||
}
|
||||
}
|
||||
m_indenterPath = m_globalFilesPath + "/indenters";
|
||||
|
||||
m_tempPath = getenv("TMPDIR");
|
||||
if (m_tempPath.isEmpty())
|
||||
m_tempPath = getenv("TEMP");
|
||||
if (m_tempPath.isEmpty())
|
||||
m_tempPath = getenv("TMP");
|
||||
if (m_tempPath.isEmpty())
|
||||
m_tempPath = "/tmp";
|
||||
while (m_tempPath.right(1) == "/")
|
||||
{
|
||||
m_tempPath.truncate(m_tempPath.length() - 1);
|
||||
}
|
||||
// On Unix based systems create a random temporary directory for security
|
||||
// reasons. Otherwise an evil human being could create a symbolic link
|
||||
// to an important existing file which gets overwritten when UiGUI writes
|
||||
// into this normally temporary but linked file.
|
||||
m_tempPath = m_tempPath + "/UniversalIndentGUI-XXXXXX";
|
||||
m_tempPath = mkdtemp(m_tempPath.local8Bit().data());
|
||||
}
|
||||
|
||||
tqDebug("Paths are:\n"
|
||||
" m_applicationBinaryPath = %s\n"
|
||||
" m_settingsPath = %s\n"
|
||||
" m_globalFilesPath =%s\n"
|
||||
" m_indenterPath = %s\n"
|
||||
" m_tempPath = %s\n"
|
||||
"Running in portable mode = %s",
|
||||
m_applicationBinaryPath.local8Bit().data(),
|
||||
m_settingsPath.local8Bit().data(),
|
||||
m_globalFilesPath.local8Bit().data(),
|
||||
m_indenterPath.local8Bit().data(),
|
||||
m_tempPath.local8Bit().data(),
|
||||
(m_portableMode ? "Yes" : "No"));
|
||||
}
|
||||
|
||||
/*
|
||||
\brief Returns the path of the applications executable.
|
||||
*/
|
||||
const TQString SettingsPaths::getApplicationBinaryPath()
|
||||
{
|
||||
if (!m_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return m_applicationBinaryPath;
|
||||
}
|
||||
|
||||
/*
|
||||
\brief Returns the path where all settings are being/should be written to.
|
||||
*/
|
||||
const TQString SettingsPaths::getSettingsPath()
|
||||
{
|
||||
if (!m_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return m_settingsPath;
|
||||
}
|
||||
|
||||
/*
|
||||
\brief Returns the path where the files concerning all users reside. For example translations.
|
||||
*/
|
||||
const TQString SettingsPaths::getGlobalFilesPath()
|
||||
{
|
||||
if (!m_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return m_globalFilesPath;
|
||||
}
|
||||
|
||||
/*
|
||||
\brief Returns the path where the indenter executables reside.
|
||||
*/
|
||||
const TQString SettingsPaths::getIndenterPath()
|
||||
{
|
||||
if (!m_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return m_indenterPath;
|
||||
}
|
||||
|
||||
/*
|
||||
\brief Returns the path where the where all temporary data should be written to.
|
||||
*/
|
||||
const TQString SettingsPaths::getTempPath()
|
||||
{
|
||||
if (!m_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return m_tempPath;
|
||||
}
|
||||
|
||||
/*
|
||||
\brief Returns true if portable mode shall be used.
|
||||
*/
|
||||
bool SettingsPaths::getPortableMode()
|
||||
{
|
||||
if (!m_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return m_portableMode;
|
||||
}
|
||||
|
||||
/*
|
||||
\brief Completely deletes the created temporary directory with all of its content.
|
||||
*/
|
||||
void SettingsPaths::cleanAndRemoveTempDir()
|
||||
{
|
||||
TQString removeCmd("rm -rf " + m_tempPath);
|
||||
int res = system(removeCmd.local8Bit().data());
|
||||
if (res == -1)
|
||||
{
|
||||
tqWarning("Unable to remove temporary folder %s.", m_tempPath.local8Bit().data());
|
||||
}
|
||||
}
|
@ -1,309 +0,0 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2006-2012 by Thomas Schweitzer *
|
||||
* thomas-schweitzer(at)arcor.de *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License version 2.0 as *
|
||||
* published by the Free Software Foundation. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program in the file LICENSE.GPL; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include "SettingsPaths.h"
|
||||
|
||||
#include <tqcoreapplication.h>
|
||||
#include <tntqfile.h>
|
||||
#include <tntqdir.h>
|
||||
#include <tqdiriterator.h>
|
||||
#include <tntqstack.h>
|
||||
#include <tqtdebug.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
//! \defgroup grp_Settings All concerning applications settings.
|
||||
|
||||
/*!
|
||||
\class SettingsPaths
|
||||
\ingroup grp_Settings
|
||||
\brief SettingsPaths is a pure static functions class from which info about the
|
||||
paths needed for settings can be retrieved.
|
||||
*/
|
||||
|
||||
bool SettingsPaths::_alreadyInitialized = false;
|
||||
TQString SettingsPaths::_applicationBinaryPath = "";
|
||||
TQString SettingsPaths::_settingsPath = "";
|
||||
TQString SettingsPaths::_globalFilesPath = "";
|
||||
TQString SettingsPaths::_indenterPath = "";
|
||||
TQString SettingsPaths::_tempPath = "";
|
||||
bool SettingsPaths::_portableMode = false;
|
||||
|
||||
/*!
|
||||
\brief Initializes all available information about the paths.
|
||||
|
||||
Mainly during this init it is detected whether to start in portable mode or not. This is
|
||||
done by testing whether the directory "config" is in the same directory as this
|
||||
applications executable file.
|
||||
In portable mode all data is ONLY written to subdirectories of the applications executable file.
|
||||
Means also that the directory "indenters" has to be there.
|
||||
In not portable mode (multiuser mode) only users home directory is used for writing config data.
|
||||
*/
|
||||
void SettingsPaths::init()
|
||||
{
|
||||
_alreadyInitialized = true;
|
||||
|
||||
tqDebug() << __LINE__ << " " << __FUNCTION__ << ": Initializing application paths.";
|
||||
|
||||
// Get the applications binary path, with respect to MacOSXs use of the .app folder.
|
||||
_applicationBinaryPath = TQCoreApplication::applicationDirPath();
|
||||
// Remove any trailing slashes
|
||||
while (_applicationBinaryPath.right(1) == "/")
|
||||
{
|
||||
_applicationBinaryPath.chop(1);
|
||||
}
|
||||
|
||||
#ifdef UNIVERSALINDENTGUI_NPP_EXPORTS
|
||||
_applicationBinaryPath += "/plugins/uigui";
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
// Because on Mac universal binaries are used, the binary path is not equal
|
||||
// to the applications (.app) path. So get the .apps path here.
|
||||
int indexOfDotApp = _applicationBinaryPath.indexOf(".app");
|
||||
if (indexOfDotApp != -1)
|
||||
{
|
||||
// Cut off after the dot of ".app".
|
||||
_applicationBinaryPath = _applicationBinaryPath.left(indexOfDotApp - 1);
|
||||
// Cut off after the first slash that was in front of ".app" (normally this is the word
|
||||
// "UniversalIndentGUI")
|
||||
_applicationBinaryPath = _applicationBinaryPath.left(_applicationBinaryPath.lastIndexOf("/"));
|
||||
}
|
||||
#endif
|
||||
|
||||
// If the "config" directory is a subdir of the applications binary path, use this one (portable
|
||||
// mode)
|
||||
_settingsPath = _applicationBinaryPath + "/config";
|
||||
if (TQFile::exists(_settingsPath))
|
||||
{
|
||||
_portableMode = true;
|
||||
TQDir dirCreator;
|
||||
_globalFilesPath = _applicationBinaryPath;
|
||||
_indenterPath = _applicationBinaryPath + "/indenters";
|
||||
dirCreator.mkpath(_settingsPath);
|
||||
_tempPath = _applicationBinaryPath + "/temp";
|
||||
//TODO: If the portable drive has write protection, use local temp path and clean it up on exit.
|
||||
dirCreator.mkpath(_tempPath);
|
||||
}
|
||||
// ... otherwise use the system specific global application data path.
|
||||
else
|
||||
{
|
||||
_portableMode = false;
|
||||
TQDir dirCreator;
|
||||
#ifdef Q_OS_WIN
|
||||
// Get the local users application settings directory.
|
||||
// Remove any trailing slashes.
|
||||
_settingsPath = TQDir::fromNativeSeparators(qgetenv("APPDATA"));
|
||||
while (_settingsPath.right(1) == "/")
|
||||
{
|
||||
_settingsPath.chop(1);
|
||||
}
|
||||
_settingsPath = _settingsPath + "/UniversalIndentGUI";
|
||||
|
||||
// On windows systems the directories "indenters", "translations" are subdirs of the
|
||||
// _applicationBinaryPath.
|
||||
_globalFilesPath = _applicationBinaryPath;
|
||||
#else
|
||||
// Remove any trailing slashes.
|
||||
_settingsPath = TQDir::homePath();
|
||||
while (_settingsPath.right(1) == "/")
|
||||
{
|
||||
_settingsPath.chop(1);
|
||||
}
|
||||
_settingsPath = _settingsPath + "/.universalindentgui";
|
||||
_globalFilesPath = "/usr/share/universalindentgui";
|
||||
#endif
|
||||
dirCreator.mkpath(_settingsPath);
|
||||
// If a highlighter config file does not exist in the users home config dir
|
||||
// copy the default config file over there.
|
||||
if (!TQFile::exists(_settingsPath + "/UiGuiSyntaxHighlightConfig.ini"))
|
||||
{
|
||||
TQFile::copy(_globalFilesPath + "/config/UiGuiSyntaxHighlightConfig.ini",
|
||||
_settingsPath + "/UiGuiSyntaxHighlightConfig.ini");
|
||||
}
|
||||
_indenterPath = _globalFilesPath + "/indenters";
|
||||
|
||||
// On different systems it may be that "TQDir::tempPath()" ends with a "/" or not. So check
|
||||
// this.
|
||||
// Remove any trailing slashes.
|
||||
_tempPath = TQDir::tempPath();
|
||||
while (_tempPath.right(1) == "/")
|
||||
{
|
||||
_tempPath.chop(1);
|
||||
}
|
||||
_tempPath = _tempPath + "/UniversalIndentGUI";
|
||||
|
||||
#if defined (Q_OS_WIN32)
|
||||
dirCreator.mkpath(_tempPath);
|
||||
#else
|
||||
// On Unix based systems create a random temporary directory for security
|
||||
// reasons. Otherwise an evil human being could create a symbolic link
|
||||
// to an important existing file which gets overwritten when UiGUI writes
|
||||
// into this normally temporary but linked file.
|
||||
char *pathTemplate = new char[_tempPath.length() + 8];
|
||||
TQByteArray pathTemplateTQBA = TQString(_tempPath + "-XXXXXX").toAscii();
|
||||
delete[] pathTemplate;
|
||||
pathTemplate = pathTemplateTQBA.data();
|
||||
pathTemplate = mkdtemp(pathTemplate);
|
||||
_tempPath = pathTemplate;
|
||||
#endif
|
||||
}
|
||||
|
||||
tqDebug() << __LINE__ << " " << __FUNCTION__ << ": Paths are:" \
|
||||
"<ul><li>_applicationBinaryPath=" <<
|
||||
_applicationBinaryPath << "</li><li>_settingsPath=" << _settingsPath <<
|
||||
"</li><li>_globalFilesPath=" << _globalFilesPath << "</li><li>_indenterPath=" <<
|
||||
_indenterPath << "</li><li>_tempPath=" << _tempPath <<
|
||||
"</li><li>Running in portable mode=" << _portableMode << "</li></ul>";
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief Returns the path of the applications executable.
|
||||
*/
|
||||
const TQString SettingsPaths::getApplicationBinaryPath()
|
||||
{
|
||||
if (!_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return _applicationBinaryPath;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief Returns the path where all settings are being/should be written to.
|
||||
*/
|
||||
const TQString SettingsPaths::getSettingsPath()
|
||||
{
|
||||
if (!_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return _settingsPath;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief Returns the path where the files concerning all users reside. For example translations.
|
||||
*/
|
||||
const TQString SettingsPaths::getGlobalFilesPath()
|
||||
{
|
||||
if (!_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return _globalFilesPath;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief Returns the path where the indenter executables reside.
|
||||
*/
|
||||
const TQString SettingsPaths::getIndenterPath()
|
||||
{
|
||||
if (!_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return _indenterPath;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief Returns the path where the where all temporary data should be written to.
|
||||
*/
|
||||
const TQString SettingsPaths::getTempPath()
|
||||
{
|
||||
if (!_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return _tempPath;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief Returns true if portable mode shall be used.
|
||||
*/
|
||||
bool SettingsPaths::getPortableMode()
|
||||
{
|
||||
if (!_alreadyInitialized)
|
||||
{
|
||||
SettingsPaths::init();
|
||||
}
|
||||
return _portableMode;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief Completely deletes the created temporary directory with all of its content.
|
||||
*/
|
||||
void SettingsPaths::cleanAndRemoveTempDir()
|
||||
{
|
||||
TQDirIterator dirIterator(_tempPath, TQDirIterator::Subdirectories);
|
||||
TQStack<TQString> directoryStack;
|
||||
bool noErrorsOccurred = true;
|
||||
|
||||
while (dirIterator.hasNext())
|
||||
{
|
||||
TQString currentDirOrFile = dirIterator.next();
|
||||
// If this dummy call isn't done here, calling "dirIterator.fileInfo().isDir()" later somehow
|
||||
// fails.
|
||||
dirIterator.fileInfo();
|
||||
|
||||
if (!currentDirOrFile.isEmpty() && dirIterator.fileName() != "." &&
|
||||
dirIterator.fileName() != "..")
|
||||
{
|
||||
// There is a path on the stack but the current path doesn't start with that path.
|
||||
// So we changed into another parent directory and the one on the stack can be deleted
|
||||
// since it must be empty.
|
||||
if (!directoryStack.isEmpty() && !currentDirOrFile.startsWith(directoryStack.top()))
|
||||
{
|
||||
TQString dirToBeRemoved = directoryStack.pop();
|
||||
bool couldRemoveDir = TQDir(dirToBeRemoved).rmdir(dirToBeRemoved);
|
||||
noErrorsOccurred &= couldRemoveDir;
|
||||
if (couldRemoveDir == false)
|
||||
{
|
||||
tqWarning() << __LINE__ << " " << __FUNCTION__ << "Could not remove the directory: " <<
|
||||
dirToBeRemoved;
|
||||
}
|
||||
//tqDebug() << "Removing Dir " << directoryStack.pop();
|
||||
}
|
||||
|
||||
// If the iterator currently points to a directory push it onto the stack.
|
||||
if (dirIterator.fileInfo().isDir())
|
||||
{
|
||||
directoryStack.push(currentDirOrFile);
|
||||
//tqDebug() << "Pushing onto Stack " << currentDirOrFile;
|
||||
}
|
||||
// otherwise it must be a file, so delete it.
|
||||
else
|
||||
{
|
||||
bool couldRemoveFile = TQFile::remove(currentDirOrFile);
|
||||
noErrorsOccurred &= couldRemoveFile;
|
||||
if (couldRemoveFile == false)
|
||||
{
|
||||
tqWarning() << __LINE__ << " " << __FUNCTION__ << "Could not remove the file: " <<
|
||||
currentDirOrFile;
|
||||
}
|
||||
//tqDebug() << "Removing File " << currentDirOrFile;
|
||||
}
|
||||
}
|
||||
}
|
||||
noErrorsOccurred &= TQDir(_tempPath).rmdir(_tempPath);
|
||||
if (noErrorsOccurred == false)
|
||||
{
|
||||
tqWarning() << __LINE__ << " " << __FUNCTION__ <<
|
||||
"While cleaning up the temp dir an error occurred.";
|
||||
}
|
||||
}
|
Loading…
Reference in new issue