Merge pull request #1963 from onionshare/mig/start-sharing-on-launch

Make it possible to automatically start a persistent share when OnionShare starts
This commit is contained in:
mig5 2025-02-12 08:35:02 +11:00 committed by GitHub
commit b14d54d9c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 62 additions and 2 deletions

View File

@ -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,

View File

@ -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",

View File

@ -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):
"""

View File

@ -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())

View File

@ -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()