Added SettingsPaths code.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
pull/3/head
Michele Calgaro 2 years ago
parent 4e57686976
commit d879569fce
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -70,6 +70,7 @@ set( CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--no-undefined"
##### directories
add_subdirectory( config )
add_subdirectory( icons )
add_subdirectory( src )

@ -113,10 +113,6 @@ unix:indenters.files += indenters/shellindent.awk
unix:translation.path = /usr/share/universalindentgui/translations
unix:translation.files = translations/*.qm
# Install highlighter default config
unix:highlighterconfig.path = /usr/share/universalindentgui/config
unix:highlighterconfig.files = config/UiGuiSyntaxHighlightConfig.ini
# Install binary
unix:target.path = /usr/bin

@ -1,5 +1,8 @@
#define VERSION "@VERSION@"
// application share location
#define APP_SHARE_PATH "@SHARE_INSTALL_PREFIX@/universal-indent-gui-tqt"
// application icons location
#define APP_ICONS_PATH "@SHARE_INSTALL_PREFIX@/universal-indent-gui-tqt/icons/"

@ -0,0 +1,5 @@
install(
FILES
UiGuiSyntaxHighlightConfig.ini
DESTINATION ${SHARE_INSTALL_PREFIX}/universal-indent-gui-tqt/config
)

@ -31,12 +31,12 @@
#include <tqscrollbar.h>
#include <tqtimer.h>
/*!
/*
\class AboutDialog
\brief Displays a dialog window with information about UniversalIndentGUI
*/
/*!
/*
\brief The constructor calls the setup function for the ui created by uic and adds
the GPL text to the text edit.
*/
@ -104,7 +104,7 @@ AboutDialog::AboutDialog(TQWidget *parent, WFlags flags) :
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
}
/*!
/*
\brief Catches language change events and retranslates all needed widgets.
*/
void AboutDialog::changeEvent(TQEvent *event)
@ -125,7 +125,7 @@ void AboutDialog::changeEvent(TQEvent *event)
//---- }
}
/*!
/*
\brief Reimplements the dialog execution function to init the credits scroller.
*/
void AboutDialog::show()
@ -135,7 +135,7 @@ void AboutDialog::show()
AboutDialogBase::show();
}
/*!
/*
\brief This slot is called each m_timer timeout to scroll the credits textbrowser.
Also changes the scroll direction and speed when reaching the start or end.
*/

@ -26,8 +26,19 @@ link_directories(
set( target universal-indent-gui-tqt )
set( ${target}_SRCS
AboutDialogBase.ui MainWindowBase.ui ToolBarWidget.ui
AboutDialog.cpp MainWindow.cpp main.cpp UiGuiVersion.cpp
#completed
ToolBarWidget.ui
SettingsPaths.cpp
UiGuiVersion.cpp
#ongoing
AboutDialogBase.ui
MainWindowBase.ui
AboutDialog.cpp
MainWindow.cpp
main.cpp
)
tde_add_executable( ${target} AUTOMOC

@ -63,9 +63,9 @@
///--
///-- using namespace tschweitzer;
//! \defgroup grp_MainWindow All concerning main window functionality.
// \defgroup grp_MainWindow All concerning main window functionality.
/*!
/*
\class MainWindow
\ingroup grp_MainWindow
\brief Is the main window of UniversalIndentGUI
@ -75,7 +75,7 @@
and user actions are being controlled. Is responsible for file open dialogs and indenter selection.
*/
/*!
/*
\brief Constructs the main window.
*/
MainWindow::MainWindow(TQString file2OpenOnStart, TQWidget *parent) :
@ -151,7 +151,7 @@ MainWindow::~MainWindow()
///-- _settings.clear();
}
///-- /*!
///-- /*
///-- \brief Initializes the main window by creating the main gui and make some _settings.
///-- */
void MainWindow::initMainWindow()
@ -196,9 +196,6 @@ void MainWindow::initMainWindow()
///-- {
///-- showMaximized();
///-- }
///-- #ifndef Q_OS_MAC // On Mac restoring the window state causes the screenshot no longer to work.
///-- restoreState(_settings->getValueByName("MainWindowState").toByteArray());
///-- #endif
///--
///-- // Handle if first run of this version
///-- // -----------------------------------
@ -246,7 +243,7 @@ void MainWindow::initMainWindow()
///-- _settings->registerObjectSlot(this, "updateRecentlyOpenedList()", "recentlyOpenedListSize");
}
/*!
/*
\brief Creates and inits the tool bar. It is added to the main window.
*/
void MainWindow::initToolBar()
@ -280,7 +277,7 @@ void MainWindow::initToolBar()
///-- bool)), m_toolBarWidget->cbLivePreview, SLOT(setChecked(bool)));
}
///-- /*!
///-- /*
///-- \brief Create and initialize the text editor component. It uses the TQScintilla widget.
///-- */
///-- void MainWindow::initTextEditor()
@ -340,7 +337,7 @@ void MainWindow::initToolBar()
///-- _qSciSourceCodeEditor->setTabWidth(_settings->getValueByName("tabWidth").toInt());
///-- }
///--
///-- /*!
///-- /*
///-- \brief Create and init the syntax _highlighter and set it to use the TQScintilla edit component.
///-- */
///-- void MainWindow::initSyntaxHighlighter()
@ -357,7 +354,7 @@ void MainWindow::initToolBar()
///-- "SyntaxHighlightingEnabled");
///-- }
///--
///-- /*!
///-- /*
///-- \brief Initializes the language of UniversalIndentGUI.
///--
///-- If the program language is defined in the _settings, the corresponding language
@ -426,7 +423,7 @@ void MainWindow::initToolBar()
///-- return translationFileLoaded;
///-- }
///--
///-- /*!
///-- /*
///-- \brief Creates and initializes the indenter.
///-- */
///-- void MainWindow::initIndenter()
@ -454,7 +451,7 @@ void MainWindow::initToolBar()
///-- _mainWindowForm->menuIndenter->addActions(_indentHandler->getIndenterMenuActions());
///-- }
///--
///-- /*!
///-- /*
///-- \brief Tries to load the by \a filePath defined file and returns its content as TQString.
///--
///-- If the file could not be loaded a error dialog will be shown.
@ -486,7 +483,7 @@ void MainWindow::initToolBar()
///-- return fileContent;
///-- }
/*!
/*
\brief Calls the source file open dialog to load a source file for the formatting preview.
If the file was successfully loaded the indenter will be called to generate the formatted source code.
*/
@ -536,7 +533,7 @@ void MainWindow::openSourceFileDialog(TQString fileName)
///-- }
}
/*!
/*
\brief Calls the source file save as dialog to save a source file under a chosen name.
If the file already exists and it should be overwritten, a warning is shown before.
*/
@ -588,7 +585,7 @@ bool MainWindow::saveasSourceFileDialog(TQAction *chosenEncodingAction)
return true;
}
/*!
/*
\brief Saves the currently shown source code to the last save or opened source file.
If no source file has been opened, because only the static example has been loaded,
the save as file dialog will be shown.
@ -619,7 +616,7 @@ bool MainWindow::saveSourceFile()
return true;
}
///-- /*!
///-- /*
///-- \brief Shows a file open dialog.
///--
///-- Shows a file open dialog with the title \a dialogHeaderStr starting in the directory \a startPath
@ -640,7 +637,7 @@ bool MainWindow::saveSourceFile()
///-- return fileContent;
///-- }
///--
///-- /*!
///-- /*
///-- \brief Updates the displaying of the source code.
///--
///-- Updates the text edit field, which is showing the loaded, and if preview is enabled formatted, source code.
@ -675,7 +672,7 @@ bool MainWindow::saveSourceFile()
///-- _textEditVScrollBar->setValue(_textEditLastScrollPos);
///-- }
///--
///-- /*!
///-- /*
///-- \brief Calls the selected indenter with the currently loaded source code to retrieve the formatted source code.
///--
///-- The original loaded source code file will not be changed.
@ -689,7 +686,7 @@ bool MainWindow::saveSourceFile()
///-- TQApplication::restoreOverrideCursor();
///-- }
///--
///-- /*!
///-- /*
///-- \brief Switches the syntax highlighting corresponding to the value \a turnOn either on or off.
///-- */
///-- void MainWindow::turnHighlightOnOff(bool turnOn)
@ -706,7 +703,7 @@ bool MainWindow::saveSourceFile()
///-- updateSourceView();
///-- }
///--
///-- /*!
///-- /*
///-- \brief Added this slot to avoid multiple calls because of changed text.
///-- */
///-- void MainWindow::sourceCodeChangedHelperSlot()
@ -714,7 +711,7 @@ bool MainWindow::saveSourceFile()
///-- TQTimer::singleShot(0, this, SLOT(sourceCodeChangedSlot()));
///-- }
///--
///-- /*!
///-- /*
///-- \brief Is emitted whenever the text inside the source view window changes. Calls the indenter
///-- to format the changed source code.
///-- */
@ -868,7 +865,7 @@ bool MainWindow::saveSourceFile()
///-- //_qSciSourceCodeEditor->SendScintilla(QsciScintillaBase::SCI_SETANCHOR, 50);
///-- }
///--
///-- /*!
///-- /*
///-- \brief This slot is called whenever one of the indenter _settings are changed.
///--
///-- It calls the selected indenter if the preview is turned on. If preview
@ -917,7 +914,7 @@ bool MainWindow::saveSourceFile()
///-- }
///-- }
/*!
/*
\brief This slot is called whenever the preview button is turned on or off.
It calls the selected indenter to format the current source code if
@ -960,7 +957,7 @@ void MainWindow::previewTurnedOnOff(bool turnOn)
///-- }
///-- }
///--
///-- /*!
///-- /*
///-- \brief This slot updates the main window title to show the currently opened
///-- source code filename.
///-- */
@ -970,7 +967,7 @@ void MainWindow::previewTurnedOnOff(bool turnOn)
///-- PROGRAM_VERSION_STRING) + " [*]" + _currentSourceFile);
}
/*!
/*
\brief Opens a dialog to save the current source code as a PDF document.
*/
void MainWindow::exportToPDF()
@ -994,7 +991,7 @@ void MainWindow::exportToPDF()
///-- }
}
/*!
/*
\brief Opens a dialog to save the current source code as a HTML document.
*/
void MainWindow::exportToHTML()
@ -1027,7 +1024,7 @@ void MainWindow::exportToHTML()
///-- }
}
///-- /*!
///-- /*
///-- \brief Loads the last opened file if this option is enabled in the _settings.
///--
///-- If the file does not exist, the default example file is tried to be loaded. If even that
@ -1082,7 +1079,7 @@ void MainWindow::exportToHTML()
///-- updateWindowTitle();
///-- }
///--
///-- /*!
///-- /*
///-- \brief Saves the _settings for the main application to the file "UniversalIndentGUI.ini".
///--
///-- Settings are for example last selected indenter, last loaded config file and so on.
@ -1103,7 +1100,7 @@ void MainWindow::exportToHTML()
///-- _highlighter->writeCurrentSettings("");
///-- }
///--
///-- /*!
///-- /*
///-- \brief Is always called when the program is quit. Calls the saveSettings function before really quits.
///-- */
///-- void MainWindow::closeEvent(TQCloseEvent *event)
@ -1119,7 +1116,7 @@ void MainWindow::exportToHTML()
///-- }
///-- }
///--
///-- /*!
///-- /*
///-- \brief This function is setup to capture tooltip events.
///--
///-- All widgets that are created by the _indentHandler object and are responsible
@ -1147,7 +1144,7 @@ void MainWindow::exportToHTML()
///-- }
///-- }
///--
///-- /*!
///-- /*
///-- \brief Is called at application exit and asks whether to save the source code file, if it has been changed.
///-- */
///-- bool MainWindow::maybeSave()
@ -1169,7 +1166,7 @@ void MainWindow::exportToHTML()
///-- return true;
///-- }
///--
///-- /*!
///-- /*
///-- \brief This slot is called whenever a language is selected in the menu. It tries to find the
///-- corresponding action in the languageInfoList and sets the language.
///-- */
@ -1205,7 +1202,7 @@ void MainWindow::exportToHTML()
///-- }
///-- }
///--
///-- /*!
///-- /*
///-- \brief Creates a menu entries in the file menu for opening and saving a file with different encodings.
///-- */
///-- void MainWindow::createEncodingMenu()
@ -1252,7 +1249,7 @@ void MainWindow::exportToHTML()
///-- SLOT(saveAsOtherEncoding(TQAction*)));
///-- }
///--
///-- /*!
///-- /*
///-- \brief This slot calls the save dialog to save the current source file with another encoding.
///--
///-- If the saving is successful and not aborted, the currently used encoding, visible in the
@ -1276,7 +1273,7 @@ void MainWindow::exportToHTML()
///-- }
///-- }
///--
///-- /*!
///-- /*
///-- \brief This slot is called whenever an encoding is selected in the settings menu.
///-- */
///-- void MainWindow::encodingChanged(TQAction *encodingAction)
@ -1307,7 +1304,7 @@ void MainWindow::exportToHTML()
///-- }
///-- }
///--
///-- /*!
///-- /*
///-- \brief Creates a menu entry under the settings menu for all available text encodings.
///-- */
///-- void MainWindow::createHighlighterMenu()
@ -1332,7 +1329,7 @@ void MainWindow::exportToHTML()
///-- SLOT(setHighlighterByAction(TQAction*)));
///-- }
///--
///-- /*!
///-- /*
///-- \brief Is called whenever the white space visibility is being changed in the menu.
///-- */
///-- void MainWindow::setWhiteSpaceVisibility(bool visible)
@ -1350,7 +1347,7 @@ void MainWindow::exportToHTML()
///-- }
///-- }
///--
///-- /*!
///-- /*
///-- \brief This slot is called whenever the number of lines in the editor changes
///-- and adapts the margin for the displayed line numbers.
///-- */
@ -1361,7 +1358,7 @@ void MainWindow::exportToHTML()
///-- _qSciSourceCodeEditor->setMarginWidth(1, lineNumbers);
///-- }
///--
///-- /*!
///-- /*
///-- \brief Catches language change events and retranslates all needed widgets.
///-- */
///-- void MainWindow::changeEvent(TQEvent *event)
@ -1421,7 +1418,7 @@ void MainWindow::exportToHTML()
///-- }
///-- }
/*!
/*
\brief Updates the list of recently opened files.
Therefore the currently open file is set at the lists first position
regarding the in the _settings set maximum list length. Overheads of the
@ -1517,7 +1514,7 @@ void MainWindow::updateRecentlyOpenedList()
///-- }
}
/*!
/*
\brief This slot empties the list of recently opened files.
*/
void MainWindow::clearRecentlyOpenedList()
@ -1541,7 +1538,7 @@ void MainWindow::clearRecentlyOpenedList()
///-- _mainWindowForm->actionClearRecentlyOpenedList->setEnabled(false);
}
/*!
/*
\brief This slot is called if an entry from the list of recently opened files is
being selected.
*/
@ -1582,7 +1579,7 @@ void MainWindow::openFileFromRecentlyOpenedList(int recentlyOpenedActionId)
///-- }
}
///-- /*!
///-- /*
///-- \brief If the dragged in object contains urls/paths to a file, accept the drag.
///-- */
///-- void MainWindow::dragEnterEvent(TQDragEnterEvent *event)
@ -1593,7 +1590,7 @@ void MainWindow::openFileFromRecentlyOpenedList(int recentlyOpenedActionId)
///-- }
///-- }
///--
///-- /*!
///-- /*
///-- \brief If the dropped in object contains urls/paths to a file, open that file.
///-- */
///-- void MainWindow::dropEvent(TQDropEvent *event)
@ -1607,7 +1604,7 @@ void MainWindow::openFileFromRecentlyOpenedList(int recentlyOpenedActionId)
///-- event->acceptProposedAction();
///-- }
/*!
/*
\brief Show the About dialog.
*/
void MainWindow::showAboutDialog()
@ -1620,7 +1617,7 @@ void MainWindow::showAboutDialog()
m_aboutDialog->show();
}
///-- /*!
///-- /*
///-- \brief Sets the label in the status bar to show the \a line and \a column number.
///-- */
///-- void MainWindow::setStatusBarCursorPosInfo(int line, int column)

@ -45,7 +45,7 @@ class MainWindow : public MainWindowBase
TQ_OBJECT
public:
//! Constructor
// Constructor
MainWindow(TQString file2OpenOnStart = "", TQWidget *parent = NULL);
~MainWindow();

@ -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());
}
}

@ -22,29 +22,28 @@
class TQString;
class SettingsPaths
{
public:
static void init();
static void cleanAndRemoveTempDir();
static bool getPortableMode();
static const TQString getApplicationBinaryPath();
static const TQString getSettingsPath();
static const TQString getGlobalFilesPath();
static const TQString getIndenterPath();
static const TQString getSettingsPath();
static const TQString getTempPath();
static bool getPortableMode();
static void cleanAndRemoveTempDir();
private:
SettingsPaths();
static bool _alreadyInitialized;
static TQString _applicationBinaryPath;
static TQString _settingsPath;
static TQString _globalFilesPath;
static TQString _indenterPath;
static TQString _tempPath;
static bool _portableMode;
static bool m_alreadyInitialized;
static bool m_portableMode;
static TQString m_applicationBinaryPath;
static TQString m_globalFilesPath;
static TQString m_indenterPath;
static TQString m_settingsPath;
static TQString m_tempPath;
};
#endif // SETTINGSPATHS_H
#endif

@ -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.";
}
}

@ -25,7 +25,7 @@
// -- #include "UiGuiSettings.h"
// -- #include "UiGuiSystemInfo.h"
// -- #include "IndentHandler.h"
// -- #include "SettingsPaths.h"
#include "SettingsPaths.h"
#include <tqapplication.h>
#include <tqtextcodec.h>
@ -179,7 +179,7 @@ int main(int argc, char *argv[])
// -- delete indentHandler;
delete mainWindow;
// -- SettingsPaths::cleanAndRemoveTempDir();
SettingsPaths::cleanAndRemoveTempDir();
// -- TSLogger::deleteInstance();
return returnValue;

Loading…
Cancel
Save