diff options
-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 | ||||
-rw-r--r-- | ui/GeneralSettings.ui | 36 |
4 files changed, 171 insertions, 2 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(); } diff --git a/ui/GeneralSettings.ui b/ui/GeneralSettings.ui index f957a3ac..f5f8271b 100644 --- a/ui/GeneralSettings.ui +++ b/ui/GeneralSettings.ui @@ -6,7 +6,7 @@ <rect> <x>0</x> <y>0</y> - <width>643</width> + <width>645</width> <height>721</height> </rect> </property> @@ -38,6 +38,13 @@ </property> </widget> </item> + <item> + <widget class="QCheckBox" name="restoreTextEditorPageCheckBox"> + <property name="text"> + <string>Automatically restore unsaved Text Editor pages after an application crash.</string> + </property> + </widget> + </item> </layout> </item> </layout> @@ -98,6 +105,33 @@ </widget> </item> <item> + <widget class="QGroupBox" name="dataBox"> + <property name="title"> + <string>Data</string> + </property> + <layout class="QGridLayout" name="gridLayout_7"> + <item row="0" column="0"> + <layout class="QVBoxLayout" name="verticalLayout_7"> + <item> + <widget class="QPushButton" name="clearAllLogFilesButton"> + <property name="text"> + <string>Clear All Log Files</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="clearAllDataObjectsButton"> + <property name="text"> + <string>Clear All Data Objects Files</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> + </item> + <item> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> |