More reliance on GLib

This commit is contained in:
kramo
2023-07-01 10:44:15 +02:00
parent 1f25bed842
commit 495755f278

View File

@@ -17,12 +17,10 @@
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import re
import subprocess
from pathlib import Path from pathlib import Path
from time import time from time import time
from gi.repository import Gtk from gi.repository import GLib, Gtk
from src import shared from src import shared
from src.game import Game from src.game import Game
@@ -41,62 +39,42 @@ class FlatpakSourceIterator(SourceIterator):
icon_theme = Gtk.IconTheme.new() icon_theme = Gtk.IconTheme.new()
icon_theme.add_search_path(str(self.source.data_location["icons"])) icon_theme.add_search_path(str(self.source.data_location["icons"]))
try: blacklist = (
process = subprocess.run( {"hu.kramo.Cartridges"}
("flatpak-spawn", "--host", "flatpak", "list", "--columns=application"), if shared.schema.get_boolean("flatpak-import-launchers")
capture_output=True, else {
encoding="utf-8", "hu.kramo.Cartridges",
check=True, "com.valvesoftware.Steam",
) "net.lutris.Lutris",
flatpak_ids = process.stdout.split("\n") "com.heroicgameslauncher.hgl",
"com.usebottles.Bottles",
to_remove = ( "io.itch.itch",
{"hu.kramo.Cartridges"} }
if shared.schema.get_boolean("flatpak-import-launchers") )
else {
"hu.kramo.Cartridges",
"com.valvesoftware.Steam",
"net.lutris.Lutris",
"com.heroicgameslauncher.hgl",
"com.usebottles.Bottles",
"io.itch.itch",
}
)
for item in to_remove:
if item in flatpak_ids:
flatpak_ids.remove(item)
except subprocess.CalledProcessError:
return
for entry in (self.source.data_location["applications"]).iterdir(): for entry in (self.source.data_location["applications"]).iterdir():
flatpak_id = entry.stem keyfile = GLib.KeyFile.new()
if flatpak_id not in flatpak_ids: try:
continue keyfile.load_from_file(str(entry), 0)
with entry.open("r", encoding="utf-8") as open_file: if "Game" not in keyfile.get_string_list("Desktop Entry", "Categories"):
string = open_file.read() continue
desktop_values = {"Name": None, "Icon": None, "Categories": None} if (
for key in desktop_values: flatpak_id := keyfile.get_string("Desktop Entry", "X-Flatpak")
if regex := re.findall(f"{key}=(.*)\n", string): ) in blacklist:
desktop_values[key] = regex[0] continue
if not desktop_values["Name"]: name = keyfile.get_string("Desktop Entry", "Name")
continue
if not desktop_values["Categories"]: except GLib.GError:
continue
if not "Game" in desktop_values["Categories"].split(";"):
continue continue
values = { values = {
"source": self.source.id, "source": self.source.id,
"added": added_time, "added": added_time,
"name": desktop_values["Name"], "name": name,
"game_id": self.source.game_id_format.format(game_id=flatpak_id), "game_id": self.source.game_id_format.format(game_id=flatpak_id),
"executable": self.source.executable_format.format( "executable": self.source.executable_format.format(
flatpak_id=flatpak_id flatpak_id=flatpak_id
@@ -105,10 +83,16 @@ class FlatpakSourceIterator(SourceIterator):
game = Game(values) game = Game(values)
additional_data = {} additional_data = {}
if icon_name := desktop_values["Icon"]:
try:
if ( if (
icon_path := icon_theme.lookup_icon( icon_path := icon_theme.lookup_icon(
icon_name, None, 512, 1, shared.win.get_direction(), 0 keyfile.get_string("Desktop Entry", "Icon"),
None,
512,
1,
shared.win.get_direction(),
0,
) )
.get_file() .get_file()
.get_path() .get_path()
@@ -116,6 +100,8 @@ class FlatpakSourceIterator(SourceIterator):
additional_data = {"local_icon_path": Path(icon_path)} additional_data = {"local_icon_path": Path(icon_path)}
else: else:
pass pass
except GLib.GError:
pass
# Produce game # Produce game
yield (game, additional_data) yield (game, additional_data)