aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/utils/FilesystemUtils.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2023-11-07 07:18:06 +0000
committersaturneric <[email protected]>2023-11-07 07:18:06 +0000
commit3ad7fecdb6458fdd6f146bed19fe643c7f93e905 (patch)
tree522f7a5dd0389ad0771d01a50ea49ef646940894 /src/core/utils/FilesystemUtils.cpp
parentrefactor: improve the code structure of core (diff)
downloadGpgFrontend-3ad7fecdb6458fdd6f146bed19fe643c7f93e905.tar.gz
GpgFrontend-3ad7fecdb6458fdd6f146bed19fe643c7f93e905.zip
refactor: remove CommonUtils at core
Diffstat (limited to 'src/core/utils/FilesystemUtils.cpp')
-rw-r--r--src/core/utils/FilesystemUtils.cpp59
1 files changed, 57 insertions, 2 deletions
diff --git a/src/core/utils/FilesystemUtils.cpp b/src/core/utils/FilesystemUtils.cpp
index edf86297..84c8caf4 100644
--- a/src/core/utils/FilesystemUtils.cpp
+++ b/src/core/utils/FilesystemUtils.cpp
@@ -30,7 +30,7 @@
namespace GpgFrontend {
-auto GetOnlyFileNameWithPath(const std::string& path) -> std::string {
+auto GetOnlyFileNameWithPath(const std::string &path) -> std::string {
// Create a path object from given string
std::filesystem::path path_obj(path);
// Check if file name in the path object has extension
@@ -42,7 +42,7 @@ auto GetOnlyFileNameWithPath(const std::string& path) -> std::string {
return {};
}
-auto GetFileExtension(const std::string& path) -> std::string {
+auto GetFileExtension(const std::string &path) -> std::string {
// Create a path object from given string
std::filesystem::path path_obj(path);
@@ -55,4 +55,59 @@ auto GetFileExtension(const std::string& path) -> std::string {
return {};
}
+/**
+ * @brief
+ *
+ */
+auto GetFileSizeByPath(const std::filesystem::path &path,
+ const std::string &filename_pattern) -> int64_t {
+ auto dir = QDir(QString::fromStdString(path.u8string()));
+ QFileInfoList file_list = dir.entryInfoList(
+ QStringList() << QString::fromStdString(filename_pattern), QDir::Files);
+ qint64 total_size = 0;
+
+ for (const QFileInfo &file_info : file_list) {
+ total_size += file_info.size();
+ }
+ return total_size;
+}
+
+/**
+ * @brief
+ *
+ */
+auto GetHumanFriendlyFileSize(int64_t size) -> std::string {
+ auto num = static_cast<double>(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 DeleteAllFilesByPattern(const std::filesystem::path &path,
+ const std::string &filename_pattern) {
+ auto dir = QDir(QString::fromStdString(path.u8string()));
+
+ QStringList log_files = dir.entryList(
+ QStringList() << QString::fromStdString(filename_pattern), QDir::Files);
+
+ for (const auto &file : log_files) {
+ QFile::remove(dir.absoluteFilePath(file));
+ }
+}
+
} // namespace GpgFrontend \ No newline at end of file