104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
# Copyright 2022 (c) Anna Schumaker
|
|
"""A customized Gio.ListStore for tracking Playlist GObjects."""
|
|
from gi.repository import GObject
|
|
from gi.repository import Gio
|
|
from gi.repository import Gtk
|
|
from .. import format
|
|
from . import table
|
|
|
|
|
|
class Playlist(table.Row):
|
|
"""Our shared Playlist Row object."""
|
|
|
|
propertyid = GObject.Property(type=int)
|
|
|
|
name = GObject.Property(type=str)
|
|
active = GObject.Property(type=bool, default=False)
|
|
|
|
n_tracks = GObject.Property(type=int)
|
|
|
|
children = GObject.Property(type=Gtk.FilterListModel)
|
|
|
|
def __init__(self, table: Gio.ListModel, propertyid: int,
|
|
name: str, **kwargs):
|
|
"""Initialize a Playlist object."""
|
|
super().__init__(table=table, propertyid=propertyid,
|
|
name=name, **kwargs)
|
|
|
|
def add_children(self, child_table: table.Table,
|
|
child_filter: Gtk.Filter) -> None:
|
|
"""Create a FilterListModel for this playlist's children."""
|
|
self.children = Gtk.FilterListModel.new(child_table, child_filter)
|
|
self.children.set_incremental(True)
|
|
|
|
def do_update(self, column: str) -> bool:
|
|
"""Update a Playlist object."""
|
|
match column:
|
|
case "propertyid" | "name" | "n-tracks" | "children": pass
|
|
case _: return super().do_update(column)
|
|
return True
|
|
|
|
@GObject.Property(type=table.Row)
|
|
def parent(self) -> table.Row | None:
|
|
"""Get this playlist's parent playlist."""
|
|
return None
|
|
|
|
|
|
class Table(table.Table):
|
|
"""A table.Table with extra functionality for Playlists."""
|
|
|
|
active_playlist = GObject.Property(type=Playlist)
|
|
treemodel = GObject.Property(type=Gtk.TreeListModel)
|
|
|
|
def __init__(self, sql: GObject.TYPE_PYOBJECT, **kwargs):
|
|
"""Initialize a Playlist Table."""
|
|
super().__init__(sql=sql, **kwargs)
|
|
self.treemodel = Gtk.TreeListModel.new(root=self,
|
|
passthrough=False,
|
|
autoexpand=False,
|
|
create_func=self.__create_tree)
|
|
|
|
def __create_tree(self, plist: Playlist) -> Gtk.FilterListModel | None:
|
|
return plist.children
|
|
|
|
def do_get_sort_key(self, playlist: Playlist) -> tuple[str]:
|
|
"""Get a sort key for the requested Playlist."""
|
|
return format.sort_key(playlist.name)
|
|
|
|
def clear(self) -> None:
|
|
"""Clear the Table."""
|
|
self.active_playlist = None
|
|
super().clear()
|
|
|
|
def construct(self, propertyid: int, name: str, **kwargs) -> Playlist:
|
|
"""Construct a new Playlist object."""
|
|
res = super().construct(propertyid=propertyid, name=name, **kwargs)
|
|
if res.active:
|
|
self.sql.set_active_playlist(res)
|
|
return res
|
|
|
|
def delete(self, playlist: Playlist) -> bool:
|
|
"""Delete a playlist from the database."""
|
|
if playlist.active:
|
|
self.sql.set_active_playlist(None)
|
|
return super().delete(playlist)
|
|
|
|
def update(self, playlist: Playlist, column: str, newval) -> bool:
|
|
"""Update a Playlist in the Database."""
|
|
match column:
|
|
case "active":
|
|
return self.update_playlist_property(playlist, column, newval)
|
|
case _:
|
|
return super().update(playlist, column, newval)
|
|
|
|
def update_playlist_property(self, playlist: Playlist,
|
|
column: str, newval) -> bool:
|
|
"""Update the playlists_common table."""
|
|
match column:
|
|
case "active":
|
|
self.active_playlist = playlist if playlist.active else None
|
|
|
|
return self.sql(f"""UPDATE playlist_properties
|
|
SET {column}=? WHERE propertyid=?""",
|
|
newval, playlist.propertyid) is not None
|