The Collection playlist uses this to emit the "refreshed" signal to notify higher layers that the contents of the playlist has changed. Eventually, this will also be used to update playlists when sorting. Implements: Issue #15 (Convert Playlists into Gio.ListModels) Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
# Copyright 2021 (c) Anna Schumaker.
|
|
#
|
|
# Table: libraries
|
|
# +-----------+-----------+---------+------+
|
|
# | libraryid | plstateid | enabled | path |
|
|
# +-----------+-----------+---------+------+
|
|
import pathlib
|
|
from gi.repository import GObject
|
|
from . import sql
|
|
from . import playlist
|
|
from . import track
|
|
from . import user
|
|
|
|
class Library(playlist.Playlist):
|
|
def __init__(self, row):
|
|
playlist.Playlist.__init__(self, row, "folder-music")
|
|
self._path = pathlib.Path(row["path"])
|
|
self._enabled = row["enabled"]
|
|
|
|
@GObject.Property
|
|
def name(self): return str(self._path)
|
|
|
|
@GObject.Property
|
|
def path(self): return self._path
|
|
|
|
@GObject.Property(type=bool,default=True)
|
|
def enabled(self): return self._enabled
|
|
|
|
@enabled.setter
|
|
def enabled(self, newval):
|
|
sql.execute("UPDATE libraries SET enabled=? WHERE libraryid=?",
|
|
[ newval, self.rowid ])
|
|
sql.commit()
|
|
self._enabled = newval
|
|
user.Table.find("Collection").refresh()
|
|
|
|
def delete(self): Table.delete(self)
|
|
|
|
def tracks(self):
|
|
cursor = sql.execute(f"SELECT trackid FROM tracks "
|
|
"WHERE libraryid=?", [ self.rowid ])
|
|
return [ track.Table.get(row["trackid"]) for row in cursor.fetchall() ]
|
|
|
|
|
|
|
|
class LibraryTable(playlist.Model):
|
|
def __init__(self):
|
|
playlist.Model.__init__(self, "libraries", "path")
|
|
|
|
def do_create(self):
|
|
sql.execute("CREATE TABLE IF NOT EXISTS libraries "
|
|
"(libraryid INTEGER PRIMARY KEY, "
|
|
" plstateid INTEGER NOT NULL, "
|
|
" enabled INTEGER DEFAULT 1, "
|
|
" path TEXT UNIQUE, "
|
|
" FOREIGN KEY (plstateid) REFERENCES playlist_states(plstateid))")
|
|
|
|
def do_factory(self, row):
|
|
return Library(row)
|
|
|
|
def do_insert(self, plstate, path):
|
|
return sql.execute("INSERT INTO libraries (plstateid, path) "
|
|
"VALUES (?, ?)", [ plstate.rowid, str(path) ])
|
|
|
|
def do_lookup(self, path):
|
|
return sql.execute("SELECT * FROM libraries WHERE path=?", [ str(path) ])
|
|
|
|
|
|
Table = LibraryTable()
|