[gnome-music/wip/mschraal/coverart: 2/5] albumartcache: Move get_media_title to utils



commit b3cd183ae5bcd4f5bd1f37b1ac96c4f3aed7051e
Author: Marinus Schraal <mschraal src gnome org>
Date:   Sun Sep 25 14:55:31 2016 +0200

    albumartcache: Move get_media_title to utils
    
    Makes little sense to have it in AlbumArtCache class. Also remove all
    the corner case handling that has no obvious use in the current usage
    pattern.

 gnomemusic/albumartcache.py |   29 ++---------------------------
 gnomemusic/mpris.py         |    7 +++----
 gnomemusic/player.py        |    2 +-
 gnomemusic/utils.py         |   12 ++++++++++++
 gnomemusic/view.py          |   20 ++++++++++----------
 gnomemusic/widgets.py       |   28 +++++++++++++++-------------
 6 files changed, 43 insertions(+), 55 deletions(-)
---
diff --git a/gnomemusic/albumartcache.py b/gnomemusic/albumartcache.py
index 39d7464..b93f885 100644
--- a/gnomemusic/albumartcache.py
+++ b/gnomemusic/albumartcache.py
@@ -158,31 +158,6 @@ class AlbumArtCache(GObject.GObject):
     def __repr__(self):
         return '<AlbumArtCache>'
 
-    @staticmethod
-    def get_media_title(media, escaped=False):
-        title = media.get_title()
-        if title:
-            if escaped:
-                return GLib.markup_escape_text(title)
-            else:
-                return title
-        uri = media.get_url()
-        if uri is None:
-            return _("Untitled")
-
-        uri_file = Gio.File.new_for_path(uri)
-        basename = uri_file.get_basename()
-
-        try:
-            title = GLib.uri_unescape_string(basename, '')
-        except:
-            title = _("Untitled")
-            pass
-        if escaped:
-            return GLib.markup_escape_text(title)
-
-        return title
-
     @log
     def __init__(self):
         GObject.GObject.__init__(self)
@@ -211,7 +186,7 @@ class AlbumArtCache(GObject.GObject):
     def _lookup_local(self, item, callback, itr, width, height):
         """Checks if there is already a local art file, if not calls
         the remote lookup function"""
-        album = self.get_media_title(item)
+        album = utils.get_media_title(item)
         artist = utils.get_artist_name(item)
 
         def stream_open(thumb_file, result, arguments):
@@ -279,7 +254,7 @@ class AlbumArtCache(GObject.GObject):
         Lookup remote art through Grilo and if found copy locally. Call
         _lookup_local to finish retrieving suitable art.
         """
-        album = self.get_media_title(item)
+        album = utils.get_media_title(item)
         artist = utils.get_artist_name(item)
 
         @log
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index b754ca4..fbd59fe 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -24,7 +24,6 @@
 # delete this exception statement from your version.
 
 from gnomemusic.player import PlaybackStatus, RepeatType
-from gnomemusic.albumartcache import AlbumArtCache
 from gnomemusic.grilo import grilo
 from gnomemusic.playlists import Playlists
 import gnomemusic.utils as utils
@@ -298,7 +297,7 @@ class MediaPlayer2Service(Server):
             pass
 
         try:
-            title = AlbumArtCache.get_media_title(media)
+            title = utils.get_media_title(media)
             assert title is not None
             metadata['xesam:title'] = GLib.Variant('s', title)
         except:
@@ -402,7 +401,7 @@ class MediaPlayer2Service(Server):
     def _get_active_playlist(self):
         playlist = self._get_playlist_from_id(self.player.playlistId) \
             if self.player.playlistType == 'Playlist' else None
-        playlistName = AlbumArtCache.get_media_title(playlist) \
+        playlistName = utils.get_media_title(playlist) \
             if playlist else ''
         return (playlist is not None,
                 (self._get_playlist_path(playlist), playlistName, ''))
@@ -644,7 +643,7 @@ class MediaPlayer2Service(Server):
         if order != 'Alphabetical':
             return []
         playlists = [(self._get_playlist_path(playlist),
-                      AlbumArtCache.get_media_title(playlist) or '', '')
+                      utils.get_media_title(playlist) or '', '')
                      for playlist in self.playlists]
         return playlists[index:index + max_count] if not reverse \
             else playlists[index + max_count - 1:index - 1 if index - 1 >= 0 else None:-1]
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index fff3787..d260a86 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -612,7 +612,7 @@ class Player(GObject.GObject):
         self.cache.lookup(
             media, ART_SIZE, ART_SIZE, self._on_cache_lookup, None)
 
-        self._currentTitle = AlbumArtCache.get_media_title(media)
+        self._currentTitle = utils.get_media_title(media)
         self.titleLabel.set_label(self._currentTitle)
 
         self._currentTimestamp = int(time.time())
diff --git a/gnomemusic/utils.py b/gnomemusic/utils.py
index 3fd8322..0faa42c 100644
--- a/gnomemusic/utils.py
+++ b/gnomemusic/utils.py
@@ -42,3 +42,15 @@ def get_artist_name(item):
     return (item.get_album_artist()
             or item.get_artist()
             or _("Unknown Artist"))
+
+
+def get_media_title(item):
+    """Returns the title of the media item.
+
+    :param item: A Grilo Media object
+    :return: The title
+    :rtype:
+    """
+
+    return (item.get_title()
+            or _("Untitled"))
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index e511d0c..25e1c07 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -241,7 +241,7 @@ class ViewContainer(Gtk.Stack):
 
         self._offset += 1
         artist = utils.get_artist_name(item)
-        title = AlbumArtCache.get_media_title(item)
+        title = utils.get_media_title(item)
 
         _iter = self.model.append(None)
         self.model.set(_iter,
@@ -427,7 +427,7 @@ class Albums(ViewContainer):
             child.check.set_active(not child.check.get_active())
             return
 
-        title = AlbumArtCache.get_media_title(item)
+        title = utils.get_media_title(item)
         self._escaped_title = title
         self._artist = utils.get_artist_name(item)
 
@@ -482,7 +482,7 @@ class Albums(ViewContainer):
 
     def _create_album_item(self, item):
         artist = utils.get_artist_name(item)
-        title = AlbumArtCache.get_media_title(item)
+        title = utils.get_media_title(item)
 
         builder = Gtk.Builder.new_from_resource('/org/gnome/Music/AlbumCover.ui')
 
@@ -655,14 +655,14 @@ class Songs(ViewContainer):
                 self.view.show()
             return
         self._offset += 1
-        item.set_title(AlbumArtCache.get_media_title(item))
+        item.set_title(utils.get_media_title(item))
         artist = utils.get_artist_name(item)
         if item.get_url() is None:
             return
         self.model.insert_with_valuesv(
             -1,
             [2, 3, 5, 9],
-            [AlbumArtCache.get_media_title(item),
+            [utils.get_media_title(item),
              artist, item, bool(item.get_lyrics())])
         # TODO: change "bool(item.get_lyrics())" --> item.get_favourite() once query works properly
 
@@ -1221,7 +1221,7 @@ class Playlist(ViewContainer):
         _iter = self.playlists_model.insert_with_valuesv(
             index,
             [2, 5],
-            [AlbumArtCache.get_media_title(item), item])
+            [utils.get_media_title(item), item])
         if self.playlists_model.iter_n_children(None) == 1:
             _iter = self.playlists_model.get_iter_first()
             selection = self.playlists_sidebar.get_generic_view().get_selection()
@@ -1342,7 +1342,7 @@ class Playlist(ViewContainer):
             self.emit('playlist-songs-loaded')
             return
         self._offset += 1
-        title = AlbumArtCache.get_media_title(item)
+        title = utils.get_media_title(item)
         item.set_title(title)
         artist = item.get_artist() or _("Unknown Artist")
         model.insert_with_valuesv(
@@ -1592,8 +1592,8 @@ class Search(ViewContainer):
             self._albumWidget.update(artist, title, item,
                                      self.header_bar, self.selection_toolbar)
             self.header_bar.set_state(ToolbarState.SEARCH_VIEW)
-            escaped_title = AlbumArtCache.get_media_title(item)
-            self.header_bar.header_bar.set_title(escaped_title)
+            title = utils.get_media_title(item)
+            self.header_bar.header_bar.set_title(title)
             self.header_bar.header_bar.sub_title = artist
             self.set_visible_child(self._albumWidget)
             self.header_bar.searchbar.show_bar(False)
@@ -1685,7 +1685,7 @@ class Search(ViewContainer):
             return
 
         self._offset += 1
-        title = AlbumArtCache.get_media_title(item)
+        title = utils.get_media_title(item)
         item.set_title(title)
         artist = utils.get_artist_name(item)
 
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index 5700892..cd9d4a4 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -190,7 +190,7 @@ class AlbumWidget(Gtk.EventBox):
             if (self._iter_to_clean
                     and self._player.playlistId == self._album):
                 item = self.model[self._iter_to_clean][5]
-                title = AlbumArtCache.get_media_title(item)
+                title = utils.get_media_title(item)
                 self.model[self._iter_to_clean][0] = title
                 # Hide now playing icon
                 self.model[self._iter_to_clean][6] = False
@@ -365,9 +365,10 @@ class AlbumWidget(Gtk.EventBox):
         if track:
             self._duration = self._duration + track.get_duration()
             _iter = self.model.append()
-            escapedTitle = AlbumArtCache.get_media_title(track, True)
+            title = utils.get_media_title(track)
+            escaped_title = GLib.markup_escape_text(title)
             self.model[_iter][0, 1, 2, 3, 4, 5, 9] = [
-                escapedTitle,
+                escaped_title,
                 self._player.seconds_to_string(track.get_duration()),
                 '',
                 '',
@@ -413,7 +414,7 @@ class AlbumWidget(Gtk.EventBox):
         while _iter:
             song = playlist[_iter][5]
             self._duration += song.get_duration()
-            escaped_title = AlbumArtCache.get_media_title(song, True)
+            escaped_title = GLib.markup_escape_text(utils.get_media_title(song))
             if (song == current_song):
                 title = '<b>%s</b>' % escaped_title
                 song_passed = True
@@ -530,18 +531,19 @@ class ArtistAlbums(Gtk.Box):
                 itr = playlist.iter_next(itr)
                 continue
 
-            escapedTitle = AlbumArtCache.get_media_title(song, True)
+            escaped_title = GLib.markup_escape_text(utils.get_media_title(song))
             if (song == currentSong):
                 song_widget.now_playing_sign.show()
-                song_widget.title.set_markup('<b>%s</b>' % escapedTitle)
+                song_widget.title.set_markup('<b>%s</b>' % escaped_title)
                 song_passed = True
             elif (song_passed):
                 song_widget.now_playing_sign.hide()
-                song_widget.title.set_markup('<span>%s</span>' % escapedTitle)
+                song_widget.title.set_markup('<span>%s</span>' % escaped_title)
             else:
                 song_widget.now_playing_sign.hide()
-                song_widget.title\
-                    .set_markup('<span color=\'grey\'>%s</span>' % escapedTitle)
+                song_widget.title.set_markup(
+                    '<span color=\'grey\'>%s</span>' % escaped_title
+                )
             itr = playlist.iter_next(itr)
         return False
 
@@ -551,10 +553,10 @@ class ArtistAlbums(Gtk.Box):
         while itr:
             song = self.model.get_value(itr, 5)
             song_widget = song.song_widget
-            escapedTitle = AlbumArtCache.get_media_title(song, True)
+            escaped_title = GLib.markup_escape_text(utils.get_media_title(song))
             if song_widget.can_be_played:
                 song_widget.now_playing_sign.hide()
-            song_widget.title.set_markup('<span>%s</span>' % escapedTitle)
+            song_widget.title.set_markup('<span>%s</span>' % escaped_title)
             itr = self.model.iter_next(itr)
         return False
 
@@ -678,7 +680,7 @@ class ArtistAlbumWidget(Gtk.Box):
                 ui.get_object('num')\
                     .set_markup('<span color=\'grey\'>%d</span>'
                                 % len(self.songs))
-                title = AlbumArtCache.get_media_title(track)
+                title = utils.get_media_title(track)
                 ui.get_object('title').set_text(title)
                 ui.get_object('title').set_alignment(0.0, 0.5)
                 ui.get_object('title').set_max_width_chars(MAX_TITLE_WIDTH)
@@ -868,7 +870,7 @@ class PlaylistDialog():
         self.model.set(
             new_iter,
             [0, 1, 2],
-            [AlbumArtCache.get_media_title(item), False, item]
+            [utils.get_media_title(item), False, item]
         )
         return new_iter
 


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