Skip to content

Commit

Permalink
Add torrent name validator on create torrent dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
xoriole committed Aug 1, 2023
1 parent 8b5ca87 commit a137a01
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/tribler/gui/dialogs/createtorrentdialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
import re
import typing

from PyQt5 import uic
from PyQt5.QtCore import QDir, pyqtSignal
from PyQt5.QtGui import QValidator
from PyQt5.QtWidgets import QAction, QFileDialog, QSizePolicy, QTreeWidgetItem

from tribler.gui.defs import BUTTON_TYPE_NORMAL
Expand All @@ -17,6 +20,24 @@ def __init__(self, parent):
QTreeWidgetItem.__init__(self, parent)


class TorrentNameValidator(QValidator):
"""
Validator used in Torrent name QLineEdit field to disallow multiline text.
If a new line character is detected, then it is converted to space using fixup().
Docs: https://doc.qt.io/qtforpython-5/PySide2/QtGui/QValidator.html
"""
ESCAPE_CHARS_REGEX = r'[\n\r\t]'

def validate(self, text: str, pos: int) -> typing.Tuple['QValidator.State', str, int]:
if re.search(self.ESCAPE_CHARS_REGEX, text):
return QValidator.Intermediate, text, pos
return QValidator.Acceptable, text, pos

def fixup(self, text: str) -> str:
return re.sub(self.ESCAPE_CHARS_REGEX, ' ', text)


class CreateTorrentDialog(DialogContainer):
create_torrent_notification = pyqtSignal(dict)
add_to_channel_selected = pyqtSignal(str)
Expand All @@ -27,6 +48,7 @@ def __init__(self, parent):
uic.loadUi(get_ui_file_path('createtorrentdialog.ui'), self.dialog_widget)

self.dialog_widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
self.dialog_widget.create_torrent_name_field.setValidator(TorrentNameValidator(parent=self))
connect(self.dialog_widget.btn_cancel.clicked, self.close_dialog)
connect(self.dialog_widget.create_torrent_choose_files_button.clicked, self.on_choose_files_clicked)
connect(self.dialog_widget.create_torrent_choose_dir_button.clicked, self.on_choose_dir_clicked)
Expand Down
Empty file.
25 changes: 25 additions & 0 deletions src/tribler/gui/dialogs/tests/test_createtorrentdialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from PyQt5.QtGui import QValidator

from tribler.gui.dialogs.createtorrentdialog import TorrentNameValidator


def test_torrent_name_validator():
"""
Tests if the torrent name validator marks the input as valid if there are no multiline characters.
Upon fixup, the invalid characters are accepted correctly.
"""
def assert_text_is_valid(validator: QValidator, original_text: str, expected_to_be_valid: bool):
state, text, pos = validator.validate(original_text, len(original_text))
assert state == QValidator.Acceptable if expected_to_be_valid else QValidator.Intermediate
assert text == original_text
assert pos == len(original_text)

validator = TorrentNameValidator(None)

invalid_name = """line 1
line2.torrent
"""
assert_text_is_valid(validator, invalid_name, expected_to_be_valid=False)

fixed_name = validator.fixup(invalid_name)
assert_text_is_valid(validator, fixed_name, expected_to_be_valid=True)

0 comments on commit a137a01

Please sign in to comment.