Add option to remove problematic filename characters

This commit is contained in:
Jonas Kvinge 2020-04-09 19:59:31 +02:00
parent 8f4056faa6
commit 4dd78d89a0
5 changed files with 51 additions and 29 deletions

View File

@ -84,7 +84,11 @@ OrganiseDialog::OrganiseDialog(TaskManager *task_manager, CollectionBackend *bac
setWindowFlags(windowFlags()|Qt::WindowMaximizeButtonHint);
connect(ui_->button_box->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(Reset()));
QPushButton *button_save = ui_->button_box->addButton("Save settings", QDialogButtonBox::ApplyRole);
connect(button_save, SIGNAL(clicked()), SLOT(SaveSettings()));
button_save->setIcon(IconLoader::Load("document-save"));
ui_->button_box->button(QDialogButtonBox::RestoreDefaults)->setIcon(IconLoader::Load("edit-undo"));
connect(ui_->button_box->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()), SLOT(RestoreDefaults()));
ui_->aftercopying->setItemIcon(1, IconLoader::Load("edit-delete"));
@ -115,6 +119,7 @@ OrganiseDialog::OrganiseDialog(TaskManager *task_manager, CollectionBackend *bac
connect(ui_->destination, SIGNAL(currentIndexChanged(int)), SLOT(UpdatePreviews()));
connect(ui_->naming, SIGNAL(textChanged()), SLOT(UpdatePreviews()));
connect(ui_->remove_problematic, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->remove_non_fat, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->remove_non_ascii, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
connect(ui_->allow_ascii_ext, SIGNAL(toggled(bool)), SLOT(UpdatePreviews()));
@ -164,6 +169,7 @@ void OrganiseDialog::closeEvent(QCloseEvent*) {
void OrganiseDialog::accept() {
SaveGeometry();
SaveSettings();
const QModelIndex destination = ui_->destination->model()->index(ui_->destination->currentIndex(), 0);
@ -181,8 +187,6 @@ void OrganiseDialog::accept() {
organise->Start();
SaveGeometry();
QDialog::accept();
}
@ -232,11 +236,29 @@ void OrganiseDialog::SaveGeometry() {
}
void OrganiseDialog::RestoreDefaults() {
ui_->naming->setPlainText(kDefaultFormat);
ui_->remove_problematic->setChecked(true);
ui_->remove_non_fat->setChecked(false);
ui_->remove_non_ascii->setChecked(false);
ui_->allow_ascii_ext->setChecked(false);
ui_->replace_spaces->setChecked(true);
ui_->overwrite->setChecked(false);
ui_->mark_as_listened->setChecked(false);
ui_->albumcover->setChecked(true);
ui_->eject_after->setChecked(false);
SaveSettings();
}
void OrganiseDialog::LoadSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);
ui_->naming->setPlainText(s.value("format", kDefaultFormat).toString());
ui_->remove_problematic->setChecked(s.value("remove_problematic", true).toBool());
ui_->remove_non_fat->setChecked(s.value("remove_non_fat", false).toBool());
ui_->remove_non_ascii->setChecked(s.value("remove_non_ascii", false).toBool());
ui_->allow_ascii_ext->setChecked(s.value("allow_ascii_ext", false).toBool());
@ -263,6 +285,7 @@ void OrganiseDialog::SaveSettings() {
QSettings s;
s.beginGroup(kSettingsGroup);
s.setValue("format", ui_->naming->toPlainText());
s.setValue("remove_problematic", ui_->remove_problematic->isChecked());
s.setValue("remove_non_fat", ui_->remove_non_fat->isChecked());
s.setValue("remove_non_ascii", ui_->remove_non_ascii->isChecked());
s.setValue("allow_ascii_ext", ui_->allow_ascii_ext->isChecked());
@ -434,6 +457,7 @@ void OrganiseDialog::UpdatePreviews() {
// Update the format object
format_.set_format(ui_->naming->toPlainText());
format_.set_remove_problematic(ui_->remove_problematic->isChecked());
format_.set_remove_non_fat(ui_->remove_non_fat->isChecked());
format_.set_remove_non_ascii(ui_->remove_non_ascii->isChecked());
format_.set_allow_ascii_ext(ui_->allow_ascii_ext->isChecked());
@ -465,20 +489,6 @@ void OrganiseDialog::UpdatePreviews() {
QSize OrganiseDialog::sizeHint() const { return QSize(650, 0); }
void OrganiseDialog::Reset() {
ui_->naming->setPlainText(kDefaultFormat);
ui_->remove_non_fat->setChecked(false);
ui_->remove_non_ascii->setChecked(false);
ui_->allow_ascii_ext->setChecked(false);
ui_->replace_spaces->setChecked(true);
ui_->overwrite->setChecked(false);
ui_->mark_as_listened->setChecked(false);
ui_->albumcover->setChecked(true);
ui_->eject_after->setChecked(false);
}
void OrganiseDialog::OrganiseFinished(const QStringList files_with_errors, const QStringList log) {
if (files_with_errors.isEmpty()) return;

View File

@ -85,7 +85,6 @@ class OrganiseDialog : public QDialog {
void LoadGeometry();
void SaveGeometry();
void LoadSettings();
void SaveSettings();
SongList LoadSongsBlocking(const QStringList &filenames);
void SetLoadingSongs(bool loading);
@ -98,7 +97,8 @@ class OrganiseDialog : public QDialog {
void reject();
private slots:
void Reset();
void SaveSettings();
void RestoreDefaults();
void InsertTag(const QString &tag);
void UpdatePreviews();

View File

@ -91,6 +91,13 @@
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="remove_problematic">
<property name="text">
<string>Remove problematic characters from filenames</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="remove_non_fat">
<property name="text">
@ -242,11 +249,8 @@
</item>
<item>
<widget class="QDialogButtonBox" name="button_box">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults</set>
</property>
</widget>
</item>

View File

@ -70,6 +70,7 @@ const QStringList OrganiseFormat::kKnownTags = QStringList() << "title"
<< "lyrics";
const QRegExp OrganiseFormat::kInvalidDirCharacters("[/\\\\]");
const QRegExp OrganiseFormat::kProblematicCharacters("[:?*\"<>|]");
// From http://en.wikipedia.org/wiki/8.3_filename#Directory_table
const QRegExp OrganiseFormat::kInvalidFatCharacters("[^a-zA-Z0-9!#\\$%&'()\\-@\\^_`{}~/. ]");
@ -86,6 +87,7 @@ const QRgb OrganiseFormat::SyntaxHighlighter::kBlockColorDark = qRgb(64, 64, 64)
OrganiseFormat::OrganiseFormat(const QString &format)
: format_(format),
remove_problematic_(false),
remove_non_fat_(false),
remove_non_ascii_(false),
allow_ascii_ext_(false),
@ -116,8 +118,9 @@ QString OrganiseFormat::GetFilenameForSong(const Song &song) const {
filename = Utilities::PathWithoutFilenameExtension(filename) + song.basefilename();
}
if (remove_problematic_) filename = filename.remove(kProblematicCharacters);
if (remove_non_fat_ || (remove_non_ascii_ && !allow_ascii_ext_)) filename = Utilities::UnicodeToAscii(filename);
if (remove_non_fat_) filename.remove(kInvalidFatCharacters);
if (remove_non_fat_) filename = filename.remove(kInvalidFatCharacters);
if (remove_non_ascii_) {
int ascii = 128;
@ -151,7 +154,7 @@ QString OrganiseFormat::GetFilenameForSong(const Song &song) const {
QString part = parts_old[i];
for (int j = 0 ; j < kInvalidPrefixCharactersCount ; ++j) {
if (part.startsWith(kInvalidPrefixCharacters[j])) {
part.remove(0, 1);
part = part.remove(0, 1);
break;
}
}
@ -262,6 +265,7 @@ QString OrganiseFormat::TagValue(const QString &tag, const Song &song) const {
// Replace characters that really shouldn't be in paths
value = value.remove(kInvalidDirCharacters);
if (remove_problematic_) value = value.remove('.');
value = value.trimmed();
return value;

View File

@ -44,22 +44,25 @@ class OrganiseFormat {
static const char *kBlockPattern;
static const QStringList kKnownTags;
static const QRegExp kInvalidDirCharacters;
static const QRegExp kProblematicCharacters;
static const QRegExp kInvalidFatCharacters;
static const char kInvalidPrefixCharacters[];
static const int kInvalidPrefixCharactersCount;
QString format() const { return format_; }
bool remove_problematic() const { return remove_problematic_; }
bool remove_non_fat() const { return remove_non_fat_; }
bool remove_non_ascii() const { return remove_non_ascii_; }
bool allow_ascii_ext() const { return allow_ascii_ext_; }
bool replace_spaces() const { return replace_spaces_; }
void set_format(const QString &v);
void set_remove_non_fat(bool v) { remove_non_fat_ = v; }
void set_remove_non_ascii(bool v) { remove_non_ascii_ = v; }
void set_allow_ascii_ext(bool v) { allow_ascii_ext_ = v; }
void set_replace_spaces(bool v) { replace_spaces_ = v; }
void set_remove_problematic(const bool v) { remove_problematic_ = v; }
void set_remove_non_fat(const bool v) { remove_non_fat_ = v; }
void set_remove_non_ascii(const bool v) { remove_non_ascii_ = v; }
void set_allow_ascii_ext(const bool v) { allow_ascii_ext_ = v; }
void set_replace_spaces(const bool v) { replace_spaces_ = v; }
bool IsValid() const;
QString GetFilenameForSong(const Song &song) const;
@ -90,6 +93,7 @@ class OrganiseFormat {
QString TagValue(const QString &tag, const Song &song) const;
QString format_;
bool remove_problematic_;
bool remove_non_fat_;
bool remove_non_ascii_;
bool allow_ascii_ext_;