diff options
Diffstat (limited to 'src/core/function/GlobalSettingStation.cpp')
-rw-r--r-- | src/core/function/GlobalSettingStation.cpp | 148 |
1 files changed, 50 insertions, 98 deletions
diff --git a/src/core/function/GlobalSettingStation.cpp b/src/core/function/GlobalSettingStation.cpp index 5a6852f7..5dc10dc1 100644 --- a/src/core/function/GlobalSettingStation.cpp +++ b/src/core/function/GlobalSettingStation.cpp @@ -28,21 +28,20 @@ #include "GlobalSettingStation.h" -#include "GpgFrontendBuildInstallInfo.h" +#include "core/utils/FilesystemUtils.h" #include "core/utils/IOUtils.h" namespace GpgFrontend { -class GlobalSettingStation::Impl { +class GlobalSettingStation::Impl + : SingletonFunctionObject<GlobalSettingStation::Impl> { public: /** * @brief Construct a new Global Setting Station object * */ - explicit Impl(int channel) noexcept { - using namespace std::filesystem; - using namespace libconfig; - + explicit Impl(int channel) noexcept + : SingletonFunctionObject<GlobalSettingStation::Impl>(channel) { SPDLOG_INFO("app path: {}", app_path_.u8string()); SPDLOG_INFO("app configure path: {}", app_configure_path_.u8string()); SPDLOG_INFO("app data path: {}", app_data_path_.u8string()); @@ -54,8 +53,9 @@ class GlobalSettingStation::Impl { SPDLOG_INFO("app data objects files total size: {}", GetDataObjectsFilesSize()); - if (!is_directory(app_configure_path_)) + if (!is_directory(app_configure_path_)) { create_directory(app_configure_path_); + } if (!is_directory(app_data_path_)) create_directory(app_data_path_); if (!is_directory(app_log_path_)) create_directory(app_log_path_); if (!is_directory(config_dir_path_)) create_directory(config_dir_path_); @@ -66,7 +66,7 @@ class GlobalSettingStation::Impl { SPDLOG_DEBUG("user interface configuration successfully written to {}", main_config_path_.u8string()); - } catch (const FileIOException &fioex) { + } catch (const libconfig::FileIOException &fioex) { SPDLOG_DEBUG( "i/o error while writing UserInterface configuration file {}", main_config_path_.u8string()); @@ -76,31 +76,34 @@ class GlobalSettingStation::Impl { this->ui_cfg_.readFile(main_config_path_.u8string().c_str()); SPDLOG_DEBUG("user interface configuration successfully read from {}", main_config_path_.u8string()); - } catch (const FileIOException &fioex) { + } catch (const libconfig::FileIOException &fioex) { SPDLOG_ERROR("i/o error while reading UserInterface configure file"); - } catch (const ParseException &pex) { + } catch (const libconfig::ParseException &pex) { SPDLOG_ERROR("parse error at {} : {} - {}", pex.getFile(), pex.getLine(), pex.getError()); } } } - libconfig::Setting &GetMainSettings() noexcept { return ui_cfg_.getRoot(); } + auto GetMainSettings() noexcept -> libconfig::Setting & { + return ui_cfg_.getRoot(); + } - std::string GetLogFilesSize() const { - return get_human_readable_size( - get_files_size_at_path(app_log_path_, "*.log")); + [[nodiscard]] auto GetLogFilesSize() const -> std::string { + return GetHumanFriendlyFileSize(GetFileSizeByPath(app_log_path_, "*.log")); } - std::string GetDataObjectsFilesSize() const { - return get_human_readable_size( - get_files_size_at_path(app_data_objs_path_, "*")); + [[nodiscard]] auto GetDataObjectsFilesSize() const -> std::string { + return GetHumanFriendlyFileSize( + GetFileSizeByPath(app_data_objs_path_, "*")); } - void ClearAllLogFiles() const { delete_all_files(app_log_path_, "*.log"); } + void ClearAllLogFiles() const { + DeleteAllFilesByPattern(app_log_path_, "*.log"); + } void ClearAllDataObjects() const { - delete_all_files(app_data_objs_path_, "*"); + DeleteAllFilesByPattern(app_data_objs_path_, "*"); } /** @@ -109,7 +112,7 @@ class GlobalSettingStation::Impl { * @return libconfig::Setting& */ template <typename T> - T LookupSettings(std::string path, T default_value) noexcept { + auto LookupSettings(std::string path, T default_value) noexcept -> T { T value = default_value; try { value = static_cast<T>(GetMainSettings().lookup(path)); @@ -124,9 +127,11 @@ class GlobalSettingStation::Impl { * * @return std::filesystem::path */ - [[nodiscard]] std::filesystem::path GetAppDir() const { return app_path_; } + [[nodiscard]] auto GetAppDir() const -> std::filesystem::path { + return app_path_; + } - [[nodiscard]] std::filesystem::path GetAppDataPath() const { + [[nodiscard]] auto GetAppDataPath() const -> std::filesystem::path { return app_data_path_; } @@ -135,7 +140,7 @@ class GlobalSettingStation::Impl { * * @return std::filesystem::path */ - [[nodiscard]] std::filesystem::path GetLogDir() const { + [[nodiscard]] auto GetLogDir() const -> std::filesystem::path { return app_log_path_; } @@ -144,7 +149,7 @@ class GlobalSettingStation::Impl { * * @return std::filesystem::path */ - [[nodiscard]] std::filesystem::path GetStandaloneDatabaseDir() const { + [[nodiscard]] auto GetStandaloneDatabaseDir() const -> std::filesystem::path { auto db_path = app_configure_path_ / "db"; if (!std::filesystem::exists(db_path)) { std::filesystem::create_directory(db_path); @@ -152,7 +157,7 @@ class GlobalSettingStation::Impl { return db_path; } - [[nodiscard]] std::filesystem::path GetAppConfigPath() const { + [[nodiscard]] auto GetAppConfigPath() const -> std::filesystem::path { return app_configure_path_; } @@ -161,7 +166,7 @@ class GlobalSettingStation::Impl { * * @return std::filesystem::path */ - [[nodiscard]] std::filesystem::path GetStandaloneGpgBinDir() const { + [[nodiscard]] auto GetStandaloneGpgBinDir() const -> std::filesystem::path { return app_resource_path_ / "gpg1.4" / "gpg"; } @@ -170,7 +175,7 @@ class GlobalSettingStation::Impl { * * @return std::filesystem::path */ - [[nodiscard]] std::filesystem::path GetLocaleDir() const { + [[nodiscard]] auto GetLocaleDir() const -> std::filesystem::path { return app_locale_path_; } @@ -179,7 +184,7 @@ class GlobalSettingStation::Impl { * * @return std::filesystem::path */ - [[nodiscard]] std::filesystem::path GetResourceDir() const { + [[nodiscard]] auto GetResourceDir() const -> std::filesystem::path { return app_resource_path_; } @@ -188,7 +193,7 @@ class GlobalSettingStation::Impl { * * @return std::filesystem::path */ - [[nodiscard]] std::filesystem::path GetCertsDir() const { + [[nodiscard]] auto GetCertsDir() const -> std::filesystem::path { return app_resource_path_ / "certs"; } @@ -197,13 +202,12 @@ class GlobalSettingStation::Impl { * */ void SyncSettings() noexcept { - using namespace libconfig; try { ui_cfg_.writeFile(main_config_path_.u8string().c_str()); SPDLOG_DEBUG("updated ui configuration successfully written to {}", main_config_path_.u8string()); - } catch (const FileIOException &fioex) { + } catch (const libconfig::FileIOException &fioex) { SPDLOG_ERROR("i/o error while writing ui configuration file: {}", main_config_path_.u8string()); } @@ -254,61 +258,6 @@ class GlobalSettingStation::Impl { * */ void init_app_secure_key() {} - - /** - * @brief - * - */ - int64_t get_files_size_at_path(std::filesystem::path path, - std::string filename_pattern) const { - auto dir = QDir(QString::fromStdString(path.u8string())); - QFileInfoList fileList = dir.entryInfoList( - QStringList() << QString::fromStdString(filename_pattern), QDir::Files); - qint64 totalSize = 0; - - for (const QFileInfo &fileInfo : fileList) { - totalSize += fileInfo.size(); - } - return totalSize; - } - - /** - * @brief - * - */ - std::string get_human_readable_size(int64_t size) const { - double num = size; - QStringList list; - list << "KB" - << "MB" - << "GB" - << "TB"; - - QStringListIterator i(list); - QString unit("bytes"); - - while (num >= 1024.0 && i.hasNext()) { - unit = i.next(); - num /= 1024.0; - } - return (QString().setNum(num, 'f', 2) + " " + unit).toStdString(); - } - - /** - * @brief - * - */ - void delete_all_files(std::filesystem::path path, - std::string filename_pattern) const { - auto dir = QDir(QString::fromStdString(path.u8string())); - - QStringList logFiles = dir.entryList( - QStringList() << QString::fromStdString(filename_pattern), QDir::Files); - - for (const auto &file : logFiles) { - QFile::remove(dir.absoluteFilePath(file)); - } - } }; GlobalSettingStation::GlobalSettingStation(int channel) noexcept @@ -319,51 +268,54 @@ GlobalSettingStation::~GlobalSettingStation() noexcept = default; void GlobalSettingStation::SyncSettings() noexcept { p_->SyncSettings(); } -libconfig::Setting &GlobalSettingStation::GetMainSettings() noexcept { +auto GlobalSettingStation::GetMainSettings() noexcept -> libconfig::Setting & { return p_->GetMainSettings(); } -std::filesystem::path GlobalSettingStation::GetAppDir() const { +auto GlobalSettingStation::GetAppDir() const -> std::filesystem::path { return p_->GetAppDir(); } -std::filesystem::path GlobalSettingStation::GetAppDataPath() const { +auto GlobalSettingStation::GetAppDataPath() const -> std::filesystem::path { return p_->GetAppDataPath(); } -[[nodiscard]] std::filesystem::path GlobalSettingStation::GetLogDir() const { +[[nodiscard]] auto GlobalSettingStation::GetLogDir() const + -> std::filesystem::path { return p_->GetLogDir(); } -std::filesystem::path GlobalSettingStation::GetStandaloneDatabaseDir() const { +auto GlobalSettingStation::GetStandaloneDatabaseDir() const + -> std::filesystem::path { return p_->GetStandaloneDatabaseDir(); } -std::filesystem::path GlobalSettingStation::GetAppConfigPath() const { +auto GlobalSettingStation::GetAppConfigPath() const -> std::filesystem::path { return p_->GetAppConfigPath(); } -std::filesystem::path GlobalSettingStation::GetStandaloneGpgBinDir() const { +auto GlobalSettingStation::GetStandaloneGpgBinDir() const + -> std::filesystem::path { return p_->GetStandaloneGpgBinDir(); } -std::filesystem::path GlobalSettingStation::GetLocaleDir() const { +auto GlobalSettingStation::GetLocaleDir() const -> std::filesystem::path { return p_->GetLocaleDir(); } -std::filesystem::path GlobalSettingStation::GetResourceDir() const { +auto GlobalSettingStation::GetResourceDir() const -> std::filesystem::path { return p_->GetResourceDir(); } -std::filesystem::path GlobalSettingStation::GetCertsDir() const { +auto GlobalSettingStation::GetCertsDir() const -> std::filesystem::path { return p_->GetCertsDir(); } -std::string GlobalSettingStation::GetLogFilesSize() const { +auto GlobalSettingStation::GetLogFilesSize() const -> std::string { return p_->GetLogFilesSize(); } -std::string GlobalSettingStation::GetDataObjectsFilesSize() const { +auto GlobalSettingStation::GetDataObjectsFilesSize() const -> std::string { return p_->GetDataObjectsFilesSize(); } |