From 3283fb76077ca40601de4ad3d1532862f20b5108 Mon Sep 17 00:00:00 2001 From: Saptak S Date: Tue, 30 Nov 2021 02:48:20 +0530 Subject: [PATCH] Updates autoconnect to use tab widget --- .../{auto_connect.py => connection_tab.py} | 77 +++++++++++++++++-- desktop/src/onionshare/main_window.py | 36 +-------- desktop/src/onionshare/tab_widget.py | 48 +++++++++++- 3 files changed, 120 insertions(+), 41 deletions(-) rename desktop/src/onionshare/{auto_connect.py => connection_tab.py} (64%) diff --git a/desktop/src/onionshare/auto_connect.py b/desktop/src/onionshare/connection_tab.py similarity index 64% rename from desktop/src/onionshare/auto_connect.py rename to desktop/src/onionshare/connection_tab.py index 399fef0f..eeb91d1e 100644 --- a/desktop/src/onionshare/auto_connect.py +++ b/desktop/src/onionshare/connection_tab.py @@ -24,16 +24,25 @@ from onionshare_cli.settings import Settings from . import strings from .gui_common import GuiCommon +from .tor_connection import TorConnectionWidget -class AutoConnect(QtWidgets.QWidget): +class AutoConnectTab(QtWidgets.QWidget): """ - GUI window that appears in the very beginning to ask user if + Initial Tab that appears in the very beginning to ask user if should auto connect. """ - def __init__(self, common, parent=None): - super(AutoConnect, self).__init__(parent) - common.log("AutoConnect", "__init__") + + close_this_tab = QtCore.Signal() + tor_is_connected = QtCore.Signal() + tor_is_disconnected = QtCore.Signal() + def __init__(self, common, tab_id, status_bar): + super(AutoConnectTab, self).__init__() + self.common = common + self.common.log("AutoConnectTab", "__init__") + + self.status_bar = status_bar + self.tab_id = tab_id # Was auto connected? self.curr_settings = Settings(common) @@ -76,8 +85,20 @@ class AutoConnect(QtWidgets.QWidget): description_widget = QtWidgets.QWidget() description_widget.setLayout(description_layout) + # Tor connection widget + self.tor_con = TorConnectionWidget(self.common, self.status_bar) + self.tor_con.success.connect(self.tor_con_success) + self.tor_con.fail.connect(self.tor_con_fail) + self.tor_con.hide() + + # Error label + self.error_label = QtWidgets.QLabel() + self.error_label.setStyleSheet(self.common.gui.css["tor_settings_error"]) + self.error_label.setWordWrap(True) + # CTA buttons self.connect_button = QtWidgets.QPushButton(strings._("gui_autoconnect_start")) + self.connect_button.clicked.connect(self.connect_clicked) self.connect_button.setStyleSheet( common.gui.css["autoconnect_start_button"] ) @@ -98,6 +119,7 @@ class AutoConnect(QtWidgets.QWidget): content_layout.addStretch() content_layout.addWidget(self.image) content_layout.addWidget(description_widget) + content_layout.addWidget(self.tor_con) content_layout.addWidget(cta_widget) content_layout.addStretch() content_layout.setAlignment(QtCore.Qt.AlignCenter) @@ -111,7 +133,52 @@ class AutoConnect(QtWidgets.QWidget): self.setLayout(self.layout) def toggle_auto_connect(self): + """ + Auto connect checkbox clicked + """ + self.common.log("AutoConnectTab", "autoconnect_checkbox_clicked") self.curr_settings.set( "auto_connect", self.enable_autoconnect_checkbox.isChecked() ) self.curr_settings.save() + + def connect_clicked(self): + """ + Connect button clicked. Try to connect to tor. + """ + self.common.log("AutoConnectTab", "connect_clicked") + + self.error_label.setText("") + self.connect_button.hide() + self.configure_button.hide() + + if not self.common.gui.local_only: + self.tor_con.show() + self.tor_con.start(self.curr_settings) + else: + self.close_this_tab.emit() + + def tor_con_success(self): + """ + Finished testing tor connection. + """ + self.tor_con.hide() + self.connect_button.show() + self.configure_button.show() + if ( + self.common.gui.onion.is_authenticated() + and not self.tor_con.wasCanceled() + ): + # Tell the tabs that Tor is connected + self.tor_is_connected.emit() + # Close the tab + self.close_this_tab.emit() + + def tor_con_fail(self, msg): + """ + Finished testing tor connection. + """ + self.tor_con.hide() + self.connect_button.show() + self.configure_button.show() + self.error_label.setText(msg) diff --git a/desktop/src/onionshare/main_window.py b/desktop/src/onionshare/main_window.py index fd550970..8c38c38d 100644 --- a/desktop/src/onionshare/main_window.py +++ b/desktop/src/onionshare/main_window.py @@ -155,19 +155,10 @@ class MainWindow(QtWidgets.QMainWindow): layout = QtWidgets.QVBoxLayout() layout.addWidget(self.tabs) - self.connected_central_widget = QtWidgets.QWidget() - self.connected_central_widget.setLayout(layout) - - - # Auto connect OnionShare? - auto_connect = AutoConnect(self.common, self) - if not auto_connect.auto_connect_enabled: - auto_connect.configure_button.clicked.connect( - lambda: self.open_tor_settings("autoconnect") - ) - auto_connect.connect_button.clicked.connect(self.start_onionshare) - self.setCentralWidget(auto_connect) - self.show() + central_widget = QtWidgets.QWidget() + central_widget.setLayout(layout) + self.setCentralWidget(central_widget) + self.show() # Create the close warning dialog -- the dialog widget needs to be in the constructor # in order to test it @@ -183,25 +174,6 @@ class MainWindow(QtWidgets.QMainWindow): ) self.close_dialog.setDefaultButton(self.close_dialog.reject_button) - def start_onionshare(self): - """ - Once the user clicks on connect in the initial screen, start tor connection - and show new tab screen - """ - self.setCentralWidget(self.connected_central_widget) - self.show() - - # Start the "Connecting to Tor" dialog, which calls onion.connect() - tor_con = TorConnectionDialog(self.common) - tor_con.canceled.connect(self.tor_connection_canceled) - tor_con.open_tor_settings.connect(self.tor_connection_open_tor_settings) - if not self.common.gui.local_only: - tor_con.start() - self.settings_have_changed() - - # After connecting to Tor, check for updates - self.check_for_updates() - def tor_connection_canceled(self): """ If the user cancels before Tor finishes connecting, ask if they want to diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index 7162fcc4..d26947e4 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -28,6 +28,7 @@ from .threads import EventHandlerThread from .gui_common import GuiCommon from .tor_settings_tab import TorSettingsTab from .settings_tab import SettingsTab +from .connection_tab import AutoConnectTab class TabWidget(QtWidgets.QTabWidget): @@ -98,6 +99,7 @@ class TabWidget(QtWidgets.QTabWidget): if not ( type(self.tabs[tab_id]) is SettingsTab or type(self.tabs[tab_id]) is TorSettingsTab + or type(self.tabs[tab_id]) is AutoConnectTab ): self.tabs[tab_id].cleanup() @@ -138,6 +140,7 @@ class TabWidget(QtWidgets.QTabWidget): if ( type(self.tabs[tab_id]) is SettingsTab or type(self.tabs[tab_id]) is TorSettingsTab + or type(self.tabs[tab_id]) is AutoConnectTab ): # Blank the server status indicator self.status_bar.server_status_image_label.clear() @@ -159,8 +162,12 @@ class TabWidget(QtWidgets.QTabWidget): pass def new_tab_clicked(self): - # Create a new tab - self.add_tab() + # if already connected to tor, create a new tab + # Else open the initial connection tab + if self.common.gui.onion.is_authenticated(): + self.add_tab() + else: + self.open_connection_tab() def load_tab(self, mode_settings_id): # Load the tab's mode settings @@ -198,6 +205,22 @@ class TabWidget(QtWidgets.QTabWidget): # Bring the window to front, in case this is being added by an event self.bring_to_front.emit() + def open_connection_tab(self): + self.common.log("TabWidget", "open_connection_tab") + + # See if a connection tab is already open, and if so switch to it + for tab_id in self.tabs: + if type(self.tabs[tab_id]) is AutoConnectTab: + self.setCurrentIndex(self.indexOf(self.tabs[tab_id])) + return + + connection_tab = AutoConnectTab(self.common, self.current_tab_id, self.status_bar) + connection_tab.close_this_tab.connect(self.close_connection_tab) + self.tabs[self.current_tab_id] = connection_tab + self.current_tab_id += 1 + index = self.addTab(connection_tab, strings._("gui_settings_window_title")) + self.setCurrentIndex(index) + def open_settings_tab(self): self.common.log("TabWidget", "open_settings_tab") @@ -281,6 +304,7 @@ class TabWidget(QtWidgets.QTabWidget): if not ( type(self.tabs[tab_id]) is SettingsTab or type(self.tabs[tab_id]) is TorSettingsTab + or type(self.tabs[tab_id]) is AutoConnectTab ): tab = self.widget(self.indexOf(self.tabs[tab_id])) if tab.settings.get("persistent", "enabled"): @@ -298,6 +322,7 @@ class TabWidget(QtWidgets.QTabWidget): if ( type(self.tabs[tab_id]) is SettingsTab or type(self.tabs[tab_id]) is TorSettingsTab + or type(self.tabs[tab_id]) is AutoConnectTab ): self.common.log("TabWidget", "closing a settings tab") @@ -333,6 +358,14 @@ class TabWidget(QtWidgets.QTabWidget): else: self.common.log("TabWidget", "user does not want to close the tab") + def close_connection_tab(self): + self.common.log("TabWidget", "close_connection_tab") + for tab_id in self.tabs: + if type(self.tabs[tab_id]) is AutoConnectTab: + index = self.indexOf(self.tabs[tab_id]) + self.close_tab(index) + return + def close_settings_tab(self): self.common.log("TabWidget", "close_settings_tab") for tab_id in self.tabs: @@ -357,6 +390,7 @@ class TabWidget(QtWidgets.QTabWidget): if not ( type(self.tabs[tab_id]) is SettingsTab or type(self.tabs[tab_id]) is TorSettingsTab + or type(self.tabs[tab_id]) is AutoConnectTab ): mode = self.tabs[tab_id].get_mode() if mode: @@ -381,7 +415,10 @@ class TabWidget(QtWidgets.QTabWidget): if type(self.tabs[tab_id]) is SettingsTab: self.tabs[tab_id].tor_is_connected() else: - if not type(self.tabs[tab_id]) is TorSettingsTab: + if not ( + type(self.tabs[tab_id]) is TorSettingsTab + or type(self.tabs[tab_id]) is AutoConnectTab + ): mode = self.tabs[tab_id].get_mode() if mode: mode.tor_connection_started() @@ -391,7 +428,10 @@ class TabWidget(QtWidgets.QTabWidget): if type(self.tabs[tab_id]) is SettingsTab: self.tabs[tab_id].tor_is_disconnected() else: - if not type(self.tabs[tab_id]) is TorSettingsTab: + if not ( + type(self.tabs[tab_id]) is TorSettingsTab + or type(self.tabs[tab_id]) is AutoConnectTab + ): mode = self.tabs[tab_id].get_mode() if mode: mode.tor_connection_stopped()