Skip to content

Commit

Permalink
Add ProtonUp-Qt shortcut to Steam library (#177)
Browse files Browse the repository at this point in the history
* Add install_steam_library_shortcut

* Steam shortcut: Create shortcuts file if not exist

* Steam shortcut: Fix Flatpak run command

* Add "Add Steam shortcut" to about dialog

* Steam shortcut: Add app icon

* Steam shortcut: Add Tooltip to button
  • Loading branch information
DavidoTek authored Apr 6, 2023
1 parent 4aef45a commit 2a91c1d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pupgui2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

APP_NAME = 'ProtonUp-Qt'
APP_VERSION = '2.8.0'
APP_ID = 'net.davidotek.pupgui2'
APP_ICON_FILE = os.path.join(xdg_config_home, 'pupgui/appicon256.png')
APP_GHAPI_URL = 'https://api.github.com/repos/Davidotek/ProtonUp-qt/releases'
DAVIDOTEK_KOFI_URL = 'https://ko-fi.com/davidotek'
PROTONUPQT_GITHUB_URL = 'https://github.com/DavidoTek/ProtonUp-Qt'
Expand Down
10 changes: 10 additions & 0 deletions pupgui2/pupgui2aboutdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

from pupgui2.constants import APP_NAME, APP_VERSION, APP_GHAPI_URL, ABOUT_TEXT, BUILD_INFO
from pupgui2.constants import DAVIDOTEK_KOFI_URL, PROTONUPQT_GITHUB_URL
from pupgui2.steamutil import install_steam_library_shortcut
from pupgui2.util import config_theme, apply_dark_theme, config_advanced_mode
from pupgui2.util import open_webbrowser_thread
from pupgui2.util import install_directory


class PupguiAboutDialog(QObject):
Expand Down Expand Up @@ -68,6 +70,8 @@ def setup_ui(self):
self.ui.checkAdvancedMode.setChecked(config_advanced_mode() == 'enabled')
self.ui.checkAdvancedMode.stateChanged.connect(self.check_advanced_mode_state_changed)

self.ui.btnAddSteamShortcut.clicked.connect(self.btn_add_steam_shortcut_clicked)

if os.getenv('APPIMAGE') is None:
self.ui.btnCheckForUpdates.setVisible(False)

Expand Down Expand Up @@ -113,3 +117,9 @@ def tag_name_to_version(self, tag_name : str):
tag_name = tag_name.replace('v', '')
vers = tag_name.split('.')
return [0, 0, 0] if len(vers) != 3 else vers

def btn_add_steam_shortcut_clicked(self):
result = install_steam_library_shortcut(install_directory())
if result != 1:
self.ui.btnAddSteamShortcut.setText(self.tr('Added shortcut!'))
self.ui.btnAddSteamShortcut.setEnabled(False)
Binary file added pupgui2/resources/img/appicon256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions pupgui2/resources/ui/pupgui2_aboutdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnAddSteamShortcut">
<property name="toolTip">
<string>Adds a shortcut to open ProtonUp-Qt from your Steam library.
To remove the shortcut, open Steam and select &quot;remove non-Steam game from your library&quot;.</string>
</property>
<property name="text">
<string>Add Steam shortcut</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnAboutQt">
<property name="text">
Expand Down
77 changes: 77 additions & 0 deletions pupgui2/steamutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import vdf
import requests
import threading
import pkgutil
from steam.utils.appcache import parse_appinfo

from PySide6.QtCore import Signal
from PySide6.QtWidgets import QMessageBox, QApplication

from pupgui2.constants import APP_NAME, APP_ID, APP_ICON_FILE
from pupgui2.constants import LOCAL_AWACY_GAME_LIST, PROTONDB_API_URL
from pupgui2.constants import STEAM_STL_INSTALL_PATH, STEAM_STL_CONFIG_PATH, STEAM_STL_SHELL_FILES, STEAM_STL_FISH_VARIABLES
from pupgui2.datastructures import SteamApp, AWACYStatus, BasicCompatTool, CTType
Expand Down Expand Up @@ -497,3 +499,78 @@ def remove_steamtinkerlaunch(compat_folder='', remove_config=True, ctmod_object=
except IOError as e:
print('Something went wrong trying to uninstall SteamTinkerLaunch. Aborting...', e)
return False


def install_steam_library_shortcut(steam_config_folder: str, remove_shortcut=False) -> int:
"""
Adds a shortcut to launch this app to the Steam Library
Return: 0=success, 1=error, 2=already installed
"""
users_folder = os.path.realpath(os.path.join(os.path.expanduser(steam_config_folder), os.pardir, 'userdata'))

try:
if not os.path.isfile(APP_ICON_FILE):
with open(APP_ICON_FILE, 'wb') as f:
f.write(pkgutil.get_data(__name__, 'resources/img/appicon256.png'))

for userf in os.listdir(users_folder):
user_cfg_dir = os.path.join(users_folder, userf, 'config')
shortcuts_file = os.path.join(user_cfg_dir, 'shortcuts.vdf')

if not os.path.exists(user_cfg_dir):
continue

shortcuts_vdf = {}
sid=-1
if os.path.exists(shortcuts_file):
with open(shortcuts_file, 'rb') as f:
shortcuts_vdf = vdf.binary_load(f)

for sid in list(shortcuts_vdf.get('shortcuts', {}).keys()):
svalue = shortcuts_vdf.get('shortcuts', {}).get(sid)
if APP_NAME in svalue.get('AppName', ''):
if remove_shortcut:
shortcuts_vdf.get('shortcuts', {}).pop(sid)
else:
return 2

with open(shortcuts_file, 'wb') as f:
if not remove_shortcut:
run_config = ['', '']
if os.path.exists('/.flatpak-info'):
run_config = [f'/usr/bin/flatpak', f'run {APP_ID}']
elif exe := subprocess.run(['which', APP_ID], universal_newlines=True, stdout=subprocess.PIPE).stdout.strip():
run_config = [exe, '']
elif exe := os.getenv('APPIMAGE'):
if APP_NAME in exe:
exe = os.path.join(exe, os.pardir, APP_NAME + '*.AppImage') # remove version from file name
run_config = [exe, '']
else:
return 1

sid = str(int(sid) + 1)
shortcuts_vdf.setdefault('shortcuts', {})[sid] = {
'appid': 1621167219,
'AppName': APP_NAME,
'Exe': f'"{run_config[0]}"',
'StartDir': './',
'icon': APP_ICON_FILE,
'ShortcutPath': '',
'LaunchOptions': run_config[1],
'IsHidden': 0,
'AllowDesktopConfig': 1,
'AllowOverlay': 1,
'OpenVR': 0,
'Devkit': 0,
'DevkitGameID': '',
'DevkitOverrideAppID': 0,
'LastPlayTime': 0,
'FlatpakAppID': '',
'tags': {}
}

f.write(vdf.binary_dumps(shortcuts_vdf))
except Exception as e:
print(f'Error: Could not add {APP_NAME} as Steam shortcut:', e)

return 0

0 comments on commit 2a91c1d

Please sign in to comment.