From 15a249eb546885a6fe69c3292000da175526c966 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 16 Jan 2021 22:35:48 +0800 Subject: [PATCH 1/2] Improve detection of file extension string --- src/base/utils/fs.cpp | 12 +++++++++--- src/gui/autoexpandabledialog.cpp | 10 ++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/base/utils/fs.cpp b/src/base/utils/fs.cpp index e48429f3f..47921ce53 100644 --- a/src/base/utils/fs.cpp +++ b/src/base/utils/fs.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include @@ -76,9 +77,14 @@ QString Utils::Fs::toUniformPath(const QString &path) */ QString Utils::Fs::fileExtension(const QString &filename) { - const QString ext = QString(filename).remove(QB_EXT); - const int pointIndex = ext.lastIndexOf('.'); - return (pointIndex >= 0) ? ext.mid(pointIndex + 1) : QString(); + const QString name = filename.endsWith(QB_EXT) +#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) + ? filename.chopped(QB_EXT.length()) +#else + ? filename.left(filename.length() - QB_EXT.length()) +#endif + : filename; + return QMimeDatabase().suffixForFileName(name); } QString Utils::Fs::fileName(const QString &filePath) diff --git a/src/gui/autoexpandabledialog.cpp b/src/gui/autoexpandabledialog.cpp index aa5f470d2..b59a0c1b1 100644 --- a/src/gui/autoexpandabledialog.cpp +++ b/src/gui/autoexpandabledialog.cpp @@ -28,6 +28,7 @@ #include "autoexpandabledialog.h" +#include "base/utils/fs.h" #include "ui_autoexpandabledialog.h" #include "utils.h" @@ -57,12 +58,9 @@ QString AutoExpandableDialog::getText(QWidget *parent, const QString &title, con d.m_ui->textEdit->selectAll(); if (excludeExtension) { - int lastDotIndex = text.lastIndexOf('.'); - if ((lastDotIndex > 3) && (text.mid(lastDotIndex - 4, 4).toLower() == ".tar")) - lastDotIndex -= 4; - // Select file name without extension, except dot files like .gitignore - if (lastDotIndex > 0) - d.m_ui->textEdit->setSelection(0, lastDotIndex); + const QString extension = Utils::Fs::fileExtension(text); + if (!extension.isEmpty()) + d.m_ui->textEdit->setSelection(0, (text.length() - extension.length() - 1)); } bool res = d.exec(); From 1479b612147240f3f60c70647adc8ce7ef038db2 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 16 Jan 2021 23:24:32 +0800 Subject: [PATCH 2/2] Simplify code for checking free disk space `QStorageInfo::bytesAvailable()` is guaranteed to return `-1` for an invalid path. https://doc.qt.io/qt-5/qstorageinfo.html#bytesAvailable --- src/base/utils/fs.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/base/utils/fs.cpp b/src/base/utils/fs.cpp index 47921ce53..2724cf510 100644 --- a/src/base/utils/fs.cpp +++ b/src/base/utils/fs.cpp @@ -273,8 +273,6 @@ bool Utils::Fs::isValidFileSystemName(const QString &name, const bool allowSepar qint64 Utils::Fs::freeDiskSpaceOnPath(const QString &path) { - if (path.isEmpty()) return -1; - return QStorageInfo(path).bytesAvailable(); }