Skip to content

Commit

Permalink
Merge pull request #11332 from ronso0/playlist-export
Browse files Browse the repository at this point in the history
playlist export: update with newly selected extension, don't just append
  • Loading branch information
daschuer authored Mar 21, 2023
2 parents 420ea59 + 625c0a5 commit e9d1ed8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/util/file.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
#include "util/file.h"

#include <QFileDialog>
#include <QRegExp> // required for 'indexIn(QString &str, int pos)
#include <QRegularExpression>

namespace {

const QRegularExpression kExtractExtensionRegex(R"(\(\*\.(.*)\)$)");
// extract all extensions from the file filters string "Abc (*.m3u);;Cbx (*.pls)..."
// lazy match doesn't work here unfortunately "(\(\*\.(.*?)\))"
const QRegExp kFileFilterRegex(R"(\(\*\.(.[^\)]*)\))");

} //anonymous namespace

QString filePathWithSelectedExtension(const QString& fileLocationInput,
const QString& fileFilter) {
const QString& selectedFileFilter,
const QString& fileFilters) {
if (fileLocationInput.isEmpty()) {
return {};
}
QString fileLocation = fileLocationInput;
if (fileFilter.isEmpty()) {
if (selectedFileFilter.isEmpty()) {
return fileLocation;
}

// Extract 'ext' from QFileDialog file filter string 'Funky type (*.ext)'
const auto extMatch = kExtractExtensionRegex.match(fileFilter);
const auto extMatch = kExtractExtensionRegex.match(selectedFileFilter);
const QString ext = extMatch.captured(1);
if (ext.isNull()) {
return fileLocation;
}
const QFileInfo fileName(fileLocation);
if (!ext.isEmpty() && fileName.suffix() != ext) {
// Check if fileLocation ends with any of the available extensions
int pos = 0;
// Extract all extensions from the filter list
while ((pos = kFileFilterRegex.indexIn(fileFilters, pos)) != -1) {
if (ext == kFileFilterRegex.cap(1)) {
// If it matches chop the current extension incl. dot and break
fileLocation.chop(ext.length() + 1);
break;
}
pos += kFileFilterRegex.matchedLength();
}
fileLocation.append(".").append(ext);
}
return fileLocation;
Expand Down Expand Up @@ -54,7 +70,8 @@ QString getFilePathWithVerifiedExtensionFromFileDialog(
}
const QString fileLocationAdjusted = filePathWithSelectedExtension(
fileLocation,
selectedFileFilter);
selectedFileFilter,
fileFilters);
// If the file path has the selected suffix we can assume the user either
// selected a new file or already confirmed overwriting an existing file.
// Return the file path. Also when the adjusted file path does not exist yet.
Expand Down
3 changes: 2 additions & 1 deletion src/util/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
// Otherwise add it manually.
// Works around https://bugreports.qt.io/browse/QTBUG-27186
QString filePathWithSelectedExtension(const QString& fileLocationInput,
const QString& fileFilter);
const QString& fileFilter,
const QString& fileFilters);

// Due to Qt bug https://bugreports.qt.io/browse/QTBUG-27186 we may need to
// manually add the selected extension to the selected file name.
Expand Down

0 comments on commit e9d1ed8

Please sign in to comment.