Skip to content

Commit

Permalink
PICARD-2960: Prevent dir separator replacement being set to dir separ…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
phw committed Sep 1, 2024
1 parent e17ecfb commit 5f5945d
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions picard/ui/options/renaming_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand All @@ -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):
Expand All @@ -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'])
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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]?$')

Expand Down

0 comments on commit 5f5945d

Please sign in to comment.