From 6ae86a2d9a5880722e81b8cda2bed262fd6f7de5 Mon Sep 17 00:00:00 2001 From: Jannick Kremer Date: Sun, 30 Jun 2024 14:20:26 +0100 Subject: [PATCH] Fix illegal characters in filenames when saving txt This fixes #46 --- src/QUMainWindow.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/QUMainWindow.cpp b/src/QUMainWindow.cpp index bd24ee9..e9098a3 100644 --- a/src/QUMainWindow.cpp +++ b/src/QUMainWindow.cpp @@ -31,6 +31,12 @@ #include "compact_lang_det.h" #include "srtparser.h" +std::vector> FILENAME_REPLACEMENTS{ + std::make_pair(std::string("?:\""), ""), + std::make_pair(std::string("<"), "("), + std::make_pair(std::string(">"), ")"), + std::make_pair(std::string("/\\|*"), "-")}; + QUMainWindow::QUMainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::QUMainWindow) { ui->setupUi(this); @@ -375,7 +381,11 @@ void QUMainWindow::initStatusBar() { bool QUMainWindow::on_pushButton_SaveToFile_clicked() { - QString suggestedAbsoluteFilePath = QDir::toNativeSeparators(fileInfo_MP3->absolutePath()).append(QDir::separator()).append("%1 - %2.txt").arg(ui->lineEdit_Artist->text(), ui->lineEdit_Title->text()); + QString suggestedFileName = QString("%1 - %2.txt").arg(ui->lineEdit_Artist->text(), ui->lineEdit_Title->text()); + for (auto repl : FILENAME_REPLACEMENTS) + for (auto in_char : repl.first) + suggestedFileName.replace(in_char, repl.second); + QString suggestedAbsoluteFilePath = QDir::toNativeSeparators(fileInfo_MP3->absolutePath()).append(QDir::separator()).append(suggestedFileName); QString fileName = QFileDialog::getSaveFileName(this, tr("Please choose file"), suggestedAbsoluteFilePath, tr("Text files (*.txt)")); if(fileName.isEmpty())