gsettings: Add collection storing and loading
This commit is contained in:
@@ -24,6 +24,7 @@ from gi.repository import Gio, GLib
|
|||||||
from .config import APP_ID, LOCALEDIR, PKGDATADIR
|
from .config import APP_ID, LOCALEDIR, PKGDATADIR
|
||||||
|
|
||||||
DATA_DIR = Path(GLib.get_user_data_dir(), "cartridges")
|
DATA_DIR = Path(GLib.get_user_data_dir(), "cartridges")
|
||||||
|
settings = Gio.Settings.new(APP_ID)
|
||||||
state_settings = Gio.Settings.new(f"{APP_ID}.State")
|
state_settings = Gio.Settings.new(f"{APP_ID}.State")
|
||||||
|
|
||||||
_RESOURCES = ("data", "icons", "ui")
|
_RESOURCES = ("data", "icons", "ui")
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from typing import override
|
|||||||
|
|
||||||
from gi.repository import Adw
|
from gi.repository import Adw
|
||||||
|
|
||||||
from cartridges import games
|
from cartridges import collections, games
|
||||||
from cartridges.games import Game
|
from cartridges.games import Game
|
||||||
from cartridges.sources import Source, steam
|
from cartridges.sources import Source, steam
|
||||||
|
|
||||||
@@ -31,6 +31,7 @@ class Application(Adw.Application):
|
|||||||
saved = tuple(games.load())
|
saved = tuple(games.load())
|
||||||
new = self.import_games(steam, skip_ids={g.game_id for g in saved})
|
new = self.import_games(steam, skip_ids={g.game_id for g in saved})
|
||||||
games.model.splice(0, 0, (*saved, *new))
|
games.model.splice(0, 0, (*saved, *new))
|
||||||
|
collections.load()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def import_games(*sources: Source, skip_ids: Iterable[str]) -> Generator[Game]:
|
def import_games(*sources: Source, skip_ids: Iterable[str]) -> Generator[Game]:
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
# SPDX-FileCopyrightText: Copyright 2025 Jamie Gravendeel
|
# SPDX-FileCopyrightText: Copyright 2025 Jamie Gravendeel
|
||||||
|
|
||||||
|
from collections.abc import Generator
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from gi.repository import Gio, GObject
|
from gi.repository import Gio, GObject
|
||||||
|
|
||||||
|
from cartridges import settings
|
||||||
|
|
||||||
|
|
||||||
class Collection(GObject.Object):
|
class Collection(GObject.Object):
|
||||||
"""Collection data class."""
|
"""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)
|
model = Gio.ListStore.new(Collection)
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<schemalist gettext-domain="cartridges">
|
<schemalist gettext-domain="cartridges">
|
||||||
<schema id="@APP_ID@" path="@PREFIX@/">
|
<schema id="@APP_ID@" path="@PREFIX@/">
|
||||||
|
<key name="collections" type="aa{sv}">
|
||||||
|
<default>[]</default>
|
||||||
|
</key>
|
||||||
</schema>
|
</schema>
|
||||||
<schema id="@APP_ID@.State" path="@PREFIX@/State/">
|
<schema id="@APP_ID@.State" path="@PREFIX@/State/">
|
||||||
<key name="width" type="i">
|
<key name="width" type="i">
|
||||||
|
|||||||
Reference in New Issue
Block a user