Update desktop/main.py
This commit is contained in:
parent
3c0fc02829
commit
1832611ff0
@ -107,7 +107,14 @@ def configure_runtime_environment() -> None:
|
|||||||
|
|
||||||
configure_runtime_environment()
|
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.QtWidgets import QApplication, QMainWindow, QMessageBox
|
||||||
from PySide6.QtWebEngineCore import (
|
from PySide6.QtWebEngineCore import (
|
||||||
QWebEngineCertificateError,
|
QWebEngineCertificateError,
|
||||||
@ -212,7 +219,9 @@ def open_in_system_browser(url: QUrl) -> None:
|
|||||||
else:
|
else:
|
||||||
logger.warning("System browser did not confirm opening URL: %s", url_str)
|
logger.warning("System browser did not confirm opening URL: %s", url_str)
|
||||||
except Exception:
|
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:
|
class SingleInstance:
|
||||||
@ -244,7 +253,9 @@ class LockedPage(QWebEnginePage):
|
|||||||
logger.debug("certificateError signal unavailable", exc_info=True)
|
logger.debug("certificateError signal unavailable", exc_info=True)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.featurePermissionRequested.connect(self._on_feature_permission_requested)
|
self.featurePermissionRequested.connect(
|
||||||
|
self._on_feature_permission_requested
|
||||||
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.debug("featurePermissionRequested signal unavailable", exc_info=True)
|
logger.debug("featurePermissionRequested signal unavailable", exc_info=True)
|
||||||
|
|
||||||
@ -277,7 +288,10 @@ class LockedPage(QWebEnginePage):
|
|||||||
nav_type,
|
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)
|
open_in_system_browser(url)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
@ -312,7 +326,9 @@ class LockedPage(QWebEnginePage):
|
|||||||
@Slot(QWebEngineCertificateError)
|
@Slot(QWebEngineCertificateError)
|
||||||
def _on_certificate_error(self, error):
|
def _on_certificate_error(self, error):
|
||||||
try:
|
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()
|
error.rejectCertificate()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to reject certificate")
|
logger.exception("Failed to reject certificate")
|
||||||
@ -388,12 +404,22 @@ class MainWindow(QMainWindow):
|
|||||||
def _apply_webengine_settings(self) -> None:
|
def _apply_webengine_settings(self) -> None:
|
||||||
try:
|
try:
|
||||||
settings = self.browser.settings()
|
settings = self.browser.settings()
|
||||||
settings.setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanOpenWindows, False)
|
settings.setAttribute(
|
||||||
settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, False)
|
QWebEngineSettings.WebAttribute.JavascriptCanOpenWindows, False
|
||||||
settings.setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessRemoteUrls, False)
|
)
|
||||||
settings.setAttribute(QWebEngineSettings.WebAttribute.FullScreenSupportEnabled, 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.PluginsEnabled, False)
|
||||||
settings.setAttribute(QWebEngineSettings.WebAttribute.ErrorPageEnabled, True)
|
settings.setAttribute(
|
||||||
|
QWebEngineSettings.WebAttribute.ErrorPageEnabled, True
|
||||||
|
)
|
||||||
|
|
||||||
for attr_name, value in (
|
for attr_name, value in (
|
||||||
("HyperlinkAuditingEnabled", False),
|
("HyperlinkAuditingEnabled", False),
|
||||||
@ -404,7 +430,9 @@ class MainWindow(QMainWindow):
|
|||||||
attr = getattr(QWebEngineSettings.WebAttribute, attr_name)
|
attr = getattr(QWebEngineSettings.WebAttribute, attr_name)
|
||||||
settings.setAttribute(attr, value)
|
settings.setAttribute(attr, value)
|
||||||
except Exception:
|
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")
|
logger.info("Applied WebEngine security settings")
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -435,7 +463,9 @@ class MainWindow(QMainWindow):
|
|||||||
logger.info("Loading start URL: %s", START_URL)
|
logger.info("Loading start URL: %s", START_URL)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to load start URL")
|
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
|
raise
|
||||||
|
|
||||||
def _deny_download(self, item) -> None:
|
def _deny_download(self, item) -> None:
|
||||||
@ -499,14 +529,18 @@ class MainWindow(QMainWindow):
|
|||||||
try:
|
try:
|
||||||
self.browser.reload()
|
self.browser.reload()
|
||||||
except Exception:
|
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):
|
def excepthook(exc_type, exc_value, exc_tb):
|
||||||
tb_text = "".join(traceback.format_exception(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)
|
logger.critical("Unhandled exception:\n%s", tb_text)
|
||||||
try:
|
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:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -530,7 +564,11 @@ def configure_qt_paths() -> None:
|
|||||||
QCoreApplication.setApplicationVersion("2.0")
|
QCoreApplication.setApplicationVersion("2.0")
|
||||||
|
|
||||||
if is_frozen():
|
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 = [
|
candidates = [
|
||||||
resource_dir() / helper_name,
|
resource_dir() / helper_name,
|
||||||
@ -547,7 +585,9 @@ def configure_qt_paths() -> None:
|
|||||||
logger.info("Using QtWebEngineProcess: %s", candidate)
|
logger.info("Using QtWebEngineProcess: %s", candidate)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
logger.warning("QtWebEngineProcess helper not found in expected locations")
|
logger.warning(
|
||||||
|
"QtWebEngineProcess helper not found in expected locations"
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
os.environ.pop("QTWEBENGINEPROCESS_PATH", None)
|
os.environ.pop("QTWEBENGINEPROCESS_PATH", None)
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -577,13 +617,19 @@ def main():
|
|||||||
instance = SingleInstance(LOCK_FILE)
|
instance = SingleInstance(LOCK_FILE)
|
||||||
if not instance.acquire():
|
if not instance.acquire():
|
||||||
logger.warning("Another instance is already running")
|
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)
|
sys.exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logger.info(
|
logger.info(
|
||||||
"Qt AppDataLocation(s): %s",
|
"Qt AppDataLocation(s): %s",
|
||||||
QStandardPaths.standardLocations(QStandardPaths.StandardLocation.AppDataLocation),
|
QStandardPaths.standardLocations(
|
||||||
|
QStandardPaths.StandardLocation.AppDataLocation
|
||||||
|
),
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.debug("Failed to query Qt standard paths", exc_info=True)
|
logger.debug("Failed to query Qt standard paths", exc_info=True)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user