Files
aostats-custom/src/gui/app.py
T

162 lines
6.0 KiB
Python

import logging
import os
import subprocess
import sys
import requests
from PySide6 import QtCore # type: ignore
from PySide6.QtCore import Qt # type: ignore
from PySide6 import QtGui # type: ignore
from PySide6.QtWidgets import QApplication # type: ignore
from PySide6.QtWidgets import QMessageBox # type: ignore
from PySide6.QtWidgets import QPushButton # type: ignore
from ..engine import InitializationResult
from ..engine import initialize
from ..utils.config import config
from ..environment import setup_logging
from .modules.main import MainWidget
from .styling import style
from ..utils.version import current_version as get_current_version
from ..utils.version import latest_version as get_latest_version
from ..utils.assets import path
from ..utils.assets import scripts
from ..utils import platform_utils
from ..utils.platform_utils import capture_permissions_ok
sys.path.append(os.path.dirname(os.path.abspath('__file__')))
logger = logging.getLogger(__name__)
def fix_network():
script_name = platform_utils.repair_script()
if not script_name:
sys.exit(0)
script_path = scripts(script_name)
if platform_utils.is_windows():
os.system(script_path)
else:
subprocess.call(['sh', script_path, sys.executable])
sys.exit(0)
def run():
setup_logging()
app_argv = [a for a in sys.argv if a not in ('--verbose', '-v')]
logger.info("Albion Online Stats v%s starting", get_current_version())
logger.debug("python: %s (%s)", sys.executable, sys.version)
logger.debug("platform: %s", platform_utils.system())
initialization_result = initialize()
logger.debug("backend initialization result: %s", initialization_result)
conf = config()
window_config = conf['window']
WIDTH = window_config['width']
HEIGHT = window_config['height']
perms_ok = True
if platform_utils.is_linux():
perms_ok = capture_permissions_ok()
logger.debug("capture permissions probe: %s", perms_ok)
app = QApplication(app_argv)
app.setStyleSheet(style)
clipboard = app.clipboard()
widget = MainWidget(clipboard)
widget.setWindowOpacity(window_config['opacity'])
widget.setWindowTitle('Albion Online Stats')
widget.setWindowIcon(QtGui.QIcon(path('albion-stats-icon.png')))
if window_config['always_on_top']:
widget.setWindowFlag(Qt.WindowStaysOnTopHint)
if window_config['frameless']:
widget.setWindowFlag(Qt.FramelessWindowHint)
if window_config.get('fullscreen', True):
widget.showMaximized()
else:
widget.resize(WIDTH, HEIGHT)
widget.show()
current_version, latest_version = (
get_current_version(), get_latest_version())
if latest_version and current_version != latest_version:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Update available!")
msg.setText("Another version of app is avaliable.")
msg.setInformativeText("You are using app in version {}, latest version is {}".format(
current_version, latest_version))
msg.setStandardButtons(QMessageBox.Ok)
msg.show()
need_capture_fix = (
initialization_result != InitializationResult.Ok
or (platform_utils.is_linux() and not perms_ok)
)
if need_capture_fix:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setWindowTitle("Unable to track network traffic data!")
if initialization_result == InitializationResult.BackendNotInstalled:
msg.setText(
"The packet capture backend (pyaoaddons) is not installed.")
msg.setInformativeText(
"pyaoaddons 0.2.8 ships prebuilt wheels only for CPython 3.6-3.10.<br>\
Use one of those Python versions and reinstall the app \
(<code>python -m pip install .</code>), or build pyaoaddons from source.<br><br>\
See the README for details.")
msg.setStandardButtons(QMessageBox.Ok)
elif platform_utils.is_windows():
msg.setText(
"On Windows make sure that Npcap is installed in your system.")
msg.setInformativeText("Npcap can be installed from <a href='{}'>here</a> <br>\
<b>Make sure to install with the \"Install Npcap in WinPcap API-compatible Mode\"</b> option<br><br>\
In case where npcap is installed try to fix npcap and restart the app"
.format('https://nmap.org/npcap/dist/npcap-0.9990.exe'))
msg.setStandardButtons(QMessageBox.Ok)
button = QPushButton("Fix npcap")
button.clicked.connect(fix_network)
msg.addButton(button, QMessageBox.NoRole)
else:
permissions_missing = platform_utils.is_linux() and not perms_ok
if permissions_missing:
msg.setText(
"The app does not have permission to capture network packets.")
msg.setInformativeText(
"Capturing packets requires elevated privileges. Use the button below \
to grant the Python interpreter the needed capabilities \
(or run the app with <code>sudo</code>).")
else:
msg.setText(
"Make sure that libpcap is installed and network interfaces are available.")
msg.setInformativeText(
"Install libpcap:<br>\
<b>Debian/Ubuntu:</b> <code>sudo apt install libpcap0.8</code><br>\
<b>Fedora:</b> <code>sudo dnf install libpcap</code><br>\
<b>Arch:</b> <code>sudo pacman -S libpcap</code>")
msg.setStandardButtons(QMessageBox.Ok)
if platform_utils.repair_script():
button = QPushButton("Fix capture permissions")
button.clicked.connect(fix_network)
msg.addButton(button, QMessageBox.NoRole)
msg.show()
sys.exit(app.exec_())