diff --git a/onionshare/onion.py b/onionshare/onion.py
index 83472ffc..780a397d 100644
--- a/onionshare/onion.py
+++ b/onionshare/onion.py
@@ -216,7 +216,10 @@ class Onion(object):
print("{}: {}% - {}{}".format(strings._('connecting_to_tor'), progress, summary, "\033[K"), end="\r")
if callable(tor_status_update_func):
- tor_status_update_func(progress, summary)
+ if not tor_status_update_func(progress, summary):
+ # If the dialog was canceled, stop connecting to Tor
+ print()
+ return False
if summary == 'Done':
print("")
diff --git a/onionshare_gui/__init__.py b/onionshare_gui/__init__.py
index d0fb3652..dcbe2aec 100644
--- a/onionshare_gui/__init__.py
+++ b/onionshare_gui/__init__.py
@@ -27,7 +27,6 @@ from onionshare.onionshare import OnionShare
from onionshare.settings import Settings
from .onionshare_gui import OnionShareGui
-from .tor_connection_dialog import TorConnectionDialog
class Application(QtWidgets.QApplication):
"""
@@ -87,35 +86,22 @@ def main():
if not valid:
sys.exit()
- # Load settings
- settings = Settings()
- settings.load()
-
# Start the Onion
onion = Onion()
- def exit_early():
- # Wait for tor to exit
- onion.cleanup()
- sys.exit()
-
- tor_con = TorConnectionDialog(settings, onion)
- tor_con.canceled.connect(exit_early)
- tor_con.start()
-
# Start the OnionShare app
web.set_stay_open(stay_open)
app = OnionShare(onion, debug, local_only, stay_open)
+ # Launch the gui
+ gui = OnionShareGui(onion, qtapp, app, filenames)
+
# Clean up when app quits
def shutdown():
onion.cleanup()
app.cleanup()
qtapp.aboutToQuit.connect(shutdown)
- # Launch the gui
- gui = OnionShareGui(qtapp, app, filenames)
-
# All done
sys.exit(qtapp.exec_())
diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py
index badc10ef..60674a12 100644
--- a/onionshare_gui/onionshare_gui.py
+++ b/onionshare_gui/onionshare_gui.py
@@ -24,6 +24,7 @@ from onionshare import strings, helpers, web
from onionshare.settings import Settings
from onionshare.onion import *
+from .tor_connection_dialog import TorConnectionDialog
from .menu import Menu
from .file_selection import FileSelection
from .server_status import ServerStatus
@@ -42,23 +43,31 @@ class OnionShareGui(QtWidgets.QMainWindow):
starting_server_step3 = QtCore.pyqtSignal()
starting_server_error = QtCore.pyqtSignal(str)
- def __init__(self, qtapp, app, filenames):
+ def __init__(self, onion, qtapp, app, filenames):
super(OnionShareGui, self).__init__()
+ self.onion = onion
self.qtapp = qtapp
self.app = app
self.setWindowTitle('OnionShare')
self.setWindowIcon(QtGui.QIcon(helpers.get_resource_path('images/logo.png')))
+ # Load settings
+ self.settings = Settings()
+ self.settings.load()
+
+ # Start the "Connecting to Tor" dialog, which calls onion.connect()
+ tor_con = TorConnectionDialog(self.settings, self.onion)
+ tor_con.canceled.connect(self._tor_connection_canceled)
+ tor_con.start()
+
# Menu bar
self.setMenuBar(Menu(self.qtapp))
# Check for updates in a new thread, if enabled
system = platform.system()
if system == 'Windows' or system == 'Darwin':
- settings = Settings()
- settings.load()
- if settings.get('use_autoupdate'):
+ if self.settings.get('use_autoupdate'):
def update_available(update_url, installed_version, latest_version):
Alert(strings._("update_available", True).format(update_url, installed_version, latest_version))
@@ -130,6 +139,16 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.timer.timeout.connect(self.check_for_requests)
self.timer.start(500)
+ def _tor_connection_canceled(self):
+ """
+ If the user cancels before Tor finishes connecting, quit.
+ """
+ def quit():
+ self.qtapp.quit()
+
+ # Wait 1ms for the event loop to finish closing the TorConnectionDialog before quitting
+ QtCore.QTimer.singleShot(1, quit)
+
def start_server(self):
"""
Start the onionshare server. This uses multiple threads to start the Tor onion
@@ -310,22 +329,26 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.status_bar.clearMessage()
def closeEvent(self, e):
- if self.server_status.status != self.server_status.STATUS_STOPPED:
- dialog = QtWidgets.QMessageBox()
- dialog.setWindowTitle("OnionShare")
- dialog.setText(strings._('gui_quit_warning', True))
- quit_button = dialog.addButton(strings._('gui_quit_warning_quit', True), QtWidgets.QMessageBox.YesRole)
- dont_quit_button = dialog.addButton(strings._('gui_quit_warning_dont_quit', True), QtWidgets.QMessageBox.NoRole)
- dialog.setDefaultButton(dont_quit_button)
- reply = dialog.exec_()
+ try:
+ if self.server_status.status != self.server_status.STATUS_STOPPED:
+ dialog = QtWidgets.QMessageBox()
+ dialog.setWindowTitle("OnionShare")
+ dialog.setText(strings._('gui_quit_warning', True))
+ quit_button = dialog.addButton(strings._('gui_quit_warning_quit', True), QtWidgets.QMessageBox.YesRole)
+ dont_quit_button = dialog.addButton(strings._('gui_quit_warning_dont_quit', True), QtWidgets.QMessageBox.NoRole)
+ dialog.setDefaultButton(dont_quit_button)
+ reply = dialog.exec_()
- # Quit
- if reply == 0:
- self.stop_server()
- e.accept()
- # Don't Quit
- else:
- e.ignore()
+ # Quit
+ if reply == 0:
+ self.stop_server()
+ e.accept()
+ # Don't Quit
+ else:
+ e.ignore()
+
+ except:
+ e.accept()
class ZipProgressBar(QtWidgets.QProgressBar):
diff --git a/onionshare_gui/tor_connection_dialog.py b/onionshare_gui/tor_connection_dialog.py
index a3372547..ab5c6583 100644
--- a/onionshare_gui/tor_connection_dialog.py
+++ b/onionshare_gui/tor_connection_dialog.py
@@ -56,8 +56,12 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
self.setValue(0)
try:
self.onion.connect(self.settings, tor_status_update)
+
+ # Close the dialog after connecting
+ self.setValue(self.maximum())
+
except BundledTorCanceled as e:
- self.close()
+ self.cancel()
except Exception as e:
print(e.args[0])
# TODO: Open settings to connect to Tor properly
@@ -66,3 +70,6 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
def tor_status_update(self, progress, summary):
self.setValue(int(progress))
self.setLabelText("{}
{}".format(strings._('connecting_to_tor', True), summary))
+
+ # Return False if the dialog was canceled
+ return not self.wasCanceled()