Skip to content

Commit a137a01

Browse files
committed
Add torrent name validator on create torrent dialog
1 parent 8b5ca87 commit a137a01

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/tribler/gui/dialogs/createtorrentdialog.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import os
2+
import re
3+
import typing
24

35
from PyQt5 import uic
46
from PyQt5.QtCore import QDir, pyqtSignal
7+
from PyQt5.QtGui import QValidator
58
from PyQt5.QtWidgets import QAction, QFileDialog, QSizePolicy, QTreeWidgetItem
69

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

1922

23+
class TorrentNameValidator(QValidator):
24+
"""
25+
Validator used in Torrent name QLineEdit field to disallow multiline text.
26+
If a new line character is detected, then it is converted to space using fixup().
27+
28+
Docs: https://doc.qt.io/qtforpython-5/PySide2/QtGui/QValidator.html
29+
"""
30+
ESCAPE_CHARS_REGEX = r'[\n\r\t]'
31+
32+
def validate(self, text: str, pos: int) -> typing.Tuple['QValidator.State', str, int]:
33+
if re.search(self.ESCAPE_CHARS_REGEX, text):
34+
return QValidator.Intermediate, text, pos
35+
return QValidator.Acceptable, text, pos
36+
37+
def fixup(self, text: str) -> str:
38+
return re.sub(self.ESCAPE_CHARS_REGEX, ' ', text)
39+
40+
2041
class CreateTorrentDialog(DialogContainer):
2142
create_torrent_notification = pyqtSignal(dict)
2243
add_to_channel_selected = pyqtSignal(str)
@@ -27,6 +48,7 @@ def __init__(self, parent):
2748
uic.loadUi(get_ui_file_path('createtorrentdialog.ui'), self.dialog_widget)
2849

2950
self.dialog_widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
51+
self.dialog_widget.create_torrent_name_field.setValidator(TorrentNameValidator(parent=self))
3052
connect(self.dialog_widget.btn_cancel.clicked, self.close_dialog)
3153
connect(self.dialog_widget.create_torrent_choose_files_button.clicked, self.on_choose_files_clicked)
3254
connect(self.dialog_widget.create_torrent_choose_dir_button.clicked, self.on_choose_dir_clicked)

src/tribler/gui/dialogs/tests/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from PyQt5.QtGui import QValidator
2+
3+
from tribler.gui.dialogs.createtorrentdialog import TorrentNameValidator
4+
5+
6+
def test_torrent_name_validator():
7+
"""
8+
Tests if the torrent name validator marks the input as valid if there are no multiline characters.
9+
Upon fixup, the invalid characters are accepted correctly.
10+
"""
11+
def assert_text_is_valid(validator: QValidator, original_text: str, expected_to_be_valid: bool):
12+
state, text, pos = validator.validate(original_text, len(original_text))
13+
assert state == QValidator.Acceptable if expected_to_be_valid else QValidator.Intermediate
14+
assert text == original_text
15+
assert pos == len(original_text)
16+
17+
validator = TorrentNameValidator(None)
18+
19+
invalid_name = """line 1
20+
line2.torrent
21+
"""
22+
assert_text_is_valid(validator, invalid_name, expected_to_be_valid=False)
23+
24+
fixed_name = validator.fixup(invalid_name)
25+
assert_text_is_valid(validator, fixed_name, expected_to_be_valid=True)

0 commit comments

Comments
 (0)