1
0
Fork 0
mirror of synced 2024-06-02 10:44:40 +12:00
Rare/rare/app.py

184 lines
7.6 KiB
Python
Raw Normal View History

2021-04-05 20:57:11 +12:00
import configparser
2021-03-18 02:16:33 +13:00
import logging
2021-03-19 00:45:59 +13:00
import os
2021-03-25 05:01:12 +13:00
import sys
import time
import traceback
2021-03-18 02:16:33 +13:00
2021-08-29 06:03:25 +12:00
import qtawesome
2021-03-18 02:16:33 +13:00
from PyQt5.QtCore import QSettings, QTranslator
2021-08-29 06:03:25 +12:00
from PyQt5.QtGui import QIcon, QPalette
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QStyleFactory, QMessageBox
2021-03-18 02:16:33 +13:00
from legendary.core import LegendaryCore
2021-08-24 02:55:21 +12:00
from rare import languages_path, resources_path, cache_dir
2021-04-08 08:39:23 +12:00
from rare.components.dialogs.launch_dialog import LaunchDialog
from rare.components.main_window import MainWindow
from rare.components.tray_icon import TrayIcon
from rare.utils.utils import get_lang, load_color_scheme
2021-03-18 02:16:33 +13:00
2021-05-17 21:38:30 +12:00
start_time = time.strftime('%y-%m-%d--%H-%M') # year-month-day-hour-minute
file_name = os.path.join(cache_dir, f"logs/Rare_{start_time}.log")
if not os.path.exists(os.path.dirname(file_name)):
os.makedirs(os.path.dirname(file_name))
2021-03-18 02:16:33 +13:00
logger = logging.getLogger("Rare")
def excepthook(exc_type, exc_value, exc_tb):
tb = "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
print("Error")
logger.fatal(tb)
QMessageBox.warning(None, "Error", tb)
QApplication.exit(1)
2021-03-25 05:01:12 +13:00
class App(QApplication):
def __init__(self, args):
2021-03-25 05:01:12 +13:00
super(App, self).__init__(sys.argv)
2021-04-20 01:44:28 +12:00
self.args = args # add some options
2021-03-27 06:23:22 +13:00
# init Legendary
2021-04-05 20:57:11 +12:00
try:
self.core = LegendaryCore()
except configparser.MissingSectionHeaderError as e:
logger.warning(f"Config is corrupt: {e}")
if config_path := os.environ.get('XDG_CONFIG_HOME'):
path = os.path.join(config_path, 'legendary')
else:
path = os.path.expanduser('~/.config/legendary')
with open(os.path.join(path, "config.ini"), "w") as config_file:
config_file.write("[Legendary]")
self.core = LegendaryCore()
2021-04-08 08:39:23 +12:00
if "Legendary" not in self.core.lgd.config.sections():
2021-03-27 06:23:22 +13:00
self.core.lgd.config.add_section("Legendary")
self.core.lgd.save_config()
# workaround if egl sync enabled, but no programdata_path
# programdata_path might be unset if logging in through the browser
if self.core.egl_sync_enabled:
if self.core.egl.programdata_path is None:
self.core.lgd.config.remove_option("Legendary", "egl_sync")
self.core.lgd.save_config()
else:
if not os.path.exists(self.core.egl.programdata_path):
self.core.lgd.config.remove_option("Legendary", "egl_sync")
self.core.lgd.save_config()
2021-05-21 09:00:38 +12:00
2021-04-05 20:57:11 +12:00
# set Application name for settings
self.mainwindow = None
self.tray_icon = None
self.launch_dialog = None
2021-03-25 05:01:12 +13:00
self.setApplicationName("Rare")
self.setOrganizationName("Rare")
settings = QSettings()
2021-03-25 05:01:12 +13:00
# Translator
self.translator = QTranslator()
lang = settings.value("language", get_lang(), type=str)
if os.path.exists(p := os.path.join(languages_path, lang + ".qm")):
self.translator.load(p)
logger.info("Your language is supported: " + lang)
2021-03-25 05:01:12 +13:00
elif not lang == "en":
logger.info("Your language is not supported")
self.installTranslator(self.translator)
# Style
2021-05-01 06:45:12 +12:00
self.setStyle(QStyleFactory.create("Fusion"))
if settings.value("color_scheme", None) is None and settings.value("style_sheet", None) is None:
settings.setValue("color_scheme", "")
settings.setValue("style_sheet", "RareStyle")
if color := settings.value("color_scheme", False):
settings.setValue("style_sheet", "")
custom_palette = load_color_scheme(os.path.join(resources_path, "colors", color + ".scheme"))
if custom_palette is not None:
self.setPalette(custom_palette)
2021-08-29 06:03:25 +12:00
qtawesome.set_defaults(color=custom_palette.color(QPalette.Text))
elif style := settings.value("style_sheet", False):
settings.setValue("color_scheme", "")
stylesheet = open(os.path.join(resources_path, "stylesheets", style, "stylesheet.qss")).read()
style_resource_path = os.path.join(resources_path, "stylesheets", style, "")
if os.name == "nt":
style_resource_path = style_resource_path.replace('\\', '/')
self.setStyleSheet(stylesheet.replace("@path@", style_resource_path))
2021-08-29 06:03:25 +12:00
qtawesome.set_defaults(color="white")
# lk: for qresources stylesheets, not an ideal solution for modability,
# lk: too many extra steps and I don't like binary files in git, even as strings.
# importlib.import_module("rare.resources.stylesheets." + style)
# resource = QFile(f":/{style}/stylesheet.qss")
# resource.open(QIODevice.ReadOnly)
# self.setStyleSheet(QTextStream(resource).readAll())
self.setWindowIcon(QIcon(os.path.join(resources_path, "images", "Rare.png")))
2021-03-25 05:01:12 +13:00
2021-04-05 20:57:11 +12:00
# launch app
2021-04-20 01:44:28 +12:00
self.launch_dialog = LaunchDialog(self.core, args.offline)
self.launch_dialog.quit_app.connect(self.launch_dialog.close)
self.launch_dialog.quit_app.connect(lambda ec: exit(ec))
2021-03-25 05:01:12 +13:00
self.launch_dialog.start_app.connect(self.start_app)
self.launch_dialog.start_app.connect(self.launch_dialog.close)
2021-04-20 01:44:28 +12:00
if not args.silent or args.subparser == "launch":
self.launch_dialog.login()
2021-03-25 05:01:12 +13:00
2021-04-20 01:44:28 +12:00
def start_app(self, offline=False):
self.args.offline = offline
2021-04-14 02:56:44 +12:00
self.mainwindow = MainWindow(self.core, self.args)
self.mainwindow.quit_app.connect(self.exit_app)
2021-04-08 00:46:27 +12:00
self.tray_icon = TrayIcon(self)
self.tray_icon.exit_action.triggered.connect(self.exit_app)
2021-04-08 00:46:27 +12:00
self.tray_icon.start_rare.triggered.connect(self.mainwindow.show)
self.tray_icon.activated.connect(self.tray)
2021-04-20 01:44:28 +12:00
if not offline:
self.mainwindow.tab_widget.downloadTab.finished.connect(lambda x: self.tray_icon.showMessage(
self.tr("Download finished"),
self.tr("Download finished. {} is playable now").format(self.core.get_game(x[1]).app_title),
QSystemTrayIcon.Information, 4000) if (
x[0] and QSettings().value("notification", True, bool)) else None)
2021-03-18 02:16:33 +13:00
if not self.args.silent:
self.mainwindow.show()
2021-04-08 00:46:27 +12:00
def tray(self, reason):
if reason == QSystemTrayIcon.DoubleClick:
self.mainwindow.show()
logger.info("Show App")
def exit_app(self, exit_code=0):
if self.tray_icon is not None:
self.tray_icon.deleteLater()
if self.mainwindow is not None:
self.mainwindow.close()
self.processEvents()
self.exit(exit_code)
2021-03-25 05:01:12 +13:00
def start(args):
# set excepthook to show dialog with exception
sys.excepthook = excepthook
# configure logging
if args.debug:
logging.basicConfig(format='[%(name)s] %(levelname)s: %(message)s', level=logging.DEBUG)
logging.getLogger().setLevel(level=logging.DEBUG)
2021-09-13 07:19:51 +12:00
# keep requests, asyncio and pillow quiet
logging.getLogger('requests').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING)
2021-09-13 07:19:51 +12:00
logging.getLogger("PIL.TiffImagePlugin").setLevel(logging.WARNING)
logging.getLogger("asyncio").setLevel(logging.WARNING)
logging.getLogger("PIL.PngImagePlugin").setLevel(logging.WARNING)
else:
logging.basicConfig(
format='[%(name)s] %(levelname)s: %(message)s',
level=logging.INFO,
filename=file_name,
)
while True:
app = App(args)
exit_code = app.exec_()
# if not restart
# restart app
2021-09-13 07:19:51 +12:00
app.deleteLater()
if exit_code != -133742:
break