Refactor
This commit is contained in:
22
src/game.py
22
src/game.py
@@ -33,16 +33,26 @@ class game(Gtk.Box):
|
|||||||
play_revealer = Gtk.Template.Child()
|
play_revealer = Gtk.Template.Child()
|
||||||
title_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)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self.parent_widget = parent_widget
|
self.parent_widget = parent_widget
|
||||||
self.name = title
|
self.added = data["added"]
|
||||||
self.pixbuf = pixbuf
|
self.executable = data["executable"]
|
||||||
self.game_id = game_id
|
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)
|
if "removed" in data.keys():
|
||||||
self.cover.set_pixbuf(pixbuf)
|
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.event_contoller_motion = Gtk.EventControllerMotion.new()
|
||||||
self.add_controller(self.event_contoller_motion)
|
self.add_controller(self.event_contoller_motion)
|
||||||
|
|||||||
17
src/main.py
17
src/main.py
@@ -114,10 +114,17 @@ class CartridgesApplication(Adw.Application):
|
|||||||
def on_launch_game_action(self, widget, callback=None):
|
def on_launch_game_action(self, widget, callback=None):
|
||||||
|
|
||||||
# Launch the game and update the last played value
|
# 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]})
|
game_id = self.win.active_game_id
|
||||||
self.win.update_games([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")
|
||||||
run_command(self.win, self.win.games[self.win.active_game_id]["executable"])
|
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:
|
if self.win.stack.get_visible_child() == self.win.overview:
|
||||||
self.win.show_overview(None, self.win.active_game_id)
|
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)
|
self.win.on_go_back_action(None, None)
|
||||||
|
|
||||||
# Create toast for undoing the remove action
|
# 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.set_button_label(_("Undo"))
|
||||||
toast.connect("button-clicked", self.win.on_undo_remove_action, game_id)
|
toast.connect("button-clicked", self.win.on_undo_remove_action, game_id)
|
||||||
toast.set_priority(Adw.ToastPriority.HIGH)
|
toast.set_priority(Adw.ToastPriority.HIGH)
|
||||||
|
|||||||
@@ -78,8 +78,7 @@ def bottles_parser(parent_widget, action):
|
|||||||
|
|
||||||
values["game_id"] = "bottles_" + game["id"]
|
values["game_id"] = "bottles_" + game["id"]
|
||||||
|
|
||||||
if values["game_id"] in parent_widget.games and "removed" not in parent_widget.games[
|
if values["game_id"] in parent_widget.games_temp and not parent_widget.games_temp[values["game_id"]].removed:
|
||||||
values["game_id"]].keys():
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
values["name"] = game["name"]
|
values["name"] = game["name"]
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
def create_details_window(parent_widget, game_id = None):
|
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 gi.repository import Adw, Gtk, Gio, GLib, GdkPixbuf
|
||||||
from .create_dialog import create_dialog
|
from .create_dialog import create_dialog
|
||||||
from .save_games import save_games
|
from .save_games import save_games
|
||||||
@@ -31,7 +31,7 @@ def create_details_window(parent_widget, game_id = None):
|
|||||||
transient_for = parent_widget
|
transient_for = parent_widget
|
||||||
)
|
)
|
||||||
|
|
||||||
games = parent_widget.games
|
games = parent_widget.games_temp
|
||||||
pixbuf = None
|
pixbuf = None
|
||||||
|
|
||||||
if game_id == None:
|
if game_id == None:
|
||||||
@@ -43,8 +43,8 @@ def create_details_window(parent_widget, game_id = None):
|
|||||||
else:
|
else:
|
||||||
window.set_title(_("Edit Game Details"))
|
window.set_title(_("Edit Game Details"))
|
||||||
cover = Gtk.Picture.new_for_pixbuf((parent_widget.visible_widgets | parent_widget.hidden_widgets)[game_id].pixbuf)
|
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))
|
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))
|
executable = Gtk.Entry.new_with_buffer(Gtk.EntryBuffer.new((games[game_id].executable), -1))
|
||||||
apply_button = Gtk.Button.new_with_label(_("Apply"))
|
apply_button = Gtk.Button.new_with_label(_("Apply"))
|
||||||
|
|
||||||
image_filter = Gtk.FileFilter(
|
image_filter = Gtk.FileFilter(
|
||||||
@@ -157,8 +157,6 @@ def create_details_window(parent_widget, game_id = None):
|
|||||||
|
|
||||||
game_id = "imported_" + str(max(numbers)+1)
|
game_id = "imported_" + str(max(numbers)+1)
|
||||||
|
|
||||||
games[game_id] = {}
|
|
||||||
|
|
||||||
values["game_id"] = game_id
|
values["game_id"] = game_id
|
||||||
values["hidden"] = False
|
values["hidden"] = False
|
||||||
values["source"] = "imported"
|
values["source"] = "imported"
|
||||||
@@ -180,8 +178,17 @@ def create_details_window(parent_widget, game_id = None):
|
|||||||
values["name"] = final_name
|
values["name"] = final_name
|
||||||
values["executable"] = final_executable
|
values["executable"] = final_executable
|
||||||
|
|
||||||
games[game_id].update(values)
|
path = os.path.join(os.path.join(os.environ.get("XDG_DATA_HOME"), "cartridges", "games", game_id + ".json"))
|
||||||
save_games(games)
|
|
||||||
|
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])
|
parent_widget.update_games([game_id])
|
||||||
if parent_widget.stack.get_visible_child() == parent_widget.overview:
|
if parent_widget.stack.get_visible_child() == parent_widget.overview:
|
||||||
parent_widget.show_overview(None, game_id)
|
parent_widget.show_overview(None, game_id)
|
||||||
|
|||||||
@@ -17,11 +17,11 @@
|
|||||||
#
|
#
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# 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
|
from gi.repository import GdkPixbuf
|
||||||
import os, zlib
|
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:
|
if os.path.isfile(cover_path) == False:
|
||||||
return parent_widget.placeholder_pixbuf
|
return parent_widget.placeholder_pixbuf
|
||||||
@@ -29,7 +29,5 @@ def get_cover(game, parent_widget):
|
|||||||
open_file = open(cover_path, "rb")
|
open_file = open(cover_path, "rb")
|
||||||
data = zlib.decompress(open_file.read())
|
data = zlib.decompress(open_file.read())
|
||||||
open_file.close()
|
open_file.close()
|
||||||
try:
|
|
||||||
return GdkPixbuf.Pixbuf.new_from_data(data, *parent_widget.games[game["game_id"]]["pixbuf_options"])
|
return GdkPixbuf.Pixbuf.new_from_data(data, *pixbuf_options)
|
||||||
except KeyError:
|
|
||||||
return parent_widget.placeholder_pixbuf
|
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ def heroic_parser(parent_widget, action):
|
|||||||
app_name = game["app_name"]
|
app_name = game["app_name"]
|
||||||
values["game_id"] = "heroic_epic_" + 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
|
continue
|
||||||
|
|
||||||
values["name"] = game["title"]
|
values["name"] = game["title"]
|
||||||
@@ -113,8 +113,7 @@ def heroic_parser(parent_widget, action):
|
|||||||
|
|
||||||
values["game_id"] = "heroic_gog_" + app_name
|
values["game_id"] = "heroic_gog_" + app_name
|
||||||
|
|
||||||
if values["game_id"] in parent_widget.games and "removed" not in parent_widget.games[
|
if values["game_id"] in parent_widget.games_temp and not parent_widget.games_temp[values["game_id"]].removed:
|
||||||
values["game_id"]].keys():
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Get game title from library.json as it's not present in installed.json
|
# 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
|
values["game_id"] = "heroic_sideload_" + app_name
|
||||||
|
|
||||||
if values["game_id"] in parent_widget.games and "removed" not in parent_widget.games[
|
if values["game_id"] in parent_widget.games_temp and not parent_widget.games_temp[values["game_id"]].removed:
|
||||||
values["game_id"]].keys():
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
values["name"] = item["title"]
|
values["name"] = item["title"]
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ def steam_parser(parent_widget, action):
|
|||||||
|
|
||||||
values["game_id"] = "steam_" + values["appid"]
|
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
|
continue
|
||||||
|
|
||||||
values["executable"] = "xdg-open steam://rungameid/" + values["appid"]
|
values["executable"] = "xdg-open steam://rungameid/" + values["appid"]
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
self.games_temp = {}
|
||||||
self.visible_widgets = {}
|
self.visible_widgets = {}
|
||||||
self.hidden_widgets = {}
|
self.hidden_widgets = {}
|
||||||
self.filtered = {}
|
self.filtered = {}
|
||||||
@@ -104,13 +105,9 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
self.add_controller(back_mouse_button)
|
self.add_controller(back_mouse_button)
|
||||||
|
|
||||||
def update_games(self, games):
|
def update_games(self, games):
|
||||||
# Update the displayed games and the self.games instance variable to reference later
|
current_games = get_games()
|
||||||
self.games = get_games()
|
|
||||||
|
|
||||||
for game_id in games:
|
for game_id in games:
|
||||||
if game_id in self.busy_games:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if game_id in self.visible_widgets:
|
if game_id in self.visible_widgets:
|
||||||
self.library.remove(self.visible_widgets[game_id])
|
self.library.remove(self.visible_widgets[game_id])
|
||||||
self.filtered.pop(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_library.remove(self.hidden_widgets[game_id])
|
||||||
self.hidden_filtered.pop(self.hidden_widgets[game_id])
|
self.hidden_filtered.pop(self.hidden_widgets[game_id])
|
||||||
self.hidden_widgets.pop(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():
|
current_game = current_games[game_id]
|
||||||
continue
|
|
||||||
|
|
||||||
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"]:
|
if entry.removed:
|
||||||
self.visible_widgets[game_id] = entry
|
continue
|
||||||
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)
|
if game_id in self.busy_games:
|
||||||
entry.menu_button.get_popover().connect("notify::visible", self.set_active_game, game_id)
|
continue
|
||||||
entry.get_parent().set_focusable(False)
|
|
||||||
|
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 == {}:
|
if self.visible_widgets == {}:
|
||||||
self.library_bin.set_child(self.notice_empty)
|
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")
|
return GLib.DateTime.new_from_unix_utc(timestamp).format("%x")
|
||||||
|
|
||||||
def show_overview(self, widget, game_id):
|
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)
|
self.overview_menu_button.set_menu_model(self.game_options)
|
||||||
else:
|
else:
|
||||||
self.overview_menu_button.set_menu_model(self.hidden_game_options)
|
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
|
pixbuf = (self.visible_widgets | self.hidden_widgets)[self.active_game_id].pixbuf
|
||||||
self.overview_cover.set_pixbuf(pixbuf)
|
self.overview_cover.set_pixbuf(pixbuf)
|
||||||
self.overview_blurred_cover.set_pixbuf(pixbuf.scale_simple(2, 3, GdkPixbuf.InterpType.BILINEAR))
|
self.overview_blurred_cover.set_pixbuf(pixbuf.scale_simple(2, 3, GdkPixbuf.InterpType.BILINEAR))
|
||||||
self.overview_title.set_label(game["name"])
|
self.overview_title.set_label(game.name)
|
||||||
self.overview_header_bar_title.set_title(game["name"])
|
self.overview_header_bar_title.set_title(game.name)
|
||||||
self.overview_added.set_label(_("Added: ") + self.get_time(game["added"]))
|
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_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):
|
def a_z_sort(self, child1, child2):
|
||||||
name1 = child1.get_first_child().name.lower()
|
name1 = child1.get_first_child().name.lower()
|
||||||
@@ -255,8 +256,8 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
return self.a_z_sort(child1, child2)
|
return self.a_z_sort(child1, child2)
|
||||||
|
|
||||||
def newest_sort(self, child1, child2):
|
def newest_sort(self, child1, child2):
|
||||||
time1 = self.games[child1.get_first_child().game_id]["added"]
|
time1 = self.games_temp[child1.get_first_child().game_id].added
|
||||||
time2 = self.games[child2.get_first_child().game_id]["added"]
|
time2 = self.games_temp[child2.get_first_child().game_id].added
|
||||||
if time1 > time2:
|
if time1 > time2:
|
||||||
return -1
|
return -1
|
||||||
elif time1 < time2:
|
elif time1 < time2:
|
||||||
@@ -265,8 +266,8 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
return self.a_z_sort(child1, child2)
|
return self.a_z_sort(child1, child2)
|
||||||
|
|
||||||
def oldest_sort(self, child1, child2):
|
def oldest_sort(self, child1, child2):
|
||||||
time1 = self.games[child1.get_first_child().game_id]["added"]
|
time1 = self.games_temp[child1.get_first_child().game_id].added
|
||||||
time2 = self.games[child2.get_first_child().game_id]["added"]
|
time2 = self.games_temp[child2.get_first_child().game_id].added
|
||||||
if time1 > time2:
|
if time1 > time2:
|
||||||
return 1
|
return 1
|
||||||
elif time1 < time2:
|
elif time1 < time2:
|
||||||
@@ -275,8 +276,8 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
return self.a_z_sort(child1, child2)
|
return self.a_z_sort(child1, child2)
|
||||||
|
|
||||||
def last_played_sort(self, child1, child2):
|
def last_played_sort(self, child1, child2):
|
||||||
time1 = self.games[child1.get_first_child().game_id]["last_played"]
|
time1 = self.games_temp[child1.get_first_child().game_id].last_played
|
||||||
time2 = self.games[child2.get_first_child().game_id]["last_played"]
|
time2 = self.games_temp[child2.get_first_child().game_id].last_played
|
||||||
if time1 > time2:
|
if time1 > time2:
|
||||||
return -1
|
return -1
|
||||||
elif time1 < time2:
|
elif time1 < time2:
|
||||||
@@ -389,7 +390,7 @@ class CartridgesWindow(Adw.ApplicationWindow):
|
|||||||
open_file.close()
|
open_file.close()
|
||||||
data.pop("removed")
|
data.pop("removed")
|
||||||
save_games({game_id : data})
|
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[game_id].dismiss()
|
||||||
self.toasts.pop(game_id)
|
self.toasts.pop(game_id)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user