aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2023-07-13 10:19:04 +0000
committerSaturneric <[email protected]>2023-07-13 10:19:04 +0000
commit0ec05be303b3d6b64c041f7c1640eabeb55c899c (patch)
treec9b25afa3fd84f3b24c1d9114e3b25df76b702d6 /src/core/function
parentfeat: support resotring unsaved pages after a crash (diff)
downloadGpgFrontend-0ec05be303b3d6b64c041f7c1640eabeb55c899c.tar.gz
GpgFrontend-0ec05be303b3d6b64c041f7c1640eabeb55c899c.zip
feat: support all clear log and data files
Diffstat (limited to 'src/core/function')
-rw-r--r--src/core/function/GlobalSettingStation.cpp68
-rw-r--r--src/core/function/GlobalSettingStation.h34
2 files changed, 101 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