From 6b2678c5f69a8cb2b4ad96c8740b6a4c2ff69241 Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:10:40 +0200 Subject: [PATCH] Use new functions in preferences --- src/importers/bottles_importer.py | 32 +++++---- src/importers/heroic_importer.py | 42 +++++------ src/importers/itch_importer.py | 28 +++++--- src/importers/lutris_importer.py | 54 +++++++++----- src/importers/steam_importer.py | 33 ++++++--- src/preferences.py | 112 +++++++++++++----------------- src/utils/check_install.py | 2 +- 7 files changed, 167 insertions(+), 136 deletions(-) diff --git a/src/importers/bottles_importer.py b/src/importers/bottles_importer.py index f144aa5..a4d9508 100644 --- a/src/importers/bottles_importer.py +++ b/src/importers/bottles_importer.py @@ -25,26 +25,32 @@ import yaml from .check_install import check_install -def bottles_importer(win): - schema = win.schema +def bottles_installed(win, path=None): location_key = "bottles-location" - bottles_dir = Path(schema.get_string(location_key)).expanduser() + bottles_dir = ( + path if path else Path(win.schema.get_string(location_key)).expanduser() + ) check = "library.yml" if not (bottles_dir / check).is_file(): locations = ( - Path.home() - / ".var" - / "app" - / "com.usebottles.bottles" - / "data" - / "bottles", - win.data_dir / "bottles", + (Path(),) + if path + else ( + Path.home() / ".var/app/com.usebottles.bottles/data/bottles", + win.data_dir / "bottles", + ) ) - bottles_dir = check_install(check, locations, (schema, location_key)) - if not bottles_dir: - return + bottles_dir = check_install(check, locations, (win.schema, location_key)) + + return bottles_dir + + +def bottles_importer(win): + bottles_dir = bottles_installed(win) + if not bottles_dir: + return current_time = int(time()) diff --git a/src/importers/heroic_importer.py b/src/importers/heroic_importer.py index d28e108..458b351 100644 --- a/src/importers/heroic_importer.py +++ b/src/importers/heroic_importer.py @@ -26,39 +26,41 @@ from time import time from .check_install import check_install -def heroic_importer(win): - schema = win.schema +def heroic_installed(win, path=None): location_key = "heroic-location" - heroic_dir = Path(schema.get_string(location_key)).expanduser() + heroic_dir = ( + path if path else Path(win.schema.get_string(location_key)).expanduser() + ) check = "config.json" if not (heroic_dir / check).is_file(): locations = ( - Path.home() - / ".var" - / "app" - / "com.heroicgameslauncher.hgl" - / "config" - / "heroic", - win.config_dir / "heroic", + (Path(),) + if path + else ( + Path.home() / ".var/app/com.heroicgameslauncher.hgl/config/heroic", + win.config_dir / "heroic", + ) ) - if os.name == "nt": + if os.name == "nt" and not path: locations += (Path(os.getenv("appdata")) / "heroic",) - heroic_dir = check_install(check, locations, (schema, location_key)) - if not heroic_dir: - return + heroic_dir = check_install(check, locations, (win.schema, location_key)) - schema = win.schema - check = "config.json" + return heroic_dir + + +def heroic_importer(win): + heroic_dir = heroic_installed(win) + if not heroic_dir: + return current_time = int(time()) - importer = win.importer # Import Epic games - if not schema.get_boolean("heroic-import-epic"): + if not win.schema.get_boolean("heroic-import-epic"): pass elif (heroic_dir / "store_cache" / "legendary_library.json").exists(): data = (heroic_dir / "store_cache" / "legendary_library.json").read_text( @@ -112,7 +114,7 @@ def heroic_importer(win): pass # Import GOG games - if not schema.get_boolean("heroic-import-gog"): + if not win.schema.get_boolean("heroic-import-gog"): pass elif (heroic_dir / "gog_store" / "installed.json").exists(): data = (heroic_dir / "gog_store" / "installed.json").read_text("utf-8") @@ -160,7 +162,7 @@ def heroic_importer(win): importer.save_game(values, image_path if image_path.exists() else None) # Import sideloaded games - if not schema.get_boolean("heroic-import-sideload"): + if not win.schema.get_boolean("heroic-import-sideload"): pass elif (heroic_dir / "sideload_apps" / "library.json").exists(): data = (heroic_dir / "sideload_apps" / "library.json").read_text("utf-8") diff --git a/src/importers/itch_importer.py b/src/importers/itch_importer.py index 9d9dfde..6aae018 100644 --- a/src/importers/itch_importer.py +++ b/src/importers/itch_importer.py @@ -113,25 +113,35 @@ def get_games_async(win, rows, importer): task.run_in_thread(create_func(current_time, win, row)) -def itch_importer(win): - schema = win.schema +def itch_installed(win, path=None): location_key = "itch-location" - itch_dir = Path(schema.get_string(location_key)).expanduser() + itch_dir = path if path else Path(win.schema.get_string(location_key)).expanduser() check = Path("db") / "butler.db" if not (itch_dir / check).is_file(): locations = ( - Path.home() / ".var" / "app" / "io.itch.itch" / "config" / "itch", - win.config_dir / "itch", + (Path(),) + if path + else ( + Path.home() / ".var/app/io.itch.itch/config/itch", + win.config_dir / "itch", + ) ) - if os.name == "nt": + if os.name == "nt" and not path: locations += (Path(os.getenv("appdata")) / "itch",) - if not check_install(check, locations, (schema, location_key)): - return + itch_dir = check_install(check, locations, (win.schema, location_key)) - database_path = (Path(schema.get_string(location_key)) / "db").expanduser() + return itch_dir + + +def itch_importer(win): + itch_dir = itch_installed(win) + if not itch_dir: + return + + database_path = (itch_dir / "db").expanduser() db_cache_dir = win.cache_dir / "cartridges" / "itch" db_cache_dir.mkdir(parents=True, exist_ok=True) diff --git a/src/importers/lutris_importer.py b/src/importers/lutris_importer.py index 2f63986..9dc4656 100644 --- a/src/importers/lutris_importer.py +++ b/src/importers/lutris_importer.py @@ -25,38 +25,56 @@ from time import time from .check_install import check_install -def lutris_importer(win): - schema = win.schema +def lutris_installed(win, path=None): location_key = "lutris-location" - lutris_dir = Path(schema.get_string(location_key)).expanduser() + lutris_dir = ( + path if path else Path(win.schema.get_string(location_key)).expanduser() + ) check = "pga.db" if not (lutris_dir / check).is_file(): locations = ( - Path.home() / ".var" / "app" / "net.lutris.Lutris" / "data" / "lutris", - win.data_dir / "lutris", + (Path(),) + if path + else ( + Path.home() / ".var/app/net.lutris.Lutris/data/lutris", + win.data_dir / "lutris", + ) ) - lutris_dir = check_install(check, locations, (schema, location_key)) - if not lutris_dir: - return + lutris_dir = check_install(check, locations, (win.schema, location_key)) + return lutris_dir + + +def lutris_cache_exists(win, path=None): cache_key = "lutris-cache-location" - cache_dir = Path(schema.get_string(cache_key)).expanduser() + cache_dir = path if path else Path(win.schema.get_string(cache_key)).expanduser() cache_check = "coverart" if not (cache_dir / cache_check).exists(): cache_locations = ( - Path.home() / ".var" / "app" / "net.lutris.Lutris" / "cache" / "lutris", - win.cache_dir / "lutris", + (Path(),) + if path + else ( + Path.home() / ".var" / "app" / "net.lutris.Lutris" / "cache" / "lutris", + win.cache_dir / "lutris", + ) ) - cache_dir = check_install(check, cache_locations, (schema, location_key)) - if not cache_dir: - return + cache_dir = check_install(cache_check, cache_locations, (win.schema, cache_key)) - database_path = (Path(schema.get_string(location_key))).expanduser() - cache_dir = Path(schema.get_string(cache_key)).expanduser() + return cache_dir + + +def lutris_importer(win): + lutris_dir = lutris_installed(win) + if not lutris_dir: + return + + cache_dir = lutris_cache_exists(win) + if not cache_dir: + return db_cache_dir = win.cache_dir / "cartridges" / "lutris" db_cache_dir.mkdir(parents=True, exist_ok=True) @@ -64,7 +82,7 @@ def lutris_importer(win): # Copy the file because sqlite3 doesn't like databases in /run/user/ database_tmp_path = db_cache_dir / "pga.db" - for db_file in database_path.glob("pga.db*"): + for db_file in lutris_dir.glob("pga.db*"): copyfile(db_file, (db_cache_dir / db_file.name)) db_request = """ @@ -87,7 +105,7 @@ def lutris_importer(win): # No need to unlink temp files as they disappear when the connection is closed database_tmp_path.unlink(missing_ok=True) - if not schema.get_boolean("lutris-import-steam"): + if not win.schema.get_boolean("lutris-import-steam"): rows = [row for row in rows if not row[3] == "steam"] current_time = int(time()) diff --git a/src/importers/steam_importer.py b/src/importers/steam_importer.py index 5921833..b9798fd 100644 --- a/src/importers/steam_importer.py +++ b/src/importers/steam_importer.py @@ -122,31 +122,42 @@ def get_games_async(win, appmanifests, steam_dir, importer): ) -def steam_importer(win): - schema = win.schema +def steam_installed(win, path=None): location_key = "steam-location" - steam_dir = Path(schema.get_string(location_key)).expanduser() + steam_dir = Path(win.schema.get_string(location_key)).expanduser() check = "steamapps" if not (steam_dir / check).is_file(): subdirs = ("steam", "Steam") - locations = ( - Path.home() / ".steam", - win.data_dir / "Steam", - Path.home() / ".var" / "app" / "com.valvesoftware.Steam" / "data" / "Steam", + (path,) + if path + else ( + steam_dir, + Path.home() / ".steam", + win.data_dir / "Steam", + Path.home() / ".var/app/com.valvesoftware.Steam/data/Steam", + ) ) if os.name == "nt": locations += (Path(os.getenv("programfiles(x86)")) / "Steam",) - steam_dir = check_install(check, locations, (schema, location_key), subdirs) - if not steam_dir: - return + steam_dir = check_install(check, locations, (win.schema, location_key), subdirs) + + return steam_dir + + +def steam_importer(win): + steam_dir = steam_installed(win) + if not steam_dir: + return appmanifests = [] - steam_dirs = [Path(directory) for directory in schema.get_strv("steam-extra-dirs")] + steam_dirs = [ + Path(directory) for directory in win.schema.get_strv("steam-extra-dirs") + ] steam_dirs.append(steam_dir) for directory in steam_dirs: diff --git a/src/preferences.py b/src/preferences.py index 13a8d12..19d233e 100644 --- a/src/preferences.py +++ b/src/preferences.py @@ -19,63 +19,61 @@ import os from pathlib import Path -from shutil import move, copyfile +from shutil import copyfile, move from gi.repository import Adw, Gio, GLib, Gtk +from .bottles_importer import bottles_installed from .create_dialog import create_dialog from .get_games import get_games +from .heroic_importer import heroic_installed +from .itch_importer import itch_installed +from .lutris_importer import lutris_cache_exists, lutris_installed from .save_game import save_game +from .steam_importer import steam_installed class ImportPreferences: def __init__( self, - window, + win, source_id, name, - install_key, - paths, + check_func, expander_row, file_chooser_button, config=False, ): def set_dir(_source, result, _unused): try: - path = Path(window.file_chooser.select_folder_finish(result).get_path()) - - def response(widget, response): - if response == "choose_folder": - window.choose_folder(widget, set_dir) - - if not any((path / current_path).exists() for current_path in paths): - create_dialog( - window.win, - _("Installation Not Found"), - # The variable is the name of the game launcher - _("Select the {} configuration directory.").format(name) - if config - # The variable is the name of the game launcher - else _("Select the {} data directory.").format(name), - "choose_folder", - _("Set Location"), - ).connect("response", response) - else: - window.schema.set_string( - install_key, - str(path), - ) + path = Path(win.file_chooser.select_folder_finish(result).get_path()) except GLib.GError: - pass + return - window.schema.bind( + def response(widget, response): + if response == "choose_folder": + win.choose_folder(widget, set_dir) + + if not check_func(win, path): + create_dialog( + win, + _("Installation Not Found"), + # The variable is the name of the game launcher + _("Select the {} configuration directory.").format(name) if config + # The variable is the name of the game launcher + else _("Select the {} data directory.").format(name), + "choose_folder", + _("Set Location"), + ).connect("response", response) + + win.schema.bind( source_id, expander_row, "enable-expansion", Gio.SettingsBindFlags.DEFAULT, ) - file_chooser_button.connect("clicked", window.choose_folder, set_dir) + file_chooser_button.connect("clicked", win.choose_folder, set_dir) @Gtk.Template(resource_path="/hu/kramo/Cartridges/gtk/preferences.ui") @@ -170,12 +168,7 @@ class PreferencesWindow(Adw.PreferencesWindow): self, "steam", "Steam", - "steam-location", - [ - "steamapps", - Path("steam") / "steamapps", - Path("Steam") / "steamapps", - ], + steam_installed, self.steam_expander_row, self.steam_file_chooser_button, ) @@ -192,7 +185,7 @@ class PreferencesWindow(Adw.PreferencesWindow): value.append(self.file_chooser.select_folder_finish(result).get_path()) self.schema.set_strv("steam-extra-dirs", value) except GLib.GError: - pass + return update_revealer() def clear_steam_dirs(*_unused): @@ -211,8 +204,7 @@ class PreferencesWindow(Adw.PreferencesWindow): self, "lutris", "Lutris", - "lutris-location", - ["pga.db"], + lutris_installed, self.lutris_expander_row, self.lutris_file_chooser_button, ) @@ -226,26 +218,21 @@ class PreferencesWindow(Adw.PreferencesWindow): def set_cache_dir(_source, result, _unused): try: path = Path(self.file_chooser.select_folder_finish(result).get_path()) - - def response(widget, response): - if response == "choose_folder": - self.choose_folder(widget, set_cache_dir) - - if not (path / "coverart").exists(): - create_dialog( - self.win, - _("Cache Not Found"), - _("Select the Lutris cache directory."), - "choose_folder", - _("Set Location"), - ).connect("response", response) - else: - self.schema.set_string( - "lutris-cache-location", - str(path), - ) except GLib.GError: - pass + return + + def response(widget, response): + if response == "choose_folder": + self.choose_folder(widget, set_cache_dir) + + if not lutris_cache_exists(path).exists(): + create_dialog( + self.win, + _("Cache Not Found"), + _("Select the Lutris cache directory."), + "choose_folder", + _("Set Location"), + ).connect("response", response) self.lutris_cache_file_chooser_button.connect( "clicked", self.choose_folder, set_cache_dir @@ -259,8 +246,7 @@ class PreferencesWindow(Adw.PreferencesWindow): self, "heroic", "Heroic", - "heroic-location", - ["config.json"], + heroic_installed, self.heroic_expander_row, self.heroic_file_chooser_button, True, @@ -290,8 +276,7 @@ class PreferencesWindow(Adw.PreferencesWindow): self, "bottles", "Bottles", - "bottles-location", - ["library.yml"], + bottles_installed, self.bottles_expander_row, self.bottles_file_chooser_button, ) @@ -304,8 +289,7 @@ class PreferencesWindow(Adw.PreferencesWindow): self, "itch", "itch", - "itch-location", - [Path("db") / "butler.db"], + itch_installed, self.itch_expander_row, self.itch_file_chooser_button, True, diff --git a/src/utils/check_install.py b/src/utils/check_install.py index 239aca2..5454be3 100644 --- a/src/utils/check_install.py +++ b/src/utils/check_install.py @@ -22,7 +22,7 @@ from pathlib import Path def check_install(check, locations, setting=None, subdirs=(Path(),)): for location in locations: - for subdir in subdirs: + for subdir in (Path(),) + subdirs: if (location / subdir / check).is_file() or ( location / subdir / check ).exists():