Cleanups
This commit is contained in:
@@ -27,6 +27,6 @@ def check_install(check, locations, setting=None, subdirs=(Path(),)):
|
||||
location / subdir / check
|
||||
).exists():
|
||||
if setting:
|
||||
setting[0].set_string(setting[1], str(location))
|
||||
setting[0].set_string(setting[1], str(location / subdir))
|
||||
|
||||
return location
|
||||
return location / subdir
|
||||
|
||||
@@ -31,12 +31,12 @@ from .save_game import save_game
|
||||
from .steamgriddb import SGDBSave
|
||||
|
||||
|
||||
def create_details_window(parent_widget, game_id=None):
|
||||
def create_details_window(win, game_id=None):
|
||||
window = Adw.Window(
|
||||
modal=True, default_width=500, default_height=-1, transient_for=parent_widget
|
||||
modal=True, default_width=500, default_height=-1, transient_for=win
|
||||
)
|
||||
|
||||
games = parent_widget.games
|
||||
games = win.games
|
||||
cover_deleted = False
|
||||
|
||||
cover_button_edit = Gtk.Button(
|
||||
@@ -85,7 +85,7 @@ def create_details_window(parent_widget, game_id=None):
|
||||
apply_button = Gtk.Button.new_with_label(_("Confirm"))
|
||||
else:
|
||||
window.set_title(_("Edit Game Details"))
|
||||
game_cover.new_pixbuf(path=parent_widget.games[game_id].get_cover_path())
|
||||
game_cover.new_pixbuf(path=win.games[game_id].get_cover_path())
|
||||
developer = Gtk.Entry.new_with_buffer(
|
||||
Gtk.EntryBuffer.new(games[game_id].developer, -1)
|
||||
)
|
||||
@@ -95,7 +95,7 @@ def create_details_window(parent_widget, game_id=None):
|
||||
)
|
||||
apply_button = Gtk.Button.new_with_label(_("Apply"))
|
||||
|
||||
if parent_widget.games[game_id].get_cover_path():
|
||||
if win.games[game_id].get_cover_path():
|
||||
cover_button_delete_revealer.set_reveal_child(True)
|
||||
|
||||
image_filter = Gtk.FileFilter(name=_("Images"))
|
||||
@@ -314,33 +314,33 @@ def create_details_window(parent_widget, game_id=None):
|
||||
values["executable"] = final_executable_split
|
||||
|
||||
if cover_deleted:
|
||||
(parent_widget.covers_dir / f"{game_id}.tiff").unlink(missing_ok=True)
|
||||
(parent_widget.covers_dir / f"{game_id}.gif").unlink(missing_ok=True)
|
||||
(win.covers_dir / f"{game_id}.tiff").unlink(missing_ok=True)
|
||||
(win.covers_dir / f"{game_id}.gif").unlink(missing_ok=True)
|
||||
|
||||
save_cover(
|
||||
parent_widget,
|
||||
win,
|
||||
game_id,
|
||||
None,
|
||||
game_cover.get_pixbuf(),
|
||||
game_cover.get_animation(),
|
||||
)
|
||||
|
||||
path = parent_widget.games_dir / f"{game_id}.json"
|
||||
path = win.games_dir / f"{game_id}.json"
|
||||
|
||||
if path.exists():
|
||||
data = json.loads(path.read_text("utf-8"))
|
||||
data.update(values)
|
||||
save_game(parent_widget, data)
|
||||
save_game(win, data)
|
||||
else:
|
||||
save_game(parent_widget, values)
|
||||
save_game(win, values)
|
||||
|
||||
if game_cover.get_pixbuf():
|
||||
parent_widget.update_games([game_id])
|
||||
win.update_games([game_id])
|
||||
else:
|
||||
SGDBSave(parent_widget, {(game_id, values["name"])})
|
||||
SGDBSave(win, {(game_id, values["name"])})
|
||||
|
||||
window.close()
|
||||
parent_widget.show_details_view(None, game_id)
|
||||
win.show_details_view(None, game_id)
|
||||
|
||||
def focus_executable(_widget):
|
||||
window.set_focus(executable)
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
from gi.repository import Adw
|
||||
|
||||
|
||||
def create_dialog(parent_widget, heading, body, extra_option=None, extra_label=None):
|
||||
dialog = Adw.MessageDialog.new(parent_widget, heading, body)
|
||||
def create_dialog(win, heading, body, extra_option=None, extra_label=None):
|
||||
dialog = Adw.MessageDialog.new(win, heading, body)
|
||||
dialog.add_response("dismiss", _("Dismiss"))
|
||||
|
||||
if extra_option:
|
||||
|
||||
@@ -20,18 +20,16 @@
|
||||
import json
|
||||
|
||||
|
||||
def get_games(parent_widget, game_ids=None):
|
||||
def get_games(win, game_ids=None):
|
||||
games = {}
|
||||
|
||||
if not parent_widget.games_dir.exists():
|
||||
if not win.games_dir.exists():
|
||||
return {}
|
||||
|
||||
if game_ids:
|
||||
game_files = [
|
||||
parent_widget.games_dir / f"{game_id}.json" for game_id in game_ids
|
||||
]
|
||||
game_files = [win.games_dir / f"{game_id}.json" for game_id in game_ids]
|
||||
else:
|
||||
game_files = parent_widget.games_dir.iterdir()
|
||||
game_files = win.games_dir.iterdir()
|
||||
|
||||
for game in game_files:
|
||||
data = json.loads(game.read_text("utf-8"))
|
||||
|
||||
@@ -28,8 +28,8 @@ from .steamgriddb import SGDBSave
|
||||
|
||||
|
||||
class Importer:
|
||||
def __init__(self, parent_widget):
|
||||
self.parent_widget = parent_widget
|
||||
def __init__(self, win):
|
||||
self.win = win
|
||||
self.total_queue = 0
|
||||
self.queue = 0
|
||||
self.games_no = 0
|
||||
@@ -48,7 +48,7 @@ class Importer:
|
||||
modal=True,
|
||||
default_width=350,
|
||||
default_height=-1,
|
||||
transient_for=parent_widget,
|
||||
transient_for=win,
|
||||
deletable=False,
|
||||
)
|
||||
|
||||
@@ -56,10 +56,10 @@ class Importer:
|
||||
|
||||
def save_game(self, values=None, cover_path=None, pixbuf=None):
|
||||
if values:
|
||||
save_game(self.parent_widget, values)
|
||||
save_game(self.win, values)
|
||||
|
||||
if cover_path or pixbuf:
|
||||
save_cover(self.parent_widget, values["game_id"], cover_path, pixbuf)
|
||||
save_cover(self.win, values["game_id"], cover_path, pixbuf)
|
||||
|
||||
self.games.add((values["game_id"], values["name"]))
|
||||
|
||||
@@ -76,7 +76,7 @@ class Importer:
|
||||
self.queue = len(self.games)
|
||||
self.import_statuspage.set_title(_("Importing Covers…"))
|
||||
self.update_progressbar()
|
||||
SGDBSave(self.parent_widget, self.games, self)
|
||||
SGDBSave(self.win, self.games, self)
|
||||
else:
|
||||
self.done()
|
||||
|
||||
@@ -87,7 +87,7 @@ class Importer:
|
||||
|
||||
if self.games_no == 0:
|
||||
create_dialog(
|
||||
self.parent_widget,
|
||||
self.win,
|
||||
_("No Games Found"),
|
||||
_("No new games were found on your system."),
|
||||
"open_preferences",
|
||||
@@ -96,14 +96,14 @@ class Importer:
|
||||
|
||||
elif self.games_no == 1:
|
||||
create_dialog(
|
||||
self.parent_widget,
|
||||
self.win,
|
||||
_("Game Imported"),
|
||||
_("Successfully imported 1 game."),
|
||||
).connect("response", self.response, "import")
|
||||
elif self.games_no > 1:
|
||||
games_no = self.games_no
|
||||
create_dialog(
|
||||
self.parent_widget,
|
||||
self.win,
|
||||
_("Games Imported"),
|
||||
# The variable is the number of games
|
||||
_("Successfully imported {} games.").format(games_no),
|
||||
@@ -111,12 +111,12 @@ class Importer:
|
||||
|
||||
def response(self, _widget, response, page_name=None, expander_row=None):
|
||||
if response == "open_preferences":
|
||||
self.parent_widget.get_application().on_preferences_action(
|
||||
self.win.get_application().on_preferences_action(
|
||||
None, page_name=page_name, expander_row=expander_row
|
||||
)
|
||||
elif self.sgdb_exception:
|
||||
create_dialog(
|
||||
self.parent_widget,
|
||||
self.win,
|
||||
_("Couldn't Connect to SteamGridDB"),
|
||||
self.sgdb_exception,
|
||||
"open_preferences",
|
||||
@@ -124,12 +124,12 @@ class Importer:
|
||||
).connect("response", self.response, "sgdb")
|
||||
self.sgdb_exception = None
|
||||
elif (
|
||||
self.parent_widget.schema.get_boolean("steam")
|
||||
and self.parent_widget.schema.get_boolean("steam-extra-dirs-hint")
|
||||
and not self.parent_widget.schema.get_strv("steam-extra-dirs")
|
||||
self.win.schema.get_boolean("steam")
|
||||
and self.win.schema.get_boolean("steam-extra-dirs-hint")
|
||||
and not self.win.schema.get_strv("steam-extra-dirs")
|
||||
):
|
||||
steam_library_path = (
|
||||
Path(self.parent_widget.schema.get_string("steam-location"))
|
||||
Path(self.win.schema.get_string("steam-location"))
|
||||
/ "steamapps"
|
||||
/ "libraryfolders.vdf"
|
||||
)
|
||||
@@ -137,9 +137,9 @@ class Importer:
|
||||
steam_library_path.exists()
|
||||
and steam_library_path.read_text("utf-8").count('"path"') > 1
|
||||
):
|
||||
self.parent_widget.schema.set_boolean("steam-extra-dirs-hint", False)
|
||||
self.win.schema.set_boolean("steam-extra-dirs-hint", False)
|
||||
create_dialog(
|
||||
self.parent_widget,
|
||||
self.win,
|
||||
_("Extra Steam Libraries"),
|
||||
_(
|
||||
"Looks like you have multiple Steam libraries. Would you like to add them in preferences?"
|
||||
|
||||
@@ -43,32 +43,32 @@ def resize_animation(cover_path):
|
||||
|
||||
|
||||
def save_cover(
|
||||
parent_widget,
|
||||
win,
|
||||
game_id,
|
||||
cover_path=None,
|
||||
pixbuf=None,
|
||||
animation_path=None,
|
||||
):
|
||||
parent_widget.covers_dir.mkdir(parents=True, exist_ok=True)
|
||||
win.covers_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Remove previous covers
|
||||
(parent_widget.covers_dir / f"{game_id}.tiff").unlink(missing_ok=True)
|
||||
(parent_widget.covers_dir / f"{game_id}.gif").unlink(missing_ok=True)
|
||||
(win.covers_dir / f"{game_id}.tiff").unlink(missing_ok=True)
|
||||
(win.covers_dir / f"{game_id}.gif").unlink(missing_ok=True)
|
||||
|
||||
if animation_path:
|
||||
copyfile(animation_path, parent_widget.covers_dir / f"{game_id}.gif")
|
||||
copyfile(animation_path, win.covers_dir / f"{game_id}.gif")
|
||||
return
|
||||
|
||||
if not pixbuf:
|
||||
if not cover_path:
|
||||
return
|
||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
||||
str(cover_path), *parent_widget.image_size, False
|
||||
str(cover_path), *win.image_size, False
|
||||
)
|
||||
|
||||
pixbuf.savev(
|
||||
str(parent_widget.covers_dir / f"{game_id}.tiff"),
|
||||
str(win.covers_dir / f"{game_id}.tiff"),
|
||||
"tiff",
|
||||
["compression"],
|
||||
["8"] if parent_widget.schema.get_boolean("high-quality-images") else ["7"],
|
||||
["8"] if win.schema.get_boolean("high-quality-images") else ["7"],
|
||||
)
|
||||
|
||||
@@ -20,9 +20,9 @@
|
||||
import json
|
||||
|
||||
|
||||
def save_game(parent_widget, game):
|
||||
parent_widget.games_dir.mkdir(parents=True, exist_ok=True)
|
||||
def save_game(win, game):
|
||||
win.games_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
(parent_widget.games_dir / f'{game["game_id"]}.json').write_text(
|
||||
(win.games_dir / f'{game["game_id"]}.json').write_text(
|
||||
json.dumps(game, indent=4, sort_keys=True), "utf-8"
|
||||
)
|
||||
|
||||
@@ -8,8 +8,8 @@ from .save_cover import save_cover, resize_animation
|
||||
|
||||
|
||||
class SGDBSave:
|
||||
def __init__(self, parent_widget, games, importer=None):
|
||||
self.parent_widget = parent_widget
|
||||
def __init__(self, win, games, importer=None):
|
||||
self.win = win
|
||||
self.importer = importer
|
||||
self.exception = None
|
||||
|
||||
@@ -27,19 +27,19 @@ class SGDBSave:
|
||||
Gio.Task.new(None, None, self.task_done).run_in_thread(create_func(game))
|
||||
|
||||
def update_cover(self, task, game):
|
||||
if self.parent_widget.schema.get_boolean("sgdb") and (
|
||||
self.parent_widget.schema.get_boolean("sgdb-prefer")
|
||||
if self.win.schema.get_boolean("sgdb") and (
|
||||
self.win.schema.get_boolean("sgdb-prefer")
|
||||
or (
|
||||
not (self.parent_widget.covers_dir / f"{game[0]}.gif").is_file()
|
||||
and not (self.parent_widget.covers_dir / f"{game[0]}.tiff").is_file()
|
||||
not (self.win.covers_dir / f"{game[0]}.gif").is_file()
|
||||
and not (self.win.covers_dir / f"{game[0]}.tiff").is_file()
|
||||
)
|
||||
):
|
||||
if not self.importer:
|
||||
self.parent_widget.games[game[0]].set_loading(1)
|
||||
self.win.games[game[0]].set_loading(1)
|
||||
|
||||
url = "https://www.steamgriddb.com/api/v2/"
|
||||
headers = {
|
||||
"Authorization": f'Bearer {self.parent_widget.schema.get_string("sgdb-key")}'
|
||||
"Authorization": f'Bearer {self.win.schema.get_string("sgdb-key")}'
|
||||
}
|
||||
|
||||
try:
|
||||
@@ -63,7 +63,7 @@ class SGDBSave:
|
||||
response = None
|
||||
|
||||
try:
|
||||
if self.parent_widget.schema.get_boolean("sgdb-animated"):
|
||||
if self.win.schema.get_boolean("sgdb-animated"):
|
||||
try:
|
||||
grid = requests.get(
|
||||
f'{url}grids/game/{search_result.json()["data"][0]["id"]}?dimensions=600x900&types=animated',
|
||||
@@ -91,7 +91,7 @@ class SGDBSave:
|
||||
Path(tmp_file.get_path()).write_bytes(response.content)
|
||||
|
||||
save_cover(
|
||||
self.parent_widget,
|
||||
self.win,
|
||||
game[0],
|
||||
tmp_file.get_path(),
|
||||
animation_path=resize_animation(tmp_file.get_path())
|
||||
@@ -109,7 +109,7 @@ class SGDBSave:
|
||||
|
||||
if self.exception:
|
||||
create_dialog(
|
||||
self.parent_widget,
|
||||
self.win,
|
||||
_("Couldn't Connect to SteamGridDB"),
|
||||
self.exception,
|
||||
"open_preferences",
|
||||
@@ -117,10 +117,8 @@ class SGDBSave:
|
||||
).connect("response", self.response)
|
||||
|
||||
game_id = result.propagate_value()[1]
|
||||
self.parent_widget.update_games([game_id])
|
||||
self.win.update_games([game_id])
|
||||
|
||||
def response(self, _widget, response):
|
||||
if response == "open_preferences":
|
||||
self.parent_widget.get_application().on_preferences_action(
|
||||
None, page_name="sgdb"
|
||||
)
|
||||
self.win.get_application().on_preferences_action(None, page_name="sgdb")
|
||||
|
||||
Reference in New Issue
Block a user