From 74a799f0c11adcc96041db18019960d21e8d74e6 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sat, 28 Apr 2018 12:03:10 -0700 Subject: [PATCH] Work in progress commit, moving the timer_callback logic from ShareMode into Mode so ReceiveMode can use it as well --- onionshare_gui/mode.py | 41 +++++++++++++++++++++++-- onionshare_gui/onionshare_gui.py | 9 +++--- onionshare_gui/receive_mode/__init__.py | 13 ++++++++ onionshare_gui/server_status.py | 2 +- onionshare_gui/share_mode/__init__.py | 40 ++++++++++++------------ 5 files changed, 79 insertions(+), 26 deletions(-) diff --git a/onionshare_gui/mode.py b/onionshare_gui/mode.py index 90d1ec7b..a1f475d1 100644 --- a/onionshare_gui/mode.py +++ b/onionshare_gui/mode.py @@ -86,6 +86,43 @@ class Mode(QtWidgets.QWidget): """ This method is called regularly on a timer. """ + self.common.log('Mode', 'timer_callback') + # If the auto-shutdown timer has stopped, stop the server + print(self.server_status.status) ## HERE IS THE PROBLEM, self.server_status.status isn't getting updated + if self.server_status.status == ServerStatus.STATUS_STARTED: + print('debug1') + if self.app.shutdown_timer and self.common.settings.get('shutdown_timeout'): + print('debug2') + if self.timeout > 0: + print('debug3') + now = QtCore.QDateTime.currentDateTime() + seconds_remaining = now.secsTo(self.server_status.timeout) + + # Update the server button + server_button_text = self.get_stop_server_shutdown_timeout_text() + self.server_status.server_button.setText(server_button_text.format(seconds_remaining)) + + self.status_bar.clearMessage() + if not self.app.shutdown_timer.is_alive(): + if self.timeout_finished_should_stop_server(): + self.server_status.stop_server() + + def timer_callback_custom(self): + """ + Add custom timer code. + """ + pass + + def get_stop_server_shutdown_timeout_text(self): + """ + Return the string to put on the stop server button, if there's a shutdown timeout + """ + pass + + def timeout_finished_should_stop_server(self): + """ + The shutdown timer expired, should we stop the server? Returns a bool + """ pass def start_server(self): @@ -214,7 +251,7 @@ class Mode(QtWidgets.QWidget): """ self.common.log('Mode', 'stop_server') - if self.server_status.status != self.server_status.STATUS_STOPPED: + if self.server_status.status != ServerStatus.STATUS_STOPPED: try: self.web.stop(self.app.port) except: @@ -237,7 +274,7 @@ class Mode(QtWidgets.QWidget): """ Handle connection from Tor breaking. """ - if self.server_status.status != self.server_status.STATUS_STOPPED: + if self.server_status.status != ServerStatus.STATUS_STOPPED: self.server_status.stop_server() self.handle_tor_broke_custom() diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index f3f7150a..15f8a514 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -341,6 +341,7 @@ class OnionShareGui(QtWidgets.QMainWindow): # If we switched off the shutdown timeout setting, ensure the widget is hidden. if not self.common.settings.get('shutdown_timeout'): self.share_mode.server_status.shutdown_timeout_container.hide() + self.receive_mode.server_status.shutdown_timeout_container.hide() d = SettingsDialog(self.common, self.onion, self.qtapp, self.config, self.local_only) d.settings_saved.connect(reload_settings) @@ -348,6 +349,7 @@ class OnionShareGui(QtWidgets.QMainWindow): # When settings close, refresh the server status UI self.share_mode.server_status.update() + self.receive_mode.server_status.update() def check_for_updates(self): """ @@ -367,6 +369,7 @@ class OnionShareGui(QtWidgets.QMainWindow): Check for messages communicated from the web app, and update the GUI accordingly. Also, call ShareMode and ReceiveMode's timer_callbacks. """ + self.common.log('OnionShareGui', 'timer_callback') self.update() if not self.local_only: @@ -413,10 +416,8 @@ class OnionShareGui(QtWidgets.QMainWindow): elif event["path"] != '/favicon.ico': self.status_bar.showMessage('[#{0:d}] {1:s}: {2:s}'.format(web.error404_count, strings._('other_page_loaded', True), event["path"])) - if self.mode == self.MODE_SHARE: - self.share_mode.timer_callback() - else: - self.receive_mode.timer_callback() + self.share_mode.timer_callback() + self.receive_mode.timer_callback() def copy_url(self): """ diff --git a/onionshare_gui/receive_mode/__init__.py b/onionshare_gui/receive_mode/__init__.py index 290eb029..43457566 100644 --- a/onionshare_gui/receive_mode/__init__.py +++ b/onionshare_gui/receive_mode/__init__.py @@ -58,6 +58,19 @@ class ReceiveMode(Mode): """ pass + def get_stop_server_shutdown_timeout_text(self): + """ + Return the string to put on the stop server button, if there's a shutdown timeout + """ + return strings._('gui_receive_stop_server_shutdown_timeout', True) + + def timeout_finished_should_stop_server(self): + """ + The shutdown timer expired, should we stop the server? Returns a bool + """ + # TODO: wait until the final upload is done before stoppign the server? + return True + def start_server_custom(self): """ Starting the server. diff --git a/onionshare_gui/server_status.py b/onionshare_gui/server_status.py index 4a0fba40..bbc3a450 100644 --- a/onionshare_gui/server_status.py +++ b/onionshare_gui/server_status.py @@ -211,7 +211,7 @@ class ServerStatus(QtWidgets.QWidget): if self.mode == ServerStatus.MODE_SHARE: self.server_button.setText(strings._('gui_share_stop_server', True)) else: - self.server_button.setText(strings._('gui_share_stop_server', True)) + self.server_button.setText(strings._('gui_receive_stop_server', True)) if self.common.settings.get('shutdown_timeout'): self.shutdown_timeout_container.hide() if self.mode == ServerStatus.MODE_SHARE: diff --git a/onionshare_gui/share_mode/__init__.py b/onionshare_gui/share_mode/__init__.py index 3f319864..15ba2f18 100644 --- a/onionshare_gui/share_mode/__init__.py +++ b/onionshare_gui/share_mode/__init__.py @@ -118,7 +118,7 @@ class ShareMode(Mode): # Always start with focus on file selection self.file_selection.setFocus() - def timer_callback(self): + def timer_callback_custom(self): """ This method is called regularly on a timer while share mode is active. """ @@ -126,24 +126,26 @@ class ShareMode(Mode): if self.new_download: self.downloads.downloads_container.vbar.setValue(self.downloads.downloads_container.vbar.maximum()) self.new_download = False - - # If the auto-shutdown timer has stopped, stop the server - if self.server_status.status == self.server_status.STATUS_STARTED: - if self.app.shutdown_timer and self.common.settings.get('shutdown_timeout'): - if self.timeout > 0: - now = QtCore.QDateTime.currentDateTime() - seconds_remaining = now.secsTo(self.server_status.timeout) - self.server_status.server_button.setText(strings._('gui_share_stop_server_shutdown_timeout', True).format(seconds_remaining)) - if not self.app.shutdown_timer.is_alive(): - # If there were no attempts to download the share, or all downloads are done, we can stop - if self.web.download_count == 0 or self.web.done: - self.server_status.stop_server() - self.status_bar.clearMessage() - self.server_status_label.setText(strings._('close_on_timeout', True)) - # A download is probably still running - hold off on stopping the share - else: - self.status_bar.clearMessage() - self.server_status_label.setText(strings._('timeout_download_still_running', True)) + + def get_stop_server_shutdown_timeout_text(self): + """ + Return the string to put on the stop server button, if there's a shutdown timeout + """ + return strings._('gui_share_stop_server_shutdown_timeout', True) + + def timeout_finished_should_stop_server(self): + """ + The shutdown timer expired, should we stop the server? Returns a bool + """ + # If there were no attempts to download the share, or all downloads are done, we can stop + if self.web.download_count == 0 or self.web.done: + self.server_status.stop_server() + self.server_status_label.setText(strings._('close_on_timeout', True)) + return True + # A download is probably still running - hold off on stopping the share + else: + self.server_status_label.setText(strings._('timeout_download_still_running', True)) + return False def start_server_custom(self): """