[Pkg-privacy-commits] [onionshare] 178/256: Move connecting to Tor into its own separate thread

Ulrike Uhlig ulrike at moszumanska.debian.org
Fri May 26 12:53:35 UTC 2017


This is an automated email from the git hooks/post-receive script.

ulrike pushed a commit to branch master
in repository onionshare.

commit 388202e1ea59ec6b3bb22ec157b1c00e11dfd1b0
Author: Micah Lee <micah at micahflee.com>
Date:   Tue May 16 11:44:34 2017 -0700

    Move connecting to Tor into its own separate thread
---
 onionshare/onion.py                     |  3 +-
 onionshare_gui/tor_connection_dialog.py | 89 ++++++++++++++++++++++++---------
 2 files changed, 68 insertions(+), 24 deletions(-)

diff --git a/onionshare/onion.py b/onionshare/onion.py
index 97965fb..eb97c6a 100644
--- a/onionshare/onion.py
+++ b/onionshare/onion.py
@@ -222,6 +222,7 @@ class Onion(object):
                 if callable(tor_status_update_func):
                     if not tor_status_update_func(progress, summary):
                         # If the dialog was canceled, stop connecting to Tor
+                        common.log('Onion', 'connect', 'tor_status_update_func returned false, canceling connecting to Tor')
                         print()
                         return False
 
@@ -415,7 +416,7 @@ class Onion(object):
         Returns a (address, port) tuple for the Tor SOCKS port
         """
         common.log('Onion', 'get_tor_socks_port')
-        
+
         if self.settings.get('connection_type') == 'bundled':
             return ('127.0.0.1', self.tor_socks_port)
         elif self.settings.get('connection_type') == 'automatic':
diff --git a/onionshare_gui/tor_connection_dialog.py b/onionshare_gui/tor_connection_dialog.py
index 068175d..2a6cfd5 100644
--- a/onionshare_gui/tor_connection_dialog.py
+++ b/onionshare_gui/tor_connection_dialog.py
@@ -50,39 +50,82 @@ class TorConnectionDialog(QtWidgets.QProgressDialog):
         # Don't show if connection takes less than 100ms (for non-bundled tor)
         self.setMinimumDuration(100)
 
+        # Start displaying the status at 0
+        self.tor_status_update(0, '')
+
     def start(self):
         common.log('TorConnectionDialog', 'start')
 
-        # If bundled tor, prepare to display Tor connection status
-        if self.settings.get('connection_type') == 'bundled':
-            tor_status_update = self.tor_status_update
-        else:
-            tor_status_update = None
+        t = TorConnectionThread(self, self.settings, self.onion)
+        t.tor_status_update.connect(self.tor_status_update)
+        t.connected_to_tor.connect(self.connected_to_tor)
+        t.canceled_connecting_to_tor.connect(self.canceled_connecting_to_tor)
+        t.error_connection_to_tor.connect(self.error_connection_to_tor)
+        t.start()
+
+        # Wait for the thread to start
+        time.sleep(0.1)
+
+    def tor_status_update(self, progress, summary):
+        self.setValue(int(progress))
+        self.setLabelText("<strong>{}</strong><br>{}".format(strings._('connecting_to_tor', True), summary))
+
+    def connected_to_tor(self):
+        common.log('TorConnectionDialog', 'connected_to_tor')
+
+        # Close the dialog after connecting
+        self.setValue(self.maximum())
+
+    def canceled_connecting_to_tor(self):
+        common.log('TorConnectionDialog', 'canceled_connecting_to_tor')
+
+        # Cancel connecting to Tor
+        self.cancel()
+
+    def error_connection_to_tor(self):
+        common.log('TorConnectionDialog', 'error_connection_to_tor')
+
+        # Cancel connecting to Tor
+        self.cancel()
+
+        # Display the exception in an alert box
+        Alert("{}\n\n{}".format(e.args[0], strings._('gui_tor_connection_error_settings', True)), QtWidgets.QMessageBox.Warning)
+
+        # Open settings
+        self.open_settings.emit()
+
+class TorConnectionThread(QtCore.QThread):
+    tor_status_update = QtCore.pyqtSignal(str, str)
+    connected_to_tor = QtCore.pyqtSignal()
+    canceled_connecting_to_tor = QtCore.pyqtSignal()
+    error_connection_to_tor = QtCore.pyqtSignal(str)
+
+    def __init__(self, dialog, settings, onion):
+        super(TorConnectionThread, self).__init__()
+        common.log('TorConnectionThread', '__init__')
+
+        self.dialog = dialog
+        self.settings = settings
+        self.onion = onion
+
+    def run(self):
+        common.log('TorConnectionThread', 'run')
 
         # Connect to the Onion
-        self.setValue(0)
         try:
-            self.onion.connect(self.settings, tor_status_update)
-
-            # Close the dialog after connecting
-            self.setValue(self.maximum())
+            common.log('TorConnectionThread', 'run', 'about to run onion.connect')
+            self.onion.connect(self.settings, self._tor_status_update)
+            common.log('TorConnectionThread', 'run', 'onion.connect succeeded')
+            self.connected_to_tor.emit()
 
         except BundledTorCanceled as e:
-            self.cancel()
+            self.canceled_connecting_to_tor.emit()
 
         except Exception as e:
-            # Cancel connecting to Tor
-            self.cancel()
-
-            # Display the exception in an alert box
-            Alert("{}\n\n{}".format(e.args[0], strings._('gui_tor_connection_error_settings', True)), QtWidgets.QMessageBox.Warning)
-
-            # Open settings
-            self.open_settings.emit()
+            self.error_connection_to_tor.emit(e.args[0])
 
-    def tor_status_update(self, progress, summary):
-        self.setValue(int(progress))
-        self.setLabelText("<strong>{}</strong><br>{}".format(strings._('connecting_to_tor', True), summary))
+    def _tor_status_update(self, progress, summary):
+        self.tor_status_update.emit(progress, summary)
 
         # Return False if the dialog was canceled
-        return not self.wasCanceled()
+        return not self.dialog.wasCanceled()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/onionshare.git



More information about the Pkg-privacy-commits mailing list