[gnome-music/wip/no-more-load-more] [PATCH] grilo: don't hammer tracker with a query for every album



commit 9d88c19f0f01ca788bec49c176bbbbff05389cd8
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Thu Aug 29 16:23:52 2013 +0200

    [PATCH] grilo: don't hammer tracker with a query for every album
    
    If tracker didn't give us a thumbnail the first time, it's because
    it doesn't have one, so there is no point in querying again (especially
    if we're not really querying for the thumbnail, but only for
    artist and album fields, which we already know!). Instead, what
    we want is to resolve the item using the other sources (GRL_RESOLVE_FULL),
    in particular grl-local-metadata and grl-lastfm-albumart.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=706027

 gnomemusic/grilo.py |   36 +++++++++++++++++++++++-------------
 gnomemusic/query.py |   24 ------------------------
 gnomemusic/view.py  |   16 +++++++---------
 3 files changed, 30 insertions(+), 46 deletions(-)
---
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 4c2b7f9..59aa207 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -37,23 +37,35 @@ class Grilo(GObject.GObject):
         'ready': (GObject.SIGNAL_RUN_FIRST, None, ())
     }
 
+    # Note that we ask for thumbnail in the first query too,
+    # but since we don't pass the Grl.ResolutionFlags.FULL,
+    # we only get a result if tracker has it readily available
+    # (usually it's not, because it requires tracker compiled
+    # with UPnP support)
+    # In any case, we'll proceed to fetch the thumbnail later
+    # by resolving the item, through grl-local-metadata or
+    # grl-lastfm-albumart
     METADATA_KEYS = [
         Grl.METADATA_KEY_ID, Grl.METADATA_KEY_TITLE,
         Grl.METADATA_KEY_ARTIST, Grl.METADATA_KEY_ALBUM,
         Grl.METADATA_KEY_DURATION,
-        Grl.METADATA_KEY_CREATION_DATE]
+        Grl.METADATA_KEY_CREATION_DATE,
+        Grl.METADATA_KEY_THUMBNAIL
+    ]
 
     METADATA_THUMBNAIL_KEYS = [
         Grl.METADATA_KEY_ID,
-        Grl.METADATA_KEY_THUMBNAIL,
+        Grl.METADATA_KEY_THUMBNAIL
     ]
 
     def __init__(self):
         GObject.GObject.__init__(self)
 
-        self.options = Grl.OperationOptions()
-        self.options.set_flags(Grl.ResolutionFlags.FULL |
-                               Grl.ResolutionFlags.IDLE_RELAY)
+        self.fast_options = Grl.OperationOptions()
+        self.fast_options.set_flags(Grl.ResolutionFlags.IDLE_RELAY)
+        self.full_options = Grl.OperationOptions()
+        self.full_options.set_flags(Grl.ResolutionFlags.FULL |
+                                    Grl.ResolutionFlags.IDLE_RELAY)
 
         self.registry = Grl.Registry.get_default()
         try:
@@ -96,8 +108,8 @@ class Grilo(GObject.GObject):
     def populate_album_songs(self, album_id, callback):
         self.populate_items(Query.album_songs(album_id), 0, callback)
 
-    def populate_items(self, query, offset, callback):
-        options = self.options.copy()
+    def populate_items(self, query, offset, callback, count=50):
+        options = self.fast_options.copy()
         options.set_skip(offset)
 
         def _callback(source, param, item, count, data, offset):
@@ -108,16 +120,14 @@ class Grilo(GObject.GObject):
         print('yeah')
 
     def search(self, q):
-        options = self.options.copy()
         for source in self.sources:
             print(source.get_name() + ' - ' + q)
             source.search(q, [Grl.METADATA_KEY_ID], 0, 10,
-                          options, self._search_callback, source)
+                          self.fast_options, self._search_callback, source)
 
-    def get_album_art_for_album_id(self, album_id, _callback):
-        options = self.options.copy()
-        query = Query.get_album_for_id(album_id)
-        self.tracker.query(query, self.METADATA_THUMBNAIL_KEYS, options, _callback, None)
+    def get_album_art_for_album(self, album, _callback, data):
+        self.tracker.resolve(album, self.METADATA_THUMBNAIL_KEYS,
+                             self.full_options, _callback, data)
 
 Grl.init(None)
 
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index 29e0bac..daa407b 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -231,27 +231,3 @@ class Query():
 
         return query
 
-    @staticmethod
-    def get_album_for_id(album_id):
-        query = """
-    SELECT DISTINCT
-        rdf:type(?album)
-        tracker:id(?album) AS id
-        (
-            SELECT
-                nmm:artistName(?artist)
-            WHERE {
-                ?album nmm:albumArtist ?artist
-            }
-            LIMIT 1
-        ) AS artist
-        nie:title(?album) AS title
-        nie:title(?album) AS album
-    WHERE {
-        ?album a nmm:MusicAlbum  .
-        FILTER (
-            tracker:id(?album) = %(album_id)s
-        )
-    }
-    """.replace("\n", " ").strip() % {'album_id': album_id}
-        return query
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index 1b6fb2c..c73f08b 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -195,10 +195,12 @@ class ViewContainer(Stack):
 
         GLib.idle_add(add_new_item)
 
-    def _insert_album_art(self, item, cb_item, itr, x=False):
-        if item and cb_item and not item.get_thumbnail():
-            if cb_item.get_thumbnail():
-                item.set_thumbnail(cb_item.get_thumbnail())
+    def _insert_album_art(self, source, op_id, item, itr, error):
+        if error:
+            return
+
+        thumbnail = item.get_thumbnail()
+        if thumbnail:
             albumArtCache.get_default().lookup(
                 item,
                 self._iconWidth,
@@ -206,11 +208,7 @@ class ViewContainer(Stack):
                 self._on_lookup_ready, itr)
 
     def _update_album_art(self, item, itr):
-        grilo.get_album_art_for_album_id(
-            item.get_id(),
-            lambda source, count, cb_item, x, y, z:
-            self._insert_album_art(item, cb_item, itr, True)
-        )
+        grilo.get_album_art_for_album(item, self._insert_album_art, itr)
 
     def _on_lookup_ready(self, icon, path, _iter):
         if icon:


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