gsettings: Add collection storing and loading

This commit is contained in:
Jamie Gravendeel
2025-12-06 18:44:36 +01:00
parent 27b2745c74
commit 9a9514a1ae
4 changed files with 29 additions and 1 deletions

View File

@@ -24,6 +24,7 @@ from gi.repository import Gio, GLib
from .config import APP_ID, LOCALEDIR, PKGDATADIR
DATA_DIR = Path(GLib.get_user_data_dir(), "cartridges")
settings = Gio.Settings.new(APP_ID)
state_settings = Gio.Settings.new(f"{APP_ID}.State")
_RESOURCES = ("data", "icons", "ui")

View File

@@ -8,7 +8,7 @@ from typing import override
from gi.repository import Adw
from cartridges import games
from cartridges import collections, games
from cartridges.games import Game
from cartridges.sources import Source, steam
@@ -31,6 +31,7 @@ class Application(Adw.Application):
saved = tuple(games.load())
new = self.import_games(steam, skip_ids={g.game_id for g in saved})
games.model.splice(0, 0, (*saved, *new))
collections.load()
@staticmethod
def import_games(*sources: Source, skip_ids: Iterable[str]) -> Generator[Game]:

View File

@@ -1,10 +1,13 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: Copyright 2025 Jamie Gravendeel
from collections.abc import Generator
from typing import Any
from gi.repository import Gio, GObject
from cartridges import settings
class Collection(GObject.Object):
"""Collection data class."""
@@ -31,4 +34,24 @@ class Collection(GObject.Object):
)
def _get_collections() -> Generator[Collection]:
for data in settings.get_value("collections").unpack():
if data.get("removed"):
continue
collection = Collection()
for prop, value in data.items():
try:
collection.set_property(prop, value)
except TypeError:
continue
yield collection
def load():
"""Load collections from GSettings."""
model.splice(0, 0, tuple(_get_collections()))
model = Gio.ListStore.new(Collection)

View File

@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<schemalist gettext-domain="cartridges">
<schema id="@APP_ID@" path="@PREFIX@/">
<key name="collections" type="aa{sv}">
<default>[]</default>
</key>
</schema>
<schema id="@APP_ID@.State" path="@PREFIX@/State/">
<key name="width" type="i">