[gnome-music/wip/jfelder/playlists-core-rewrite-prep-work: 7/18] playlists: Manage playlists internally



commit e5765c7c6aca439bd503d4d8881e93ff83e0c713
Author: Jean Felder <jfelder src gnome org>
Date:   Mon Jul 1 21:41:44 2019 +0200

    playlists: Manage playlists internally
    
    This commit makes the Playlists class load and maintain an internal
    list of playlists available by using a Gio.ListStore
    
    Next commits will make the potential consumers of this API use it.
    
    This is based on initial work done by Georges Basile Stavracas Neto.

 gnomemusic/playlists.py | 63 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 58 insertions(+), 5 deletions(-)
---
diff --git a/gnomemusic/playlists.py b/gnomemusic/playlists.py
index 7912dc9b..85a4aca3 100644
--- a/gnomemusic/playlists.py
+++ b/gnomemusic/playlists.py
@@ -27,10 +27,12 @@
 
 
 import gi
+gi.require_version("Dazzle", "1.0")
 gi.require_version('Grl', '0.3')
-from gi.repository import Grl, GLib, GObject
+from gi.repository import Dazzle, Gio, Grl, GLib, GObject
 from gnomemusic.grilo import grilo
 from gnomemusic.query import Query
+import gnomemusic.utils as utils
 from gettext import gettext as _
 
 from gnomemusic import log
@@ -176,6 +178,7 @@ class Playlists(GObject.GObject):
             "RecentlyAdded": RecentlyAdded(),
             "Favorites": Favorites()
         }
+        self._model = Gio.ListStore.new(Playlist)
 
         self._pls_todelete = {}
 
@@ -184,7 +187,9 @@ class Playlists(GObject.GObject):
     @log
     def _on_grilo_ready(self, data=None):
         """For all smart playlists: get id, if exists; if not, create
-        the playlist and get id."""
+        the playlist and get id.
+        Also gather the user playlists.
+        """
 
         def playlist_id_fetched_cb(cursor, res, playlist):
             """ Called after the playlist id is fetched """
@@ -221,6 +226,22 @@ class Playlists(GObject.GObject):
                 Query.get_playlist_with_tag(playlist.props.tag_text), None,
                 callback, playlist)
 
+        # Gather the available user playlists too
+        grilo.populate_user_playlists(
+            0, self._populate_user_playlists_finish_cb)
+
+    @log
+    def _populate_user_playlists_finish_cb(
+            self, source, param, item, remaining=0, data=None):
+        """Fill in the list of playlists currently available"""
+        if not item:
+            return
+
+        playlist = Playlist(
+            pl_id=item.get_id(), title=utils.get_media_title(item))
+
+        self._model.append(playlist)
+
     @log
     def _create_smart_playlist(self, playlist):
         """ Create the tag and the smart playlist, and fetch the newly created
@@ -336,7 +357,11 @@ class Playlists(GObject.GObject):
 
     @log
     def _smart_playlist_update_finished(self, source, res, smart_playlist):
-        self.emit('playlist-updated', smart_playlist.props.pl_id)
+        if smart_playlist in self._model:
+            self.emit("playlist-updated", smart_playlist)
+            return
+
+        self._model.append(smart_playlist)
 
     @log
     def update_all_smart_playlists(self):
@@ -346,8 +371,13 @@ class Playlists(GObject.GObject):
     @log
     def create_playlist(self, title):
         def get_callback(source, param, item, count, data, error):
-            if item:
-                self.emit('playlist-created', item)
+            if not item:
+                return
+
+            new_playlist = Playlist(
+                pl_id=item.get_id(), title=utils.get_media_title(item))
+            self._model.append(new_playlist)
+            self.emit("playlist-created", item)
 
         def cursor_callback(cursor, res, data):
             try:
@@ -474,6 +504,25 @@ class Playlists(GObject.GObject):
                 Query.change_song_position(playlist_id, item_id, new_position),
                 GLib.PRIORITY_LOW, None, update_callback, item)
 
+    @log
+    def get_playlists_model(self):
+        """Retrieves the currently loaded playlists.
+
+        :return: a list of Playlists
+        :rtype: GioListStore
+        """
+        return self._model
+
+    @log
+    def get_user_playlists(self):
+        def user_playlists_filter(playlist):
+            return (playlist.props.id not in self._pls_todelete.keys()
+                    and not playlist.props.is_smart)
+
+        model_filter = Dazzle.ListModelFilter.new(self._model)
+        model_filter.set_filter_func(user_playlists_filter)
+        return model_filter
+
     @log
     def get_smart_playlist(self, name):
         """SmartPlaylist getter
@@ -520,6 +569,7 @@ class Playlists(GObject.GObject):
             "playlist": playlist,
             "index": index
         }
+        self._model.remove(index)
 
     @log
     def undo_pending_deletion(self, playlist):
@@ -532,6 +582,9 @@ class Playlists(GObject.GObject):
         playlist_id = playlist.get_id()
         index = self._pls_todelete[playlist_id]["index"]
         self._pls_todelete.pop(playlist_id)
+        playlist = Playlist(
+            pl_id=playlist_id, title=utils.get_media_title(playlist))
+        self._model.insert(index, playlist)
 
         return index
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]