From 706a04242ff7a70b2fc50b2dbda78f435db9d9e6 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 26 Oct 2021 22:00:39 -0700 Subject: [PATCH] Show message in Tor Settings tab if any tabs have active services, to prevent the user from changing settings without stopping them --- .../src/onionshare/resources/locale/en.json | 1 + desktop/src/onionshare/tab_widget.py | 21 ++++++--- desktop/src/onionshare/tor_settings_tab.py | 44 +++++++++++++++---- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/desktop/src/onionshare/resources/locale/en.json b/desktop/src/onionshare/resources/locale/en.json index 3f380466..398782af 100644 --- a/desktop/src/onionshare/resources/locale/en.json +++ b/desktop/src/onionshare/resources/locale/en.json @@ -72,6 +72,7 @@ "gui_settings_bridge_custom_placeholder": "type address:port (one per line)", "gui_settings_moat_bridges_invalid": "You have not requested a bridge from torproject.org yet.", "gui_settings_tor_bridges_invalid": "None of the bridges you added work. Double-check them or add others.", + "gui_settings_stop_active_tabs_label": "There are services running in some of your tabs.\nYou must stop all services to change your Tor settings.", "gui_settings_button_save": "Save", "gui_settings_button_cancel": "Cancel", "gui_settings_button_help": "Help", diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index 0ab19279..ead4d960 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -50,6 +50,7 @@ class TabWidget(QtWidgets.QTabWidget): # tab's index, which changes as tabs are re-arranged. self.tabs = {} self.current_tab_id = 0 # Each tab has a unique id + self.tor_settings_tab = None # Define the new tab button self.new_tab_button = QtWidgets.QPushButton("+", parent=self) @@ -230,18 +231,20 @@ class TabWidget(QtWidgets.QTabWidget): self.setCurrentIndex(self.indexOf(self.tabs[tab_id])) return - tor_settings_tab = TorSettingsTab(self.common, self.current_tab_id) - tor_settings_tab.close_this_tab.connect(self.close_tor_settings_tab) - self.tabs[self.current_tab_id] = tor_settings_tab + self.tor_settings_tab = TorSettingsTab( + self.common, self.current_tab_id, self.are_tabs_active() + ) + self.tor_settings_tab.close_this_tab.connect(self.close_tor_settings_tab) + self.tabs[self.current_tab_id] = self.tor_settings_tab self.current_tab_id += 1 index = self.addTab( - tor_settings_tab, strings._("gui_tor_settings_window_title") + self.tor_settings_tab, strings._("gui_tor_settings_window_title") ) self.setCurrentIndex(index) # In macOS, manually create a close button because tabs don't seem to have them otherwise if self.common.platform == "Darwin": - self.macos_create_close_button(tor_settings_tab, index) + self.macos_create_close_button(self.tor_settings_tab, index) def change_title(self, tab_id, title): shortened_title = title @@ -256,6 +259,11 @@ class TabWidget(QtWidgets.QTabWidget): index = self.indexOf(self.tabs[tab_id]) self.setTabIcon(index, QtGui.QIcon(GuiCommon.get_resource_path(icon_path))) + # The icon changes when the server status changes, so if we have an open + # Tor Settings tab, tell it to update + if self.tor_settings_tab: + self.tor_settings_tab.active_tabs_changed(self.are_tabs_active()) + def change_persistent(self, tab_id, is_persistent): self.common.log( "TabWidget", @@ -307,6 +315,9 @@ class TabWidget(QtWidgets.QTabWidget): self.removeTab(index) del self.tabs[tab.tab_id] + if type(self.tabs[tab_id]) is TorSettingsTab: + self.tor_settings_tab = None + # If the last tab is closed, open a new one if self.count() == 0: self.new_tab_clicked() diff --git a/desktop/src/onionshare/tor_settings_tab.py b/desktop/src/onionshare/tor_settings_tab.py index e3dc9bee..c8990901 100644 --- a/desktop/src/onionshare/tor_settings_tab.py +++ b/desktop/src/onionshare/tor_settings_tab.py @@ -41,7 +41,7 @@ class TorSettingsTab(QtWidgets.QWidget): close_this_tab = QtCore.Signal() - def __init__(self, common, tab_id): + def __init__(self, common, tab_id, are_tabs_active): super(TorSettingsTab, self).__init__() self.common = common @@ -351,16 +351,36 @@ class TorSettingsTab(QtWidgets.QWidget): buttons_layout.addWidget(self.test_tor_button) buttons_layout.addWidget(self.save_button) - # Layout - layout = QtWidgets.QVBoxLayout() - layout.addWidget(columns_wrapper) - layout.addStretch() - layout.addWidget(self.tor_con) - layout.addStretch() - layout.addLayout(buttons_layout) + # Main layout + main_layout = QtWidgets.QVBoxLayout() + main_layout.addWidget(columns_wrapper) + main_layout.addStretch() + main_layout.addWidget(self.tor_con) + main_layout.addStretch() + main_layout.addLayout(buttons_layout) + self.main_widget = QtWidgets.QWidget() + self.main_widget.setLayout(main_layout) + # Tabs are active label + active_tabs_label = QtWidgets.QLabel( + strings._("gui_settings_stop_active_tabs_label") + ) + active_tabs_label.setAlignment(QtCore.Qt.AlignHCenter) + + # Active tabs layout + active_tabs_layout = QtWidgets.QVBoxLayout() + active_tabs_layout.addStretch() + active_tabs_layout.addWidget(active_tabs_label) + active_tabs_layout.addStretch() + self.active_tabs_widget = QtWidgets.QWidget() + self.active_tabs_widget.setLayout(active_tabs_layout) + + layout = QtWidgets.QVBoxLayout() + layout.addWidget(self.main_widget) + layout.addWidget(self.active_tabs_widget) self.setLayout(layout) + self.active_tabs_changed(are_tabs_active) self.reload_settings() def reload_settings(self): @@ -454,6 +474,14 @@ class TorSettingsTab(QtWidgets.QWidget): self.bridge_use_checkbox.setCheckState(QtCore.Qt.Unchecked) self.bridge_settings.hide() + def active_tabs_changed(self, are_tabs_active): + if are_tabs_active: + self.main_widget.hide() + self.active_tabs_widget.show() + else: + self.main_widget.show() + self.active_tabs_widget.hide() + def connection_type_bundled_toggled(self, checked): """ Connection type bundled was toggled