1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-27 23:14:31 +00:00

Merge pull request #14229 from Chocobo1/ext

Improve detection of file extension string
This commit is contained in:
Mike Tzou 2021-01-18 12:43:54 +08:00 committed by GitHub
commit 2b9c7e04a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 11 deletions

View File

@ -55,6 +55,7 @@
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QStorageInfo>
#include <QRegularExpression>
@ -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)
@ -267,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();
}

View File

@ -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();