[gnome-music] Refactor albumart



commit 8ea50d9f7b7348d8aff6ddc4ad435c44792a9cfb
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Tue Apr 15 21:52:52 2014 +0200

    Refactor albumart

 gnomemusic/albumArtCache.py |  128 ++++++++++++++++++-------------------------
 gnomemusic/grilo.py         |    4 +-
 gnomemusic/notification.py  |    6 +-
 gnomemusic/player.py        |    2 +-
 gnomemusic/view.py          |   12 ++--
 gnomemusic/widgets.py       |    4 +-
 6 files changed, 67 insertions(+), 89 deletions(-)
---
diff --git a/gnomemusic/albumArtCache.py b/gnomemusic/albumArtCache.py
index 12ce356..f447dc1 100644
--- a/gnomemusic/albumArtCache.py
+++ b/gnomemusic/albumArtCache.py
@@ -37,6 +37,56 @@ import os
 from gnomemusic import log
 import logging
 logger = logging.getLogger(__name__)
+frame_lock = threading.Lock()
+
+
+ log
+def _make_icon_frame(pixbuf, path=None):
+
+    border = 1.5
+    w = pixbuf.get_width()
+    h = pixbuf.get_height()
+    pixbuf = pixbuf.scale_simple(w - border * 2,
+                                 h - border * 2,
+                                 0)
+
+    result = _draw_rounded_path(0, 0, w, h, 3)
+
+    pixbuf.copy_area(border, border,
+                     w - border * 4,
+                     h - border * 4,
+                     result,
+                     border * 2, border * 2)
+    return pixbuf
+
+
+ log
+def _draw_rounded_path(x, y, width, height, radius):
+    degrees = pi / 180
+
+    global frame_lock
+    frame_lock.acquire()
+    #if key not in frame_cache:
+    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
+    ctx = cairo.Context(surface)
+    ctx.new_sub_path()
+    ctx.arc(x + width - radius, y + radius, radius - 0.5,
+            -90 * degrees, 0 * degrees)
+    ctx.arc(x + width - radius, y + height - radius, radius - 0.5,
+            0 * degrees, 90 * degrees)
+    ctx.arc(x + radius, y + height - radius, radius - 0.5,
+            90 * degrees, 180 * degrees)
+    ctx.arc(x + radius, y + radius, radius - 0.5, 180 * degrees,
+            270 * degrees)
+    ctx.close_path()
+    ctx.set_line_width(0.6)
+    ctx.set_source_rgb(0.2, 0.2, 0.2)
+    ctx.stroke_preserve()
+    ctx.set_source_rgb(1, 1, 1)
+    ctx.fill()
+    res = Gdk.pixbuf_get_from_surface(surface, 0, 0, width, height)
+    frame_lock.release()
+    return res
 
 
 class LookupRequest:
@@ -61,9 +111,6 @@ class LookupRequest:
 
     @log
     def finish(self, pixbuf):
-        if pixbuf:
-            # Cache the path on the original item for faster retrieval
-            self.item.set_thumbnail(GLib.filename_to_uri(self.path, None))
         self.callback(pixbuf, self.path, self.data)
 
     @log
@@ -97,8 +144,9 @@ class LookupRequest:
                     self.width *= (width / height)
                 scale = max(width / self.width, height / self.height)
                 pixbuf = pixbuf.scale_simple(width / scale, height / scale, 2)
-                self.finish(pixbuf)
-                return
+            pixbuf = _make_icon_frame(pixbuf)
+            self.finish(pixbuf)
+            return
         except Exception as error:
             if AlbumArtCache.get_default().logLookupErrors:
                 print('ERROR:', error)
@@ -147,25 +195,6 @@ class GetUriRequest:
     def _on_read_ready(self, outstream, res, user_data=None):
         try:
             self.stream = outstream.read_finish(res)
-
-            try:
-                streamInfo =\
-                    self.stream.query_info('standard::content-type', None)
-                contentType = streamInfo.get_content_type()
-
-                if contentType == 'image/png':
-                    self.path += '.png'
-                elif contentType == 'image/jpeg':
-                    self.path += '.jpeg'
-                else:
-                    print('Thumbnail format not supported, not caching')
-                    self.stream.close(None)
-                    return
-            except Exception as e:
-                print('Failed to query thumbnail content type')
-                self.path += '.jpeg'
-                return
-
             newFile = Gio.File.new_for_path(self.path)
             newFile.replace_async(None, False,
                                   Gio.FileCreateFlags.REPLACE_DESTINATION,
@@ -198,7 +227,6 @@ class GetUriRequest:
 
 class AlbumArtCache:
     instance = None
-    degrees = pi / 180
 
     @classmethod
     def get_default(self):
@@ -238,8 +266,6 @@ class AlbumArtCache:
         self.logLookupErrors = False
         self.requested_uris = {}
         self.cacheDir = os.path.join(GLib.get_user_cache_dir(), 'media-art')
-        self.frame_cache = {}
-        self.frame_lock = threading.Lock()
 
         try:
             Gio.file_new_for_path(self.cacheDir).make_directory(None)
@@ -247,7 +273,7 @@ class AlbumArtCache:
             pass
 
     @log
-    def make_default_icon(self, width, height):
+    def get_default_icon(self, width, height):
         # get a small pixbuf with the given path
         icon = Gtk.IconTheme.get_default().load_icon('folder-music-symbolic', max(width, height) / 4, 0)
 
@@ -267,55 +293,9 @@ class AlbumArtCache:
                        icon.get_height() * 3 / 2,
                        1, 1,
                        GdkPixbuf.InterpType.NEAREST, 0xff)
-        return self._make_icon_frame(result)
-
-    @log
-    def _make_icon_frame(self, pixbuf):
-        border = 1.5
-        w = pixbuf.get_width()
-        h = pixbuf.get_height()
-        pixbuf = pixbuf.scale_simple(w - border * 2,
-                                     h - border * 2,
-                                     0)
-
-        result = self._draw_rounded_path(0, 0, w, h, 3)
-
-        pixbuf.copy_area(border, border,
-                         w - border * 4,
-                         h - border * 4,
-                         result,
-                         border * 2, border * 2)
-
         return result
 
     @log
-    def _draw_rounded_path(self, x, y, width, height, radius):
-        key = "%dx%d %dx%d:%d" % (width, height, x, y, radius)
-        self.frame_lock.acquire()
-        if key not in self.frame_cache:
-            surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
-            ctx = cairo.Context(surface)
-            ctx.new_sub_path()
-            ctx.arc(x + width - radius, y + radius, radius - 0.5,
-                    -90 * self.degrees, 0 * self.degrees)
-            ctx.arc(x + width - radius, y + height - radius, radius - 0.5,
-                    0 * self.degrees, 90 * self.degrees)
-            ctx.arc(x + radius, y + height - radius, radius - 0.5,
-                    90 * self.degrees, 180 * self.degrees)
-            ctx.arc(x + radius, y + radius, radius - 0.5, 180 * self.degrees,
-                    270 * self.degrees)
-            ctx.close_path()
-            ctx.set_line_width(0.6)
-            ctx.set_source_rgb(0.2, 0.2, 0.2)
-            ctx.stroke_preserve()
-            ctx.set_source_rgb(1, 1, 1)
-            ctx.fill()
-            self.frame_cache[key] = Gdk.pixbuf_get_from_surface(surface, 0, 0, width, height)
-        res = self.frame_cache[key].copy()
-        self.frame_lock.release()
-        return res
-
-    @log
     def lookup(self, item, width, height, callback, data=None):
         request = LookupRequest(item, width, height, callback, data)
         request.start()
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 409d8ff..4e59326 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -163,8 +163,8 @@ class Grilo(GObject.GObject):
         if count != -1:
             options.set_count(count)
 
-        def _callback(source, param, item, count, data, offset):
-            callback(source, param, item, count)
+        def _callback(source, param, item, remaining, data, offset):
+            callback(source, param, item, remaining)
         self.tracker.query(query, self.METADATA_KEYS, options, _callback, None)
 
     @log
diff --git a/gnomemusic/notification.py b/gnomemusic/notification.py
index 36a02c4..49e3ac7 100644
--- a/gnomemusic/notification.py
+++ b/gnomemusic/notification.py
@@ -50,7 +50,7 @@ class NotificationManager:
         self._isPlaying = False
 
         self._albumArtCache = AlbumArtCache.get_default()
-        self._symbolicIcon = self._albumArtCache.make_default_icon(IMAGE_SIZE, IMAGE_SIZE)
+        self._symbolicIcon = self._albumArtCache.get_default_icon(IMAGE_SIZE, IMAGE_SIZE)
 
         self._player.connect('playing-changed', self._on_playing_changed)
         self._player.connect('current-changed', self._update_track)
@@ -107,8 +107,6 @@ class NotificationManager:
             if not image:
                 image = self._symbolicIcon
 
-            width = image.get_width()
-            height = image.get_height()
             rowStride = image.get_rowstride()
             hasAlpha = image.get_has_alpha()
             bitsPerSample = image.get_bits_per_sample()
@@ -116,7 +114,7 @@ class NotificationManager:
             data = image.get_pixels()
 
             serialized = GLib.Variant('(iiibiiay)',
-                                      [width, height, rowStride, hasAlpha,
+                                      [IMAGE_SIZE, IMAGE_SIZE, rowStride, hasAlpha,
                                        bitsPerSample, nChannels, data])
             self._notification.set_hint('image-data', serialized)
 
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 3a0f328..1ff807d 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -85,7 +85,7 @@ class Player(GObject.GObject):
         self.currentTrack = None
         self._lastState = Gst.State.PAUSED
         self.cache = AlbumArtCache.get_default()
-        self._symbolicIcon = self.cache.make_default_icon(ART_SIZE, ART_SIZE)
+        self._symbolicIcon = self.cache.get_default_icon(ART_SIZE, ART_SIZE)
 
         Gst.init(None)
 
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index 00bc6f8..621e563 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -135,8 +135,8 @@ class ViewContainer(Gtk.Stack):
         self._loadMore.widget.hide()
         self._connect_view()
         self.cache = albumArtCache.get_default()
-        self._symbolicIcon = self.cache.make_default_icon(self._iconHeight,
-                                                          self._iconWidth)
+        self._symbolicIcon = self.cache.get_default_icon(self._iconHeight,
+                                                         self._iconWidth)
 
         self._init = False
         grilo.connect('ready', self._on_grilo_ready)
@@ -284,7 +284,7 @@ class ViewContainer(Gtk.Stack):
                             [str(item.get_id()), '', title,
                              artist, self._symbolicIcon, item,
                              0, icon_name, False, icon_name == self.errorIconName])
-            GLib.idle_add(self._update_album_art, item, _iter)
+            self._update_album_art(item, _iter)
 
         GLib.idle_add(add_new_item)
 
@@ -312,7 +312,7 @@ class ViewContainer(Gtk.Stack):
         if icon:
             self._model.set_value(
                 _iter, 4,
-                albumArtCache.get_default()._make_icon_frame(icon))
+                icon)
             self.view.queue_draw()
 
     @log
@@ -441,8 +441,8 @@ class Songs(ViewContainer):
         self._iconHeight = 32
         self._iconWidth = 32
         self.cache = albumArtCache.get_default()
-        self._symbolicIcon = self.cache.make_default_icon(self._iconHeight,
-                                                          self._iconWidth)
+        self._symbolicIcon = self.cache.get_default_icon(self._iconHeight,
+                                                         self._iconWidth)
         self._add_list_renderers()
         self.player = player
         self.player.connect('playlist-item-changed', self.update_model)
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index 4356686..ddc0209 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -115,7 +115,7 @@ class AlbumWidget(Gtk.EventBox):
 
     tracks = []
     duration = 0
-    symbolicIcon = ALBUM_ART_CACHE.make_default_icon(256, 256)
+    symbolicIcon = ALBUM_ART_CACHE.get_default_icon(256, 256)
     filter = None
 
     @log
@@ -552,7 +552,7 @@ class ArtistAlbumWidget(Gtk.HBox):
         self.ui.add_from_resource('/org/gnome/Music/ArtistAlbumWidget.ui')
 
         self.cache = AlbumArtCache.get_default()
-        pixbuf = self.cache.make_default_icon(128, 128)
+        pixbuf = self.cache.get_default_icon(128, 128)
         GLib.idle_add(self._update_album_art)
 
         self.ui.get_object('cover').set_from_pixbuf(pixbuf)


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