From e137b9673fc0b418b0ad24d7c3676760061aba12 Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Wed, 7 Mar 2018 16:13:22 +1100 Subject: [PATCH] #662 Allow local-only mode in GUI --- onionshare_gui/__init__.py | 2 +- onionshare_gui/onionshare_gui.py | 44 +++++++++-------- onionshare_gui/settings_dialog.py | 80 +++++++++++++++++-------------- 3 files changed, 68 insertions(+), 58 deletions(-) diff --git a/onionshare_gui/__init__.py b/onionshare_gui/__init__.py index 24e627bb..c1d84440 100644 --- a/onionshare_gui/__init__.py +++ b/onionshare_gui/__init__.py @@ -108,7 +108,7 @@ def main(): app = OnionShare(onion, local_only, stay_open, shutdown_timeout) # Launch the gui - gui = OnionShareGui(onion, qtapp, app, filenames, config) + gui = OnionShareGui(onion, qtapp, app, filenames, config, local_only) # Clean up when app quits def shutdown(): diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 7dd641dd..0f404ff0 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -43,7 +43,7 @@ class OnionShareGui(QtWidgets.QMainWindow): starting_server_step3 = QtCore.pyqtSignal() starting_server_error = QtCore.pyqtSignal(str) - def __init__(self, onion, qtapp, app, filenames, config=False): + def __init__(self, onion, qtapp, app, filenames, config=False, local_only=False): super(OnionShareGui, self).__init__() self._initSystemTray() @@ -53,6 +53,7 @@ class OnionShareGui(QtWidgets.QMainWindow): self.onion = onion self.qtapp = qtapp self.app = app + self.local_only = local_only self.setWindowTitle('OnionShare') self.setWindowIcon(QtGui.QIcon(common.get_resource_path('images/logo.png'))) @@ -219,7 +220,8 @@ class OnionShareGui(QtWidgets.QMainWindow): tor_con = TorConnectionDialog(self.qtapp, self.settings, self.onion) tor_con.canceled.connect(self._tor_connection_canceled) tor_con.open_settings.connect(self._tor_connection_open_settings) - tor_con.start() + if not self.local_only: + tor_con.start() # Start the timer self.timer.start(500) @@ -339,19 +341,20 @@ class OnionShareGui(QtWidgets.QMainWindow): # We might've stopped the main requests timer if a Tor connection failed. # If we've reloaded settings, we probably succeeded in obtaining a new # connection. If so, restart the timer. - if self.onion.is_authenticated(): - if not self.timer.isActive(): - self.timer.start(500) - # If there were some files listed for sharing, we should be ok to - # re-enable the 'Start Sharing' button now. - if self.server_status.file_selection.get_num_files() > 0: - self.server_status.server_button.setEnabled(True) - self.status_bar.clearMessage() + if not self.local_only: + if self.onion.is_authenticated(): + if not self.timer.isActive(): + self.timer.start(500) + # If there were some files listed for sharing, we should be ok to + # re-enable the 'Start Sharing' button now. + if self.server_status.file_selection.get_num_files() > 0: + self.server_status.server_button.setEnabled(True) + self.status_bar.clearMessage() # If we switched off the shutdown timeout setting, ensure the widget is hidden. if not self.settings.get('shutdown_timeout'): self.server_status.shutdown_timeout_container.hide() - d = SettingsDialog(self.onion, self.qtapp, self.config) + d = SettingsDialog(self.onion, self.qtapp, self.config, self.local_only) d.settings_saved.connect(reload_settings) d.exec_() @@ -549,15 +552,16 @@ class OnionShareGui(QtWidgets.QMainWindow): """ self.update() - # Have we lost connection to Tor somehow? - if not self.onion.is_authenticated(): - self.timer.stop() - if self.server_status.status != self.server_status.STATUS_STOPPED: - self.server_status.stop_server() - self.server_status.server_button.setEnabled(False) - self.status_bar.showMessage(strings._('gui_tor_connection_lost', True)) - if self.systemTray.supportsMessages() and self.settings.get('systray_notifications'): - self.systemTray.showMessage(strings._('gui_tor_connection_lost', True), strings._('gui_tor_connection_error_settings', True)) + if not self.local_only: + # Have we lost connection to Tor somehow? + if not self.onion.is_authenticated(): + self.timer.stop() + if self.server_status.status != self.server_status.STATUS_STOPPED: + self.server_status.stop_server() + self.server_status.server_button.setEnabled(False) + self.status_bar.showMessage(strings._('gui_tor_connection_lost', True)) + if self.systemTray.supportsMessages() and self.settings.get('systray_notifications'): + self.systemTray.showMessage(strings._('gui_tor_connection_lost', True), strings._('gui_tor_connection_error_settings', True)) # scroll to the bottom of the dl progress bar log pane # if a new download has been added diff --git a/onionshare_gui/settings_dialog.py b/onionshare_gui/settings_dialog.py index 7c81afc6..15d0251a 100644 --- a/onionshare_gui/settings_dialog.py +++ b/onionshare_gui/settings_dialog.py @@ -34,13 +34,14 @@ class SettingsDialog(QtWidgets.QDialog): """ settings_saved = QtCore.pyqtSignal() - def __init__(self, onion, qtapp, config=False): + def __init__(self, onion, qtapp, config=False, local_only=False): super(SettingsDialog, self).__init__() common.log('SettingsDialog', '__init__') self.onion = onion self.qtapp = qtapp self.config = config + self.local_only = local_only self.setModal(True) self.setWindowTitle(strings._('gui_settings_window_title', True)) @@ -671,48 +672,52 @@ class SettingsDialog(QtWidgets.QDialog): # If Tor isn't connected, or if Tor settings have changed, Reinitialize # the Onion object reboot_onion = False - if self.onion.is_authenticated(): - common.log('SettingsDialog', 'save_clicked', 'Connected to Tor') - def changed(s1, s2, keys): - """ - Compare the Settings objects s1 and s2 and return true if any values - have changed for the given keys. - """ - for key in keys: - if s1.get(key) != s2.get(key): - return True - return False + if not self.local_only: + if self.onion.is_authenticated(): + common.log('SettingsDialog', 'save_clicked', 'Connected to Tor') + def changed(s1, s2, keys): + """ + Compare the Settings objects s1 and s2 and return true if any values + have changed for the given keys. + """ + for key in keys: + if s1.get(key) != s2.get(key): + return True + return False - if changed(settings, self.old_settings, [ - 'connection_type', 'control_port_address', - 'control_port_port', 'socks_address', 'socks_port', - 'socket_file_path', 'auth_type', 'auth_password', - 'no_bridges', 'tor_bridges_use_obfs4', - 'tor_bridges_use_meek_lite_amazon', 'tor_bridges_use_meek_lite_azure', - 'tor_bridges_use_custom_bridges']): + if changed(settings, self.old_settings, [ + 'connection_type', 'control_port_address', + 'control_port_port', 'socks_address', 'socks_port', + 'socket_file_path', 'auth_type', 'auth_password', + 'no_bridges', 'tor_bridges_use_obfs4', + 'tor_bridges_use_meek_lite_amazon', 'tor_bridges_use_meek_lite_azure', + 'tor_bridges_use_custom_bridges']): + reboot_onion = True + + else: + common.log('SettingsDialog', 'save_clicked', 'Not connected to Tor') + # Tor isn't connected, so try connecting reboot_onion = True - else: - common.log('SettingsDialog', 'save_clicked', 'Not connected to Tor') - # Tor isn't connected, so try connecting - reboot_onion = True + # Do we need to reinitialize Tor? + if reboot_onion: + # Reinitialize the Onion object + common.log('SettingsDialog', 'save_clicked', 'rebooting the Onion') + self.onion.cleanup() - # Do we need to reinitialize Tor? - if reboot_onion: - # Reinitialize the Onion object - common.log('SettingsDialog', 'save_clicked', 'rebooting the Onion') - self.onion.cleanup() + tor_con = TorConnectionDialog(self.qtapp, settings, self.onion) + tor_con.start() - tor_con = TorConnectionDialog(self.qtapp, settings, self.onion) - tor_con.start() + common.log('SettingsDialog', 'save_clicked', 'Onion done rebooting, connected to Tor: {}'.format(self.onion.connected_to_tor)) - common.log('SettingsDialog', 'save_clicked', 'Onion done rebooting, connected to Tor: {}'.format(self.onion.connected_to_tor)) + if self.onion.is_authenticated() and not tor_con.wasCanceled(): + self.settings_saved.emit() + self.close() - if self.onion.is_authenticated() and not tor_con.wasCanceled(): + else: self.settings_saved.emit() self.close() - else: self.settings_saved.emit() self.close() @@ -856,11 +861,12 @@ class SettingsDialog(QtWidgets.QDialog): common.log('SettingsDialog', 'closeEvent') # On close, if Tor isn't connected, then quit OnionShare altogether - if not self.onion.is_authenticated(): - common.log('SettingsDialog', 'closeEvent', 'Closing while not connected to Tor') + if not self.local_only: + if not self.onion.is_authenticated(): + common.log('SettingsDialog', 'closeEvent', 'Closing while not connected to Tor') - # Wait 1ms for the event loop to finish, then quit - QtCore.QTimer.singleShot(1, self.qtapp.quit) + # Wait 1ms for the event loop to finish, then quit + QtCore.QTimer.singleShot(1, self.qtapp.quit) def _update_autoupdate_timestamp(self, autoupdate_timestamp): common.log('SettingsDialog', '_update_autoupdate_timestamp')