2021-02-20 00:57:55 +13:00
|
|
|
import os
|
2024-01-25 04:50:44 +13:00
|
|
|
import locale
|
2021-02-20 00:57:55 +13:00
|
|
|
from logging import getLogger
|
|
|
|
|
2024-02-03 01:51:14 +13:00
|
|
|
from PyQt5.QtCore import QSettings, Qt, pyqtSlot, QUrl
|
|
|
|
from PyQt5.QtGui import QDesktopServices
|
2021-09-19 02:34:43 +12:00
|
|
|
from PyQt5.QtWidgets import QWidget, QMessageBox
|
2021-02-20 00:57:55 +13:00
|
|
|
|
2022-03-06 10:48:32 +13:00
|
|
|
from rare.components.tabs.settings.widgets.rpc import RPCSettings
|
2024-01-29 06:49:01 +13:00
|
|
|
from rare.models.options import options, LibraryView
|
2023-02-08 00:41:59 +13:00
|
|
|
from rare.shared import LegendaryCoreSingleton
|
2021-04-30 20:05:35 +12:00
|
|
|
from rare.ui.components.tabs.settings.rare import Ui_RareSettings
|
2022-07-27 02:58:17 +12:00
|
|
|
from rare.utils.misc import (
|
2021-12-24 22:09:50 +13:00
|
|
|
get_translations,
|
|
|
|
get_color_schemes,
|
|
|
|
set_color_pallete,
|
|
|
|
get_style_sheets,
|
|
|
|
set_style_sheet,
|
2023-03-05 07:56:04 +13:00
|
|
|
format_size,
|
2021-12-24 22:09:50 +13:00
|
|
|
)
|
2023-02-08 00:41:59 +13:00
|
|
|
from rare.utils.paths import create_desktop_link, desktop_link_path, log_dir, desktop_links_supported
|
2021-02-20 00:57:55 +13:00
|
|
|
|
|
|
|
logger = getLogger("RareSettings")
|
2021-02-20 06:09:00 +13:00
|
|
|
|
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
class RareSettings(QWidget):
|
2023-02-06 20:19:31 +13:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super(RareSettings, self).__init__(parent=parent)
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui = Ui_RareSettings()
|
|
|
|
self.ui.setupUi(self)
|
2022-02-26 06:43:27 +13:00
|
|
|
self.core = LegendaryCoreSingleton()
|
2023-09-20 10:39:15 +12:00
|
|
|
self.settings = QSettings(self)
|
|
|
|
|
2021-02-20 00:57:55 +13:00
|
|
|
# Select lang
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.lang_select.addItem(self.tr("System default"), options.language.default)
|
2024-01-25 04:50:44 +13:00
|
|
|
for lang_code, title in get_translations():
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.lang_select.addItem(title, lang_code)
|
2024-01-29 06:49:01 +13:00
|
|
|
language = self.settings.value(*options.language)
|
2024-02-17 00:03:27 +13:00
|
|
|
if (index := self.ui.lang_select.findData(language, Qt.UserRole)) > 0:
|
|
|
|
self.ui.lang_select.setCurrentIndex(index)
|
2021-02-20 00:57:55 +13:00
|
|
|
else:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.lang_select.setCurrentIndex(0)
|
|
|
|
self.ui.lang_select.currentIndexChanged.connect(self.on_lang_changed)
|
2021-05-18 10:56:01 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.color_select.addItem(self.tr("None"), "")
|
2024-01-29 06:49:01 +13:00
|
|
|
for item in get_color_schemes():
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.color_select.addItem(item, item)
|
2024-01-29 06:49:01 +13:00
|
|
|
color = self.settings.value(*options.color_scheme)
|
2024-02-17 00:03:27 +13:00
|
|
|
if (index := self.ui.color_select.findData(color, Qt.UserRole)) > 0:
|
|
|
|
self.ui.color_select.setCurrentIndex(index)
|
|
|
|
self.ui.color_select.setDisabled(False)
|
|
|
|
self.ui.style_select.setDisabled(True)
|
2021-05-18 10:56:01 +12:00
|
|
|
else:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.color_select.setCurrentIndex(0)
|
|
|
|
self.ui.color_select.currentIndexChanged.connect(self.on_color_select_changed)
|
2021-05-18 10:56:01 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.style_select.addItem(self.tr("None"), "")
|
2024-01-29 06:49:01 +13:00
|
|
|
for item in get_style_sheets():
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.style_select.addItem(item, item)
|
2024-01-29 06:49:01 +13:00
|
|
|
style = self.settings.value(*options.style_sheet)
|
2024-02-17 00:03:27 +13:00
|
|
|
if (index := self.ui.style_select.findData(style, Qt.UserRole)) > 0:
|
|
|
|
self.ui.style_select.setCurrentIndex(index)
|
|
|
|
self.ui.style_select.setDisabled(False)
|
|
|
|
self.ui.color_select.setDisabled(True)
|
2021-05-18 10:56:01 +12:00
|
|
|
else:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.style_select.setCurrentIndex(0)
|
|
|
|
self.ui.style_select.currentIndexChanged.connect(self.on_style_select_changed)
|
2021-05-18 10:56:01 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.view_combo.addItem(self.tr("Game covers"), LibraryView.COVER)
|
|
|
|
self.ui.view_combo.addItem(self.tr("Vertical list"), LibraryView.VLIST)
|
2024-01-29 06:49:01 +13:00
|
|
|
view = LibraryView(self.settings.value(*options.library_view))
|
2024-02-17 00:03:27 +13:00
|
|
|
if (index := self.ui.view_combo.findData(view)) > -1:
|
|
|
|
self.ui.view_combo.setCurrentIndex(index)
|
2024-01-29 06:49:01 +13:00
|
|
|
else:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.view_combo.setCurrentIndex(0)
|
|
|
|
self.ui.view_combo.currentIndexChanged.connect(self.on_view_combo_changed)
|
2024-01-29 06:49:01 +13:00
|
|
|
|
2022-01-05 09:31:17 +13:00
|
|
|
self.rpc = RPCSettings(self)
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.right_layout.insertWidget(1, self.rpc, alignment=Qt.AlignTop)
|
2021-04-28 03:40:10 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.sys_tray.setChecked(self.settings.value(*options.sys_tray))
|
|
|
|
self.ui.sys_tray.stateChanged.connect(
|
|
|
|
lambda: self.settings.setValue(options.sys_tray.key, self.ui.sys_tray.isChecked())
|
2021-09-09 08:50:42 +12:00
|
|
|
)
|
2023-09-20 10:39:15 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.auto_update.setChecked(self.settings.value(*options.auto_update))
|
|
|
|
self.ui.auto_update.stateChanged.connect(
|
|
|
|
lambda: self.settings.setValue(options.auto_update.key, self.ui.auto_update.isChecked())
|
2021-09-09 08:50:42 +12:00
|
|
|
)
|
2023-09-20 10:39:15 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.confirm_start.setChecked(self.settings.value(*options.confirm_start))
|
|
|
|
self.ui.confirm_start.stateChanged.connect(
|
|
|
|
lambda: self.settings.setValue(options.confirm_start.key, self.ui.confirm_start.isChecked())
|
2021-09-09 08:50:42 +12:00
|
|
|
)
|
2023-09-20 10:39:15 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.auto_sync_cloud.setChecked(self.settings.value(*options.auto_sync_cloud))
|
|
|
|
self.ui.auto_sync_cloud.stateChanged.connect(
|
|
|
|
lambda: self.settings.setValue(options.auto_sync_cloud.key, self.ui.auto_sync_cloud.isChecked())
|
2021-09-09 08:50:42 +12:00
|
|
|
)
|
2023-09-20 10:39:15 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.notification.setChecked(self.settings.value(*options.notification))
|
|
|
|
self.ui.notification.stateChanged.connect(
|
|
|
|
lambda: self.settings.setValue(options.notification.key, self.ui.notification.isChecked())
|
2021-09-09 08:50:42 +12:00
|
|
|
)
|
2023-09-20 10:39:15 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.save_size.setChecked(self.settings.value(*options.save_size))
|
|
|
|
self.ui.save_size.stateChanged.connect(self.save_window_size)
|
2023-09-20 10:39:15 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.log_games.setChecked(self.settings.value(*options.log_games))
|
|
|
|
self.ui.log_games.stateChanged.connect(
|
|
|
|
lambda: self.settings.setValue(options.log_games.key, self.ui.log_games.isChecked())
|
2021-09-11 02:36:26 +12:00
|
|
|
)
|
2021-04-11 00:58:34 +12:00
|
|
|
|
2023-02-08 00:41:59 +13:00
|
|
|
if desktop_links_supported():
|
|
|
|
self.desktop_link = desktop_link_path("Rare", "desktop")
|
|
|
|
self.start_menu_link = desktop_link_path("Rare", "start_menu")
|
2021-05-23 06:28:50 +12:00
|
|
|
else:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.desktop_link_btn.setToolTip(self.tr("Not supported"))
|
|
|
|
self.ui.desktop_link_btn.setDisabled(True)
|
|
|
|
self.ui.startmenu_link_btn.setToolTip(self.tr("Not supported"))
|
|
|
|
self.ui.startmenu_link_btn.setDisabled(True)
|
2023-02-08 00:41:59 +13:00
|
|
|
self.desktop_link = ""
|
2021-05-23 06:28:50 +12:00
|
|
|
self.start_menu_link = ""
|
|
|
|
|
2023-02-08 00:41:59 +13:00
|
|
|
if self.desktop_link and self.desktop_link.exists():
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.desktop_link_btn.setText(self.tr("Remove desktop link"))
|
2021-11-22 10:44:45 +13:00
|
|
|
|
2023-02-08 00:41:59 +13:00
|
|
|
if self.start_menu_link and self.start_menu_link.exists():
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.startmenu_link_btn.setText(self.tr("Remove start menu link"))
|
2021-05-23 06:28:50 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.desktop_link_btn.clicked.connect(self.create_desktop_link)
|
|
|
|
self.ui.startmenu_link_btn.clicked.connect(self.create_start_menu_link)
|
2021-05-23 06:28:50 +12:00
|
|
|
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.log_dir_open_button.clicked.connect(self.open_directory)
|
|
|
|
self.ui.log_dir_clean_button.clicked.connect(self.clean_logdir)
|
2021-05-23 06:28:50 +12:00
|
|
|
|
|
|
|
# get size of logdir
|
2022-09-13 19:08:03 +12:00
|
|
|
size = sum(
|
|
|
|
log_dir().joinpath(f).stat().st_size
|
|
|
|
for f in log_dir().iterdir()
|
|
|
|
if log_dir().joinpath(f).is_file()
|
|
|
|
)
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.log_dir_size_label.setText(format_size(size))
|
2021-05-23 06:28:50 +12:00
|
|
|
# self.log_dir_clean_button.setVisible(False)
|
|
|
|
# self.log_dir_size_label.setVisible(False)
|
|
|
|
|
2024-02-03 01:51:14 +13:00
|
|
|
@pyqtSlot()
|
2021-05-23 06:28:50 +12:00
|
|
|
def clean_logdir(self):
|
2022-09-13 19:08:03 +12:00
|
|
|
for f in log_dir().iterdir():
|
|
|
|
try:
|
|
|
|
if log_dir().joinpath(f).is_file():
|
|
|
|
log_dir().joinpath(f).unlink()
|
|
|
|
except PermissionError as e:
|
|
|
|
logger.error(e)
|
|
|
|
size = sum(
|
|
|
|
log_dir().joinpath(f).stat().st_size
|
|
|
|
for f in log_dir().iterdir()
|
|
|
|
if log_dir().joinpath(f).is_file()
|
|
|
|
)
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.log_dir_size_label.setText(format_size(size))
|
2021-05-23 06:28:50 +12:00
|
|
|
|
2024-02-03 01:51:14 +13:00
|
|
|
@pyqtSlot()
|
2021-05-23 06:28:50 +12:00
|
|
|
def create_start_menu_link(self):
|
2021-09-19 02:34:43 +12:00
|
|
|
try:
|
|
|
|
if not os.path.exists(self.start_menu_link):
|
2023-02-08 00:41:59 +13:00
|
|
|
if not create_desktop_link(app_name="rare_shortcut", link_type="start_menu"):
|
|
|
|
return
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.startmenu_link_btn.setText(self.tr("Remove start menu link"))
|
2021-09-19 02:34:43 +12:00
|
|
|
else:
|
|
|
|
os.remove(self.start_menu_link)
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.startmenu_link_btn.setText(self.tr("Create start menu link"))
|
2021-09-19 02:34:43 +12:00
|
|
|
except PermissionError as e:
|
|
|
|
logger.error(str(e))
|
2021-12-24 22:09:50 +13:00
|
|
|
QMessageBox.warning(
|
|
|
|
self,
|
2022-07-30 18:02:08 +12:00
|
|
|
self.tr("Error"),
|
|
|
|
self.tr("Permission error, cannot remove {}").format(self.start_menu_link),
|
2021-12-24 22:09:50 +13:00
|
|
|
)
|
2021-05-23 06:28:50 +12:00
|
|
|
|
2024-02-03 01:51:14 +13:00
|
|
|
@pyqtSlot()
|
2021-05-23 06:28:50 +12:00
|
|
|
def create_desktop_link(self):
|
2021-09-19 02:34:43 +12:00
|
|
|
try:
|
2023-02-08 00:41:59 +13:00
|
|
|
if not os.path.exists(self.desktop_link):
|
|
|
|
if not create_desktop_link(app_name="rare_shortcut", link_type="desktop"):
|
|
|
|
return
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.desktop_link_btn.setText(self.tr("Remove Desktop link"))
|
2021-09-19 02:34:43 +12:00
|
|
|
else:
|
2023-02-08 00:41:59 +13:00
|
|
|
os.remove(self.desktop_link)
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.desktop_link_btn.setText(self.tr("Create desktop link"))
|
2021-09-19 02:34:43 +12:00
|
|
|
except PermissionError as e:
|
2022-07-30 18:02:08 +12:00
|
|
|
logger.error(str(e))
|
2021-12-24 22:09:50 +13:00
|
|
|
logger.warning(
|
|
|
|
self,
|
2022-07-30 18:02:08 +12:00
|
|
|
self.tr("Error"),
|
|
|
|
self.tr("Permission error, cannot remove {}").format(self.start_menu_link),
|
2021-12-24 22:09:50 +13:00
|
|
|
)
|
2021-02-20 00:57:55 +13:00
|
|
|
|
2024-01-29 06:49:01 +13:00
|
|
|
@pyqtSlot(int)
|
|
|
|
def on_color_select_changed(self, index: int):
|
2024-02-17 00:03:27 +13:00
|
|
|
scheme = self.ui.color_select.itemData(index, Qt.UserRole)
|
2023-03-04 03:51:26 +13:00
|
|
|
if scheme:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.style_select.setCurrentIndex(0)
|
|
|
|
self.ui.style_select.setDisabled(True)
|
2021-05-18 10:56:01 +12:00
|
|
|
else:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.style_select.setDisabled(False)
|
2024-01-29 06:49:01 +13:00
|
|
|
self.settings.setValue("color_scheme", scheme)
|
|
|
|
set_color_pallete(scheme)
|
2021-05-18 10:56:01 +12:00
|
|
|
|
2024-01-29 06:49:01 +13:00
|
|
|
@pyqtSlot(int)
|
|
|
|
def on_style_select_changed(self, index: int):
|
2024-02-17 00:03:27 +13:00
|
|
|
style = self.ui.style_select.itemData(index, Qt.UserRole)
|
2021-05-18 10:56:01 +12:00
|
|
|
if style:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.color_select.setCurrentIndex(0)
|
|
|
|
self.ui.color_select.setDisabled(True)
|
2021-05-18 10:56:01 +12:00
|
|
|
else:
|
2024-02-17 00:03:27 +13:00
|
|
|
self.ui.color_select.setDisabled(False)
|
2024-01-29 06:49:01 +13:00
|
|
|
self.settings.setValue("style_sheet", style)
|
|
|
|
set_style_sheet(style)
|
|
|
|
|
|
|
|
@pyqtSlot(int)
|
|
|
|
def on_view_combo_changed(self, index: int):
|
2024-02-17 00:03:27 +13:00
|
|
|
view = LibraryView(self.ui.view_combo.itemData(index, Qt.UserRole))
|
2024-01-29 06:49:01 +13:00
|
|
|
self.settings.setValue(options.library_view.key, int(view))
|
2021-05-18 10:56:01 +12:00
|
|
|
|
2024-02-03 01:51:14 +13:00
|
|
|
@pyqtSlot()
|
|
|
|
def open_directory(self):
|
|
|
|
QDesktopServices.openUrl(QUrl(f"file://{log_dir()}"))
|
2021-05-01 21:35:58 +12:00
|
|
|
|
2024-02-03 01:51:14 +13:00
|
|
|
@pyqtSlot()
|
2021-04-11 01:05:03 +12:00
|
|
|
def save_window_size(self):
|
2024-02-17 00:03:27 +13:00
|
|
|
self.settings.setValue(options.save_size.key, self.ui.save_size.isChecked())
|
2023-09-20 10:39:15 +12:00
|
|
|
self.settings.remove(options.window_size.key)
|
2021-03-28 05:51:38 +13:00
|
|
|
|
2024-02-03 01:51:14 +13:00
|
|
|
@pyqtSlot(int)
|
2024-01-24 09:02:22 +13:00
|
|
|
def on_lang_changed(self, index: int):
|
2024-02-17 00:03:27 +13:00
|
|
|
lang_code = self.ui.lang_select.itemData(index, Qt.UserRole)
|
2024-01-25 04:50:44 +13:00
|
|
|
if lang_code == locale.getlocale()[0]:
|
2024-01-24 09:02:22 +13:00
|
|
|
self.settings.remove(options.language.key)
|
2024-01-25 04:50:44 +13:00
|
|
|
else:
|
|
|
|
self.settings.setValue(options.language.key, lang_code)
|