235 lines
5.5 KiB
C++
235 lines
5.5 KiB
C++
#include "paths.hpp"
|
|
#include "config.hpp"
|
|
#include "whereami++.h"
|
|
|
|
#include <paths.h>
|
|
#include <QString>
|
|
|
|
namespace myx {
|
|
|
|
namespace filesystem {
|
|
|
|
QString Paths::executableFileName() const
|
|
{
|
|
return( m_executableFileName );
|
|
}
|
|
|
|
|
|
void Paths::setExecutableFileName( const QString& executableFileName )
|
|
{
|
|
m_executableFileName = executableFileName;
|
|
}
|
|
|
|
|
|
QString Paths::configFileName() const
|
|
{
|
|
return( m_configFileName );
|
|
}
|
|
|
|
|
|
void Paths::setConfigFileName( const QString& configFileName )
|
|
{
|
|
m_configFileName = configFileName;
|
|
}
|
|
|
|
|
|
QFileInfo Paths::executableFilePath() const
|
|
{
|
|
return( m_executableFilePath );
|
|
}
|
|
|
|
|
|
void Paths::setExecutableFilePath( const QFileInfo& executableFilePath )
|
|
{
|
|
m_executableFilePath = executableFilePath;
|
|
}
|
|
|
|
|
|
QFileInfo Paths::configFilePath() const
|
|
{
|
|
return( m_configFilePath );
|
|
}
|
|
|
|
|
|
void Paths::setConfigFilePath( const QFileInfo& configFilePath )
|
|
{
|
|
m_configFilePath = configFilePath;
|
|
}
|
|
|
|
|
|
Paths::Paths() :
|
|
m_prefixDirectory( "/opt/" ORGANIZATION_NAME_LOWER "/" PROJECT_NAME_LOWER ),
|
|
m_binaryDirectory( m_prefixDirectory.absolutePath() + "/bin" ),
|
|
m_configDirectory( m_prefixDirectory.absolutePath() + "/etc" ),
|
|
m_cacheDirectory( m_prefixDirectory.absolutePath() + "/var" ),
|
|
m_tempDirectory( QString::fromLocal8Bit( qgetenv( qPrintable( "TMPDIR" ) ) ) ),
|
|
m_dataDirectory( m_prefixDirectory.absolutePath() + "/share" ),
|
|
m_homeDirectory( QString::fromLocal8Bit( qgetenv( qPrintable( "HOME" ) ) ) ),
|
|
m_executableFileName( PROJECT_NAME_LOWER ),
|
|
m_configFileName( PROJECT_NAME_LOWER ".conf" ),
|
|
m_executableFilePath( m_binaryDirectory.absolutePath() + "/" + m_executableFileName ),
|
|
m_configFilePath( m_binaryDirectory.absolutePath() + "/" + m_configFileName )
|
|
{
|
|
if ( m_tempDirectory.absolutePath().isEmpty() || ( m_tempDirectory.path() == "." ) )
|
|
{
|
|
m_tempDirectory = _PATH_TMP;
|
|
}
|
|
}
|
|
|
|
|
|
QDir Paths::prefixDirectory() const
|
|
{
|
|
return( m_prefixDirectory );
|
|
}
|
|
|
|
|
|
void Paths::setPrefixDirectory( const QString& prefixDirectory )
|
|
{
|
|
m_prefixDirectory = prefixDirectory;
|
|
}
|
|
|
|
|
|
QDir Paths::binaryDirectory() const
|
|
{
|
|
return( m_binaryDirectory );
|
|
}
|
|
|
|
|
|
void Paths::setBinaryDirectory( const QString& binaryDirectory )
|
|
{
|
|
m_binaryDirectory = binaryDirectory;
|
|
}
|
|
|
|
|
|
QDir Paths::configDirectory() const
|
|
{
|
|
return( m_configDirectory );
|
|
}
|
|
|
|
|
|
void Paths::setConfigDirectory( const QString& configDirectory )
|
|
{
|
|
m_configDirectory = configDirectory;
|
|
}
|
|
|
|
|
|
QDir Paths::cacheDirectory() const
|
|
{
|
|
return( m_cacheDirectory );
|
|
}
|
|
|
|
|
|
void Paths::setCacheDirectory( const QString& cacheDirectory )
|
|
{
|
|
m_cacheDirectory = cacheDirectory;
|
|
}
|
|
|
|
|
|
QDir Paths::tempDirectory() const
|
|
{
|
|
return( m_tempDirectory );
|
|
}
|
|
|
|
|
|
void Paths::setTempDirectory( const QString& tempDirectory )
|
|
{
|
|
m_tempDirectory = tempDirectory;
|
|
}
|
|
|
|
|
|
QDir Paths::dataDirectory() const
|
|
{
|
|
return( m_dataDirectory );
|
|
}
|
|
|
|
|
|
void Paths::setDataDirectory( const QString& dataDirectory )
|
|
{
|
|
m_dataDirectory = dataDirectory;
|
|
}
|
|
|
|
|
|
bool Paths::updatePaths()
|
|
{
|
|
whereami::whereami_path_t executablePath = whereami::getExecutablePath();
|
|
|
|
m_executableFileName = QString::fromStdString( executablePath.basename() );
|
|
m_binaryDirectory = QString::fromStdString( executablePath.dirname() );
|
|
m_executableFilePath = QFile( m_binaryDirectory.absolutePath() + "/" + m_executableFileName );
|
|
|
|
if ( m_binaryDirectory.absolutePath().endsWith( "/bin" ) )
|
|
{
|
|
m_prefixDirectory = m_binaryDirectory.absolutePath().remove( QRegExp( "/bin$" ) );
|
|
m_configDirectory = m_prefixDirectory.absolutePath() + "/etc";
|
|
m_cacheDirectory = m_prefixDirectory.absolutePath() + "/var";
|
|
m_dataDirectory = m_prefixDirectory.absolutePath() + "/share";
|
|
m_configFilePath = QFile( m_configDirectory.absolutePath() + "/" + PROJECT_NAME_LOWER + ".conf" );
|
|
}
|
|
|
|
if ( m_prefixDirectory.absolutePath().startsWith( "/opt" ) ||
|
|
m_prefixDirectory.absolutePath().startsWith( "/usr" ) )
|
|
{
|
|
QString dataDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_DATA_HOME" ) ) );
|
|
if ( dataDirectory.isEmpty() )
|
|
{
|
|
dataDirectory = m_homeDirectory.absolutePath() + ".local/share/";
|
|
}
|
|
m_dataDirectory = dataDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER;
|
|
|
|
QString configDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_CONFIG_HOME" ) ) );
|
|
if ( configDirectory.isEmpty() )
|
|
{
|
|
configDirectory = m_homeDirectory.absolutePath() + ".config/";
|
|
}
|
|
m_configDirectory = configDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER;
|
|
|
|
QString cacheDirectory = QString::fromLocal8Bit( qgetenv( qPrintable( "XDG_CACHE_HOME" ) ) );
|
|
if ( cacheDirectory.isEmpty() )
|
|
{
|
|
cacheDirectory = m_homeDirectory.absolutePath() + ".cache/";
|
|
}
|
|
m_cacheDirectory = cacheDirectory + ORGANIZATION_NAME_LOWER + "/" + PROJECT_NAME_LOWER;
|
|
}
|
|
|
|
return( true );
|
|
} // Paths::updatePaths
|
|
|
|
|
|
bool Paths::makePaths()
|
|
{
|
|
bool status = true;
|
|
|
|
if ( !m_dataDirectory.mkpath( m_dataDirectory.absolutePath() ) ) { status = false; }
|
|
if ( !m_configDirectory.mkpath( m_configDirectory.absolutePath() ) ) { status = false; }
|
|
if ( !m_cacheDirectory.mkpath( m_cacheDirectory.absolutePath() ) ) { status = false; }
|
|
return( status );
|
|
}
|
|
|
|
|
|
QString Paths::findConfigFile( const QString& defaultConfigFile )
|
|
{
|
|
if ( QFileInfo( defaultConfigFile ).isReadable() )
|
|
{
|
|
m_configFilePath = defaultConfigFile;
|
|
return( defaultConfigFile );
|
|
}
|
|
|
|
auto fileName = QString::fromLocal8Bit( qgetenv( qPrintable( PROJECT_NAME_UPPER "_CONFIG" ) ) );
|
|
if ( QFileInfo( fileName ).isReadable() )
|
|
{
|
|
m_configFilePath = fileName;
|
|
return( fileName );
|
|
}
|
|
|
|
if ( QFileInfo( m_configFilePath ).isReadable() )
|
|
{
|
|
return( m_configFilePath.absoluteFilePath() );
|
|
}
|
|
|
|
return( QString() );
|
|
} // Paths::findConfigFile
|
|
|
|
} // namespace filesystem
|
|
|
|
} // namespace myx
|