Рефакторинг 2

This commit is contained in:
Andrei Astafev 2020-04-22 10:17:37 +03:00
parent 44817a8a94
commit 4e2cff180c
21 changed files with 665 additions and 535 deletions

View File

@ -13,6 +13,14 @@ else()
endif()
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/find)
if(NOT DEFINED MYXLIB_MASTER_PROJECT)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(MYXLIB_MASTER_PROJECT ON)
else()
set(MYXLIB_MASTER_PROJECT OFF)
endif()
endif()
include(CMLibCommon)
option(MYXLIB_HEADER_ONLY "Build header only version of library" ON)
@ -31,18 +39,13 @@ find_package(
COMPONENTS
Core
Network
Gui
Widgets
DBus
Concurrent
Sql
REQUIRED)
# Автоматически генерируемый заголовочный файл
cmlib_config_hpp_generate()
# cmlib_config_hpp_generate()
configure_file(${CMAKE_SOURCE_DIR}/src/myx/base/config_flags.hpp.in
${CMAKE_BINARY_DIR}/include/myx/base/config_flags.hpp)
# configure_file(${CMAKE_SOURCE_DIR}/src/myx/base/config_flags.hpp.in
# ${CMAKE_BINARY_DIR}/include/myx/base/config_flags.hpp)
# Подключение внешних проектов
include(ExternalProject)

View File

@ -27,13 +27,18 @@ add_pvs_check(${TRGT})
# Создание цели для автоматического форматирования кода
add_format_sources(${TRGT} ${TRGT_cpp})
add_dependencies(${TRGT} base_static)
add_dependencies(${TRGT} filesystem_static)
target_include_directories(${TRGT} PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_link_libraries(${TRGT} myx-filesystem)
if(NOT MYXLIB_HEADER_ONLY)
add_dependencies(${TRGT} base_static filesystem_static)
target_link_libraries(${TRGT} myx-filesystem)
else()
target_compile_definitions(${TRGT} PUBLIC MYXLIB_HEADER_ONLY)
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
add_dependencies(${TRGT} base filesystem)
endif()
target_link_libraries(${TRGT} Qt5::Core)
target_link_libraries(${TRGT} Threads::Threads)
@ -48,4 +53,4 @@ cotire(${TRGT})
add_dependencies(${TRGT} create_auxilary_symlinks)
# Правила для установки
install(TARGETS ${TRGT} COMPONENT EXAMPLES RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS ${TRGT} COMPONENT examples RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@ -1,5 +1,4 @@
#include "cmlib_private_config.hpp"
// #include <myx/base/cmlib_private_config.hpp>
#include <myx/base/config.hpp>
#include <myx/filesystem/paths.hpp>
@ -8,12 +7,15 @@
#include <QCoreApplication>
#include <QDebug>
#define CMLIB_PROJECT_NAME "myxlib"
namespace MF = myx::filesystem;
// Переменные для защиты экземпляра класса MF::PathsMT
std::atomic< MF::PathsMT* > MF::PathsMT::mInstance;
std::mutex MF::PathsMT::mMutex;
int main( int argc, char** argv )
{
(void)argc;

View File

@ -33,14 +33,19 @@ add_pvs_check(${TRGT})
# Создание цели для автоматического форматирования кода
add_format_sources(${TRGT} ${TRGT_cpp})
add_dependencies(${TRGT} base_static)
add_dependencies(${TRGT} qt_static)
# Qt5
target_include_directories(${TRGT} PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_link_libraries(${TRGT} myx-qt)
if(NOT MYXLIB_HEADER_ONLY)
add_dependencies(${TRGT} base_static qt_static)
target_link_libraries(${TRGT} myx-qt)
else()
target_compile_definitions(${TRGT} PUBLIC MYXLIB_HEADER_ONLY)
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
add_dependencies(${TRGT} base qt)
endif()
target_link_libraries(${TRGT} Qt5::Core)
# Имя выходного файла для цели
@ -54,4 +59,4 @@ cotire(${TRGT})
add_dependencies(${TRGT} create_auxilary_symlinks)
# Правила для установки
install(TARGETS ${TRGT} COMPONENT EXAMPLES RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(TARGETS ${TRGT} COMPONENT examples RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@ -16,8 +16,15 @@ set(TRGT_hpp
set(TRGT_headers ${TRGT_hpp})
# cmake-format: on
add_common_library(${TRGT} OUTPUT_NAME myx-${TRGT} SOURCES ${TRGT_cpp} ${TRGT_headers})
common_target_properties(${TRGT})
if(MYXLIB_HEADER_ONLY)
add_library(${TRGT} INTERFACE)
else()
add_common_library(
${TRGT}
OUTPUT_NAME myx-${TRGT}
SOURCES ${TRGT_cpp} ${TRGT_headers})
common_target_properties(${TRGT})
endif()
# Создание цели для проверки утилитой clang-tidy
add_clang_tidy_check(${TRGT} ${TRGT_cpp} ${TRGT_headers})
@ -34,19 +41,38 @@ add_pvs_check(${TRGT})
# Создание цели для автоматического форматирования кода
add_format_sources(${TRGT} ${TRGT_cpp} ${TRGT_headers})
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
if(MYXLIB_HEADER_ONLY)
target_include_directories(${TRGT} SYSTEM INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
else()
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
cotire(${TRGT})
cotire(${TRGT})
install(
TARGETS ${TRGT}_static
COMPONENT libs-dev
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(
TARGETS ${TRGT}_shared
COMPONENT main
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
endif()
install(
FILES ${TRGT_headers}
${CMAKE_BINARY_DIR}/include/myx/base/compiler_features.hpp
COMPONENT headers
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${TRGT})
install(
FILES ${CMAKE_BINARY_DIR}/${TRGT}.pc
COMPONENT headers
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
# Цель, используемая только для установки заголовочных файлов без компиляции проекта
add_custom_target(${TRGT}-install-headers COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=DEV -P
"${CMAKE_BINARY_DIR}/cmake_install.cmake")
# Правила для установки
install(TARGETS ${TRGT}_static COMPONENT DEV ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(TARGETS ${TRGT}_shared COMPONENT DEV LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
install(FILES ${CMAKE_BINARY_DIR}/include/config_flags.hpp ${TRGT_headers} COMPONENT DEV
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${TRGT})
install(FILES ${CMAKE_BINARY_DIR}/${TRGT}.pc COMPONENT DEV DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
add_custom_target(
${TRGT}-install-headers COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=headers -P
"${CMAKE_BINARY_DIR}/cmake_install.cmake")

View File

@ -3,7 +3,7 @@
#pragma once
#include <myx/base/config_flags.hpp>
// #include <myx/base/config_flags.hpp>
#ifdef MYXLIB_HEADER_ONLY
#include "config-inl.hpp"

View File

@ -3,22 +3,31 @@ set(TRGT filesystem)
# cmake-format: off
# Список файлов исходных текстов
if(NOT MYXLIB_HEADER_ONLY)
set(TRGT_cpp
${CMAKE_CURRENT_SOURCE_DIR}/current_executable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/paths.cpp
${CMAKE_CURRENT_SOURCE_DIR}/paths_mt.cpp)
endif()
# Список заголовочных файлов
set(TRGT_hpp
${CMAKE_CURRENT_SOURCE_DIR}/current_executable.hpp
${CMAKE_CURRENT_SOURCE_DIR}/paths.hpp
${CMAKE_CURRENT_SOURCE_DIR}/paths_mt.hpp)
${CMAKE_CURRENT_SOURCE_DIR}/paths_mt.hpp
${CMAKE_CURRENT_SOURCE_DIR}/current_executable-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/paths-inl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/paths_mt-inl.hpp)
set(TRGT_headers ${TRGT_hpp})
# cmake-format: on
add_common_library(${TRGT} OUTPUT_NAME myx-${TRGT} SOURCES ${TRGT_cpp} ${TRGT_headers})
common_target_properties(${TRGT})
if(NOT MYXLIB_HEADER_ONLY)
add_common_library(${TRGT} OUTPUT_NAME myx-${TRGT} SOURCES ${TRGT_cpp} ${TRGT_headers})
common_target_properties(${TRGT})
else()
add_library(${TRGT} INTERFACE)
endif()
# Создание цели для проверки утилитой clang-tidy
add_clang_tidy_check(${TRGT} ${TRGT_cpp} ${TRGT_headers})
@ -35,19 +44,24 @@ add_pvs_check(${TRGT})
# Создание цели для автоматического форматирования кода
add_format_sources(${TRGT} ${TRGT_cpp} ${TRGT_headers})
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
if(NOT MYXLIB_HEADER_ONLY)
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
cotire(${TRGT})
cotire(${TRGT})
install(TARGETS ${TRGT}_static COMPONENT libs-dev ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(TARGETS ${TRGT}_shared COMPONENT main LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
else()
target_include_directories(${TRGT} SYSTEM INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
endif()
install(FILES ${TRGT_headers} COMPONENT DEV DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${TRGT})
install(FILES ${CMAKE_BINARY_DIR}/${TRGT}.pc COMPONENT DEV DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
# Цель, используемая только для установки заголовочных файлов без компиляции проекта
add_custom_target(${TRGT}-install-headers COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=DEV -P
"${CMAKE_BINARY_DIR}/cmake_install.cmake")
# Правила для установки
install(TARGETS ${TRGT}_static COMPONENT DEV ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(TARGETS ${TRGT}_shared COMPONENT DEV LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
install(FILES ${TRGT_headers} COMPONENT DEV DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${TRGT})
install(FILES ${CMAKE_BINARY_DIR}/${TRGT}.pc COMPONENT DEV DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

View File

@ -0,0 +1,41 @@
#ifndef MYX_BASE_CURRENT_EXECUTABLE_INL_HPP_
#define MYX_BASE_CURRENT_EXECUTABLE_INL_HPP_
#pragma once
#ifndef MYXLIB_HEADER_ONLY
#include <myx/filesystem/current_executable.hpp>
#endif
#include <myx/base/config.hpp>
#include <paths.h>
#include <QCoreApplication>
#include <QString>
namespace myx {
namespace filesystem {
#if !defined ( __linux__ )
error "Class CurrentExecutable is supported only in Linux"
#endif
CurrentExecutable::CurrentExecutable() :
m_procFilePath( QStringLiteral( "/proc/self/exe" ) )
{
m_canonicalFilePath = m_procFilePath.canonicalFilePath();
}
const QFileInfo& CurrentExecutable::canonicalFilePath() const
{
return( m_canonicalFilePath );
}
} // namespace filesystem
} // namespace myx
#endif // MYX_BASE_CURRENT_EXECUTABLE_INL_HPP_

View File

@ -1,31 +1,2 @@
#include <myx/base/config.hpp>
#include <myx/filesystem/current_executable.hpp>
#include <paths.h>
#include <QCoreApplication>
#include <QString>
namespace myx {
namespace filesystem {
#if !defined ( __linux__ )
error "Class CurrentExecutable is supported only in Linux"
#endif
CurrentExecutable::CurrentExecutable() :
m_procFilePath( QStringLiteral( "/proc/self/exe" ) )
{
m_canonicalFilePath = m_procFilePath.canonicalFilePath();
}
const QFileInfo& CurrentExecutable::canonicalFilePath() const
{
return( m_canonicalFilePath );
}
} // namespace filesystem
} // namespace myx
#include <myx/filesystem/current_executable-inl.hpp>

View File

@ -42,4 +42,8 @@ public:
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "current_executable-inl.hpp"
#endif
#endif // MYX_FILESYSTEM_CURRENT_EXECUTABLE_HPP_

View File

@ -0,0 +1,359 @@
#ifndef MYX_BASE_PATHS_INL_HPP_
#define MYX_BASE_PATHS_INL_HPP_
#pragma once
#ifndef MYXLIB_HEADER_ONLY
#include <myx/filesystem/paths.hpp>
#endif
#include <myx/base/config.hpp>
#include <myx/filesystem/current_executable.hpp>
#include <paths.h>
#include <QCoreApplication>
#include <QString>
namespace myx {
namespace filesystem {
Paths::Paths() = default;
Paths::HierarchyType Paths::getHierarchyType()
{
QRegExp binUnityRegexp( "/s*bin/unity$" );
QRegExp binRegexp( "/s*bin$" );
auto binaryDir = m_currentExecutable.m_canonicalFilePath.canonicalPath();
if ( binUnityRegexp.indexIn( binaryDir ) >= 0 )
{
binaryDir.remove( binUnityRegexp );
QFileInfo etcDirInfo { binaryDir + "/etc" };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { binaryDir + "/files/data" };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { binaryDir + "/files/lib" };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { binaryDir + "/files/log" };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kDevelopment );
}
if ( binRegexp.indexIn( binaryDir ) == -1 )
{
return ( HierarchyType::kFlat );
}
QRegExp optRegexp( "^/opt(/|/.+/)" + m_projectName + "/" );
if ( optRegexp.indexIn( binaryDir ) >= 0 )
{
binaryDir.remove( binRegexp );
QFileInfo etcDirInfo { binaryDir + "/etc" };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { binaryDir + "/files/data" };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { binaryDir + "/files/lib" };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { binaryDir + "/files/log" };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kOpt );
}
if ( binaryDir.startsWith( QStringLiteral( "/usr/local" ) ) )
{
QFileInfo etcDirInfo { "/usr/local/etc/" + m_projectName };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { "/usr/local/share/" + m_projectName };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { "/var/lib/" + m_projectName };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { "/var/log/" + m_projectName };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kUsr );
}
if ( binaryDir.startsWith( QStringLiteral( "/usr" ) ) )
{
QFileInfo etcDirInfo { "/etc/" + m_projectName };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { "/usr/share/" + m_projectName };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { "/var/lib/" + m_projectName };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { "/var/log/" + m_projectName };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kUsr );
}
if ( binaryDir.startsWith( m_homeDirectory.canonicalPath() + "/.local/bin" ) ||
binaryDir.startsWith( m_homeDirectory.canonicalPath() + "/bin" ) )
{
QFileInfo etcDirInfo { m_userConfigDirectory.canonicalPath() };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { m_userConstDataDirectory.canonicalPath() };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { m_userVarDataDirectory.canonicalPath() };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { m_userLogDirectory.canonicalPath() };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return( HierarchyType::kHome );
}
binaryDir.remove( binRegexp );
QFileInfo etcDirInfo { binaryDir + "/etc" };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { binaryDir + "/files/data" };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { binaryDir + "/files/lib" };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { binaryDir + "/files/log" };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kDevelopment );
} // Paths::getHierarchyType
bool Paths::init( const QString& projectDir, const QString& configFileExtension )
{
m_projectName = projectDir.isEmpty() ? m_currentExecutable.m_canonicalFilePath.fileName()
: projectDir;
m_configFileExtension = configFileExtension.isEmpty() ? QStringLiteral( "conf" )
: configFileExtension;
m_configFileName = m_projectName + "." + m_configFileExtension;
m_homeDirectory = QDir::homePath();
m_tempDirectory = QDir::tempPath();
auto configHome = QString::fromLocal8Bit( qgetenv( "XDG_CONFIG_HOME" ) );
if ( configHome.isEmpty() )
{
configHome = m_homeDirectory.canonicalPath() + "/.config";
}
m_userConfigDirectory = configHome + "/" + m_projectName;
auto dataHome = QString::fromLocal8Bit( qgetenv( "XDG_DATA_HOME" ) );
if ( dataHome.isEmpty() )
{
dataHome = m_homeDirectory.canonicalPath() + "/.local/share";
}
dataHome += "/" + m_projectName;
m_userConstDataDirectory = dataHome + "/data";
m_userVarDataDirectory = dataHome + "/lib";
m_userLogDirectory = dataHome + "/log";
m_hierarchyType = getHierarchyType();
if ( m_hierarchyType == HierarchyType::kFlat )
{
m_systemConstDataDirectory = m_currentExecutable.m_canonicalFilePath.canonicalPath();
m_systemVarDataDirectory = m_currentExecutable.m_canonicalFilePath.canonicalPath();
m_systemConfigDirectory = m_currentExecutable.m_canonicalFilePath.canonicalPath();
m_systemLogDirectory = m_currentExecutable.m_canonicalFilePath.canonicalPath();
}
m_configFilePath = m_systemConfigDirectory.canonicalPath() + "/" + m_configFileName;
return( true );
} // Paths::updatePaths
bool Paths::makeDefaultDirectories()
{
bool status = true;
if ( !m_userConfigDirectory.mkpath( m_userConfigDirectory.absolutePath() ) ) { status = false; }
if ( !m_userVarDataDirectory.mkpath( m_userVarDataDirectory.absolutePath() ) ) { status = false; }
if ( !m_userConstDataDirectory.mkpath( m_userConstDataDirectory.absolutePath() ) ) { status = false; }
if ( !m_userLogDirectory.mkpath( m_userLogDirectory.absolutePath() ) ) { status = false; }
return( status );
}
QString Paths::findConfigFile( const QString& defaultConfigFile )
{
if ( !defaultConfigFile.isEmpty() && QFileInfo( defaultConfigFile ).isReadable() )
{
m_configFilePath = defaultConfigFile;
return( defaultConfigFile );
}
auto fileName = QString::fromLocal8Bit( qgetenv( QCoreApplication::applicationName()
.toUpper().toUtf8() + "_CONFIG" ) );
if ( QFileInfo( fileName ).isReadable() )
{
m_configFilePath = fileName;
return( fileName );
}
if ( QFileInfo( m_configFilePath ).isReadable() )
{
return( m_configFilePath.absoluteFilePath() );
}
return( QString() );
} // Paths::findConfigFile
QDir Paths::executableFileDirectory() const
{
return( m_currentExecutable.m_canonicalFilePath.dir() );
}
const QDir& Paths::userConfigDirectory() const
{
return( m_userConfigDirectory );
}
const QDir& Paths::systemConfigDirectory() const
{
return( m_systemConfigDirectory );
}
const QFileInfo& Paths::configFilePath() const
{
return( m_configFilePath );
}
const QString& Paths::configFileName() const
{
return( m_configFileName );
}
const QString& Paths::configFileExtension() const
{
return( m_configFileExtension );
}
const QDir& Paths::userVarDataDirectory() const
{
return( m_userVarDataDirectory );
}
const QDir& Paths::systemVarDataDirectory() const
{
return( m_systemVarDataDirectory );
}
const QDir& Paths::userConstDataDirectory() const
{
return( m_userConstDataDirectory );
}
const QDir& Paths::systemConstDataDirectory() const
{
return( m_systemConstDataDirectory );
}
const QDir& Paths::userLogDirectory() const
{
return( m_userLogDirectory );
}
const QDir& Paths::systemLogDirectory() const
{
return( m_systemLogDirectory );
}
const QDir& Paths::tempDirectory() const
{
return( m_tempDirectory );
}
const QDir& Paths::homeDirectory() const
{
return( m_homeDirectory );
}
const QString& Paths::projectName() const
{
return( m_projectName );
}
QString Paths::executableFileName() const
{
return( m_currentExecutable.m_canonicalFilePath.fileName() );
}
const QFileInfo& Paths::executableFilePath() const
{
return( m_currentExecutable.m_canonicalFilePath );
}
} // namespace filesystem
} // namespace myx
#endif // MYX_BASE_PATHS_INL_HPP_

View File

@ -1,349 +1,2 @@
#include <myx/base/config.hpp>
#include <myx/filesystem/current_executable.hpp>
#include <myx/filesystem/paths.hpp>
#include <paths.h>
#include <QCoreApplication>
#include <QString>
namespace myx {
namespace filesystem {
Paths::Paths() = default;
Paths::HierarchyType Paths::getHierarchyType()
{
QRegExp binUnityRegexp( "/s*bin/unity$" );
QRegExp binRegexp( "/s*bin$" );
auto binaryDir = m_currentExecutable.m_canonicalFilePath.canonicalPath();
if ( binUnityRegexp.indexIn( binaryDir ) >= 0 )
{
binaryDir.remove( binUnityRegexp );
QFileInfo etcDirInfo { binaryDir + "/etc" };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { binaryDir + "/files/data" };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { binaryDir + "/files/lib" };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { binaryDir + "/files/log" };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kDevelopment );
}
if ( binRegexp.indexIn( binaryDir ) == -1 )
{
return ( HierarchyType::kFlat );
}
QRegExp optRegexp( "^/opt(/|/.+/)" + m_projectName + "/" );
if ( optRegexp.indexIn( binaryDir ) >= 0 )
{
binaryDir.remove( binRegexp );
QFileInfo etcDirInfo { binaryDir + "/etc" };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { binaryDir + "/files/data" };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { binaryDir + "/files/lib" };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { binaryDir + "/files/log" };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kOpt );
}
if ( binaryDir.startsWith( QStringLiteral( "/usr/local" ) ) )
{
QFileInfo etcDirInfo { "/usr/local/etc/" + m_projectName };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { "/usr/local/share/" + m_projectName };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { "/var/lib/" + m_projectName };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { "/var/log/" + m_projectName };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kUsr );
}
if ( binaryDir.startsWith( QStringLiteral( "/usr" ) ) )
{
QFileInfo etcDirInfo { "/etc/" + m_projectName };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { "/usr/share/" + m_projectName };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { "/var/lib/" + m_projectName };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { "/var/log/" + m_projectName };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kUsr );
}
if ( binaryDir.startsWith( m_homeDirectory.canonicalPath() + "/.local/bin" ) ||
binaryDir.startsWith( m_homeDirectory.canonicalPath() + "/bin" ) )
{
QFileInfo etcDirInfo { m_userConfigDirectory.canonicalPath() };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { m_userConstDataDirectory.canonicalPath() };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { m_userVarDataDirectory.canonicalPath() };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { m_userLogDirectory.canonicalPath() };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return( HierarchyType::kHome );
}
binaryDir.remove( binRegexp );
QFileInfo etcDirInfo { binaryDir + "/etc" };
if ( !etcDirInfo.isDir() || !etcDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo constDataDirInfo { binaryDir + "/files/data" };
if ( !constDataDirInfo.isDir() || !constDataDirInfo.isReadable() ) { return( HierarchyType::kFlat ); }
QFileInfo varDataDirInfo { binaryDir + "/files/lib" };
if ( !varDataDirInfo.isDir() || !varDataDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
QFileInfo logDirInfo { binaryDir + "/files/log" };
if ( !logDirInfo.isDir() || !logDirInfo.isWritable() ) { return( HierarchyType::kFlat ); }
m_systemConfigDirectory = etcDirInfo.canonicalFilePath();
m_systemConstDataDirectory = constDataDirInfo.canonicalFilePath();
m_systemVarDataDirectory = varDataDirInfo.canonicalFilePath();
m_systemLogDirectory = logDirInfo.canonicalFilePath();
return ( HierarchyType::kDevelopment );
} // Paths::getHierarchyType
bool Paths::init( const QString& projectDir, const QString& configFileExtension )
{
m_projectName = projectDir.isEmpty() ? m_currentExecutable.m_canonicalFilePath.fileName()
: projectDir;
m_configFileExtension = configFileExtension.isEmpty() ? QStringLiteral( "conf" )
: configFileExtension;
m_configFileName = m_projectName + "." + m_configFileExtension;
m_homeDirectory = QDir::homePath();
m_tempDirectory = QDir::tempPath();
auto configHome = QString::fromLocal8Bit( qgetenv( "XDG_CONFIG_HOME" ) );
if ( configHome.isEmpty() )
{
configHome = m_homeDirectory.canonicalPath() + "/.config";
}
m_userConfigDirectory = configHome + "/" + m_projectName;
auto dataHome = QString::fromLocal8Bit( qgetenv( "XDG_DATA_HOME" ) );
if ( dataHome.isEmpty() )
{
dataHome = m_homeDirectory.canonicalPath() + "/.local/share";
}
dataHome += "/" + m_projectName;
m_userConstDataDirectory = dataHome + "/data";
m_userVarDataDirectory = dataHome + "/lib";
m_userLogDirectory = dataHome + "/log";
m_hierarchyType = getHierarchyType();
if ( m_hierarchyType == HierarchyType::kFlat )
{
m_systemConstDataDirectory = m_currentExecutable.m_canonicalFilePath.canonicalPath();
m_systemVarDataDirectory = m_currentExecutable.m_canonicalFilePath.canonicalPath();
m_systemConfigDirectory = m_currentExecutable.m_canonicalFilePath.canonicalPath();
m_systemLogDirectory = m_currentExecutable.m_canonicalFilePath.canonicalPath();
}
m_configFilePath = m_systemConfigDirectory.canonicalPath() + "/" + m_configFileName;
return( true );
} // Paths::updatePaths
bool Paths::makeDefaultDirectories()
{
bool status = true;
if ( !m_userConfigDirectory.mkpath( m_userConfigDirectory.absolutePath() ) ) { status = false; }
if ( !m_userVarDataDirectory.mkpath( m_userVarDataDirectory.absolutePath() ) ) { status = false; }
if ( !m_userConstDataDirectory.mkpath( m_userConstDataDirectory.absolutePath() ) ) { status = false; }
if ( !m_userLogDirectory.mkpath( m_userLogDirectory.absolutePath() ) ) { status = false; }
return( status );
}
QString Paths::findConfigFile( const QString& defaultConfigFile )
{
if ( !defaultConfigFile.isEmpty() && QFileInfo( defaultConfigFile ).isReadable() )
{
m_configFilePath = defaultConfigFile;
return( defaultConfigFile );
}
auto fileName = QString::fromLocal8Bit( qgetenv( QCoreApplication::applicationName()
.toUpper().toUtf8() + "_CONFIG" ) );
if ( QFileInfo( fileName ).isReadable() )
{
m_configFilePath = fileName;
return( fileName );
}
if ( QFileInfo( m_configFilePath ).isReadable() )
{
return( m_configFilePath.absoluteFilePath() );
}
return( QString() );
} // Paths::findConfigFile
QDir Paths::executableFileDirectory() const
{
return( m_currentExecutable.m_canonicalFilePath.dir() );
}
const QDir& Paths::userConfigDirectory() const
{
return( m_userConfigDirectory );
}
const QDir& Paths::systemConfigDirectory() const
{
return( m_systemConfigDirectory );
}
const QFileInfo& Paths::configFilePath() const
{
return( m_configFilePath );
}
const QString& Paths::configFileName() const
{
return( m_configFileName );
}
const QString& Paths::configFileExtension() const
{
return( m_configFileExtension );
}
const QDir& Paths::userVarDataDirectory() const
{
return( m_userVarDataDirectory );
}
const QDir& Paths::systemVarDataDirectory() const
{
return( m_systemVarDataDirectory );
}
const QDir& Paths::userConstDataDirectory() const
{
return( m_userConstDataDirectory );
}
const QDir& Paths::systemConstDataDirectory() const
{
return( m_systemConstDataDirectory );
}
const QDir& Paths::userLogDirectory() const
{
return( m_userLogDirectory );
}
const QDir& Paths::systemLogDirectory() const
{
return( m_systemLogDirectory );
}
const QDir& Paths::tempDirectory() const
{
return( m_tempDirectory );
}
const QDir& Paths::homeDirectory() const
{
return( m_homeDirectory );
}
const QString& Paths::projectName() const
{
return( m_projectName );
}
QString Paths::executableFileName() const
{
return( m_currentExecutable.m_canonicalFilePath.fileName() );
}
const QFileInfo& Paths::executableFilePath() const
{
return( m_currentExecutable.m_canonicalFilePath );
}
} // namespace filesystem
} // namespace myx
#include <myx/filesystem/paths-inl.hpp>

View File

@ -218,4 +218,8 @@ public:
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "paths-inl.hpp"
#endif
#endif // MYX_FILESYSTEM_PATHS_HPP_

View File

@ -0,0 +1,44 @@
#ifndef MYX_BASE_PATHS_MT_INL_HPP_
#define MYX_BASE_PATHS_MT_INL_HPP_
#pragma once
#ifndef MYXLIB_HEADER_ONLY
#include <myx/filesystem/paths_mt.hpp>
#endif
#include <myx/base/config.hpp>
#include <myx/filesystem/current_executable.hpp>
#include <paths.h>
#include <QCoreApplication>
#include <QString>
namespace myx {
namespace filesystem {
PathsMT::PathsMT() = default;
PathsMT& PathsMT::instance()
{
volatile PathsMT* localInstance = mInstance.load( std::memory_order_acquire );
if ( localInstance == nullptr )
{
std::lock_guard< std::mutex > myLock( mMutex );
localInstance = mInstance.load( std::memory_order_relaxed );
if ( localInstance == nullptr ) // -V1036
{
localInstance = new PathsMT();
mInstance.store( const_cast< PathsMT* >( localInstance ), std::memory_order_release ); // NOLINT
}
}
return( const_cast< PathsMT& >( *localInstance ) ); // NOLINT
}
} // namespace filesystem
} // namespace myx
#endif // MYX_BASE_PATHS_MT_INL_HPP_

View File

@ -1,34 +1,2 @@
#include <myx/base/config.hpp>
#include <myx/filesystem/current_executable.hpp>
#include <myx/filesystem/paths_mt.hpp>
#include <paths.h>
#include <QCoreApplication>
#include <QString>
namespace myx {
namespace filesystem {
PathsMT::PathsMT() = default;
PathsMT& PathsMT::instance()
{
volatile PathsMT* localInstance = mInstance.load( std::memory_order_acquire );
if ( localInstance == nullptr )
{
std::lock_guard< std::mutex > myLock( mMutex );
localInstance = mInstance.load( std::memory_order_relaxed );
if ( localInstance == nullptr ) // -V1036
{
localInstance = new PathsMT();
mInstance.store( const_cast< PathsMT* >( localInstance ), std::memory_order_release ); // NOLINT
}
}
return( const_cast< PathsMT& >( *localInstance ) ); // NOLINT
}
} // namespace filesystem
} // namespace myx
#include <myx/filesystem/paths_mt-inl.hpp>

View File

@ -47,4 +47,8 @@ public:
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "paths_mt-inl.hpp"
#endif
#endif // MYX_FILESYSTEM_PATHS_MT_HPP_

View File

@ -3,19 +3,26 @@ set(TRGT qt)
# cmake-format: off
# Список файлов исходных текстов
if(NOT MYXLIB_HEADER_ONLY)
set(TRGT_cpp
${CMAKE_CURRENT_SOURCE_DIR}/translators.cpp)
endif()
# Список заголовочных файлов (используется для установки)
set(TRGT_hpp
${CMAKE_CURRENT_SOURCE_DIR}/backports.hpp
${CMAKE_CURRENT_SOURCE_DIR}/translators.hpp)
${CMAKE_CURRENT_SOURCE_DIR}/translators.hpp
${CMAKE_CURRENT_SOURCE_DIR}/translators-inl.hpp)
set(TRGT_headers ${TRGT_hpp})
# cmake-format: on
add_common_library(${TRGT} OUTPUT_NAME myx-${TRGT} SOURCES ${TRGT_cpp} ${TRGT_headers})
common_target_properties(${TRGT})
if(NOT MYXLIB_HEADER_ONLY)
add_common_library(${TRGT} OUTPUT_NAME myx-${TRGT} SOURCES ${TRGT_cpp} ${TRGT_headers})
common_target_properties(${TRGT})
else()
add_library(${TRGT} INTERFACE)
endif()
# Создание цели для проверки утилитой clang-tidy
add_clang_tidy_check(${TRGT} ${TRGT_cpp} ${TRGT_headers})
@ -32,19 +39,24 @@ add_pvs_check(${TRGT})
# Создание цели для автоматического форматирования кода
add_format_sources(${TRGT} ${TRGT_cpp} ${TRGT_headers})
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
if(NOT MYXLIB_HEADER_ONLY)
target_include_directories(${TRGT} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_include_directories(${TRGT} SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/src)
cotire(${TRGT})
cotire(${TRGT})
install(TARGETS ${TRGT}_static COMPONENT libs-dev ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(TARGETS ${TRGT}_shared COMPONENT main LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
else()
target_include_directories(${TRGT} SYSTEM INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
endif()
install(FILES ${TRGT_headers} COMPONENT headers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${TRGT})
install(FILES ${CMAKE_BINARY_DIR}/${TRGT}.pc COMPONENT headers DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
# Цель, используемая только для установки заголовочных файлов без компиляции проекта
add_custom_target(${TRGT}-install-headers COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=DEV -P
add_custom_target(${TRGT}-install-headers COMMAND "${CMAKE_COMMAND}" -DCMAKE_INSTALL_COMPONENT=headers -P
"${CMAKE_BINARY_DIR}/cmake_install.cmake")
# Правила для установки
install(TARGETS ${TRGT}_static COMPONENT DEV ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(TARGETS ${TRGT}_shared COMPONENT DEV LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
install(FILES ${TRGT_headers} COMPONENT DEV DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${TRGT})
install(FILES ${CMAKE_BINARY_DIR}/${TRGT}.pc COMPONENT DEV DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

View File

@ -0,0 +1,67 @@
#ifndef MYX_QT_TRANSLATORS_INL_HPP_
#define MYX_QT_TRANSLATORS_INL_HPP_
#pragma once
#ifndef MYXLIB_HEADER_ONLY
#include <myx/qt/translators.hpp>
#endif
#include <myx/base/config.hpp>
#include <QCoreApplication>
#include <QLibraryInfo>
#include <QLocale>
namespace myx {
namespace qt {
void append_translators( QTranslatorsList& translators, const QString& appName )
{
auto* translator = new QTranslator( qApp );
if ( translator->load( QLocale(),
appName, QStringLiteral( "_" ), QStringLiteral( ":/qm" ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
translator = new QTranslator( qApp );
if ( translator->load( QLocale::system(),
QStringLiteral( "qt" ), QStringLiteral( "_" ),
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
translator = new QTranslator( qApp );
if ( translator->load( "qtbase_" + QLocale::system().name(),
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
for ( auto* i : qAsConst( translators ) )
{
qApp->installTranslator( i );
}
} // append_translators
} // namespace qt
} // namespace myx
#endif // MYX_QT_TRANSLATORS_INL_HPP_

View File

@ -1,58 +1,2 @@
#include "translators.hpp"
#include <myx/base/config.hpp>
#include <QCoreApplication>
#include <QLibraryInfo>
#include <QLocale>
namespace myx {
namespace qt {
void append_translators( QTranslatorsList& translators, const QString& appName )
{
auto* translator = new QTranslator( qApp );
if ( translator->load( QLocale(),
appName, QStringLiteral( "_" ), QStringLiteral( ":/qm" ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
translator = new QTranslator( qApp );
if ( translator->load( QLocale::system(),
QStringLiteral( "qt" ), QStringLiteral( "_" ),
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
translator = new QTranslator( qApp );
if ( translator->load( "qtbase_" + QLocale::system().name(),
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
for ( auto* i : qAsConst( translators ) )
{
qApp->installTranslator( i );
}
} // append_translators
} // namespace qt
} // namespace myx
#include <myx/qt/translators.hpp>
#include <myx/qt/translators-inl.hpp>

View File

@ -17,4 +17,8 @@ void append_translators( QTranslatorsList& translators, const QString& appName )
} // namespace myx
#ifdef MYXLIB_HEADER_ONLY
#include "translators-inl.hpp"
#endif
#endif // ifndef MYX_LOG_QT_TRANSLATORS_HPP_

View File

@ -9,6 +9,6 @@
// #define MYX_REDIS_EXPORT Q_DECL_IMPORT
// #endif
#include <myx/base/config_flags.hpp>
// #include <myx/base/config_flags.hpp>
#endif // MYX_REDIS_CONFIG_HPP_