Move autostart and autostop timer widgets into the mode settings widget

This commit is contained in:
Micah Lee 2019-11-02 19:03:37 -07:00
parent 9f920f4353
commit 01435d1eda
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
3 changed files with 116 additions and 164 deletions

View File

@ -67,12 +67,19 @@ class Mode(QtWidgets.QWidget):
self.web_thread = None
self.startup_thread = None
# Mode settings widget
self.mode_settings_widget = ModeSettingsWidget(
self.common, self.tab.tab_id, self.settings
)
self.mode_settings_widget.change_persistent.connect(self.change_persistent)
# Server status
self.server_status = ServerStatus(
self.common,
self.qtapp,
self.app,
self.settings,
self.mode_settings_widget,
None,
self.common.gui.local_only,
)
@ -86,15 +93,6 @@ class Mode(QtWidgets.QWidget):
self.starting_server_early.connect(self.start_server_early)
self.starting_server_error.connect(self.start_server_error)
# Mode settings widget
self.mode_settings_widget = ModeSettingsWidget(
self.common, self.tab.tab_id, self.tab.mode_settings
)
self.mode_settings_widget.change_persistent.connect(self.change_persistent)
self.mode_settings_widget.update_server_status.connect(
self.server_status.update
)
# Header
# Note: It's up to the downstream Mode to add this to its layout
self.header_label = QtWidgets.QLabel()
@ -144,7 +142,7 @@ class Mode(QtWidgets.QWidget):
now = QtCore.QDateTime.currentDateTime()
if self.server_status.local_only:
seconds_remaining = now.secsTo(
self.server_status.autostart_timer_widget.dateTime()
self.mode_settings_widget.autostart_timer_widget.dateTime()
)
else:
seconds_remaining = now.secsTo(

View File

@ -28,7 +28,6 @@ class ModeSettingsWidget(QtWidgets.QWidget):
"""
change_persistent = QtCore.pyqtSignal(int, bool)
update_server_status = QtCore.pyqtSignal()
def __init__(self, common, tab_id, mode_settings):
super(ModeSettingsWidget, self).__init__()
@ -61,6 +60,26 @@ class ModeSettingsWidget(QtWidgets.QWidget):
strings._("mode_settings_autostart_timer_checkbox")
)
# The autostart timer widget
self.autostart_timer_widget = QtWidgets.QDateTimeEdit()
self.autostart_timer_widget.setDisplayFormat("hh:mm A MMM d, yy")
self.autostart_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(300) # 5 minutes in the future
)
self.autostart_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime().addSecs(60)
)
self.autostart_timer_widget.setCurrentSection(
QtWidgets.QDateTimeEdit.MinuteSection
)
self.autostart_timer_widget.hide()
# Autostart timer layout
autostart_timer_layout = QtWidgets.QHBoxLayout()
autostart_timer_layout.setContentsMargins(0, 0, 0, 0)
autostart_timer_layout.addWidget(self.autostart_timer_checkbox)
autostart_timer_layout.addWidget(self.autostart_timer_widget)
# Whether or not to use an auto-stop timer
self.autostop_timer_checkbox = QtWidgets.QCheckBox()
self.autostop_timer_checkbox.clicked.connect(
@ -71,6 +90,26 @@ class ModeSettingsWidget(QtWidgets.QWidget):
strings._("mode_settings_autostop_timer_checkbox")
)
# The autostop timer widget
self.autostop_timer_widget = QtWidgets.QDateTimeEdit()
self.autostop_timer_widget.setDisplayFormat("hh:mm A MMM d, yy")
self.autostop_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(300)
)
self.autostop_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime().addSecs(60)
)
self.autostop_timer_widget.setCurrentSection(
QtWidgets.QDateTimeEdit.MinuteSection
)
self.autostop_timer_widget.hide()
# Autostop timer layout
autostop_timer_layout = QtWidgets.QHBoxLayout()
autostop_timer_layout.setContentsMargins(0, 0, 0, 0)
autostop_timer_layout.addWidget(self.autostop_timer_checkbox)
autostop_timer_layout.addWidget(self.autostop_timer_widget)
# Legacy address
self.legacy_checkbox = QtWidgets.QCheckBox()
self.legacy_checkbox.clicked.connect(self.legacy_checkbox_clicked)
@ -99,8 +138,8 @@ class ModeSettingsWidget(QtWidgets.QWidget):
advanced_layout = QtWidgets.QVBoxLayout()
advanced_layout.setContentsMargins(0, 0, 0, 0)
advanced_layout.addWidget(self.public_checkbox)
advanced_layout.addWidget(self.autostart_timer_checkbox)
advanced_layout.addWidget(self.autostop_timer_checkbox)
advanced_layout.addLayout(autostart_timer_layout)
advanced_layout.addLayout(autostop_timer_layout)
advanced_layout.addWidget(self.legacy_checkbox)
advanced_layout.addWidget(self.client_auth_checkbox)
self.advanced_widget = QtWidgets.QWidget()
@ -150,13 +189,21 @@ class ModeSettingsWidget(QtWidgets.QWidget):
self.settings.set(
"general", "autostart_timer", self.autostart_timer_checkbox.isChecked()
)
self.update_server_status.emit()
if self.autostart_timer_checkbox.isChecked():
self.autostart_timer_widget.show()
else:
self.autostart_timer_widget.hide()
def autostop_timer_checkbox_clicked(self):
self.settings.set(
"general", "autostop_timer", self.autostop_timer_checkbox.isChecked()
)
self.update_server_status.emit()
if self.autostop_timer_checkbox.isChecked():
self.autostop_timer_widget.show()
else:
self.autostop_timer_widget.hide()
def legacy_checkbox_clicked(self):
self.settings.set("general", "legacy", self.legacy_checkbox.isChecked())
@ -173,3 +220,25 @@ class ModeSettingsWidget(QtWidgets.QWidget):
self.advanced_widget.show()
self.update_ui()
def autostart_timer_reset(self):
"""
Reset the auto-start timer in the UI after stopping a share
"""
self.autostart_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(300)
)
self.autostart_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime().addSecs(60)
)
def autostop_timer_reset(self):
"""
Reset the auto-stop timer in the UI after stopping a share
"""
self.autostop_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(300)
)
self.autostop_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime().addSecs(60)
)

View File

@ -44,7 +44,14 @@ class ServerStatus(QtWidgets.QWidget):
STATUS_STARTED = 2
def __init__(
self, common, qtapp, app, mode_settings, file_selection=None, local_only=False
self,
common,
qtapp,
app,
mode_settings,
mode_settings_widget,
file_selection=None,
local_only=False,
):
super(ServerStatus, self).__init__()
@ -56,6 +63,7 @@ class ServerStatus(QtWidgets.QWidget):
self.qtapp = qtapp
self.app = app
self.settings = mode_settings
self.mode_settings_widget = mode_settings_widget
self.web = None
self.autostart_timer_datetime = None
@ -63,80 +71,6 @@ class ServerStatus(QtWidgets.QWidget):
self.resizeEvent(None)
# Auto-start timer layout
self.autostart_timer_label = QtWidgets.QLabel(
strings._("gui_settings_autostart_timer")
)
self.autostart_timer_widget = QtWidgets.QDateTimeEdit()
self.autostart_timer_widget.setDisplayFormat("hh:mm A MMM d, yy")
if self.local_only:
# For testing
self.autostart_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(15)
)
self.autostart_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime()
)
else:
# Set proposed timer to be 5 minutes into the future
self.autostart_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(300)
)
# Onion services can take a little while to start, so reduce the risk of it expiring too soon by setting the minimum to 60s from now
self.autostart_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime().addSecs(60)
)
self.autostart_timer_widget.setCurrentSection(
QtWidgets.QDateTimeEdit.MinuteSection
)
autostart_timer_layout = QtWidgets.QHBoxLayout()
autostart_timer_layout.addWidget(self.autostart_timer_label)
autostart_timer_layout.addWidget(self.autostart_timer_widget)
# Auto-start timer container, so it can all be hidden and shown as a group
autostart_timer_container_layout = QtWidgets.QVBoxLayout()
autostart_timer_container_layout.addLayout(autostart_timer_layout)
self.autostart_timer_container = QtWidgets.QWidget()
self.autostart_timer_container.setLayout(autostart_timer_container_layout)
self.autostart_timer_container.hide()
# Auto-stop timer layout
self.autostop_timer_label = QtWidgets.QLabel(
strings._("gui_settings_autostop_timer")
)
self.autostop_timer_widget = QtWidgets.QDateTimeEdit()
self.autostop_timer_widget.setDisplayFormat("hh:mm A MMM d, yy")
if self.local_only:
# For testing
self.autostop_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(15)
)
self.autostop_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime()
)
else:
# Set proposed timer to be 5 minutes into the future
self.autostop_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(300)
)
# Onion services can take a little while to start, so reduce the risk of it expiring too soon by setting the minimum to 60s from now
self.autostop_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime().addSecs(60)
)
self.autostop_timer_widget.setCurrentSection(
QtWidgets.QDateTimeEdit.MinuteSection
)
autostop_timer_layout = QtWidgets.QHBoxLayout()
autostop_timer_layout.addWidget(self.autostop_timer_label)
autostop_timer_layout.addWidget(self.autostop_timer_widget)
# Auto-stop timer container, so it can all be hidden and shown as a group
autostop_timer_container_layout = QtWidgets.QVBoxLayout()
autostop_timer_container_layout.addLayout(autostop_timer_layout)
self.autostop_timer_container = QtWidgets.QWidget()
self.autostop_timer_container.setLayout(autostop_timer_container_layout)
self.autostop_timer_container.hide()
# Server layout
self.server_button = QtWidgets.QPushButton()
self.server_button.clicked.connect(self.server_button_clicked)
@ -181,8 +115,6 @@ class ServerStatus(QtWidgets.QWidget):
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.server_button)
layout.addLayout(url_layout)
layout.addWidget(self.autostart_timer_container)
layout.addWidget(self.autostop_timer_container)
self.setLayout(layout)
def set_mode(self, share_mode, file_selection=None):
@ -215,30 +147,6 @@ class ServerStatus(QtWidgets.QWidget):
except:
pass
def autostart_timer_reset(self):
"""
Reset the auto-start timer in the UI after stopping a share
"""
self.autostart_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(300)
)
if not self.local_only:
self.autostart_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime().addSecs(60)
)
def autostop_timer_reset(self):
"""
Reset the auto-stop timer in the UI after stopping a share
"""
self.autostop_timer_widget.setDateTime(
QtCore.QDateTime.currentDateTime().addSecs(300)
)
if not self.local_only:
self.autostop_timer_widget.setMinimumDateTime(
QtCore.QDateTime.currentDateTime().addSecs(60)
)
def show_url(self):
"""
Show the URL in the UI.
@ -292,9 +200,6 @@ class ServerStatus(QtWidgets.QWidget):
Update the GUI elements based on the current state.
"""
self.common.log("ServerStatus", "update")
autostart_timer = self.settings.get("general", "autostart_timer")
autostop_timer = self.settings.get("general", "autostop_timer")
# Set the URL fields
if self.status == self.STATUS_STARTED:
# The backend Onion may have saved new settings, such as the private key.
@ -306,47 +211,21 @@ class ServerStatus(QtWidgets.QWidget):
if not self.settings.get("persistent", "password"):
self.settings.set("persistent", "password", self.web.password)
self.settings.save()
if self.settings.get("general", "autostop_timer"):
self.server_button.setToolTip(
strings._("gui_stop_server_autostop_timer_tooltip").format(
self.mode_settings_widget.autostop_timer_widget.dateTime().toString(
"h:mm AP, MMMM dd, yyyy"
)
)
)
else:
self.url_description.hide()
self.url.hide()
self.copy_url_button.hide()
self.copy_hidservauth_button.hide()
# Autostart and autostop timers
if self.status == self.STATUS_STOPPED:
if autostart_timer:
self.autostart_timer_container.show()
else:
self.autostart_timer_container.hide()
if autostop_timer:
self.autostop_timer_container.show()
else:
self.autostop_timer_container.hide()
elif self.status == self.STATUS_STARTED:
self.autostart_timer_container.hide()
self.autostop_timer_container.hide()
if autostop_timer:
self.server_button.setToolTip(
strings._("gui_stop_server_autostop_timer_tooltip").format(
self.autostop_timer_widget.dateTime().toString(
"h:mm AP, MMMM dd, yyyy"
)
)
)
elif self.status == self.STATUS_WORKING:
self.autostart_timer_container.hide()
self.autostop_timer_container.hide()
if autostart_timer:
self.server_button.setToolTip(
strings._("gui_start_server_autostart_timer_tooltip").format(
self.autostart_timer_widget.dateTime().toString(
"h:mm AP, MMMM dd, yyyy"
)
)
)
# Button
if (
self.mode == self.common.gui.MODE_SHARE
@ -390,18 +269,24 @@ class ServerStatus(QtWidgets.QWidget):
)
self.server_button.setEnabled(True)
if self.autostart_timer_datetime:
self.autostart_timer_container.hide()
self.server_button.setToolTip(
strings._("gui_start_server_autostart_timer_tooltip").format(
self.autostart_timer_widget.dateTime().toString(
self.mode_settings_widget.autostart_timer_widget.dateTime().toString(
"h:mm AP, MMMM dd, yyyy"
)
)
)
else:
self.server_button.setText(strings._("gui_please_wait"))
if self.settings.get("general", "autostop_timer"):
self.autostop_timer_container.hide()
if self.settings.get("general", "autostart_timer"):
self.server_button.setToolTip(
strings._("gui_start_server_autostart_timer_tooltip").format(
self.mode_settings_widget.autostart_timer_widget.dateTime().toString(
"h:mm AP, MMMM dd, yyyy"
)
)
)
else:
self.server_button.setStyleSheet(
self.common.gui.css["server_status_button_working"]
@ -418,11 +303,11 @@ class ServerStatus(QtWidgets.QWidget):
if self.settings.get("general", "autostart_timer"):
if self.local_only:
self.autostart_timer_datetime = (
self.autostart_timer_widget.dateTime().toPyDateTime()
self.mode_settings_widget.autostart_timer_widget.dateTime().toPyDateTime()
)
else:
self.autostart_timer_datetime = (
self.autostart_timer_widget.dateTime()
self.mode_settings_widget.autostart_timer_widget.dateTime()
.toPyDateTime()
.replace(second=0, microsecond=0)
)
@ -440,12 +325,12 @@ class ServerStatus(QtWidgets.QWidget):
if self.settings.get("general", "autostop_timer"):
if self.local_only:
self.autostop_timer_datetime = (
self.autostop_timer_widget.dateTime().toPyDateTime()
self.mode_settings_widget.autostop_timer_widget.dateTime().toPyDateTime()
)
else:
# Get the timer chosen, stripped of its seconds. This prevents confusion if the share stops at (say) 37 seconds past the minute chosen
self.autostop_timer_datetime = (
self.autostop_timer_widget.dateTime()
self.mode_settings_widget.autostop_timer_widget.dateTime()
.toPyDateTime()
.replace(second=0, microsecond=0)
)
@ -500,8 +385,8 @@ class ServerStatus(QtWidgets.QWidget):
Stop the server.
"""
self.status = self.STATUS_WORKING
self.autostart_timer_reset()
self.autostop_timer_reset()
self.mode_settings_widget.autostart_timer_reset()
self.mode_settings_widget.autostop_timer_reset()
self.update()
self.server_stopped.emit()