Better implementation for desktop entry source

This commit is contained in:
kramo
2023-08-27 13:38:15 +02:00
parent c607a65d1f
commit a1afb98b20
5 changed files with 31 additions and 132 deletions

View File

@@ -17,7 +17,9 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
import os
import shlex
import subprocess
from hashlib import sha3_256
from pathlib import Path
from time import time
@@ -62,7 +64,7 @@ class DesktopSourceIterable(SourceIterable):
icon_theme.add_search_path(str(path))
terminal_exec = self.get_terminal_exec()
launch_command, full_path = self.check_launch_command()
for path in search_paths:
if str(path).startswith("/app/"):
@@ -116,18 +118,6 @@ class DesktopSourceIterable(SourceIterable):
except GLib.GError:
pass
try:
terminal = keyfile.get_boolean("Desktop Entry", "Terminal")
except GLib.GError:
terminal = False
try:
cd_path = (
"cd " + keyfile.get_string("Desktop Entry", "Path") + " && "
)
except GLib.GError:
cd_path = ""
values = {
"source": self.source.source_id,
"added": added_time,
@@ -136,12 +126,7 @@ class DesktopSourceIterable(SourceIterable):
+ sha3_256(
str(entry).encode("utf-8"), usedforsecurity=False
).hexdigest(),
"executable": cd_path
+ (
(terminal_exec + shlex.quote(executable))
if terminal
else executable
),
"executable": f"{launch_command} {shlex.quote(str(entry if full_path else entry.stem))}",
}
game = Game(values)
@@ -177,21 +162,27 @@ class DesktopSourceIterable(SourceIterable):
yield (game, additional_data)
def get_terminal_exec(self) -> str:
match shared.schema.get_enum("desktop-terminal"):
case 0:
terminal_exec = shared.schema.get_string("desktop-terminal-custom-exec")
case 1:
terminal_exec = "xdg-terminal-exec"
case 2:
terminal_exec = "kgx -e"
case 3:
terminal_exec = "gnome-terminal --"
case 4:
terminal_exec = "konsole -e"
case 5:
terminal_exec = "xterm -e"
return terminal_exec + " "
def check_launch_command(self) -> (str, bool):
"""Check whether `gio launch` `gtk4-launch` or `gtk-launch` are available on the system"""
commands = (("gio launch", True), ("gtk4-launch", False), ("gtk-launch", False))
flatpak_str = "flatpak-spawn --host /bin/sh -c "
for command, full_path in commands:
check_command = (
"gio help launch"
if command == "gio launch"
else f"type {command} &> /dev/null"
)
if os.getenv("FLATPAK_ID") == shared.APP_ID:
check_command = flatpak_str + shlex.quote(check_command)
try:
subprocess.run(check_command, shell=True, check=True)
return command, full_path
except subprocess.CalledProcessError:
pass
return commands[2]
class DesktopLocations(NamedTuple):

View File

@@ -75,10 +75,6 @@ class CartridgesApplication(Adw.Application):
if os.name == "nt":
migrate_files_v1_to_v2()
else:
if not shared.state_schema.get_boolean("terminal-check-done"):
self.check_desktop_terminals()
shared.state_schema.set_boolean("terminal-check-done", True)
# Set fallback icon-name
Gtk.Window.set_default_icon_name(shared.APP_ID)
@@ -149,22 +145,6 @@ class CartridgesApplication(Adw.Application):
self.win.present()
def check_desktop_terminals(self) -> None:
"""Look for an installed terminal for desktop entries and set the relevant gsetting"""
terminals = ("xdg-terminal-exec", "kgx", "gnome-terminal", "konsole", "xterm")
for index, command in enumerate(terminals):
command = f"type {command} &> /dev/null"
if os.getenv("FLATPAK_ID") == shared.APP_ID:
command = "flatpak-spawn --host /bin/sh -c " + shlex.quote(command)
try:
subprocess.run(command, shell=True, check=True)
shared.schema.set_enum("desktop-terminal", index + 1)
return
except subprocess.CalledProcessError:
pass
def load_games_from_disk(self) -> None:
if shared.games_dir.is_dir():
for game_file in shared.games_dir.iterdir():

View File

@@ -28,7 +28,6 @@ from gi.repository import Adw, Gio, GLib, Gtk
from src import shared
from src.game import Game
from src.importer.sources.bottles_source import BottlesSource
from src.importer.sources.desktop_source import DesktopSource
from src.importer.sources.flatpak_source import FlatpakSource
from src.importer.sources.heroic_source import HeroicSource
from src.importer.sources.itch_source import ItchSource
@@ -98,10 +97,7 @@ class PreferencesWindow(Adw.PreferencesWindow):
flatpak_data_file_chooser_button = Gtk.Template.Child()
flatpak_import_launchers_switch = Gtk.Template.Child()
desktop_expander_row = Gtk.Template.Child()
desktop_terminal_exec_row = Gtk.Template.Child()
desktop_tereminal_custom_exec_revealer = Gtk.Template.Child()
desktop_tereminal_custom_exec = Gtk.Template.Child()
desktop_switch = Gtk.Template.Child()
sgdb_key_group = Gtk.Template.Child()
sgdb_key_entry_row = Gtk.Template.Child()
@@ -150,7 +146,6 @@ class PreferencesWindow(Adw.PreferencesWindow):
for source_class in (
BottlesSource,
FlatpakSource,
DesktopSource,
HeroicSource,
ItchSource,
LegendarySource,
@@ -165,36 +160,6 @@ class PreferencesWindow(Adw.PreferencesWindow):
else:
self.init_source_row(source)
# Desktop Terminal Executable
def set_terminal_exec(widget: Adw.ComboRow, _param: Any) -> None:
shared.schema.set_enum("desktop-terminal", widget.get_selected())
self.desktop_tereminal_custom_exec_revealer.set_reveal_child(
widget.get_selected() == 0
)
self.desktop_terminal_exec_row.connect("notify::selected", set_terminal_exec)
self.desktop_terminal_exec_row.set_selected(
terminal_value := shared.schema.get_enum("desktop-terminal")
)
if not terminal_value:
set_terminal_exec(
self.desktop_terminal_exec_row, None
) # The default value is supposed to be 4294967295, but it's 0 and I can't change it
self.desktop_tereminal_custom_exec.set_text(
shared.schema.get_string("desktop-terminal-custom-exec")
)
def desktop_custom_exec_changed(*_args: Any) -> None:
shared.schema.set_string(
"desktop-terminal-custom-exec",
self.desktop_tereminal_custom_exec.get_text(),
)
self.desktop_tereminal_custom_exec.connect(
"changed", desktop_custom_exec_changed
)
# SteamGridDB
def sgdb_key_changed(*_args: Any) -> None:
shared.schema.set_string("sgdb-key", self.sgdb_key_entry_row.get_text())
@@ -236,6 +201,7 @@ class PreferencesWindow(Adw.PreferencesWindow):
"sgdb",
"sgdb-prefer",
"sgdb-animated",
"desktop",
}
)