[gnome-music] albumartcache: Split default icon handling out



commit fac6cb4bd0df4f99681e1aae7b46d5831d3819ef
Author: Marinus Schraal <mschraal src gnome org>
Date:   Tue Aug 16 12:50:26 2016 +0200

    albumartcache: Split default icon handling out
    
    The AlbumArtCache class is cluttered with different tasks, split out the
    handling of the loading and music icons.

 gnomemusic/albumartcache.py |  131 ++++++++++++++++++++++++-------------------
 gnomemusic/notification.py  |   10 ---
 gnomemusic/player.py        |    9 ++-
 gnomemusic/view.py          |   22 +++++---
 gnomemusic/widgets.py       |   14 ++--
 5 files changed, 99 insertions(+), 87 deletions(-)
---
diff --git a/gnomemusic/albumartcache.py b/gnomemusic/albumartcache.py
index bcfde86..89430df 100644
--- a/gnomemusic/albumartcache.py
+++ b/gnomemusic/albumartcache.py
@@ -27,16 +27,21 @@
 # code, but you are not obligated to do so.  If you do not wish to do so,
 # delete this exception statement from your version.
 
-import gi
-gi.require_version('MediaArt', '2.0')
-from gi.repository import Gtk, GdkPixbuf, Gio, GLib, Gdk, MediaArt, GObject
-from gettext import gettext as _
-import cairo
+from enum import Enum
+import logging
 from math import pi
 import os
+
+import cairo
+from gettext import gettext as _
+import gi
+gi.require_version('MediaArt', '2.0')
+from gi.repository import Gdk, GdkPixbuf, Gio, GLib, GObject, Gtk, MediaArt
+
 from gnomemusic import log
 from gnomemusic.grilo import grilo
-import logging
+
+
 logger = logging.getLogger(__name__)
 
 
@@ -80,10 +85,69 @@ def _make_icon_frame(pixbuf):
     return border_pixbuf
 
 
+class DefaultIcon(GObject.GObject):
+    """Provides the symbolic fallback and loading icons."""
+
+    class Type(Enum):
+        loading = 'content-loading-symbolic'
+        music = 'folder-music-symbolic'
+
+    _cache = {}
+
+    def __repr__(self):
+        return '<DefaultIcon>'
+
+    @log
+    def _make_default_icon(self, width, height, icon_type):
+        icon = Gtk.IconTheme.get_default().load_icon(icon_type.value,
+                                                     max(width, height) / 4,
+                                                     0)
+
+        # create an empty pixbuf with the requested size
+        result = GdkPixbuf.Pixbuf.new(icon.get_colorspace(),
+                                      True,
+                                      icon.get_bits_per_sample(),
+                                      width,
+                                      height)
+        result.fill(0xffffffff)
+
+        icon.composite(result,
+                       icon.get_width() * 3 / 2,
+                       icon.get_height() * 3 / 2,
+                       icon.get_width(),
+                       icon.get_height(),
+                       icon.get_width() * 3 / 2,
+                       icon.get_height() * 3 / 2,
+                       1, 1, GdkPixbuf.InterpType.HYPER, 0x33)
+
+        final_icon = _make_icon_frame(result)
+
+        return final_icon
+
+    @log
+    def get(self, width, height, icon_type):
+        """Returns the requested symbolic icon
+
+        Returns a GdkPixbuf of the requested symbolic icon
+        in the given size.
+
+        :param int width: The width of the icon
+        :param int height: The height of the icon
+        :param enum icon_type: The DefaultIcon.Type of the icon
+
+        :return: The symbolic icon
+        :rtype: GdkPixbuf
+        """
+        if (width, height, icon_type) not in self._cache.keys():
+            new_icon = self._make_default_icon(width, height, icon_type)
+            self._cache[(width, height, icon_type)] = new_icon
+
+        return self._cache[(width, height, icon_type)]
+
+
 class AlbumArtCache(GObject.GObject):
     instance = None
     blacklist = {}
-    default_icon_cache = {}
 
     def __repr__(self):
         return '<AlbumArt>'
@@ -129,56 +193,7 @@ class AlbumArtCache(GObject.GObject):
         except Exception as e:
             logger.warn("Error: %s", e)
 
-    @log
-    def _make_default_icon(self, width, height, is_loading=False):
-        icon_name = 'folder-music-symbolic'
-        if is_loading:
-            icon_name = 'content-loading-symbolic'
-
-        icon = Gtk.IconTheme.get_default().load_icon(icon_name,
-                                                     max(width, height) / 4,
-                                                     0)
-
-        # create an empty pixbuf with the requested size
-        result = GdkPixbuf.Pixbuf.new(icon.get_colorspace(),
-                                      True,
-                                      icon.get_bits_per_sample(),
-                                      width,
-                                      height)
-        result.fill(0xffffffff)
-
-        icon.composite(result,
-                       icon.get_width() * 3 / 2,
-                       icon.get_height() * 3 / 2,
-                       icon.get_width(),
-                       icon.get_height(),
-                       icon.get_width() * 3 / 2,
-                       icon.get_height() * 3 / 2,
-                       1, 1, GdkPixbuf.InterpType.HYPER, 0x33)
-
-        final_icon = _make_icon_frame(result)
-
-        return final_icon
-
-    @log
-    def get_default_icon(self, width, height, is_loading=False):
-        """Returns the requested symbolic icon
-
-        Returns a GdkPixbuf of the requested symbolic icon
-        in the given size.
-
-        :param int width: The width of the icon
-        :param int height: The height of the icon
-        :param bool is_loading: Whether the icon is the symbolic
-        loading icon or the music icon.
-
-        :return: A GdkPixbuf of the icon
-        """
-        if (width, height, is_loading) not in self.default_icon_cache.keys():
-            new_icon = self._make_default_icon(width, height, is_loading=False)
-            self.default_icon_cache[(width, height, is_loading)] = new_icon
-
-        return self.default_icon_cache[(width, height, is_loading)]
+        self.default_icon = DefaultIcon()
 
     @log
     def lookup(self, item, width, height, callback, itr, artist, album, first=True):
@@ -239,7 +254,7 @@ class AlbumArtCache(GObject.GObject):
             self.blacklist[artist].append(album)
 
         if pixbuf is None:
-            pixbuf = self.get_default_icon(width, height, False)
+            pixbuf = self.default_icon.get(width, height, DefaultIcon.Type.music)
 
         try:
             if path:
diff --git a/gnomemusic/notification.py b/gnomemusic/notification.py
index 39bd5a0..2f6f7d3 100644
--- a/gnomemusic/notification.py
+++ b/gnomemusic/notification.py
@@ -24,16 +24,12 @@
 
 from gi.repository import GLib, Grl, Notify
 
-from gnomemusic.albumartcache import AlbumArtCache
-
 from gettext import gettext as _
 
 from gnomemusic import log
 import logging
 logger = logging.getLogger(__name__)
 
-IMAGE_SIZE = 125
-
 
 class NotificationManager:
 
@@ -51,12 +47,6 @@ class NotificationManager:
         self._notification.set_hint('resident', GLib.Variant('b', True))
         self._notification.set_hint('desktop-entry', GLib.Variant('s', 'gnome-music'))
 
-        self._isPlaying = False
-
-        self._albumArtCache = AlbumArtCache.get_default()
-        self._noArtworkIcon = self._albumArtCache.get_default_icon(IMAGE_SIZE, IMAGE_SIZE)
-        self._noArtworkIconSerialized = None
-
     @log
     def _set_actions(self, playing):
         self._notification.clear_actions()
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 3f32972..cb30ea9 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -42,7 +42,7 @@ from gi.repository import Gtk, Gdk, GLib, Gio, GObject, Gst, GstAudio, GstPbutil
 from gettext import gettext as _, ngettext
 from random import randint
 from collections import deque
-from gnomemusic.albumartcache import AlbumArtCache
+from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon
 from gnomemusic.playlists import Playlists
 import gnomemusic.utils as utils
 playlists = Playlists.get_default()
@@ -111,8 +111,9 @@ class Player(GObject.GObject):
         self.currentTrackUri = None
         self._lastState = Gst.State.PAUSED
         self.cache = AlbumArtCache.get_default()
-        self._noArtworkIcon = self.cache.get_default_icon(ART_SIZE, ART_SIZE)
-        self._loadingIcon = self.cache.get_default_icon(ART_SIZE, ART_SIZE, True)
+        self._no_artwork_icon = DefaultIcon().get(ART_SIZE,
+                                                  ART_SIZE,
+                                                  DefaultIcon.Type.music)
         self._missingPluginMessages = []
 
         Gst.init(None)
@@ -607,7 +608,7 @@ class Player(GObject.GObject):
         except:
             self._currentAlbum = album
 
-        self.coverImg.set_from_pixbuf(self._noArtworkIcon)
+        self.coverImg.set_from_pixbuf(self._no_artwork_icon)
         self.cache.lookup(
             media, ART_SIZE, ART_SIZE, self._on_cache_lookup, None, artist, album)
 
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index 79b15a4..741a28d 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -48,7 +48,7 @@ import gnomemusic.widgets as Widgets
 from gnomemusic.player import DiscoveryStatus
 from gnomemusic.playlists import Playlists, StaticPlaylists
 import gnomemusic.utils as utils
-from gnomemusic.albumartcache import AlbumArtCache
+from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon
 from gnomemusic import log
 import logging
 logger = logging.getLogger(__name__)
@@ -131,7 +131,9 @@ class ViewContainer(Gtk.Stack):
         self.view.hide()
         self._items = []
         self.cache = AlbumArtCache.get_default()
-        self._loadingIcon = self.cache.get_default_icon(self._iconWidth, self._iconHeight, True)
+        self._loading_icon = DefaultIcon().get(self._iconWidth,
+                                               self._iconHeight,
+                                               DefaultIcon.Type.loading)
 
         self._init = False
         grilo.connect('ready', self._on_grilo_ready)
@@ -223,7 +225,7 @@ class ViewContainer(Gtk.Stack):
         self.model.set(_iter,
                        [0, 1, 2, 3, 4, 5, 7, 9],
                        [str(item.get_id()), '', title,
-                        artist, self._loadingIcon, item,
+                        artist, self._loading_icon, item,
                         0, False])
         self.cache.lookup(item, self._iconWidth, self._iconHeight, self._on_lookup_ready,
                           _iter, artist, title)
@@ -1341,8 +1343,12 @@ class Search(ViewContainer):
         self.iter_to_clean = None
         self._iconHeight = 48
         self._iconWidth = 48
-        self._loadingIcon = self.cache.get_default_icon(self._iconWidth, self._iconHeight, True)
-        self._noAlbumArtIcon = self.cache.get_default_icon(self._iconWidth, self._iconHeight, False)
+        self._loading_icon = DefaultIcon().get(self._iconWidth,
+                                               self._iconHeight,
+                                               DefaultIcon.Type.loading)
+        self._no_albumart_icon = DefaultIcon().get(self._iconWidth,
+                                                   self._iconHeight,
+                                                   DefaultIcon.Type.music)
         self._add_list_renderers()
         self.player = player
         self.head_iters = [None, None, None, None]
@@ -1509,7 +1515,7 @@ class Search(ViewContainer):
                 self.head_iters[group], -1,
                 [0, 2, 3, 4, 5, 9, 11],
                 [str(item.get_id()), title, artist,
-                 self._loadingIcon, item, 2, category])
+                 self._loading_icon, item, 2, category])
             self.cache.lookup(item, self._iconWidth, self._iconHeight, self._on_lookup_ready,
                               _iter, artist, title)
         elif category == 'song':
@@ -1517,14 +1523,14 @@ class Search(ViewContainer):
                 self.head_iters[group], -1,
                 [0, 2, 3, 4, 5, 9, 11],
                 [str(item.get_id()), title, artist,
-                 self._noAlbumArtIcon, item, 2 if source.get_id() != 'grl-tracker-source' else 
bool(item.get_lyrics()), category])
+                 self._no_albumart_icon, item, 2 if source.get_id() != 'grl-tracker-source' else 
bool(item.get_lyrics()), category])
         else:
             if not artist.casefold() in self._artists:
                 _iter = self.model.insert_with_values(
                     self.head_iters[group], -1,
                     [0, 2, 4, 5, 9, 11],
                     [str(item.get_id()), artist,
-                     self._loadingIcon, item, 2, category])
+                     self._loading_icon, item, 2, category])
                 self.cache.lookup(item, self._iconWidth, self._iconHeight, self._on_lookup_ready,
                                   _iter, artist, title)
                 self._artists[artist.casefold()] = {'iter': _iter, 'albums': []}
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index 7f9745b..172e25d 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -35,7 +35,7 @@ import logging
 from gi.repository import Gtk, Gdk, Gd, GLib, GObject, Pango, Gio, GdkPixbuf
 from gettext import gettext as _, ngettext
 
-from gnomemusic.albumartcache import AlbumArtCache
+from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon
 from gnomemusic.grilo import grilo
 from gnomemusic import log
 from gnomemusic.player import DiscoveryStatus
@@ -124,8 +124,8 @@ class AlbumWidget(Gtk.EventBox):
     """
 
     _duration = 0
-    _loading_icon = ALBUM_ART_CACHE.get_default_icon(256, 256, True)
-    _no_artwork_icon = ALBUM_ART_CACHE.get_default_icon(256, 256, False)
+    _loading_icon = DefaultIcon().get(256, 256, DefaultIcon.Type.loading)
+    _no_artwork_icon = DefaultIcon().get(256, 256, DefaultIcon.Type.music)
 
     def __repr__(self):
         return '<AlbumWidget>'
@@ -626,8 +626,8 @@ class ArtistAlbumWidget(Gtk.Box):
         'tracks-loaded': (GObject.SignalFlags.RUN_FIRST, None, ()),
     }
 
-    loadingIcon = AlbumArtCache.get_default().get_default_icon(128, 128, True)
-    noArtworkIcon = ALBUM_ART_CACHE.get_default_icon(128, 128, False)
+    _loading_icon = DefaultIcon().get(256, 256, DefaultIcon.Type.loading)
+    _no_artwork_icon = DefaultIcon().get(256, 256, DefaultIcon.Type.music)
 
     def __repr__(self):
         return '<ArtistAlbumWidget>'
@@ -650,7 +650,7 @@ class ArtistAlbumWidget(Gtk.Box):
         GLib.idle_add(self._update_album_art)
 
         self.cover = self.ui.get_object('cover')
-        self.cover.set_from_pixbuf(self.loadingIcon)
+        self.cover.set_from_pixbuf(self._loading_icon)
         self.songsGrid = self.ui.get_object('grid1')
         self.ui.get_object('title').set_label(album.get_title())
         if album.get_creation_date():
@@ -721,7 +721,7 @@ class ArtistAlbumWidget(Gtk.Box):
     @log
     def _get_album_cover(self, pixbuf, path, data=None):
         if not pixbuf:
-            pixbuf = self.noArtworkIcon
+            pixbuf = self._no_artwork_icon
         self.cover.set_from_pixbuf(pixbuf)
 
     @log


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