From 9a5bd1f1774f68a42cea1850abe32e26e58e432d Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 10 Feb 2025 17:13:44 +1100 Subject: [PATCH 1/2] Make it possible to automatically start a persistent share when OnionShare starts --- cli/onionshare_cli/mode_settings.py | 6 +++++- desktop/onionshare/resources/locale/en.json | 3 ++- desktop/onionshare/tab/mode/__init__.py | 2 ++ .../tab/mode/mode_settings_widget.py | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cli/onionshare_cli/mode_settings.py b/cli/onionshare_cli/mode_settings.py index c920ab66..cfd9971c 100644 --- a/cli/onionshare_cli/mode_settings.py +++ b/cli/onionshare_cli/mode_settings.py @@ -40,7 +40,11 @@ class ModeSettings: "client_auth_priv_key": None, "client_auth_pub_key": None, }, - "persistent": {"mode": None, "enabled": False}, + "persistent": { + "mode": None, + "enabled": False, + "autostart_on_launch": False + }, "general": { "title": None, "public": False, diff --git a/desktop/onionshare/resources/locale/en.json b/desktop/onionshare/resources/locale/en.json index 287f3b00..d279d206 100644 --- a/desktop/onionshare/resources/locale/en.json +++ b/desktop/onionshare/resources/locale/en.json @@ -214,7 +214,8 @@ "mode_settings_advanced_toggle_show": "Show advanced settings", "mode_settings_advanced_toggle_hide": "Hide advanced settings", "mode_settings_title_label": "Custom title", - "mode_settings_persistent_checkbox": "Always open this tab when OnionShare is started", + "mode_settings_persistent_checkbox": "Always open this tab when OnionShare is started (the onion address will stay the same)", + "mode_settings_persistent_autostart_on_launch_checkbox": "Automatically start this onion service when OnionShare starts", "mode_settings_public_checkbox": "This is a public OnionShare service (disables private key)", "mode_settings_autostart_timer_checkbox": "Start onion service at scheduled time", "mode_settings_autostop_timer_checkbox": "Stop onion service at scheduled time", diff --git a/desktop/onionshare/tab/mode/__init__.py b/desktop/onionshare/tab/mode/__init__.py index 34d78cbd..0b7412cf 100644 --- a/desktop/onionshare/tab/mode/__init__.py +++ b/desktop/onionshare/tab/mode/__init__.py @@ -568,6 +568,8 @@ class Mode(QtWidgets.QWidget): self.primary_action.show() if not self.tab.timer.isActive(): self.tab.timer.start(500) + if self.settings.get("persistent", "enabled") and self.settings.get("persistent", "autostart_on_launch"): + self.server_status.start_server() def tor_connection_stopped(self): """ diff --git a/desktop/onionshare/tab/mode/mode_settings_widget.py b/desktop/onionshare/tab/mode/mode_settings_widget.py index 9a8cd441..ccaebcca 100644 --- a/desktop/onionshare/tab/mode/mode_settings_widget.py +++ b/desktop/onionshare/tab/mode/mode_settings_widget.py @@ -39,14 +39,24 @@ class ModeSettingsWidget(QtWidgets.QScrollArea): # Downstream Mode need to fill in this layout with its settings self.mode_specific_layout = QtWidgets.QVBoxLayout() + self.persistent_autostart_on_launch_checkbox = QtWidgets.QCheckBox() + self.persistent_autostart_on_launch_checkbox.clicked.connect(self.persistent_autostart_on_launch_checkbox_clicked) + self.persistent_autostart_on_launch_checkbox.setText(strings._("mode_settings_persistent_autostart_on_launch_checkbox")) + if self.settings.get("persistent", "autostart_on_launch"): + self.persistent_autostart_on_launch_checkbox.setCheckState(QtCore.Qt.Checked) + else: + self.persistent_autostart_on_launch_checkbox.setCheckState(QtCore.Qt.Unchecked) + # Persistent self.persistent_checkbox = QtWidgets.QCheckBox() self.persistent_checkbox.clicked.connect(self.persistent_checkbox_clicked) self.persistent_checkbox.setText(strings._("mode_settings_persistent_checkbox")) if self.settings.get("persistent", "enabled"): self.persistent_checkbox.setCheckState(QtCore.Qt.Checked) + self.persistent_autostart_on_launch_checkbox.show() else: self.persistent_checkbox.setCheckState(QtCore.Qt.Unchecked) + self.persistent_autostart_on_launch_checkbox.hide() # Public self.public_checkbox = QtWidgets.QCheckBox() @@ -150,6 +160,7 @@ class ModeSettingsWidget(QtWidgets.QScrollArea): layout = QtWidgets.QVBoxLayout() layout.addLayout(self.mode_specific_layout) layout.addWidget(self.persistent_checkbox) + layout.addWidget(self.persistent_autostart_on_launch_checkbox) layout.addWidget(self.public_checkbox) layout.addWidget(self.advanced_widget) layout.addWidget(self.toggle_advanced_button) @@ -206,6 +217,7 @@ class ModeSettingsWidget(QtWidgets.QScrollArea): def persistent_checkbox_clicked(self): self.settings.set("persistent", "enabled", self.persistent_checkbox.isChecked()) self.settings.set("persistent", "mode", self.tab.mode) + self.settings.set("persistent", "autostart_on_launch", self.persistent_autostart_on_launch_checkbox.isChecked()) self.change_persistent.emit( self.tab.tab_id, self.persistent_checkbox.isChecked() ) @@ -213,6 +225,12 @@ class ModeSettingsWidget(QtWidgets.QScrollArea): # If disabling persistence, delete the file from disk if not self.persistent_checkbox.isChecked(): self.settings.delete() + self.persistent_autostart_on_launch_checkbox.hide() + else: + self.persistent_autostart_on_launch_checkbox.show() + + def persistent_autostart_on_launch_checkbox_clicked(self): + self.settings.set("persistent", "autostart_on_launch", self.persistent_autostart_on_launch_checkbox.isChecked()) def public_checkbox_clicked(self): self.settings.set("general", "public", self.public_checkbox.isChecked()) From d2aa22663d8a46c49dd200ddcfd79ea96096c046 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 10 Feb 2025 17:37:44 +1100 Subject: [PATCH 2/2] Add a simple test of turning on/off persistence options --- desktop/tests/test_gui_share.py | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/desktop/tests/test_gui_share.py b/desktop/tests/test_gui_share.py index f0c460f2..aed2add1 100644 --- a/desktop/tests/test_gui_share.py +++ b/desktop/tests/test_gui_share.py @@ -568,3 +568,38 @@ class TestShare(GuiBaseTest): self.web_server_is_stopped(tab) self.server_status_indicator_says_closed(tab) self.close_all_tabs() + + + def test_persistent_mode_and_autostart_can_be_set(self): + """ + Test the persistent autostart on launch button can be clicked, + and that it is not visible when persistent mode is turned off + """ + tab = self.new_share_tab() + tab.get_mode().server_status.file_selection.file_list.add_file(self.tmpfiles[0]) + + # Persistent is now visible + self.assertTrue(tab.get_mode().mode_settings_widget.persistent_checkbox.isVisible()) + + # Autostart on launch is not visible + self.assertFalse(tab.get_mode().mode_settings_widget.persistent_autostart_on_launch_checkbox.isVisible()) + + # Click on persistence + tab.get_mode().mode_settings_widget.persistent_checkbox.click() + + # Autostart on launch is now visible + self.assertTrue(tab.get_mode().mode_settings_widget.persistent_autostart_on_launch_checkbox.isVisible()) + + # Click off persistence + tab.get_mode().mode_settings_widget.persistent_checkbox.click() + + # Autostart on launch is not visible + self.assertFalse(tab.get_mode().mode_settings_widget.persistent_autostart_on_launch_checkbox.isVisible()) + + self.run_all_common_setup_tests() + self.run_all_share_mode_setup_tests(tab) + self.run_all_share_mode_started_tests(tab) + self.download_share(tab) + self.web_server_is_stopped(tab) + self.server_status_indicator_says_closed(tab) + self.close_all_tabs()