# Copyright 2022 (c) Anna Schumaker. """Displays library path playlists.""" import pathlib from gi.repository import GLib from gi.repository import Gio from gi.repository import Gtk from gi.repository import Adw from .. import db from . import row from . import section DIRECTORY_FILTERS = Gio.ListStore() DIRECTORY_FILTERS.append(Gtk.FileFilter(name="Directories", mime_types=["inode/directory"])) class LibraryRow(row.TreeRow): """A TreeRow for displaying Library playlists.""" def __init__(self, *args, **kwargs): """Initialize a LibraryRow.""" super().__init__(*args, indented=False, **kwargs) self.child = row.LibraryRow() def do_bind(self) -> None: """Bind Library properties to the LibraryRow widget.""" super().do_bind() self.bind_and_set_property("enabled", "enabled", bidirectional=True) self.bind_and_set_property("online", "online", bidirectional=True) self.bind_and_set_property("deleting", "sensitive", invert_boolean=True) self.bind_and_set(self.item.queue, "running", self.child, "scanning") self.bind_and_set(self.item.queue, "progress", self.child, "progress") class Section(section.Section): """A sidebar Section for library path playlists.""" def __init__(self, table=db.libraries.Table): """Initialize our library path section.""" super().__init__(table, LibraryRow, icon_name="library-music-symbolic", title="Library Paths", subtitle="0 library paths") self.extra_widget = Gtk.Button(icon_name="folder-new", has_frame=False, tooltip_text="add new library path") self._dialog = Gtk.FileDialog(title="Pick a Directory", filters=DIRECTORY_FILTERS) self._toast = None self.extra_widget.connect("clicked", self.__show_dialog) self.table.connect("library-online", self.__library_online) def __async_ready(self, dialog: Gtk.FileDialog, task: Gio.Task) -> None: try: dir = dialog.select_folder_finish(task) self.table.create(pathlib.Path(dir.get_path())) except GLib.Error: pass def __library_online(self, table: db.libraries.Table, library: db.libraries.Library) -> None: if win := self.get_ancestor(Adw.Window): if self._toast: self._toast.dismiss() state = "online" if library.online else "offline" self._toast = win.post_toast(f"{library.path} is {state}") self._toast.set_priority(Adw.ToastPriority.HIGH) def __show_dialog(self, button: Gtk.Button) -> None: music = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_MUSIC) self._dialog.set_initial_file(Gio.File.new_for_path(music)) self._dialog.select_folder(self.get_ancestor(Gtk.Window), None, self.__async_ready) def do_get_subtitle(self, n_libraries: int) -> str: """Return a subtitle for this section.""" s_libraries = "s" if n_libraries != 1 else "" return f"{n_libraries} library path{s_libraries}"