[gnome-music/wip/gbsneto/contained-playlists: 14/35] playlists: Manage playlists internally



commit 2f44988e218cee6543125fcb2881b3c742815618
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Nov 28 19:38:58 2016 +0100

    playlists: Manage playlists internally
    
    This commit makes the Playlists class load and maintain
    an internal list of playlists available.
    
    Next commits will make the potential consumers of this
    API use it.

 gnomemusic/playlists.py |   37 +++++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)
---
diff --git a/gnomemusic/playlists.py b/gnomemusic/playlists.py
index 4b97d65..c02aafd 100644
--- a/gnomemusic/playlists.py
+++ b/gnomemusic/playlists.py
@@ -30,6 +30,7 @@ from gi.repository import Grl, GLib, GObject
 from gnomemusic import TrackerWrapper
 from gnomemusic.grilo import grilo
 from gnomemusic.query import Query
+import gnomemusic.utils as utils
 from gettext import gettext as _
 import inspect
 import time
@@ -190,6 +191,7 @@ class Playlists(GObject.GObject):
         GObject.GObject.__init__(self)
         self.tracker = TrackerWrapper().tracker
         self._static_playlists = StaticPlaylists()
+        self.playlists = []
 
         grilo.connect('ready', self._on_grilo_ready)
 
@@ -231,6 +233,21 @@ class Playlists(GObject.GObject):
                 Query.get_playlist_with_tag(playlist.tag_text), None,
                 callback, playlist)
 
+        # Gather the available playlists too
+        grilo.populate_playlists(0, self._populate_playlists_finish_cb)
+
+    @log
+    def _populate_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(item.get_id(), utils.get_media_title(item))
+        playlist.grilo_item = item
+
+        self.playlists.append(playlist)
+        self.emit('playlist-added', playlist)
+
     @log
     def _create_static_playlist(self, playlist):
         """ Create the tag and the static playlist, and fetch the newly created
@@ -341,6 +358,11 @@ class Playlists(GObject.GObject):
             # tell system we updated the playlist so playlist is reloaded
             self.emit('playlist-updated', playlist.id)
 
+            # Add the playlist to the cache
+            if not playlist in self.playlists:
+                self.playlists.append(playlist)
+                self.emit('playlist-added', playlist)
+
         # Asynchronously form the playlist's final query
         cursor.next_async(None, callback, final_query)
 
@@ -353,6 +375,12 @@ class Playlists(GObject.GObject):
     def create_playlist(self, title):
         def get_callback(source, param, item, count, data, error):
             if item:
+                new_playlist = Playlist(item.get_id(), utils.get_media_title(item))
+                new_playlist.grilo_item = item
+
+                self.playlists.append(new_playlist)
+                self.emit('playlist-added', playlist)
+
                 self.emit('playlist-created', item)
 
         def cursor_callback(cursor, res, data):
@@ -452,6 +480,15 @@ class Playlists(GObject.GObject):
             )
 
     @log
+    def get_playlists(self):
+        """Retrieves the currently loaded playlists.
+
+        :return: a list of Playlists
+        :rtype: list
+        """
+        return self.playlists.copy()
+
+    @log
     def is_static_playlist(self, playlist):
         """Checks whether the given playlist is static or not
 


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