From 5d2098b4fca894c807b087ba5d715f8c222e1522 Mon Sep 17 00:00:00 2001 From: Andrey Astafyev Date: Fri, 4 Jun 2021 16:03:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/filesystem/01_paths/paths.cpp | 4 +- src/myx/filesystem/paths.cpp | 236 ++++++++++++++++--------- src/myx/filesystem/paths.hpp | 43 ++++- 3 files changed, 187 insertions(+), 96 deletions(-) diff --git a/examples/filesystem/01_paths/paths.cpp b/examples/filesystem/01_paths/paths.cpp index c41888f..ff87fff 100644 --- a/examples/filesystem/01_paths/paths.cpp +++ b/examples/filesystem/01_paths/paths.cpp @@ -18,9 +18,9 @@ int main( int argc, char** argv ) QCoreApplication::setApplicationName( QStringLiteral( CMLIB_PROJECT_NAME ) ); MF::Paths& paths = MF::Paths::instance(); - paths.init( QStringLiteral( CMLIB_PROJECT_NAME ), QStringLiteral( "conf" ) ); + paths.init(); - qDebug() << "prefixDirectory : " << paths.prefixDirectory(); + qDebug() << "prefixDirectory : " << paths.projectDirectory(); qDebug() << "executableName : " << paths.executableName(); qDebug() << "executableFilePath : " << paths.executableFilePath(); diff --git a/src/myx/filesystem/paths.cpp b/src/myx/filesystem/paths.cpp index e226499..1fed4db 100644 --- a/src/myx/filesystem/paths.cpp +++ b/src/myx/filesystem/paths.cpp @@ -8,6 +8,7 @@ #include #include +#include namespace myx { @@ -23,20 +24,20 @@ MYXLIB_INLINE Paths::Paths() } -MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultPrefixDirectory, +MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultProjectDirectory, const QString& defaultEtcDirectory, const QString& defaultConstDataDirectory, const QString& defaultVarDataDirectory, const QString& defaultLogDirectory ) { - QFileInfo prefixDirInfo { defaultPrefixDirectory }; + QFileInfo prefixDirInfo { defaultProjectDirectory }; if ( prefixDirInfo.isDir() && prefixDirInfo.isReadable() ) { - m_prefixDirectory = defaultPrefixDirectory; + m_projectDirectory = defaultProjectDirectory; } else { - m_prefixDirectory = QStringLiteral( "." ); + m_projectDirectory = QStringLiteral( "." ); } QFileInfo etcDirInfo { defaultEtcDirectory }; @@ -84,100 +85,141 @@ MYXLIB_INLINE void Paths::setupSystemDirectories( const QString& defaultPrefixDi MYXLIB_INLINE Paths::HierarchyType Paths::getHierarchyType() { QRegExp binUnityRegexp( "/s*bin/unity$" ); - auto binaryDir = m_executableDirectory; - - if ( binUnityRegexp.indexIn( binaryDir ) >= 0 ) + if ( binUnityRegexp.indexIn( m_executableDirectory ) >= 0 ) { - binaryDir.remove( binUnityRegexp ); - setupSystemDirectories( binaryDir, - binaryDir + "/etc", - binaryDir + "/share", - binaryDir + "/var", - binaryDir + "/log" ); - return ( HierarchyType::kDevelopment ); } QRegExp binRegexp( "/s*bin$" ); - if ( binRegexp.indexIn( binaryDir ) == -1 ) + if ( binRegexp.indexIn( m_executableDirectory ) == -1 ) { - m_prefixDirectory = m_executableDirectory; + return ( HierarchyType::kFlat ); + } + + QRegExp optRegexp( "^/opt(/|/.+/)" ); + if ( optRegexp.indexIn( m_executableDirectory ) >= 0 ) + { + return ( HierarchyType::kOpt ); + } + + if ( m_executableDirectory.startsWith( QStringLiteral( "/usr/local/bin" ) ) ) + { + return ( HierarchyType::kUsrLocal ); + } + + if ( m_executableDirectory.startsWith( QStringLiteral( "/usr/local" ) ) ) + { + return ( HierarchyType::kUsrLocalOrg ); + } + + if ( m_executableDirectory.startsWith( QStringLiteral( "/usr/bin" ) ) ) + { + return ( HierarchyType::kUsr ); + } + + if ( m_executableDirectory.startsWith( m_homeDirectory + "/.local/bin" ) || + m_executableDirectory.startsWith( m_homeDirectory + "/bin" ) ) + { + return( HierarchyType::kHome ); + } + + return ( HierarchyType::kDevelopment ); +} // Paths::getHierarchyType + + +MYXLIB_INLINE void Paths::calculatePaths( HierarchyType hType ) +{ + QRegExp binUnityRegexp( "/s*bin/unity$" ); + QRegExp binRegexp( "/s*bin$" ); + auto directory = m_executableDirectory; + + switch ( hType ) + { + case HierarchyType::kFlat: + m_projectDirectory = m_executableDirectory; m_systemConstDataDirectory = m_executableDirectory; m_systemVarDataDirectory = m_executableDirectory; m_systemConfigDirectory = m_executableDirectory; m_systemLogDirectory = m_executableDirectory; + break; + case HierarchyType::kOpt: + if ( m_organizationName.isEmpty() || m_themeName.isEmpty() ) + { + QRegExp parse( "opt/(.+)-(.+)/" ); + parse.setMinimal( true ); + if ( parse.indexIn( m_executableDirectory ) ) + { + m_organizationName = parse.cap( 1 ); + m_themeName = parse.cap( 2 ); + } + } - return ( HierarchyType::kFlat ); - } + if ( m_projectName.isEmpty() ) + { + QRegExp parse( "opt/.+/(.+)/" ); + parse.setMinimal( true ); + if ( parse.indexIn( m_executableDirectory ) ) + { + m_projectName = parse.cap( 1 ); + } + } + qDebug() << m_organizationName; + qDebug() << m_themeName; + qDebug() << m_projectName; + directory.remove( binRegexp ); - QRegExp optRegexp( "^/opt(/|/.+/)" + m_projectName + "/" ); - if ( optRegexp.indexIn( binaryDir ) >= 0 ) - { - binaryDir.remove( binRegexp ); - setupSystemDirectories( binaryDir, - binaryDir + "/etc", - binaryDir + "/share", - binaryDir + "/var", - binaryDir + "/log" ); + setupSystemDirectories( directory, + directory + "/etc", + directory + "/share", + directory + "/var", + directory + "/log" ); - return ( HierarchyType::kOpt ); - } - - if ( binaryDir.startsWith( QStringLiteral( "/usr/local/bin" ) ) ) - { + break; + case HierarchyType::kUsr: + setupSystemDirectories( QStringLiteral( "/usr" ), + "/etc/" + m_projectName, + "/usr/share/" + m_projectName, + "/var/lib/" + m_projectName, + "/var/log/" + m_projectName ); + break; + case HierarchyType::kUsrLocal: setupSystemDirectories( QStringLiteral( "/usr/local" ), "/usr/local/etc/" + m_projectName, "/usr/local/share/" + m_projectName, "/var/lib/" + m_projectName, "/var/log/" + m_projectName ); - return ( HierarchyType::kUsrLocal ); - } + break; + case HierarchyType::kUsrLocalOrg: + directory.remove( QRegExp( "/bin$" ) ); + setupSystemDirectories( directory, + directory + "/etc", + directory + "/share", + directory + "/var", + directory + "/log" ); - if ( binaryDir.startsWith( QStringLiteral( "/usr/local" ) ) ) - { - binaryDir.remove( QRegExp( "/bin$" ) ); - setupSystemDirectories( binaryDir, - binaryDir + "/etc", - binaryDir + "/share", - binaryDir + "/var", - binaryDir + "/log" ); - - return ( HierarchyType::kUsrLocalOrg ); - } - - if ( binaryDir.startsWith( QStringLiteral( "/usr" ) ) ) - { - setupSystemDirectories( QStringLiteral( "/usr" ), - "/etc/" + m_projectName, - "/usr/share/" + m_projectName, - "/var/lib/" + m_projectName, - "/var/log/" + m_projectName ); - - return ( HierarchyType::kUsr ); - } - - if ( binaryDir.startsWith( m_homeDirectory + "/.local/bin" ) || - binaryDir.startsWith( m_homeDirectory + "/bin" ) ) - { - m_prefixDirectory = m_homeDirectory; + break; + case HierarchyType::kHome: + m_projectDirectory = m_homeDirectory; m_systemConfigDirectory = m_userConfigDirectory; m_systemConstDataDirectory = m_userConstDataDirectory; m_systemVarDataDirectory = m_userVarDataDirectory; m_systemLogDirectory = m_userLogDirectory; + break; + case HierarchyType::kDevelopment: + directory.remove( binUnityRegexp ); + directory.remove( binRegexp ); + setupSystemDirectories( directory, + directory + "/etc", + directory + "/share", + directory + "/var", + directory + "/log" ); - return( HierarchyType::kHome ); - } - - binaryDir.remove( binRegexp ); - setupSystemDirectories( binaryDir, - binaryDir + "/etc", - binaryDir + "/share", - binaryDir + "/var", - binaryDir + "/log" ); - - return ( HierarchyType::kDevelopment ); -} // Paths::getHierarchyType + break; + case HierarchyType::kUndefined: + ; + } // switch +} // Paths::calculatePaths MYXLIB_INLINE bool Paths::initCommon() @@ -185,24 +227,20 @@ MYXLIB_INLINE bool Paths::initCommon() m_homeDirectory = QDir::homePath(); m_tempDirectory = QDir::tempPath(); - auto configHome = QString::fromLocal8Bit( qgetenv( "XDG_CONFIG_HOME" ) ); - if ( configHome.isEmpty() ) + m_configDirectory = QString::fromLocal8Bit( qgetenv( "XDG_CONFIG_HOME" ) ); + if ( m_configDirectory.isEmpty() ) { - configHome = m_homeDirectory + "/.config"; + m_configDirectory = m_homeDirectory + "/.config"; } - m_userConfigDirectory = configHome + "/" + m_projectName; - auto dataHome = QString::fromLocal8Bit( qgetenv( "XDG_DATA_HOME" ) ); - if ( dataHome.isEmpty() ) + m_dataDirectory = QString::fromLocal8Bit( qgetenv( "XDG_DATA_HOME" ) ); + if ( m_dataDirectory.isEmpty() ) { - dataHome = m_homeDirectory + "/.local/share"; + m_dataDirectory = m_homeDirectory + "/.local/share"; } - dataHome += "/" + m_projectName; - m_userConstDataDirectory = dataHome + "/share"; - m_userVarDataDirectory = dataHome + "/var"; - m_userLogDirectory = dataHome + "/log"; m_hierarchyType = getHierarchyType(); + calculatePaths( m_hierarchyType ); m_configFilePath = m_systemConfigDirectory + "/" + m_configFileName; @@ -212,7 +250,6 @@ MYXLIB_INLINE bool Paths::initCommon() MYXLIB_INLINE bool Paths::init() { - m_projectName = m_executableName; m_configFileName = m_executableName + ".conf"; return( initCommon() ); } @@ -220,7 +257,6 @@ MYXLIB_INLINE bool Paths::init() MYXLIB_INLINE bool Paths::init( const QString& configFileName ) { - m_projectName = m_executableName; m_configFileName = configFileName; return( initCommon() ); } @@ -374,6 +410,30 @@ MYXLIB_INLINE const QString& Paths::projectName() const } +MYXLIB_INLINE const QString& Paths::organizationName() const +{ + return( m_organizationName ); +} + + +MYXLIB_INLINE void Paths::setOrganizationName( QString name ) +{ + m_organizationName = name; +} + + +MYXLIB_INLINE const QString& Paths::themeName() const +{ + return( m_themeName ); +} + + +MYXLIB_INLINE void Paths::setThemeName( QString name ) +{ + m_themeName = name; +} + + MYXLIB_INLINE const QString& Paths::executableName() const { return( m_executableName ); @@ -392,9 +452,9 @@ MYXLIB_INLINE const QString& Paths::executableDirectory() const } -MYXLIB_INLINE const QString& Paths::prefixDirectory() const +MYXLIB_INLINE const QString& Paths::projectDirectory() const { - return( m_prefixDirectory ); + return( m_projectDirectory ); } } // namespace filesystem diff --git a/src/myx/filesystem/paths.hpp b/src/myx/filesystem/paths.hpp index 5c10e09..a393508 100644 --- a/src/myx/filesystem/paths.hpp +++ b/src/myx/filesystem/paths.hpp @@ -98,7 +98,7 @@ public: /** * @brief Полный путь к базовому каталогу */ - const QString& prefixDirectory() const; + const QString& projectName() const; /** * @brief Имя исполняемого файла @@ -181,9 +181,26 @@ public: const QString& homeDirectory() const; /** - * @brief Имя подкаталога для проекта + * @brief Имя каталога для работы (темы) */ - const QString& projectName() const; + const QString& themeDirectory() const; + + /** + * @brief Имя каталога для проекта + */ + const QString& projectDirectory() const; + + /** + * @brief Название организации + */ + const QString& organizationName() const; + void setOrganizationName( QString name ); + + /** + * @brief Название работы (темы) + */ + const QString& themeName() const; + void setThemeName( QString name ); protected: Paths(); @@ -193,6 +210,12 @@ private: /// @brief Тип расположения файлов по каталогам HierarchyType m_hierarchyType { HierarchyType::kFlat }; + /// @brief Название организации + QString m_organizationName; + + /// @brief Название работы (темы) + QString m_themeName; + /// @brief Имя проекта, которое используется при формировании имён файлов и каталогов QString m_projectName; @@ -200,13 +223,20 @@ private: QString m_executableFilePath; QString m_executableDirectory; - /// @brief Общий префикс для файлов проекта - QString m_prefixDirectory; + /// @brief Общий каталог для файлов работы (темы) + QString m_themeDirectory; + + /// @brief Общий каталог для файлов проекта + QString m_projectDirectory; /// @brief Путь к каталогу с временными файлами QString m_tempDirectory; /// @brief Путь к домашнему каталогу текущего пользователя QString m_homeDirectory; + /// @brief Путь к общему пользовательскому каталогу настроек + QString m_configDirectory; + /// @brief Путь к общему пользовательскому каталогу данных + QString m_dataDirectory; /// @brief Путь к пользовательскому каталогу с изменяемыми файлами QString m_userVarDataDirectory; @@ -233,13 +263,14 @@ private: /// @brief Имя файла настройки QString m_configFileName; - void setupSystemDirectories( const QString& defaultPrefixDirectory, + void setupSystemDirectories( const QString& defaultProjectDirectory, const QString& defaultEtcDirectory, const QString& defaultConstDataDirectory, const QString& defaultVarDataDirectory, const QString& defaultLogDirectory ); bool initCommon(); HierarchyType getHierarchyType(); + void calculatePaths( HierarchyType hType ); }; // class Paths } // namespace filesystem