From e80aa374be1c8e844761499cba31d06274166646 Mon Sep 17 00:00:00 2001 From: Helder Sepulveda Date: Sun, 28 Apr 2024 18:35:27 -0400 Subject: [PATCH 1/2] Add on_paste_event to handle pasting of magnet links --- src/tribler/gui/tests/test_utilities.py | 11 ++++++++++- src/tribler/gui/tribler_window.py | 10 ++++++++++ src/tribler/gui/utilities.py | 5 +++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/tribler/gui/tests/test_utilities.py b/src/tribler/gui/tests/test_utilities.py index bfe9cc689d1..791484d016f 100644 --- a/src/tribler/gui/tests/test_utilities.py +++ b/src/tribler/gui/tests/test_utilities.py @@ -2,12 +2,13 @@ from pathlib import Path from unittest.mock import MagicMock, Mock, patch from urllib.parse import unquote_plus +from PyQt5.QtGui import (QGuiApplication) import pytest from tribler.gui.utilities import I18N_DIR, LANGUAGES_FILE, TranslatedString, compose_magnetlink, create_api_key, \ dict_item_is_any_of, duration_to_string, format_api_key, format_size, get_i18n_file_path, \ - get_languages_file_content, quote_plus_unicode, set_api_key, unicode_quoter + get_languages_file_content, quote_plus_unicode, set_api_key, unicode_quoter, copy_to_clipboard, get_clipboard_text def test_quoter_char(): @@ -277,3 +278,11 @@ def test_format_size(): assert format_size(1) == '1.0 B' assert format_size(1.5) == '1.5 B' assert format_size(2000) == '2.0 kB' + + +def test_clipboard(): + qguiapp = QGuiApplication([]) + test_text = "123_test" + copy_to_clipboard(test_text) + assert test_text == get_clipboard_text() + qguiapp.quit() diff --git a/src/tribler/gui/tribler_window.py b/src/tribler/gui/tribler_window.py index 45ff46cb91f..51311ea4438 100644 --- a/src/tribler/gui/tribler_window.py +++ b/src/tribler/gui/tribler_window.py @@ -84,6 +84,7 @@ connect, create_api_key, format_api_key, + get_clipboard_text, get_font_path, get_gui_setting, get_image_path, @@ -221,6 +222,9 @@ def __init__( self.magnet_handler = MagnetHandler(self.window) QDesktopServices.setUrlHandler("magnet", self.magnet_handler, "on_open_magnet_link") + self.paste = QShortcut(QKeySequence("Ctrl+v"), self) + connect(self.paste.activated, self.on_paste_event) + self.debug_pane_shortcut = QShortcut(QKeySequence("Ctrl+d"), self) connect(self.debug_pane_shortcut.activated, self.clicked_debug_panel_button) @@ -447,6 +451,12 @@ def on_torrent_finished(self, torrent_info): if "hidden" not in torrent_info or not torrent_info["hidden"]: self.tray_show_message(tr("Download finished"), tr("Download of %s has finished.") % {torrent_info['name']}) + def on_paste_event(self): + txt = get_clipboard_text() + self._logger.info(f'Paste event: {txt}') + if txt.startswith("magnet:?xt=urn:btih:"): + self.start_download_from_uri(txt) + def show_loading_screen(self): self.top_menu_button.setHidden(True) self.left_menu.setHidden(True) diff --git a/src/tribler/gui/utilities.py b/src/tribler/gui/utilities.py index fbcb6c290dc..2de007d3305 100644 --- a/src/tribler/gui/utilities.py +++ b/src/tribler/gui/utilities.py @@ -379,6 +379,11 @@ def copy_to_clipboard(message): cb.setText(message, mode=cb.Clipboard) +def get_clipboard_text(): + cb = QApplication.clipboard() + return cb.text(mode=cb.Clipboard) + + def html_label(text, background="#e4e4e4", color="#222222", bold=True): style = "background-color:" + background if background else '' style = style + ";color:" + color if color else style From 0443a3377e36e91c6f38865c55ca9cf7dfd45af9 Mon Sep 17 00:00:00 2001 From: Helder Sepulveda Date: Fri, 3 May 2024 08:40:05 -0400 Subject: [PATCH 2/2] Revert changes to test_utilities.py --- src/tribler/gui/tests/test_utilities.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/tribler/gui/tests/test_utilities.py b/src/tribler/gui/tests/test_utilities.py index 791484d016f..bfe9cc689d1 100644 --- a/src/tribler/gui/tests/test_utilities.py +++ b/src/tribler/gui/tests/test_utilities.py @@ -2,13 +2,12 @@ from pathlib import Path from unittest.mock import MagicMock, Mock, patch from urllib.parse import unquote_plus -from PyQt5.QtGui import (QGuiApplication) import pytest from tribler.gui.utilities import I18N_DIR, LANGUAGES_FILE, TranslatedString, compose_magnetlink, create_api_key, \ dict_item_is_any_of, duration_to_string, format_api_key, format_size, get_i18n_file_path, \ - get_languages_file_content, quote_plus_unicode, set_api_key, unicode_quoter, copy_to_clipboard, get_clipboard_text + get_languages_file_content, quote_plus_unicode, set_api_key, unicode_quoter def test_quoter_char(): @@ -278,11 +277,3 @@ def test_format_size(): assert format_size(1) == '1.0 B' assert format_size(1.5) == '1.5 B' assert format_size(2000) == '2.0 kB' - - -def test_clipboard(): - qguiapp = QGuiApplication([]) - test_text = "123_test" - copy_to_clipboard(test_text) - assert test_text == get_clipboard_text() - qguiapp.quit()