From a4b1b8efd9d341c42c2fad90386d3e0bcba49090 Mon Sep 17 00:00:00 2001 From: kramo <93832451+kra-mo@users.noreply.github.com> Date: Tue, 14 Mar 2023 16:48:00 +0100 Subject: [PATCH] Refactor --- src/game.py | 22 +++++++--- src/main.py | 17 +++++--- src/utils/bottles_parser.py | 3 +- src/utils/create_details_window.py | 23 ++++++---- src/utils/get_cover.py | 10 ++--- src/utils/heroic_parser.py | 8 ++-- src/utils/steam_parser.py | 2 +- src/window.py | 67 +++++++++++++++--------------- 8 files changed, 86 insertions(+), 66 deletions(-) diff --git a/src/game.py b/src/game.py index 70cc63e..6122174 100644 --- a/src/game.py +++ b/src/game.py @@ -33,16 +33,26 @@ class game(Gtk.Box): play_revealer = Gtk.Template.Child() title_revealer = Gtk.Template.Child() - def __init__(self, parent_widget, title, pixbuf, game_id, **kwargs): + def __init__(self, parent_widget, data, pixbuf, **kwargs): super().__init__(**kwargs) self.parent_widget = parent_widget - self.name = title - self.pixbuf = pixbuf - self.game_id = game_id + self.added = data["added"] + self.executable = data["executable"] + self.game_id = data["game_id"] + self.hidden = data["hidden"] + self.last_played = data["last_played"] + self.name = data["name"] - self.title.set_label(title) - self.cover.set_pixbuf(pixbuf) + if "removed" in data.keys(): + self.removed = True + else: + self.removed = False + + self.pixbuf = pixbuf + + self.title.set_label(self.name) + self.cover.set_pixbuf(self.pixbuf) self.event_contoller_motion = Gtk.EventControllerMotion.new() self.add_controller(self.event_contoller_motion) diff --git a/src/main.py b/src/main.py index 919f859..3f70e32 100644 --- a/src/main.py +++ b/src/main.py @@ -114,10 +114,17 @@ class CartridgesApplication(Adw.Application): def on_launch_game_action(self, widget, callback=None): # Launch the game and update the last played value - self.win.games[self.win.active_game_id]["last_played"] = int(time.time()) - save_games({self.win.active_game_id : self.win.games[self.win.active_game_id]}) - self.win.update_games([self.win.active_game_id]) - run_command(self.win, self.win.games[self.win.active_game_id]["executable"]) + + game_id = self.win.active_game_id + open_file = open(os.path.join(os.path.join(os.environ.get("XDG_DATA_HOME"), "cartridges", "games", game_id + ".json")), "r") + data = json.loads(open_file.read()) + open_file.close() + data["last_played"] = int(time.time()) + save_games({game_id : data}) + + run_command(self.win, self.win.games_temp[self.win.active_game_id].executable) + + self.win.update_games([game_id]) if self.win.stack.get_visible_child() == self.win.overview: self.win.show_overview(None, self.win.active_game_id) @@ -149,7 +156,7 @@ class CartridgesApplication(Adw.Application): self.win.on_go_back_action(None, None) # Create toast for undoing the remove action - toast = Adw.Toast.new(self.win.games[game_id]["name"] + " " + (_("removed"))) + toast = Adw.Toast.new(self.win.games_temp[game_id].name + " " + (_("removed"))) toast.set_button_label(_("Undo")) toast.connect("button-clicked", self.win.on_undo_remove_action, game_id) toast.set_priority(Adw.ToastPriority.HIGH) diff --git a/src/utils/bottles_parser.py b/src/utils/bottles_parser.py index 27bc01c..b99f681 100644 --- a/src/utils/bottles_parser.py +++ b/src/utils/bottles_parser.py @@ -78,8 +78,7 @@ def bottles_parser(parent_widget, action): values["game_id"] = "bottles_" + game["id"] - if values["game_id"] in parent_widget.games and "removed" not in parent_widget.games[ - values["game_id"]].keys(): + if values["game_id"] in parent_widget.games_temp and not parent_widget.games_temp[values["game_id"]].removed: continue values["name"] = game["name"] diff --git a/src/utils/create_details_window.py b/src/utils/create_details_window.py index 6d25cf4..d173481 100644 --- a/src/utils/create_details_window.py +++ b/src/utils/create_details_window.py @@ -18,7 +18,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later def create_details_window(parent_widget, game_id = None): - import time + import time, os, json from gi.repository import Adw, Gtk, Gio, GLib, GdkPixbuf from .create_dialog import create_dialog from .save_games import save_games @@ -31,7 +31,7 @@ def create_details_window(parent_widget, game_id = None): transient_for = parent_widget ) - games = parent_widget.games + games = parent_widget.games_temp pixbuf = None if game_id == None: @@ -43,8 +43,8 @@ def create_details_window(parent_widget, game_id = None): else: window.set_title(_("Edit Game Details")) cover = Gtk.Picture.new_for_pixbuf((parent_widget.visible_widgets | parent_widget.hidden_widgets)[game_id].pixbuf) - name = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new(games[game_id]["name"], -1)) - executable = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new((games[game_id]["executable"]), -1)) + name = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new(games[game_id].name, -1)) + executable = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new((games[game_id].executable), -1)) apply_button = Gtk.Button.new_with_label(_("Apply")) image_filter = Gtk.FileFilter( @@ -157,8 +157,6 @@ def create_details_window(parent_widget, game_id = None): game_id = "imported_" + str(max(numbers)+1) - games[game_id] = {} - values["game_id"] = game_id values["hidden"] = False values["source"] = "imported" @@ -180,8 +178,17 @@ def create_details_window(parent_widget, game_id = None): values["name"] = final_name values["executable"] = final_executable - games[game_id].update(values) - save_games(games) + path = os.path.join(os.path.join(os.environ.get("XDG_DATA_HOME"), "cartridges", "games", game_id + ".json")) + + if os.path.exists(path): + open_file = open(path, "r") + data = json.loads(open_file.read()) + open_file.close() + data.update(values) + save_games({game_id : data}) + else: + save_games({game_id : values}) + parent_widget.update_games([game_id]) if parent_widget.stack.get_visible_child() == parent_widget.overview: parent_widget.show_overview(None, game_id) diff --git a/src/utils/get_cover.py b/src/utils/get_cover.py index b90f226..9301dfb 100644 --- a/src/utils/get_cover.py +++ b/src/utils/get_cover.py @@ -17,11 +17,11 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -def get_cover(game, parent_widget): +def get_cover(game_id, pixbuf_options, parent_widget): from gi.repository import GdkPixbuf import os, zlib - cover_path = os.path.join(os.environ.get("XDG_DATA_HOME"), "cartridges", "covers", game["game_id"] + ".dat") + cover_path = os.path.join(os.environ.get("XDG_DATA_HOME"), "cartridges", "covers", game_id + ".dat") if os.path.isfile(cover_path) == False: return parent_widget.placeholder_pixbuf @@ -29,7 +29,5 @@ def get_cover(game, parent_widget): open_file = open(cover_path, "rb") data = zlib.decompress(open_file.read()) open_file.close() - try: - return GdkPixbuf.Pixbuf.new_from_data(data, *parent_widget.games[game["game_id"]]["pixbuf_options"]) - except KeyError: - return parent_widget.placeholder_pixbuf + + return GdkPixbuf.Pixbuf.new_from_data(data, *pixbuf_options) diff --git a/src/utils/heroic_parser.py b/src/utils/heroic_parser.py index 4b00170..2ce874a 100644 --- a/src/utils/heroic_parser.py +++ b/src/utils/heroic_parser.py @@ -83,7 +83,7 @@ def heroic_parser(parent_widget, action): app_name = game["app_name"] values["game_id"] = "heroic_epic_" + app_name - if values["game_id"] in parent_widget.games and "removed" not in parent_widget.games[values["game_id"]].keys(): + if values["game_id"] in parent_widget.games_temp and not parent_widget.games_temp[values["game_id"]].removed: continue values["name"] = game["title"] @@ -113,8 +113,7 @@ def heroic_parser(parent_widget, action): values["game_id"] = "heroic_gog_" + app_name - if values["game_id"] in parent_widget.games and "removed" not in parent_widget.games[ - values["game_id"]].keys(): + if values["game_id"] in parent_widget.games_temp and not parent_widget.games_temp[values["game_id"]].removed: continue # Get game title from library.json as it's not present in installed.json @@ -153,8 +152,7 @@ def heroic_parser(parent_widget, action): values["game_id"] = "heroic_sideload_" + app_name - if values["game_id"] in parent_widget.games and "removed" not in parent_widget.games[ - values["game_id"]].keys(): + if values["game_id"] in parent_widget.games_temp and not parent_widget.games_temp[values["game_id"]].removed: continue values["name"] = item["title"] diff --git a/src/utils/steam_parser.py b/src/utils/steam_parser.py index dd102e8..beaee63 100644 --- a/src/utils/steam_parser.py +++ b/src/utils/steam_parser.py @@ -87,7 +87,7 @@ def steam_parser(parent_widget, action): values["game_id"] = "steam_" + values["appid"] - if values["game_id"] in parent_widget.games and "removed" not in parent_widget.games[values["game_id"]].keys(): + if values["game_id"] in parent_widget.games_temp and not parent_widget.games_temp[values["game_id"]].removed: continue values["executable"] = "xdg-open steam://rungameid/" + values["appid"] diff --git a/src/window.py b/src/window.py index 04eb21d..26617e1 100644 --- a/src/window.py +++ b/src/window.py @@ -68,6 +68,7 @@ class CartridgesWindow(Adw.ApplicationWindow): def __init__(self, **kwargs): super().__init__(**kwargs) + self.games_temp = {} self.visible_widgets = {} self.hidden_widgets = {} self.filtered = {} @@ -104,13 +105,9 @@ class CartridgesWindow(Adw.ApplicationWindow): self.add_controller(back_mouse_button) def update_games(self, games): - # Update the displayed games and the self.games instance variable to reference later - self.games = get_games() + current_games = get_games() for game_id in games: - if game_id in self.busy_games: - continue - if game_id in self.visible_widgets: self.library.remove(self.visible_widgets[game_id]) self.filtered.pop(self.visible_widgets[game_id]) @@ -119,25 +116,29 @@ class CartridgesWindow(Adw.ApplicationWindow): self.hidden_library.remove(self.hidden_widgets[game_id]) self.hidden_filtered.pop(self.hidden_widgets[game_id]) self.hidden_widgets.pop(game_id) - if game_id in self.games: - current_game = self.games[game_id] - if "removed" in current_game.keys(): - continue + current_game = current_games[game_id] - entry = game(self, current_game["name"], get_cover(current_game, self), game_id) + entry = game(self, current_game, get_cover(current_game["game_id"], current_game["pixbuf_options"] if "pixbuf_options" in current_game.keys() else None, self)) + self.games_temp[current_game["game_id"]] = entry - if not self.games[game_id]["hidden"]: - self.visible_widgets[game_id] = entry - self.library.append(entry) - else: - self.hidden_widgets[game_id] = entry - entry.menu_button.set_menu_model(entry.hidden_game_options) - self.hidden_library.append(entry) + if entry.removed: + continue - entry.cover_button.connect("clicked", self.show_overview, game_id) - entry.menu_button.get_popover().connect("notify::visible", self.set_active_game, game_id) - entry.get_parent().set_focusable(False) + if game_id in self.busy_games: + continue + + if not self.games_temp[game_id].hidden: + self.visible_widgets[game_id] = entry + self.library.append(entry) + else: + self.hidden_widgets[game_id] = entry + entry.menu_button.set_menu_model(entry.hidden_game_options) + self.hidden_library.append(entry) + + entry.cover_button.connect("clicked", self.show_overview, game_id) + entry.menu_button.get_popover().connect("notify::visible", self.set_active_game, game_id) + entry.get_parent().set_focusable(False) if self.visible_widgets == {}: self.library_bin.set_child(self.notice_empty) @@ -211,9 +212,9 @@ class CartridgesWindow(Adw.ApplicationWindow): return GLib.DateTime.new_from_unix_utc(timestamp).format("%x") def show_overview(self, widget, game_id): - game = self.games[game_id] + game = self.games_temp[game_id] - if not game["hidden"]: + if not game.hidden: self.overview_menu_button.set_menu_model(self.game_options) else: self.overview_menu_button.set_menu_model(self.hidden_game_options) @@ -226,10 +227,10 @@ class CartridgesWindow(Adw.ApplicationWindow): pixbuf = (self.visible_widgets | self.hidden_widgets)[self.active_game_id].pixbuf self.overview_cover.set_pixbuf(pixbuf) self.overview_blurred_cover.set_pixbuf(pixbuf.scale_simple(2, 3, GdkPixbuf.InterpType.BILINEAR)) - self.overview_title.set_label(game["name"]) - self.overview_header_bar_title.set_title(game["name"]) - self.overview_added.set_label(_("Added: ") + self.get_time(game["added"])) - self.overview_last_played.set_label(_("Last played: ") + self.get_time(game["last_played"]) if game["last_played"] != 0 else _("Last played: Never")) + self.overview_title.set_label(game.name) + self.overview_header_bar_title.set_title(game.name) + self.overview_added.set_label(_("Added: ") + self.get_time(game.added)) + self.overview_last_played.set_label(_("Last played: ") + self.get_time(game.last_played) if game.last_played != 0 else _("Last played: Never")) def a_z_sort(self, child1, child2): name1 = child1.get_first_child().name.lower() @@ -255,8 +256,8 @@ class CartridgesWindow(Adw.ApplicationWindow): return self.a_z_sort(child1, child2) def newest_sort(self, child1, child2): - time1 = self.games[child1.get_first_child().game_id]["added"] - time2 = self.games[child2.get_first_child().game_id]["added"] + time1 = self.games_temp[child1.get_first_child().game_id].added + time2 = self.games_temp[child2.get_first_child().game_id].added if time1 > time2: return -1 elif time1 < time2: @@ -265,8 +266,8 @@ class CartridgesWindow(Adw.ApplicationWindow): return self.a_z_sort(child1, child2) def oldest_sort(self, child1, child2): - time1 = self.games[child1.get_first_child().game_id]["added"] - time2 = self.games[child2.get_first_child().game_id]["added"] + time1 = self.games_temp[child1.get_first_child().game_id].added + time2 = self.games_temp[child2.get_first_child().game_id].added if time1 > time2: return 1 elif time1 < time2: @@ -275,8 +276,8 @@ class CartridgesWindow(Adw.ApplicationWindow): return self.a_z_sort(child1, child2) def last_played_sort(self, child1, child2): - time1 = self.games[child1.get_first_child().game_id]["last_played"] - time2 = self.games[child2.get_first_child().game_id]["last_played"] + time1 = self.games_temp[child1.get_first_child().game_id].last_played + time2 = self.games_temp[child2.get_first_child().game_id].last_played if time1 > time2: return -1 elif time1 < time2: @@ -389,7 +390,7 @@ class CartridgesWindow(Adw.ApplicationWindow): open_file.close() data.pop("removed") save_games({game_id : data}) - self.update_games({game_id : self.games[game_id]}) + self.update_games([game_id]) self.toasts[game_id].dismiss() self.toasts.pop(game_id)