Update desktop/main.py

This commit is contained in:
dev 2026-10-14 05:12:28 +00:00
parent 3c0fc02829
commit 1832611ff0

View File

@ -107,7 +107,14 @@ def configure_runtime_environment() -> None:
configure_runtime_environment()
from PySide6.QtCore import QCoreApplication, QLockFile, QStandardPaths, QTimer, QUrl, Slot
from PySide6.QtCore import (
QCoreApplication,
QLockFile,
QStandardPaths,
QTimer,
QUrl,
Slot,
)
from PySide6.QtWidgets import QApplication, QMainWindow, QMessageBox
from PySide6.QtWebEngineCore import (
QWebEngineCertificateError,
@ -212,7 +219,9 @@ def open_in_system_browser(url: QUrl) -> None:
else:
logger.warning("System browser did not confirm opening URL: %s", url_str)
except Exception:
logger.exception("Failed to open URL in system browser: %s", safe_url_string(url))
logger.exception(
"Failed to open URL in system browser: %s", safe_url_string(url)
)
class SingleInstance:
@ -244,7 +253,9 @@ class LockedPage(QWebEnginePage):
logger.debug("certificateError signal unavailable", exc_info=True)
try:
self.featurePermissionRequested.connect(self._on_feature_permission_requested)
self.featurePermissionRequested.connect(
self._on_feature_permission_requested
)
except Exception:
logger.debug("featurePermissionRequested signal unavailable", exc_info=True)
@ -277,7 +288,10 @@ class LockedPage(QWebEnginePage):
nav_type,
)
if OPEN_EXTERNAL_LINKS_IN_SYSTEM_BROWSER and url.scheme().lower() in {"http", "https"}:
if OPEN_EXTERNAL_LINKS_IN_SYSTEM_BROWSER and url.scheme().lower() in {
"http",
"https",
}:
open_in_system_browser(url)
return False
@ -312,7 +326,9 @@ class LockedPage(QWebEnginePage):
@Slot(QWebEngineCertificateError)
def _on_certificate_error(self, error):
try:
logger.error("[TLS ERROR] %s @ %s", error.description(), safe_url_string(error.url()))
logger.error(
"[TLS ERROR] %s @ %s", error.description(), safe_url_string(error.url())
)
error.rejectCertificate()
except Exception:
logger.exception("Failed to reject certificate")
@ -388,12 +404,22 @@ class MainWindow(QMainWindow):
def _apply_webengine_settings(self) -> None:
try:
settings = self.browser.settings()
settings.setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanOpenWindows, False)
settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, False)
settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessRemoteUrls, False)
settings.setAttribute(QWebEngineSettings.WebAttribute.FullScreenSupportEnabled, False)
settings.setAttribute(
QWebEngineSettings.WebAttribute.JavascriptCanOpenWindows, False
)
settings.setAttribute(
QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, False
)
settings.setAttribute(
QWebEngineSettings.WebAttribute.LocalContentCanAccessRemoteUrls, False
)
settings.setAttribute(
QWebEngineSettings.WebAttribute.FullScreenSupportEnabled, False
)
settings.setAttribute(QWebEngineSettings.WebAttribute.PluginsEnabled, False)
settings.setAttribute(QWebEngineSettings.WebAttribute.ErrorPageEnabled, True)
settings.setAttribute(
QWebEngineSettings.WebAttribute.ErrorPageEnabled, True
)
for attr_name, value in (
("HyperlinkAuditingEnabled", False),
@ -404,7 +430,9 @@ class MainWindow(QMainWindow):
attr = getattr(QWebEngineSettings.WebAttribute, attr_name)
settings.setAttribute(attr, value)
except Exception:
logger.debug("WebAttribute unavailable: %s", attr_name, exc_info=True)
logger.debug(
"WebAttribute unavailable: %s", attr_name, exc_info=True
)
logger.info("Applied WebEngine security settings")
except Exception:
@ -435,7 +463,9 @@ class MainWindow(QMainWindow):
logger.info("Loading start URL: %s", START_URL)
except Exception:
logger.exception("Failed to load start URL")
QMessageBox.critical(self, APP_NAME, "Failed to load the application start URL.")
QMessageBox.critical(
self, APP_NAME, "Failed to load the application start URL."
)
raise
def _deny_download(self, item) -> None:
@ -499,14 +529,18 @@ class MainWindow(QMainWindow):
try:
self.browser.reload()
except Exception:
logger.exception("Failed to reload browser after render process termination")
logger.exception(
"Failed to reload browser after render process termination"
)
def excepthook(exc_type, exc_value, exc_tb):
tb_text = "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
logger.critical("Unhandled exception:\n%s", tb_text)
try:
QMessageBox.critical(None, APP_NAME, f"An unexpected error occurred:\n\n{exc_value}")
QMessageBox.critical(
None, APP_NAME, f"An unexpected error occurred:\n\n{exc_value}"
)
except Exception:
pass
@ -530,7 +564,11 @@ def configure_qt_paths() -> None:
QCoreApplication.setApplicationVersion("2.0")
if is_frozen():
helper_name = "QtWebEngineProcess.exe" if platform.system().lower() == "windows" else "QtWebEngineProcess"
helper_name = (
"QtWebEngineProcess.exe"
if platform.system().lower() == "windows"
else "QtWebEngineProcess"
)
candidates = [
resource_dir() / helper_name,
@ -547,7 +585,9 @@ def configure_qt_paths() -> None:
logger.info("Using QtWebEngineProcess: %s", candidate)
break
else:
logger.warning("QtWebEngineProcess helper not found in expected locations")
logger.warning(
"QtWebEngineProcess helper not found in expected locations"
)
else:
os.environ.pop("QTWEBENGINEPROCESS_PATH", None)
except Exception:
@ -577,13 +617,19 @@ def main():
instance = SingleInstance(LOCK_FILE)
if not instance.acquire():
logger.warning("Another instance is already running")
QMessageBox.warning(None, APP_NAME, "Another instance of this application is already running.")
QMessageBox.warning(
None,
APP_NAME,
"Another instance of this application is already running.",
)
sys.exit(1)
try:
logger.info(
"Qt AppDataLocation(s): %s",
QStandardPaths.standardLocations(QStandardPaths.StandardLocation.AppDataLocation),
QStandardPaths.standardLocations(
QStandardPaths.StandardLocation.AppDataLocation
),
)
except Exception:
logger.debug("Failed to query Qt standard paths", exc_info=True)