[gnome-music/wip/gbsneto/playlist-improvements: 3/3] playlists: rewrite static playlist creation routine



commit d1cc7e95c6ae37686f3de9524ada9db943867e9e
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Oct 18 11:42:36 2016 -0200

    playlists: rewrite static playlist creation routine
    
    This commit makes the creation of static playlists an asynchronous
    chain of operations, so we don't block the UI waiting for all the
    back and forth of DBus and Tracker.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=773192

 gnomemusic/playlists.py |   76 +++++++++++++++++++++++++++++++++-------------
 1 files changed, 54 insertions(+), 22 deletions(-)
---
diff --git a/gnomemusic/playlists.py b/gnomemusic/playlists.py
index 5f25d62..78b1d39 100644
--- a/gnomemusic/playlists.py
+++ b/gnomemusic/playlists.py
@@ -147,21 +147,70 @@ class Playlists(GObject.GObject):
 
         def callback(obj, result, playlist):
             cursor = obj.query_finish(result)
-            while (cursor.next(None)):
+
+            # Search for the playlist ID
+            while cursor.next(None):
                 playlist.ID = cursor.get_integer(1)
 
             if not playlist.ID:
-                # create the playlist
-                playlist.ID = self.create_playlist_and_return_id(playlist.TITLE, playlist.TAG_TEXT)
-
-            self.update_static_playlist(playlist)
+                # Create the  static playlist
+                self._create_static_playlist(playlist)
+            else:
+                # Update playlist
+                self.update_static_playlist(playlist)
 
+        # Start fetching all the static playlists
         for playlist in self._static_playlists.get_all():
             self.tracker.query_async(
                 Query.get_playlist_with_tag(playlist.TAG_TEXT), None,
                 callback, playlist)
 
     @log
+    def _create_static_playlist(self, playlist):
+        """ Create the tag and the static playlist, and fetch the newly created
+        playlist's songs.
+        """
+        title = playlist.TITLE
+        tag_text = playlist.TAG_TEXT
+
+        def playlist_queried_cb(obj, res, playlist):
+            """ Called after the playlist is created and the ID is fetched """
+            cursor = obj.query_finish(res)
+
+            if not cursor or not cursor.next():
+                return
+
+            # Update the playlist ID
+            playlist.ID = cursor.get_integer(0)
+
+            # Fetch the playlist contents
+            self.update_static_playlist(playlist)
+
+        def playlist_created_cb(obj, res, playlist):
+            """ Called when the static playlist is created """
+            data = obj.update_blank_finish(res)
+            playlist_urn = data.get_child_value(0).get_child_value(0).\
+            get_child_value(0).get_child_value(1).get_string()
+
+            query = Query.get_playlist_with_urn(playlist_urn)
+
+            # Start fetching the playlist
+            self.tracker.query_async(query, None, playlist_queried_cb, playlist)
+
+        def tag_created_cb(obj, res, playlist):
+            """ Called when the tag is created """
+            creation_query = Query.create_playlist_with_tag(title, tag_text)
+
+            # Start creating the playlist itself
+            self.tracker.update_blank_async(creation_query, GLib.PRIORITY_LOW,
+                                            None, playlist_created_cb, playlist)
+
+        # Start the playlist creation by creating the tag
+        self.tracker.update_blank_async(Query.create_tag(tag_text),
+                                        GLib.PRIORITY_LOW, None,
+                                        tag_created_cb, playlist)
+
+    @log
     def update_playcount(self, song_url):
         query = Query.update_playcount(song_url)
         self.tracker.update(query, GLib.PRIORITY_LOW, None)
@@ -234,23 +283,6 @@ class Playlists(GObject.GObject):
             self.update_static_playlist(playlist)
 
     @log
-    def create_playlist_and_return_id(self, title, tag_text):
-        self.tracker.update_blank(Query.create_tag(tag_text), GLib.PRIORITY_LOW, None)
-
-        data = self.tracker.update_blank(
-            Query.create_playlist_with_tag(title, tag_text), GLib.PRIORITY_LOW,
-            None)
-        playlist_urn = data.get_child_value(0).get_child_value(0).\
-            get_child_value(0).get_child_value(1).get_string()
-
-        cursor = self.tracker.query(
-            Query.get_playlist_with_urn(playlist_urn),
-            None)
-        if not cursor or not cursor.next():
-            return
-        return cursor.get_integer(0)
-
-    @log
     def create_playlist(self, title):
         def get_callback(source, param, item, count, data, error):
             if item:


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