diff --git a/picard/ui/options/renaming_compat.py b/picard/ui/options/renaming_compat.py index d204c2a044..9a386b3b7c 100644 --- a/picard/ui/options/renaming_compat.py +++ b/picard/ui/options/renaming_compat.py @@ -4,7 +4,7 @@ # # Copyright (C) 2006-2008, 2011 Lukáš Lalinský # Copyright (C) 2008-2009 Nikolai Prokoschenko -# Copyright (C) 2009-2010, 2014-2015, 2018-2022 Philipp Wolfer +# Copyright (C) 2009-2010, 2014-2015, 2018-2024 Philipp Wolfer # Copyright (C) 2011-2013 Michael Wiencek # Copyright (C) 2011-2013 Wieland Hoffmann # Copyright (C) 2013 Calvin Walton @@ -32,7 +32,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - +import os import re from PyQt6 import ( @@ -56,7 +56,10 @@ Ui_RenamingCompatOptionsPage, ) from picard.ui.forms.ui_win_compat_dialog import Ui_WinCompatDialog -from picard.ui.options import OptionsPage +from picard.ui.options import ( + OptionsCheckError, + OptionsPage, +) class RenamingCompatOptionsPage(OptionsPage): @@ -80,6 +83,7 @@ def __init__(self, parent=None): self.ui.windows_long_paths.toggled.connect(self.on_options_changed) self.ui.replace_spaces_with_underscores.toggled.connect(self.on_options_changed) self.ui.replace_dir_separator.textChanged.connect(self.on_options_changed) + self.ui.replace_dir_separator.setValidator(NoDirectorySeparatorValidator()) self.ui.btn_windows_compatibility_change.clicked.connect(self.open_win_compat_dialog) self.register_setting('ascii_filenames', ['ascii_filenames']) @@ -113,6 +117,14 @@ def save(self): for key, value in options.items(): config.setting[key] = value + def check(self): + (valid_state, _text, _pos) = self.ui.replace_dir_separator.validator().validate(self.ui.replace_dir_separator.text(), 0) + if valid_state != QtGui.QValidator.State.Acceptable: + raise OptionsCheckError( + _("Invalid directory separator replacement"), + _("The replacement for directory separators must not be itself a directory separator.") + ) + def toggle_windows_long_paths(self, state): if state and not system_supports_long_paths(): dialog = QtWidgets.QMessageBox( @@ -147,6 +159,15 @@ def open_win_compat_dialog(self): self.on_options_changed() +class NoDirectorySeparatorValidator(QtGui.QValidator): + def validate(self, text: str, pos): + if os.sep in text or (os.altsep and os.altsep in text): + state = QtGui.QValidator.State.Invalid + else: + state = QtGui.QValidator.State.Acceptable + return state, text, pos + + class WinCompatReplacementValidator(QtGui.QValidator): _re_valid_win_replacement = re.compile(r'^[^"*:<>?|/\\\s]?$')