[gnome-music] albumArtCache: Run grilo calls in main thread



commit 41e7d8cb00672e14949b5b646c523d02ca29d53b
Author: Arnel A. Borja <arnelborja src gnome org>
Date:   Thu Apr 17 17:59:27 2014 +0800

    albumArtCache: Run grilo calls in main thread

 gnomemusic/albumArtCache.py |   48 +++++++++++++++++++++++++++---------------
 gnomemusic/grilo.py         |    5 ++-
 2 files changed, 34 insertions(+), 19 deletions(-)
---
diff --git a/gnomemusic/albumArtCache.py b/gnomemusic/albumArtCache.py
index 254d7ae..40ed0f3 100644
--- a/gnomemusic/albumArtCache.py
+++ b/gnomemusic/albumArtCache.py
@@ -147,44 +147,58 @@ class AlbumArtCache:
     @log
     def lookup_worker(self, item, width, height, callback, itr, artist, album):
         try:
-            width = width or -1
-            height = height or -1
             path = MediaArt.get_path(artist, album, "album", None)[0]
             if not os.path.exists(path):
-                self.cached_thumb_not_found(item, album, artist, path, callback, itr)
-            self.read_cached_pixbuf(path, width, height, callback, itr)
+                GLib.idle_add(self.cached_thumb_not_found, item, width, height, path, callback, itr, artist, 
album)
+                return
+            width = width or -1
+            height = height or -1
+            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, width, height, True)
+            self.finish(_make_icon_frame(pixbuf), path, callback, itr)
         except Exception as e:
             logger.warn("Error: %s" % e)
 
     @log
-    def read_cached_pixbuf(self, path, width, height, callback, itr):
+    def finish(self, pixbuf, path, callback, itr):
         try:
-            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, width, height, True)
-            self.finish(_make_icon_frame(pixbuf), path, callback, itr)
+            GLib.idle_add(callback, pixbuf, path, itr)
         except Exception as e:
-            logger.debug("Error: %s" % e)
+            logger.warn("Error: %s" % e)
 
     @log
-    def finish(self, pixbuf, path, callback, itr):
+    def cached_thumb_not_found(self, item, width, height, path, callback, itr, artist, album):
         try:
-            GLib.idle_add(callback, pixbuf, path, itr)
+            uri = item.get_thumbnail()
+            if uri is None:
+                grilo.get_album_art_for_album_id(item.get_id(), self.album_art_for_album_id_callback,
+                                                 (item, width, height, path, callback, itr, artist, album))
+                return
+
+            start_new_thread(self.download_worker,
+                             (item, width, height, path, callback, itr, artist, album, uri))
         except Exception as e:
             logger.warn("Error: %s" % e)
 
     @log
-    def cached_thumb_not_found(self, item, album, artist, path, callback, itr):
+    def album_art_for_album_id_callback(self, source, param, item, count, data, error):
+        old_item, width, height, path, callback, itr, artist, album = data
         try:
             uri = item.get_thumbnail()
             if uri is None:
-                new_item = grilo.get_album_art_for_album_id(item.get_id())[0]
-                uri = new_item.get_thumbnail()
-                if uri is None:
-                    logger.warn("can't find URL for album '%s' by %s" % (album, artist))
-                    self.finish(None, path, callback, itr)
-                    return
+                logger.warn("can't find URL for album '%s' by %s" % (album, artist))
+                self.finish(None, path, callback, itr)
+                return
 
+            start_new_thread(self.download_worker, (item, width, height, path, callback, itr, artist, album, 
uri))
+        except Exception as e:
+            logger.warn("Error: %s" % e)
+
+    @log
+    def download_worker(self, item, width, height, path, callback, itr, artist, album, uri):
+        try:
             src = Gio.File.new_for_uri(uri)
             dest = Gio.File.new_for_path(path)
             src.copy(dest, Gio.FileCopyFlags.OVERWRITE)
+            self.lookup_worker(item, width, height, callback, itr, artist, album)
         except Exception as e:
             logger.warn("Error: %s" % e)
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index d3ff075..2f24f97 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -197,10 +197,11 @@ class Grilo(GObject.GObject):
         self.tracker.query(query, self.METADATA_KEYS, options, _callback, None)
 
     @log
-    def get_album_art_for_album_id(self, album_id):
+    def get_album_art_for_album_id(self, album_id, callback, data=None):
         options = self.full_options.copy()
+        options.set_count(1)
         query = Query.get_album_for_id(album_id)
-        return self.tracker.query_sync(query, self.METADATA_THUMBNAIL_KEYS, options)
+        self.tracker.query(query, self.METADATA_THUMBNAIL_KEYS, options, callback, data)
 
 
 Grl.init(None)


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