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
|
2021-04-07 21:50:35 +12:00
|
|
|
import time
|
2021-09-11 08:41:30 +12:00
|
|
|
import traceback
|
2021-03-18 02:16:33 +13:00
|
|
|
|
2021-10-21 06:19:03 +13:00
|
|
|
from PyQt5.QtCore import QThreadPool, QSettings, QTranslator
|
2021-11-13 02:17:06 +13:00
|
|
|
from PyQt5.QtGui import QIcon
|
|
|
|
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QMessageBox
|
2021-10-15 10:05:00 +13:00
|
|
|
from requests import HTTPError
|
2021-03-18 02:16:33 +13:00
|
|
|
|
2021-10-21 06:19:03 +13:00
|
|
|
import rare.shared as shared
|
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
|
2021-11-13 02:17:06 +13:00
|
|
|
from rare.utils.utils import set_color_pallete, set_style_sheet
|
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
|
2021-08-08 10:49:27 +12:00
|
|
|
file_name = os.path.join(cache_dir, f"logs/Rare_{start_time}.log")
|
2021-04-07 21:50:35 +12:00
|
|
|
if not os.path.exists(os.path.dirname(file_name)):
|
|
|
|
os.makedirs(os.path.dirname(file_name))
|
2021-09-11 08:41:30 +12:00
|
|
|
|
2021-03-18 02:16:33 +13:00
|
|
|
logger = logging.getLogger("Rare")
|
|
|
|
|
|
|
|
|
2021-09-11 08:41:30 +12:00
|
|
|
def excepthook(exc_type, exc_value, exc_tb):
|
|
|
|
tb = "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
|
|
|
|
print("Error")
|
2021-10-15 10:05:00 +13:00
|
|
|
if exc_tb == HTTPError:
|
|
|
|
try:
|
2021-10-15 10:18:13 +13:00
|
|
|
if shared.core.login():
|
2021-10-15 10:05:00 +13:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise ValueError
|
|
|
|
except Exception as e:
|
|
|
|
logger.fatal(str(e))
|
|
|
|
QMessageBox.warning(None, "Error", QApplication.tr("Failed to login"))
|
|
|
|
QApplication.exit(1)
|
|
|
|
return
|
2021-09-11 08:41:30 +12:00
|
|
|
logger.fatal(tb)
|
|
|
|
QMessageBox.warning(None, "Error", tb)
|
|
|
|
QApplication.exit(1)
|
|
|
|
|
|
|
|
|
2021-03-25 05:01:12 +13:00
|
|
|
class App(QApplication):
|
2021-10-08 07:19:24 +13:00
|
|
|
def __init__(self):
|
2021-03-25 05:01:12 +13:00
|
|
|
super(App, self).__init__(sys.argv)
|
2021-10-08 07:19:24 +13:00
|
|
|
self.args = shared.args # add some options
|
2021-03-27 06:23:22 +13:00
|
|
|
# init Legendary
|
2021-04-05 20:57:11 +12:00
|
|
|
try:
|
2021-10-08 07:19:24 +13:00
|
|
|
self.core = shared.init_legendary()
|
2021-04-05 20:57:11 +12:00
|
|
|
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]")
|
2021-10-21 06:19:03 +13:00
|
|
|
self.core = shared.init_legendary()
|
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()
|
|
|
|
|
2021-06-08 08:12:26 +12:00
|
|
|
# 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
|
2021-04-07 21:50:35 +12:00
|
|
|
self.mainwindow = None
|
2021-06-08 08:12:26 +12:00
|
|
|
self.tray_icon = None
|
|
|
|
self.launch_dialog = None
|
2021-03-25 05:01:12 +13:00
|
|
|
self.setApplicationName("Rare")
|
|
|
|
self.setOrganizationName("Rare")
|
2021-09-30 10:22:47 +13:00
|
|
|
self.settings = QSettings()
|
|
|
|
|
2021-10-08 07:19:24 +13:00
|
|
|
self.signals = shared.init_signals()
|
2021-10-15 10:05:00 +13:00
|
|
|
|
|
|
|
self.signals.exit_app.connect(self.exit)
|
|
|
|
self.signals.send_notification.connect(
|
|
|
|
lambda title:
|
|
|
|
self.tray_icon.showMessage(
|
|
|
|
self.tr("Download finished"),
|
|
|
|
self.tr("Download finished. {} is playable now").format(title),
|
|
|
|
QSystemTrayIcon.Information, 4000)
|
|
|
|
if self.settings.value("notification", True, bool) else None)
|
2021-05-21 23:09:26 +12:00
|
|
|
|
2021-03-25 05:01:12 +13:00
|
|
|
# Translator
|
|
|
|
self.translator = QTranslator()
|
2021-09-30 10:22:47 +13:00
|
|
|
lang = self.settings.value("language", self.core.language_code, type=str)
|
2021-09-02 08:12:27 +12:00
|
|
|
if os.path.exists(p := os.path.join(languages_path, lang + ".qm")):
|
|
|
|
self.translator.load(p)
|
2021-04-18 04:58:18 +12:00
|
|
|
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)
|
|
|
|
|
2021-11-13 12:56:03 +13:00
|
|
|
# translator for qt stuff
|
|
|
|
if os.path.exists(p := os.path.join(languages_path, f"qt_{lang}.qm")):
|
|
|
|
self.qt_translator = QTranslator()
|
|
|
|
self.qt_translator.load(p)
|
|
|
|
self.installTranslator(self.qt_translator)
|
|
|
|
|
2021-03-25 05:01:12 +13:00
|
|
|
# Style
|
2021-11-13 02:17:06 +13:00
|
|
|
# lk: this is a bit silly but serves well until we have a class
|
|
|
|
# lk: store the default qt style name from the system on startup as a property for later reference
|
|
|
|
self.setProperty('rareDefaultQtStyle', self.style().objectName())
|
|
|
|
|
2021-09-30 10:22:47 +13:00
|
|
|
if self.settings.value("color_scheme", None) is None and self.settings.value("style_sheet", None) is None:
|
|
|
|
self.settings.setValue("color_scheme", "")
|
|
|
|
self.settings.setValue("style_sheet", "RareStyle")
|
2021-10-21 06:19:03 +13:00
|
|
|
|
2021-11-13 02:17:06 +13:00
|
|
|
if color_scheme := self.settings.value("color_scheme", False):
|
2021-09-30 10:22:47 +13:00
|
|
|
self.settings.setValue("style_sheet", "")
|
2021-11-13 02:17:06 +13:00
|
|
|
set_color_pallete(color_scheme)
|
|
|
|
elif style_sheet := self.settings.value("style_sheet", False):
|
2021-09-30 10:22:47 +13:00
|
|
|
self.settings.setValue("color_scheme", "")
|
2021-11-13 02:17:06 +13:00
|
|
|
set_style_sheet(style_sheet)
|
2021-06-01 07:43:50 +12:00
|
|
|
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-10-08 07:19:24 +13:00
|
|
|
self.launch_dialog = LaunchDialog()
|
2021-06-08 08:12:26 +12:00
|
|
|
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)
|
2021-06-08 08:12:26 +12:00
|
|
|
self.launch_dialog.start_app.connect(self.launch_dialog.close)
|
2021-04-20 01:44:28 +12:00
|
|
|
|
2021-10-08 07:19:24 +13:00
|
|
|
if not self.args.silent or self.args.subparser == "launch":
|
2021-06-08 08:12:26 +12:00
|
|
|
self.launch_dialog.login()
|
2021-03-25 05:01:12 +13:00
|
|
|
|
2021-10-08 07:19:24 +13:00
|
|
|
def start_app(self):
|
|
|
|
self.mainwindow = MainWindow()
|
2021-10-04 08:29:33 +13:00
|
|
|
self.launch_dialog.close()
|
2021-04-08 00:46:27 +12:00
|
|
|
self.tray_icon = TrayIcon(self)
|
2021-06-08 08:12:26 +12:00
|
|
|
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-03-18 02:16:33 +13:00
|
|
|
|
2021-04-18 06:00:15 +12:00
|
|
|
if not self.args.silent:
|
|
|
|
self.mainwindow.show()
|
2021-04-18 04:58:18 +12:00
|
|
|
|
2021-04-08 00:46:27 +12:00
|
|
|
def tray(self, reason):
|
|
|
|
if reason == QSystemTrayIcon.DoubleClick:
|
|
|
|
self.mainwindow.show()
|
|
|
|
logger.info("Show App")
|
|
|
|
|
2021-06-08 08:12:26 +12:00
|
|
|
def exit_app(self, exit_code=0):
|
2021-10-21 06:19:03 +13:00
|
|
|
self.mainwindow.hide()
|
|
|
|
threadpool = QThreadPool.globalInstance()
|
|
|
|
threadpool.waitForDone()
|
2021-06-08 08:12:26 +12:00
|
|
|
if self.mainwindow is not None:
|
|
|
|
self.mainwindow.close()
|
2021-10-21 06:19:03 +13:00
|
|
|
if self.tray_icon is not None:
|
|
|
|
self.tray_icon.deleteLater()
|
2021-06-08 08:12:26 +12:00
|
|
|
self.processEvents()
|
|
|
|
self.exit(exit_code)
|
|
|
|
|
2021-03-25 05:01:12 +13:00
|
|
|
|
2021-04-09 21:36:27 +12:00
|
|
|
def start(args):
|
2021-09-11 08:41:30 +12:00
|
|
|
# set excepthook to show dialog with exception
|
|
|
|
sys.excepthook = excepthook
|
2021-10-08 07:19:24 +13:00
|
|
|
shared.init_args(args)
|
2021-09-11 08:41:30 +12:00
|
|
|
|
|
|
|
# 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
|
2021-09-11 08:41:30 +12:00
|
|
|
logging.getLogger('requests').setLevel(logging.WARNING)
|
|
|
|
logging.getLogger('urllib3').setLevel(logging.WARNING)
|
2021-09-13 07:19:51 +12:00
|
|
|
logging.getLogger("asyncio").setLevel(logging.WARNING)
|
2021-09-11 08:41:30 +12:00
|
|
|
else:
|
|
|
|
logging.basicConfig(
|
|
|
|
format='[%(name)s] %(levelname)s: %(message)s',
|
|
|
|
level=logging.INFO,
|
|
|
|
filename=file_name,
|
|
|
|
)
|
|
|
|
|
2021-04-07 21:50:35 +12:00
|
|
|
while True:
|
2021-10-08 07:19:24 +13:00
|
|
|
app = App()
|
2021-04-07 21:50:35 +12:00
|
|
|
exit_code = app.exec_()
|
|
|
|
# if not restart
|
|
|
|
# restart app
|
2021-09-13 09:28:54 +12:00
|
|
|
del app
|
2021-06-08 08:12:26 +12:00
|
|
|
if exit_code != -133742:
|
|
|
|
break
|