1
0
Fork 0
mirror of synced 2024-06-26 10:11:19 +12:00

Use QStandardPaths everywhere needed

This commit is contained in:
MultisampledNight 2022-03-31 21:29:31 +02:00
parent a73fb2e01b
commit e234656951
No known key found for this signature in database
GPG key ID: 02B1BAF6A675FF9B
4 changed files with 60 additions and 63 deletions

View file

@ -2,7 +2,7 @@ import os
import platform import platform
from logging import getLogger 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.QtGui import QPixmap
from PyQt5.QtWidgets import QGroupBox, QMessageBox, QAction, QLabel from PyQt5.QtWidgets import QGroupBox, QMessageBox, QAction, QLabel
@ -83,9 +83,10 @@ class BaseInstalledWidget(QGroupBox):
sync.triggered.connect(self.sync_game) sync.triggered.connect(self.sync_game)
self.addAction(sync) self.addAction(sync)
desktop = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation)
if os.path.exists( if os.path.exists(
os.path.expanduser(f"~/Desktop/{self.game.app_title}.desktop") os.path.join(desktop, f"{self.game.app_title}.desktop")
) or os.path.exists(os.path.expanduser(f"~/Desktop/{self.game.app_title}.lnk")): ) or os.path.exists(os.path.join(desktop, f"{self.game.app_title}.lnk")):
self.create_desktop = QAction(self.tr("Remove Desktop link")) self.create_desktop = QAction(self.tr("Remove Desktop link"))
else: else:
self.create_desktop = QAction(self.tr("Create Desktop link")) self.create_desktop = QAction(self.tr("Create Desktop link"))
@ -95,14 +96,11 @@ class BaseInstalledWidget(QGroupBox):
) )
self.addAction(self.create_desktop) self.addAction(self.create_desktop)
applications = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation)
if platform.system() == "Linux": if platform.system() == "Linux":
start_menu_file = os.path.expanduser( start_menu_file = os.path.join(applications, f"{self.game.app_title}.desktop")
f"~/.local/share/applications/{self.game.app_title}.desktop"
)
elif platform.system() == "Windows": elif platform.system() == "Windows":
start_menu_file = os.path.expandvars( start_menu_file = os.path.join(applications, "..", f"{self.game.app_title}.lnk")
"%appdata%/Microsoft/Windows/Start Menu"
)
else: else:
start_menu_file = "" start_menu_file = ""
if platform.system() in ["Windows", "Linux"]: if platform.system() in ["Windows", "Linux"]:
@ -136,23 +134,27 @@ class BaseInstalledWidget(QGroupBox):
) )
def create_desktop_link(self, type_of_link): 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( QMessageBox.warning(
self, self,
"Warning", "Warning",
f"Create a Desktop link is currently not supported on {platform.system()}", f"Create a Desktop link is currently not supported on {platform.system()}",
) )
return return
if type_of_link == "desktop":
path = os.path.expanduser(f"~/Desktop/")
elif type_of_link == "start_menu": if not os.path.exists(shortcut_path):
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"))
):
try: try:
if not create_desktop_link(self.game.app_name, self.core, type_of_link): if not create_desktop_link(self.game.app_name, self.core, type_of_link):
return return
@ -165,12 +167,8 @@ class BaseInstalledWidget(QGroupBox):
elif type_of_link == "start_menu": elif type_of_link == "start_menu":
self.create_start_menu.setText(self.tr("Remove Start menu link")) self.create_start_menu.setText(self.tr("Remove Start menu link"))
else: else:
if os.path.exists( if os.path.exists(shortcut_path):
os.path.expanduser(f"{path}{self.game.app_title}.desktop") os.remove(shortcut_path)
):
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 type_of_link == "desktop": if type_of_link == "desktop":
self.create_desktop.setText(self.tr("Create Desktop link")) self.create_desktop.setText(self.tr("Create Desktop link"))

View file

@ -4,7 +4,7 @@ import subprocess
import sys import sys
from logging import getLogger from logging import getLogger
from PyQt5.QtCore import QSettings, Qt from PyQt5.QtCore import QSettings, QStandardPaths, Qt
from PyQt5.QtWidgets import QWidget, QMessageBox from PyQt5.QtWidgets import QWidget, QMessageBox
from rare.shared import LegendaryCoreSingleton from rare.shared import LegendaryCoreSingleton
@ -113,16 +113,14 @@ class RareSettings(QWidget, Ui_RareSettings):
lambda: self.settings.setValue("show_console", self.log_games.isChecked()) lambda: self.settings.setValue("show_console", self.log_games.isChecked())
) )
desktop = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation)
applications = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation)
if platform.system() == "Linux": if platform.system() == "Linux":
self.desktop_file = os.path.expanduser("~/Desktop/Rare.desktop") self.desktop_file = os.path.join(desktop, "Rare.desktop")
self.start_menu_link = os.path.expanduser( self.start_menu_link = os.path.join(applications, "Rare.desktop")
"~/.local/share/applications/Rare.desktop"
)
elif platform.system() == "Windows": elif platform.system() == "Windows":
self.desktop_file = os.path.expanduser("~/Desktop/Rare.lnk") self.desktop_file = os.path.join(desktop, "Rare.lnk")
self.start_menu_link = os.path.expandvars( self.start_menu_link = os.path.join(applications, "..", "Rare.lnk")
"%appdata%\\Microsoft\\Windows\\Start Menu\\Rare.lnk"
)
else: else:
self.desktop_link_btn.setText(self.tr("Not supported")) self.desktop_link_btn.setText(self.tr("Not supported"))
self.desktop_link_btn.setDisabled(True) self.desktop_link_btn.setDisabled(True)

View file

@ -3,7 +3,7 @@ import platform
import shutil import shutil
from logging import getLogger 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.core import LegendaryCore
from legendary.models.game import VerifyResult 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) igame = core.get_installed_game(app_name)
# remove shortcuts link # remove shortcuts link
desktop = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation)
applications = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation)
if platform.system() == "Linux": if platform.system() == "Linux":
if os.path.exists(os.path.expanduser(f"~/Desktop/{igame.title}.desktop")): desktop_shortcut = os.path.join(desktop, f"{igame.title}.desktop")
os.remove(os.path.expanduser(f"~/Desktop/{igame.title}.desktop")) if os.path.exists(desktop_shortcut):
if os.path.exists( os.remove(desktop_shortcut)
os.path.expanduser(f"~/.local/share/applications/{igame.title}.desktop")
): applications_shortcut = os.path.join(applications, f"{igame.title}.desktop")
os.remove( if os.path.exists(applications_shortcut):
os.path.expanduser(f"~/.local/share/applications/{igame.title}.desktop") os.remove(applications_shortcut)
)
elif platform.system() == "Windows": elif platform.system() == "Windows":
if os.path.exists( game_title = igame.title.split(":")[0]
os.path.expanduser(f"~/Desktop/{igame.title.split(':')[0]}.lnk") desktop_shortcut = os.path.join(desktop, f"{game_title}.lnk")
): if os.path.exists(desktop_shortcut):
os.remove(os.path.expanduser(f"~/Desktop/{igame.title.split(':')[0]}.lnk")) os.remove(desktop_shortcut)
elif os.path.exists(
os.path.expandvars( start_menu_shortcut = os.path.join(applications, "..", f"{game_title}.lnk")
f"%appdata%/Microsoft/Windows/Start Menu/{igame.title.split(':')[0]}.lnk" if os.path.exists(start_menu_shortcut):
) os.remove(start_menu_shortcut)
):
os.remove(
os.path.expandvars(
f"%appdata%/Microsoft/Windows/Start Menu/{igame.title.split(':')[0]}.lnk"
)
)
try: try:
# Remove DLC first so directory is empty when game uninstall runs # Remove DLC first so directory is empty when game uninstall runs

View file

@ -269,9 +269,9 @@ def create_desktop_link(app_name=None, core: LegendaryCore = None, type_of_link=
if platform.system() == "Linux": if platform.system() == "Linux":
if type_of_link == "desktop": if type_of_link == "desktop":
path = os.path.expanduser("~/Desktop/") path = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation)
elif type_of_link == "start_menu": elif type_of_link == "start_menu":
path = os.path.expanduser("~/.local/share/applications/") path = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation)
else: else:
return False return False
if not os.path.exists(path): 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" "StartupWMClass=rare\n"
) )
else: 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_file.write(
"[Desktop Entry]\n" "[Desktop Entry]\n"
f"Name={igame.title}\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" "Terminal=false\n"
"StartupWMClass=rare-game\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": elif platform.system() == "Windows":
# Target of shortcut # Target of shortcut
if type_of_link == "desktop": if type_of_link == "desktop":
target_folder = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation) target_folder = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation)
elif type_of_link == "start_menu": 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: else:
logger.warning("No valid type of link") logger.warning("No valid type of link")
return False return False
@ -323,6 +328,7 @@ def create_desktop_link(app_name=None, core: LegendaryCore = None, type_of_link=
linkName = "Rare.lnk" linkName = "Rare.lnk"
else: else:
linkName = igame.title linkName = igame.title
# TODO: this conversion is not applied everywhere (see base_installed_widget), should it?
for c in r'<>?":|\/*': for c in r'<>?":|\/*':
linkName.replace(c, "") linkName.replace(c, "")