Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize shortcuts #766

Closed
wants to merge 10 commits into from
2 changes: 0 additions & 2 deletions electron/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,6 @@ class GlobalConfigV0 extends GlobalConfig {
return {
checkUpdatesInterval: 10,
enableUpdates: false,
addDesktopShortcuts: false,
addStartMenuShortcuts: false,
autoInstallDxvk: false,
checkForUpdatesOnStartup: true,
customWinePaths: isWindows ? null : [],
Expand Down
2 changes: 1 addition & 1 deletion electron/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ abstract class Game {
abstract hasUpdate() : Promise<boolean>
abstract import(path : string) : Promise<ExecResult>
abstract install(args: InstallArgs) : Promise<{status: string}>
abstract addShortcuts(): Promise<void>
abstract addShortcuts({addToStartMenu, addToDesktop}: {addToStartMenu: boolean, addToDesktop: boolean}): Promise<void>
abstract launch(launchArguments?: string) : Promise<ExecResult>
abstract moveInstall(newInstallPath : string) : Promise<string>
abstract repair() : Promise<ExecResult>
Expand Down
20 changes: 11 additions & 9 deletions electron/legendary/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,25 +275,27 @@ class LegendaryGame extends Game {
/**
* Adds a desktop shortcut to $HOME/Desktop and to /usr/share/applications
* so that the game can be opened from the start menu and the desktop folder.
* Both can be disabled with addDesktopShortcuts and addStartMenuShortcuts
* @async
* @public
*/
public async addShortcuts(fromMenu?: boolean) {
public async addShortcuts({addToStartMenu, addToDesktop}: {addToStartMenu: boolean, addToDesktop: boolean}) {
if (process.platform === 'darwin') {
return
}

const gameInfo = await this.getGameInfo()
const launchWithProtocol = `heroic://launch/${gameInfo.app_name}`
const [ desktopFile, menuFile ] = this.shortcutFiles(gameInfo.title)
const { addDesktopShortcuts, addStartMenuShortcuts } = await GlobalConfig.get().getSettings()
const sanitizedTitle = gameInfo.title
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the issue at #760 is on Windows, this actually doesn't fix it, since this title is used only on linux.
We need another solution for windows.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What title is used in windows to create the shortcut filename?

const pattern = new RegExp('[:|/|*|?|<|>||]')
This conversation was marked as resolved.
Show resolved Hide resolved
sanitizedTitle.replaceAll(pattern, '')

const [ desktopFile, menuFile ] = this.shortcutFiles(sanitizedTitle)

switch (process.platform) {
case 'linux': {
const icon = await this.getIcon(gameInfo.app_name)
const shortcut = `[Desktop Entry]
Name=${gameInfo.title}
Name=${sanitizedTitle}
Exec=xdg-open ${launchWithProtocol}
Terminal=false
Type=Application
Expand All @@ -302,12 +304,12 @@ Icon=${icon}
Categories=Game;
`

if (addDesktopShortcuts || fromMenu) {
if (addToDesktop) {
writeFile(desktopFile, shortcut, () => {
logInfo('Shortcut saved on ' + desktopFile)
})
}
if (addStartMenuShortcuts || fromMenu) {
if (addToStartMenu) {
writeFile(menuFile, shortcut, () => {
logInfo('Shortcut saved on ' + menuFile)
})
Expand All @@ -321,11 +323,11 @@ Categories=Game;
iconIndex: 0
}

if (addDesktopShortcuts || fromMenu) {
if (addToDesktop) {
shell.writeShortcutLink(desktopFile, shortcutOptions)
}

if (addStartMenuShortcuts || fromMenu) {
if (addToStartMenu) {
shell.writeShortcutLink(menuFile, shortcutOptions)
}
break
Expand Down
16 changes: 7 additions & 9 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,8 @@ ipcMain.handle('openDialog', async (e, args) => {
})

const openMessageBox = async (args: Electron.MessageBoxOptions) => {
const { response } = await showMessageBox({ ...args })
return { response }
const { response, checkboxChecked } = await showMessageBox({ ...args })
return { response, checkboxChecked }
}

ipcMain.handle('openMessageBox', async (_, args: Electron.MessageBoxOptions) => {
Expand Down Expand Up @@ -835,14 +835,12 @@ ipcMain.handle('egsSync', async (event, args) => {
}
})

ipcMain.on('addShortcut', async (event, appName: string, fromMenu: boolean) => {
ipcMain.on('addShortcut', async (event, appName: string, { desktop, startMenu } : {desktop: boolean, startMenu: boolean}) => {
const game = Game.get(appName)
game.addShortcuts(fromMenu)
openMessageBox({
buttons: [i18next.t('box.ok', 'Ok')],
message: i18next.t('box.shortcuts.message', 'Shortcuts were created on Desktop and Start Menu'),
title: i18next.t('box.shortcuts.title', 'Shortcuts')
})
const opts = {
addToDesktop: desktop, addToStartMenu: startMenu
}
game.addShortcuts(opts)
})

ipcMain.on('removeShortcut', async (event, appName: string) => {
Expand Down
2 changes: 0 additions & 2 deletions electron/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ interface About {
export interface AppSettings {
checkUpdatesInterval: number,
enableUpdates: boolean
addDesktopShortcuts: boolean
addStartMenuShortcuts: boolean
altLegendaryBin: string
audioFix: boolean
autoInstallDxvk: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Game/GameSubMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default function GamesSubmenu({
}

function handleShortcuts() {
ipcRenderer.send('addShortcut', appName, true)
ipcRenderer.send('addShortcut', appName, { desktop: true, startMenu: true })
}

useEffect(() => {
Expand Down
29 changes: 0 additions & 29 deletions src/screens/Settings/components/OtherSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ interface Props {
offlineMode: boolean
otherOptions: string
primeRun: boolean
addDesktopShortcuts: boolean
addGamesToStartMenu: boolean
discordRPC: boolean
setLauncherArgs: (value: string) => void
setOtherOptions: (value: string) => void
Expand All @@ -38,8 +36,6 @@ interface Props {
toggleOffline: () => void
togglePrimeRun: () => void
toggleUseGameMode: () => void
toggleAddDesktopShortcuts: () => void
toggleAddGamesToStartMenu: () => void
toggleDiscordRPC: () => void
targetExe: string
useGameMode: boolean
Expand All @@ -64,10 +60,6 @@ export default function OtherSettings({
primeRun,
togglePrimeRun,
setMaxRecentGames,
addDesktopShortcuts,
addGamesToStartMenu,
toggleAddDesktopShortcuts,
toggleAddGamesToStartMenu,
discordRPC,
toggleDiscordRPC,
maxRecentGames,
Expand All @@ -82,7 +74,6 @@ export default function OtherSettings({
const { platform } = useContext(ContextProvider)
const isWin = platform === 'win32'
const isLinux = platform === 'linux'
const supportsShortcuts = isWin || isLinux

return (
<>
Expand Down Expand Up @@ -158,26 +149,6 @@ export default function OtherSettings({
<ToggleSwitch value={offlineMode} handleChange={toggleOffline} />
</span>
</span>
{supportsShortcuts && isDefault && <>
<span className="setting">
<span className="toggleWrapper">
{t('setting.adddesktopshortcuts', 'Add desktop shortcuts automatically')}
<ToggleSwitch
value={addDesktopShortcuts}
handleChange={toggleAddDesktopShortcuts}
/>
</span>
</span>
<span className="setting">
<span className="toggleWrapper">
{t('setting.addgamestostartmenu', 'Add games to start menu automatically')}
<ToggleSwitch
value={addGamesToStartMenu}
handleChange={toggleAddGamesToStartMenu}
/>
</span>
</span>
</>}
{isDefault && <span className="setting">
<span className="toggleWrapper">
{t('setting.discordRPC', 'Enable Discord Rich Presence')}
Expand Down
18 changes: 0 additions & 18 deletions src/screens/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ function Settings() {
)
const [customWinePaths, setCustomWinePaths] = useState([] as Array<string>)
const [savesPath, setSavesPath] = useState('')
const {
on: addDesktopShortcuts,
toggle: toggleAddDesktopShortcuts,
setOn: setAddDesktopShortcuts
} = useToggle(false)
const {
on: addStartMenuShortcuts,
toggle: toggleAddGamesToStartMenu,
setOn: setAddGamesToStartMenu
} = useToggle(false)
const {
on: useGameMode,
toggle: toggleUseGameMode,
Expand Down Expand Up @@ -211,8 +201,6 @@ function Settings() {
setMaxWorkers(config.maxWorkers ?? 0)
setMaxRecentGames(config.maxRecentGames ?? 5)
setCustomWinePaths(config.customWinePaths || [])
setAddDesktopShortcuts(config.addDesktopShortcuts || false)
setAddGamesToStartMenu(config.addStartMenuShortcuts || false)
setCustomWinePaths(config.customWinePaths || [])
setCheckForUpdatesOnStartup(config.checkForUpdatesOnStartup || true)
setTargetExe(config.targetExe || '')
Expand All @@ -238,8 +226,6 @@ function Settings() {

const GlobalSettings = {
altLegendaryBin,
addDesktopShortcuts,
addStartMenuShortcuts,
audioFix,
autoInstallDxvk,
checkForUpdatesOnStartup,
Expand Down Expand Up @@ -409,10 +395,6 @@ function Settings() {
isDefault={isDefault}
maxRecentGames={maxRecentGames}
setMaxRecentGames={setMaxRecentGames}
addDesktopShortcuts={addDesktopShortcuts}
addGamesToStartMenu={addStartMenuShortcuts}
toggleAddDesktopShortcuts={toggleAddDesktopShortcuts}
toggleAddGamesToStartMenu={toggleAddGamesToStartMenu}
toggleDiscordRPC={toggleDiscordRPC}
discordRPC={discordRPC}
targetExe={targetExe}
Expand Down
2 changes: 1 addition & 1 deletion src/state/GlobalState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class GlobalState extends PureComponent<Props> {
if (percent > 95) {
notifyKey = 'finished'
filter = 'installed'
ipcRenderer.send('addShortcut', appName, false)
ipcRenderer.send('addShortcut', appName, { desktop: true, startMenu: true })
}

this.handleFilter(filter)
Expand Down
2 changes: 0 additions & 2 deletions src/test_helpers/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ const test_plugin = new TestType<GameInfo>({
*/
const test_appsettings = new TestType<AppSettings>({
altLegendaryBin: '',
addDesktopShortcuts: false,
addStartMenuShortcuts: false,
audioFix: false,
autoInstallDxvk: false,
autoSyncSaves: false,
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ interface About {
}
export interface AppSettings {
altLegendaryBin: string
addDesktopShortcuts: boolean
addStartMenuShortcuts: boolean
audioFix: boolean
autoInstallDxvk: boolean
autoSyncSaves: boolean
Expand Down