diff options
author | Saturneric <[email protected]> | 2023-07-13 10:19:04 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2023-07-13 10:19:04 +0000 |
commit | 0ec05be303b3d6b64c041f7c1640eabeb55c899c (patch) | |
tree | c9b25afa3fd84f3b24c1d9114e3b25df76b702d6 /src | |
parent | feat: support resotring unsaved pages after a crash (diff) | |
download | GpgFrontend-0ec05be303b3d6b64c041f7c1640eabeb55c899c.tar.gz GpgFrontend-0ec05be303b3d6b64c041f7c1640eabeb55c899c.zip |
feat: support all clear log and data files
Diffstat (limited to 'src')
-rw-r--r-- | src/core/function/GlobalSettingStation.cpp | 68 | ||||
-rw-r--r-- | src/core/function/GlobalSettingStation.h | 34 | ||||
-rw-r--r-- | src/ui/dialog/settings/SettingsGeneral.cpp | 35 |
3 files changed, 136 insertions, 1 deletions
diff --git a/src/core/function/GlobalSettingStation.cpp b/src/core/function/GlobalSettingStation.cpp index cc126ebd..6b743268 100644 --- a/src/core/function/GlobalSettingStation.cpp +++ b/src/core/function/GlobalSettingStation.cpp @@ -55,6 +55,10 @@ GpgFrontend::GlobalSettingStation::GlobalSettingStation(int channel) noexcept SPDLOG_INFO("app locale path: {}", app_locale_path_.u8string()); SPDLOG_INFO("app conf path: {}", ui_config_path_.u8string()); + SPDLOG_INFO("app log files total size: {}", GetLogFilesSize()); + SPDLOG_INFO("app data objects files total size: {}", + GetDataObjectsFilesSize()); + 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_); @@ -92,4 +96,68 @@ GpgFrontend::GlobalSettingStation::GetUISettings() noexcept { void GpgFrontend::GlobalSettingStation::init_app_secure_key() {} +int64_t GpgFrontend::GlobalSettingStation::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; +} + +std::string GpgFrontend::GlobalSettingStation::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(); +} + +std::string GpgFrontend::GlobalSettingStation::GetLogFilesSize() const { + return get_human_readable_size( + get_files_size_at_path(app_log_path_, "*.log")); +} + +std::string GpgFrontend::GlobalSettingStation::GetDataObjectsFilesSize() const { + return get_human_readable_size( + get_files_size_at_path(app_data_objs_path_, "*")); +} + +void GpgFrontend::GlobalSettingStation::ClearAllLogFiles() const { + delete_all_files(app_log_path_, "*.log"); +} + +void GpgFrontend::GlobalSettingStation::ClearAllDataObjects() const { + delete_all_files(app_data_objs_path_, "*"); +} + +void GpgFrontend::GlobalSettingStation::delete_all_files( + std::filesystem::path path, std::string filename_pattern) const { + auto dir = QDir(QString::fromStdString(path.u8string())); + + // 使用name filters来只选取以.log结尾的文件 + QStringList logFiles = dir.entryList( + QStringList() << QString::fromStdString(filename_pattern), QDir::Files); + + // 遍历并删除所有符合条件的文件 + for (const auto &file : logFiles) { + QFile::remove(dir.absoluteFilePath(file)); + } +} + GpgFrontend::GlobalSettingStation::~GlobalSettingStation() noexcept = default; diff --git a/src/core/function/GlobalSettingStation.h b/src/core/function/GlobalSettingStation.h index d70b3b63..aed99fa7 100644 --- a/src/core/function/GlobalSettingStation.h +++ b/src/core/function/GlobalSettingStation.h @@ -29,6 +29,10 @@ #ifndef GPGFRONTEND_GLOBALSETTINGSTATION_H #define GPGFRONTEND_GLOBALSETTINGSTATION_H +#include <sys/_types/_int64_t.h> + +#include <filesystem> + #include "GpgFrontendBuildInstallInfo.h" #include "core/GpgFrontendCore.h" #include "core/GpgFunctionObject.h" @@ -151,6 +155,14 @@ class GPGFRONTEND_CORE_EXPORT GlobalSettingStation return app_resource_path_ / "certs"; } + [[nodiscard]] std::string GetLogFilesSize() const; + + [[nodiscard]] std::string GetDataObjectsFilesSize() const; + + void ClearAllLogFiles() const; + + void ClearAllDataObjects() const; + /** * @brief sync the settings to the file * @@ -166,7 +178,7 @@ class GPGFRONTEND_CORE_EXPORT GlobalSettingStation std::filesystem::path app_log_path_ = app_data_path_ / "logs"; ///< Program Data Location std::filesystem::path app_data_objs_path_ = - app_data_path_ / "objs"; ///< Object storage path + app_data_path_ / "data_objs"; ///< Object storage path #ifdef LINUX_INSTALL_BUILD std::filesystem::path app_resource_path_ = @@ -200,6 +212,26 @@ class GPGFRONTEND_CORE_EXPORT GlobalSettingStation * */ void init_app_secure_key(); + + /** + * @brief + * + */ + int64_t get_files_size_at_path(std::filesystem::path path, + std::string filename_pattern) const; + + /** + * @brief + * + */ + std::string get_human_readable_size(int64_t size) const; + + /** + * @brief + * + */ + void delete_all_files(std::filesystem::path path, + std::string filename_pattern) const; }; } // namespace GpgFrontend diff --git a/src/ui/dialog/settings/SettingsGeneral.cpp b/src/ui/dialog/settings/SettingsGeneral.cpp index 719e5de8..be5190dd 100644 --- a/src/ui/dialog/settings/SettingsGeneral.cpp +++ b/src/ui/dialog/settings/SettingsGeneral.cpp @@ -63,6 +63,16 @@ GeneralTab::GeneralTab(QWidget* parent) "<b>" + QString(_("NOTE")) + _(": ") + "</b>" + _("GpgFrontend will restart automatically if you change the language!")); + ui_->dataBox->setTitle(_("Data")); + ui_->clearAllLogFilesButton->setText(QString::fromStdString( + (boost::format(_("Clear All Log (Total Size: %s)")) % + GlobalSettingStation::GetInstance().GetLogFilesSize()) + .str())); + ui_->clearAllDataObjectsButton->setText(QString::fromStdString( + (boost::format(_("Clear All Data Objects (Total Size: %s)")) % + GlobalSettingStation::GetInstance().GetDataObjectsFilesSize()) + .str())); + #ifdef MULTI_LANG_SUPPORT lang_ = SettingsDialog::ListLanguages(); for (const auto& l : lang_) { @@ -72,6 +82,31 @@ GeneralTab::GeneralTab(QWidget* parent) this, &GeneralTab::slot_language_changed); #endif + connect(ui_->clearAllLogFilesButton, &QPushButton::clicked, this, [=]() { + GlobalSettingStation::GetInstance().ClearAllLogFiles(); + ui_->clearAllLogFilesButton->setText(QString::fromStdString( + (boost::format(_("Clear All Log (Total Size: %s)")) % + GlobalSettingStation::GetInstance().GetLogFilesSize()) + .str())); + }); + + connect(ui_->clearAllDataObjectsButton, &QPushButton::clicked, this, [=]() { + QMessageBox::StandardButton reply; + reply = QMessageBox::question( + this, _("Confirm"), + _("Are you sure you want to clear all data objects?\nThis will result " + "in " + "loss of all cached form positions, statuses, key servers, etc."), + QMessageBox::Yes | QMessageBox::No); + if (reply == QMessageBox::Yes) { + GlobalSettingStation::GetInstance().ClearAllDataObjects(); + ui_->clearAllDataObjectsButton->setText(QString::fromStdString( + (boost::format(_("Clear All Data Objects (Total Size: %s)")) % + GlobalSettingStation::GetInstance().GetDataObjectsFilesSize()) + .str())); + } + }); + SetSettings(); } |