2022-05-26 17:17:03 -04:00
|
|
|
# Copyright 2022 (c) Anna Schumaker
|
|
|
|
"""Easily work with our underlying sqlite3 database."""
|
|
|
|
import pathlib
|
2022-09-23 14:46:46 -04:00
|
|
|
from gi.repository import GObject
|
2022-08-10 11:19:29 -04:00
|
|
|
from typing import Generator
|
2022-08-23 11:12:39 -04:00
|
|
|
from . import albums
|
2022-08-22 15:51:18 -04:00
|
|
|
from . import artists
|
2022-05-26 17:17:03 -04:00
|
|
|
from . import connection
|
2022-08-31 11:23:47 -04:00
|
|
|
from . import decades
|
2022-08-30 14:43:46 -04:00
|
|
|
from . import genres
|
2022-09-06 14:13:51 -04:00
|
|
|
from . import libraries
|
2022-08-10 11:19:29 -04:00
|
|
|
from . import playlist
|
2022-08-26 13:23:37 -04:00
|
|
|
from . import media
|
2022-08-10 11:19:29 -04:00
|
|
|
from . import playlists
|
2022-05-26 17:18:21 -04:00
|
|
|
from . import settings
|
2022-09-23 14:46:46 -04:00
|
|
|
from . import table
|
2022-09-23 09:16:54 -04:00
|
|
|
from . import tracks
|
2022-08-31 15:14:47 -04:00
|
|
|
from . import years
|
2022-05-26 17:17:03 -04:00
|
|
|
|
|
|
|
|
2023-10-18 11:50:29 -04:00
|
|
|
SQL_V1_SCRIPT = pathlib.Path(__file__).parent / "emmental.sql"
|
|
|
|
SQL_V2_SCRIPT = pathlib.Path(__file__).parent / "upgrade-v2.sql"
|
2022-05-26 17:17:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Connection(connection.Connection):
|
|
|
|
"""Connect to the database."""
|
|
|
|
|
2022-08-10 11:19:29 -04:00
|
|
|
active_playlist = GObject.Property(type=playlist.Playlist)
|
|
|
|
|
2022-05-26 17:17:03 -04:00
|
|
|
def __init__(self):
|
|
|
|
"""Initialize a sqlite connection."""
|
|
|
|
super().__init__()
|
2023-05-04 10:44:48 -04:00
|
|
|
self.__check_version()
|
2022-09-23 14:46:46 -04:00
|
|
|
|
2022-05-26 17:18:21 -04:00
|
|
|
self.settings = settings.Table(self)
|
2022-08-10 11:19:29 -04:00
|
|
|
self.playlists = playlists.Table(self)
|
2022-08-22 15:51:18 -04:00
|
|
|
self.artists = artists.Table(self)
|
2022-08-23 11:12:39 -04:00
|
|
|
self.albums = albums.Table(self, queue=self.artists.queue)
|
2022-08-26 13:23:37 -04:00
|
|
|
self.media = media.Table(self, queue=self.artists.queue)
|
2022-08-30 14:43:46 -04:00
|
|
|
self.genres = genres.Table(self)
|
2022-08-31 11:23:47 -04:00
|
|
|
self.decades = decades.Table(self)
|
2022-08-31 15:14:47 -04:00
|
|
|
self.years = years.Table(self, queue=self.decades.queue)
|
2022-09-06 14:13:51 -04:00
|
|
|
self.libraries = libraries.Table(self)
|
2022-05-26 17:18:21 -04:00
|
|
|
|
2022-09-23 09:16:54 -04:00
|
|
|
self.tracks = tracks.Table(self)
|
|
|
|
|
2023-05-04 10:44:48 -04:00
|
|
|
def __check_version(self) -> None:
|
|
|
|
user_version = self("PRAGMA user_version").fetchone()["user_version"]
|
|
|
|
match user_version:
|
|
|
|
case 0:
|
2023-10-18 11:50:29 -04:00
|
|
|
self.executescript(SQL_V1_SCRIPT)
|
|
|
|
self.executescript(SQL_V2_SCRIPT)
|
|
|
|
case 1:
|
|
|
|
self.executescript(SQL_V2_SCRIPT)
|
|
|
|
case 2: pass
|
2023-05-04 10:44:48 -04:00
|
|
|
case _:
|
|
|
|
raise Exception(f"Unsupported data version: {user_version}")
|
|
|
|
|
2023-02-25 20:32:10 -05:00
|
|
|
def close(self) -> None:
|
|
|
|
"""Close the database connection."""
|
|
|
|
self.settings.stop()
|
2022-08-10 11:19:29 -04:00
|
|
|
for tbl in self.playlist_tables():
|
|
|
|
tbl.stop()
|
2022-09-23 09:16:54 -04:00
|
|
|
self.tracks.stop()
|
2023-02-25 20:32:10 -05:00
|
|
|
|
|
|
|
super().close()
|
|
|
|
|
2022-08-10 11:19:29 -04:00
|
|
|
def filter(self, glob: str) -> None:
|
|
|
|
"""Filter the playlist tables."""
|
|
|
|
for tbl in self.playlist_tables():
|
|
|
|
tbl.filter(glob)
|
|
|
|
|
2022-05-26 17:18:21 -04:00
|
|
|
def load(self) -> None:
|
|
|
|
"""Load the database tables."""
|
|
|
|
self.settings.load()
|
2022-08-10 11:19:29 -04:00
|
|
|
for tbl in self.playlist_tables():
|
|
|
|
tbl.load()
|
2022-09-23 09:16:54 -04:00
|
|
|
self.tracks.load()
|
2022-08-10 11:19:29 -04:00
|
|
|
|
|
|
|
def playlist_tables(self) -> Generator[playlist.Table, None, None]:
|
|
|
|
"""Iterate over each playlist table."""
|
2022-08-30 14:43:46 -04:00
|
|
|
for tbl in [self.playlists, self.artists, self.albums, self.media,
|
2022-09-06 14:13:51 -04:00
|
|
|
self.genres, self.decades, self.years, self.libraries]:
|
2022-08-10 11:19:29 -04:00
|
|
|
yield tbl
|
2022-05-26 17:18:21 -04:00
|
|
|
|
2022-08-10 11:19:29 -04:00
|
|
|
def set_active_playlist(self, plist: playlist.Playlist) -> None:
|
|
|
|
"""Set the currently active playlist."""
|
2023-06-06 09:29:16 -04:00
|
|
|
if self.active_playlist == plist:
|
|
|
|
return
|
2022-08-10 11:19:29 -04:00
|
|
|
if self.active_playlist is not None:
|
|
|
|
self.active_playlist.active = False
|
|
|
|
|
|
|
|
self.active_playlist = plist
|
|
|
|
|
|
|
|
if plist is not None:
|
|
|
|
plist.active = True
|
|
|
|
|
2022-09-23 14:46:46 -04:00
|
|
|
@GObject.Signal(arg_types=(table.Table,))
|
|
|
|
def table_loaded(self, tbl: table.Table) -> None:
|
|
|
|
"""Signal that a table has been loaded."""
|
|
|
|
tbl.loaded = True
|