1
0
Fork 0
mirror of synced 2024-06-17 01:54:46 +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
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"))

View file

@ -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)

View file

@ -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

View file

@ -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, "")