Files
LorenEteval_Furious/Furious/Widget/GuiCustomizeNetworkTest.py
T
2026-08-12 12:20:14 +08:00

145 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Copyright (C) 2024present Loren Eteval & contributors <loren.eteval@proton.me>
#
# This file is part of Furious.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Provide widgets for GUI customize network test."""
from __future__ import annotations
from Furious.Frozenlib import *
from Furious.Qt import *
from Furious.Qt import gettext as _
from PySide6 import QtCore
from PySide6.QtWidgets import *
import logging
import functools
__all__ = ['GuiCustomizeNetworkTestDialog']
logger = logging.getLogger(__name__)
registerAppSettings('CustomNetworkSpeedTestURL')
registerAppSettings('CustomNetworkConnectivityTestURL')
class GuiCustomizeNetworkTestDialog(AppQDialog):
"""Present the GUI customize network test dialog."""
def __init__(self, *args, **kwargs):
"""Initialize the GuiCustomizeNetworkTestDialog."""
super().__init__(*args, **kwargs)
self.setWindowTitle(_('Customize Network Test URL'))
self.setWindowModality(QtCore.Qt.WindowModality.WindowModal)
self.speedTestURLText = AppQLabel(_('Enter network speed test URL:'))
self.speedTestURLEdit = QLineEdit()
speedTestURLSettings = AppSettings.get('CustomNetworkSpeedTestURL')
if isinstance(speedTestURLSettings, str):
self.speedTestURLEdit.setText(speedTestURLSettings)
else:
self.speedTestURLEdit.setText(NETWORK_SPEED_TEST_URL)
self.connectivityText = AppQLabel(_('Enter network connectivity test URL:'))
self.connectivityEdit = QLineEdit()
connectivitySettings = AppSettings.get('CustomNetworkConnectivityTestURL')
if isinstance(connectivitySettings, str):
self.connectivityEdit.setText(connectivitySettings)
else:
self.connectivityEdit.setText(NETWORK_CONNECTIVITY_TEST_URL)
self.dialogBtns = AppQDialogButtonBox(QtCore.Qt.Orientation.Horizontal)
self.dialogBtns.addButton(_('OK'), AppQDialogButtonBox.ButtonRole.AcceptRole)
self.dialogBtns.addButton(
_('Cancel'), AppQDialogButtonBox.ButtonRole.RejectRole
)
self.dialogBtns.accepted.connect(self.accept)
self.dialogBtns.rejected.connect(self.reject)
self.speedTestURLResetBtn = AppQPushButton(_('Reset'))
self.speedTestURLResetBtn.clicked.connect(
lambda: self.speedTestURLEdit.setText(NETWORK_SPEED_TEST_URL)
)
self.connectivityResetBtn = AppQPushButton(_('Reset'))
self.connectivityResetBtn.clicked.connect(
lambda: self.connectivityEdit.setText(NETWORK_CONNECTIVITY_TEST_URL)
)
self.speedTestURLHboxLayout = QHBoxLayout()
self.speedTestURLHboxLayout.addWidget(self.speedTestURLEdit)
self.speedTestURLHboxLayout.addWidget(self.speedTestURLResetBtn)
self.connectivityHboxLayout = QHBoxLayout()
self.connectivityHboxLayout.addWidget(self.connectivityEdit)
self.connectivityHboxLayout.addWidget(self.connectivityResetBtn)
layout = QFormLayout()
layout.addRow(self.speedTestURLText)
layout.addRow(self.speedTestURLHboxLayout)
layout.addRow(self.connectivityText)
layout.addRow(self.connectivityHboxLayout)
layout.addRow(self.dialogBtns)
layout.setFormAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.setLayout(layout)
self.finished.connect(functools.partial(self.handleResultCode))
def setWidthAndHeight(self):
"""Apply the default size for the GUI customize network test dialog."""
self.resize(656, 180)
def handleResultCode(self, code):
"""Handle result code."""
for widget, info, settingsName, reset in [
(
self.speedTestURLEdit,
'speed test URL',
'CustomNetworkSpeedTestURL',
NETWORK_SPEED_TEST_URL,
),
(
self.connectivityEdit,
'connectivity test URL',
'CustomNetworkConnectivityTestURL',
NETWORK_CONNECTIVITY_TEST_URL,
),
]:
if code == PySide6Legacy.enumValueWrapper(AppQDialog.DialogCode.Accepted):
value = widget.text()
logger.info(f'setting {info} to \'{value}\'')
AppSettings.set(settingsName, value)
else:
try:
settings = AppSettings.get(settingsName)
if isinstance(settings, str):
widget.setText(settings)
else:
widget.setText(reset)
except Exception as ex:
# Any non-exit exceptions
logger.error(f'error restoring {info}: {ex}')