From e234656951ce613c6b698efdcba957a1a0b047c1 Mon Sep 17 00:00:00 2001 From: MultisampledNight Date: Thu, 31 Mar 2022 21:29:31 +0200 Subject: [PATCH] Use QStandardPaths everywhere needed --- .../game_widgets/base_installed_widget.py | 50 +++++++++---------- rare/components/tabs/settings/rare.py | 16 +++--- rare/utils/legendary_utils.py | 41 +++++++-------- rare/utils/utils.py | 16 ++++-- 4 files changed, 60 insertions(+), 63 deletions(-) diff --git a/rare/components/tabs/games/game_widgets/base_installed_widget.py b/rare/components/tabs/games/game_widgets/base_installed_widget.py index ae54f0a9..8653863f 100644 --- a/rare/components/tabs/games/game_widgets/base_installed_widget.py +++ b/rare/components/tabs/games/game_widgets/base_installed_widget.py @@ -2,7 +2,7 @@ import os import platform from logging import getLogger -from PyQt5.QtCore import pyqtSignal, QProcess, QSettings, Qt, QByteArray +from PyQt5.QtCore import pyqtSignal, QProcess, QSettings, QStandardPaths, Qt, QByteArray from PyQt5.QtGui import QPixmap from PyQt5.QtWidgets import QGroupBox, QMessageBox, QAction, QLabel @@ -83,9 +83,10 @@ class BaseInstalledWidget(QGroupBox): sync.triggered.connect(self.sync_game) self.addAction(sync) + desktop = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation) if os.path.exists( - os.path.expanduser(f"~/Desktop/{self.game.app_title}.desktop") - ) or os.path.exists(os.path.expanduser(f"~/Desktop/{self.game.app_title}.lnk")): + os.path.join(desktop, f"{self.game.app_title}.desktop") + ) or os.path.exists(os.path.join(desktop, f"{self.game.app_title}.lnk")): self.create_desktop = QAction(self.tr("Remove Desktop link")) else: self.create_desktop = QAction(self.tr("Create Desktop link")) @@ -95,14 +96,11 @@ class BaseInstalledWidget(QGroupBox): ) self.addAction(self.create_desktop) + applications = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation) if platform.system() == "Linux": - start_menu_file = os.path.expanduser( - f"~/.local/share/applications/{self.game.app_title}.desktop" - ) + start_menu_file = os.path.join(applications, f"{self.game.app_title}.desktop") elif platform.system() == "Windows": - start_menu_file = os.path.expandvars( - "%appdata%/Microsoft/Windows/Start Menu" - ) + start_menu_file = os.path.join(applications, "..", f"{self.game.app_title}.lnk") else: start_menu_file = "" if platform.system() in ["Windows", "Linux"]: @@ -136,23 +134,27 @@ class BaseInstalledWidget(QGroupBox): ) def create_desktop_link(self, type_of_link): - if platform.system() not in ["Windows", "Linux"]: + if type_of_link == "desktop": + shortcut_path = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation) + elif type_of_link == "start_menu": + shortcut_path = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation) + else: + return + + if platform.system() == "Windows": + shortcut_path = os.path.join(shortcut_path, f"{self.game.app_title}.lnk") + elif platform.system() == "Linux": + shortcut_path = os.path.join(shortcut_path, f"{self.game.app_title}.desktop") + else: QMessageBox.warning( self, "Warning", f"Create a Desktop link is currently not supported on {platform.system()}", ) return - if type_of_link == "desktop": - path = os.path.expanduser(f"~/Desktop/") - elif type_of_link == "start_menu": - path = os.path.expanduser("~/.local/share/applications/") - else: - return - if not ( - os.path.exists(os.path.expanduser(f"{path}{self.game.app_title}.desktop")) - or os.path.exists(os.path.expanduser(f"{path}{self.game.app_title}.lnk")) - ): + + + if not os.path.exists(shortcut_path): try: if not create_desktop_link(self.game.app_name, self.core, type_of_link): return @@ -165,12 +167,8 @@ class BaseInstalledWidget(QGroupBox): elif type_of_link == "start_menu": self.create_start_menu.setText(self.tr("Remove Start menu link")) else: - if os.path.exists( - os.path.expanduser(f"{path}{self.game.app_title}.desktop") - ): - os.remove(os.path.expanduser(f"{path}{self.game.app_title}.desktop")) - elif os.path.exists(os.path.expanduser(f"{path}{self.game.app_title}.lnk")): - os.remove(os.path.expanduser(f"{path}{self.game.app_title}.lnk")) + if os.path.exists(shortcut_path): + os.remove(shortcut_path) if type_of_link == "desktop": self.create_desktop.setText(self.tr("Create Desktop link")) diff --git a/rare/components/tabs/settings/rare.py b/rare/components/tabs/settings/rare.py index 89ca768a..187fb94d 100644 --- a/rare/components/tabs/settings/rare.py +++ b/rare/components/tabs/settings/rare.py @@ -4,7 +4,7 @@ import subprocess import sys from logging import getLogger -from PyQt5.QtCore import QSettings, Qt +from PyQt5.QtCore import QSettings, QStandardPaths, Qt from PyQt5.QtWidgets import QWidget, QMessageBox from rare.shared import LegendaryCoreSingleton @@ -113,16 +113,14 @@ class RareSettings(QWidget, Ui_RareSettings): lambda: self.settings.setValue("show_console", self.log_games.isChecked()) ) + desktop = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation) + applications = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation) if platform.system() == "Linux": - self.desktop_file = os.path.expanduser("~/Desktop/Rare.desktop") - self.start_menu_link = os.path.expanduser( - "~/.local/share/applications/Rare.desktop" - ) + self.desktop_file = os.path.join(desktop, "Rare.desktop") + self.start_menu_link = os.path.join(applications, "Rare.desktop") elif platform.system() == "Windows": - self.desktop_file = os.path.expanduser("~/Desktop/Rare.lnk") - self.start_menu_link = os.path.expandvars( - "%appdata%\\Microsoft\\Windows\\Start Menu\\Rare.lnk" - ) + self.desktop_file = os.path.join(desktop, "Rare.lnk") + self.start_menu_link = os.path.join(applications, "..", "Rare.lnk") else: self.desktop_link_btn.setText(self.tr("Not supported")) self.desktop_link_btn.setDisabled(True) diff --git a/rare/utils/legendary_utils.py b/rare/utils/legendary_utils.py index 17ef38cd..77e09788 100644 --- a/rare/utils/legendary_utils.py +++ b/rare/utils/legendary_utils.py @@ -3,7 +3,7 @@ import platform import shutil from logging import getLogger -from PyQt5.QtCore import pyqtSignal, QCoreApplication, QObject, QRunnable +from PyQt5.QtCore import pyqtSignal, QCoreApplication, QObject, QRunnable, QStandardPaths from legendary.core import LegendaryCore from legendary.models.game import VerifyResult @@ -20,31 +20,26 @@ def uninstall(app_name: str, core: LegendaryCore, options=None): igame = core.get_installed_game(app_name) # remove shortcuts link + desktop = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation) + applications = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation) if platform.system() == "Linux": - if os.path.exists(os.path.expanduser(f"~/Desktop/{igame.title}.desktop")): - os.remove(os.path.expanduser(f"~/Desktop/{igame.title}.desktop")) - if os.path.exists( - os.path.expanduser(f"~/.local/share/applications/{igame.title}.desktop") - ): - os.remove( - os.path.expanduser(f"~/.local/share/applications/{igame.title}.desktop") - ) + desktop_shortcut = os.path.join(desktop, f"{igame.title}.desktop") + if os.path.exists(desktop_shortcut): + os.remove(desktop_shortcut) + + applications_shortcut = os.path.join(applications, f"{igame.title}.desktop") + if os.path.exists(applications_shortcut): + os.remove(applications_shortcut) elif platform.system() == "Windows": - if os.path.exists( - os.path.expanduser(f"~/Desktop/{igame.title.split(':')[0]}.lnk") - ): - os.remove(os.path.expanduser(f"~/Desktop/{igame.title.split(':')[0]}.lnk")) - elif os.path.exists( - os.path.expandvars( - f"%appdata%/Microsoft/Windows/Start Menu/{igame.title.split(':')[0]}.lnk" - ) - ): - os.remove( - os.path.expandvars( - f"%appdata%/Microsoft/Windows/Start Menu/{igame.title.split(':')[0]}.lnk" - ) - ) + game_title = igame.title.split(":")[0] + desktop_shortcut = os.path.join(desktop, f"{game_title}.lnk") + if os.path.exists(desktop_shortcut): + os.remove(desktop_shortcut) + + start_menu_shortcut = os.path.join(applications, "..", f"{game_title}.lnk") + if os.path.exists(start_menu_shortcut): + os.remove(start_menu_shortcut) try: # Remove DLC first so directory is empty when game uninstall runs diff --git a/rare/utils/utils.py b/rare/utils/utils.py index d0924086..659fd567 100644 --- a/rare/utils/utils.py +++ b/rare/utils/utils.py @@ -269,9 +269,9 @@ def create_desktop_link(app_name=None, core: LegendaryCore = None, type_of_link= if platform.system() == "Linux": if type_of_link == "desktop": - path = os.path.expanduser("~/Desktop/") + path = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation) elif type_of_link == "start_menu": - path = os.path.expanduser("~/.local/share/applications/") + path = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation) else: return False if not os.path.exists(path): @@ -294,7 +294,7 @@ def create_desktop_link(app_name=None, core: LegendaryCore = None, type_of_link= "StartupWMClass=rare\n" ) else: - with open(f"{path}{igame.title}.desktop", "w") as desktop_file: + with open(os.path.join(path, f"{igame.title}.desktop"), "w") as desktop_file: desktop_file.write( "[Desktop Entry]\n" f"Name={igame.title}\n" @@ -305,14 +305,19 @@ def create_desktop_link(app_name=None, core: LegendaryCore = None, type_of_link= "Terminal=false\n" "StartupWMClass=rare-game\n" ) - os.chmod(os.path.expanduser(f"{path}{igame.title}.desktop"), 0o755) + os.chmod(os.path.join(path, f"{igame.title}.desktop"), 0o755) + + return True elif platform.system() == "Windows": # Target of shortcut if type_of_link == "desktop": target_folder = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation) elif type_of_link == "start_menu": - target_folder = os.path.expandvars("%appdata%/Microsoft/Windows/Start Menu") + target_folder = os.path.join( + QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation), + ".." + ) else: logger.warning("No valid type of link") return False @@ -323,6 +328,7 @@ def create_desktop_link(app_name=None, core: LegendaryCore = None, type_of_link= linkName = "Rare.lnk" else: linkName = igame.title + # TODO: this conversion is not applied everywhere (see base_installed_widget), should it? for c in r'<>?":|\/*': linkName.replace(c, "")