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-03-18 02:16:33 +13:00
|
|
|
|
|
|
|
from PyQt5.QtCore import QSettings, QTranslator
|
|
|
|
from PyQt5.QtGui import QIcon
|
2021-04-08 00:46:27 +12:00
|
|
|
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon
|
2021-03-18 02:16:33 +13:00
|
|
|
|
2021-04-08 08:39:23 +12:00
|
|
|
from rare import lang_path, style_path
|
|
|
|
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
|
2021-03-18 02:16:33 +13:00
|
|
|
from custom_legendary.core import LegendaryCore
|
|
|
|
|
2021-04-07 21:50:35 +12:00
|
|
|
start_time = time.strftime('%y-%m-%d--%H:%M') # year-month-day-hour-minute
|
|
|
|
file_name = os.path.expanduser(f"~/.cache/rare/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
|
|
|
logging.basicConfig(
|
|
|
|
format='[%(name)s] %(levelname)s: %(message)s',
|
2021-04-07 21:50:35 +12:00
|
|
|
level=logging.INFO,
|
|
|
|
filename=file_name,
|
|
|
|
filemode="w"
|
|
|
|
)
|
2021-03-18 02:16:33 +13:00
|
|
|
logger = logging.getLogger("Rare")
|
|
|
|
|
|
|
|
|
2021-03-25 05:01:12 +13:00
|
|
|
class App(QApplication):
|
2021-04-09 21:36:27 +12:00
|
|
|
def __init__(self, args):
|
2021-03-25 05:01:12 +13:00
|
|
|
super(App, self).__init__(sys.argv)
|
2021-04-09 21:36:27 +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()
|
|
|
|
|
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-03-25 05:01:12 +13:00
|
|
|
self.setApplicationName("Rare")
|
|
|
|
self.setOrganizationName("Rare")
|
|
|
|
settings = QSettings()
|
|
|
|
|
|
|
|
# Translator
|
|
|
|
self.translator = QTranslator()
|
|
|
|
lang = settings.value("language", get_lang(), type=str)
|
|
|
|
if os.path.exists(lang_path + lang + ".qm"):
|
|
|
|
self.translator.load(lang_path + lang + ".qm")
|
|
|
|
logger.info("Your language is supported")
|
|
|
|
elif not lang == "en":
|
|
|
|
logger.info("Your language is not supported")
|
|
|
|
self.installTranslator(self.translator)
|
|
|
|
|
|
|
|
# Style
|
|
|
|
self.setStyleSheet(open(style_path + "RareStyle.qss").read())
|
|
|
|
self.setWindowIcon(QIcon(style_path + "Logo.png"))
|
|
|
|
|
2021-04-08 00:46:27 +12:00
|
|
|
# tray icon
|
|
|
|
|
2021-04-05 20:57:11 +12:00
|
|
|
# launch app
|
2021-03-25 05:01:12 +13:00
|
|
|
self.launch_dialog = LaunchDialog(self.core)
|
|
|
|
self.launch_dialog.start_app.connect(self.start_app)
|
|
|
|
self.launch_dialog.show()
|
|
|
|
|
|
|
|
def start_app(self):
|
|
|
|
self.mainwindow = MainWindow(self.core)
|
2021-04-08 00:46:27 +12:00
|
|
|
self.tray_icon = TrayIcon(self)
|
|
|
|
self.tray_icon.exit_action.triggered.connect(lambda: exit(0))
|
|
|
|
self.tray_icon.start_rare.triggered.connect(self.mainwindow.show)
|
|
|
|
self.tray_icon.activated.connect(self.tray)
|
|
|
|
self.mainwindow.tab_widget.downloadTab.finished.connect(lambda: self.tray_icon.showMessage(
|
|
|
|
self.tr("Download finished"), self.tr("Download finished. Game is playable now"),
|
|
|
|
QSystemTrayIcon.Information, 4000))
|
2021-03-25 05:01:12 +13:00
|
|
|
self.launch_dialog.close()
|
2021-03-18 02:16:33 +13: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-03-25 05:01:12 +13:00
|
|
|
|
2021-04-09 21:36:27 +12:00
|
|
|
def start(args):
|
2021-04-07 21:50:35 +12:00
|
|
|
while True:
|
2021-04-09 21:36:27 +12:00
|
|
|
app = App(args)
|
2021-04-07 21:50:35 +12:00
|
|
|
exit_code = app.exec_()
|
|
|
|
# if not restart
|
|
|
|
if exit_code != -133742:
|
|
|
|
break
|
|
|
|
# restart app
|
|
|
|
del app
|