[gnome-music/wip/jfelder/musicbrainz-tageditordialog: 1/3] utils: Handle more tags



commit 4d1c88327497e3f78cfa98911e02b0942363ca23
Author: Jean Felder <jfelder src gnome org>
Date:   Tue Aug 21 00:24:40 2018 +0200

    utils: Handle more tags
    
    Add utils functions to retrieve album_disc_number, composer_name,
    genre and track number.

 gnomemusic/mpris.py                     |  7 +++--
 gnomemusic/utils.py                     | 50 +++++++++++++++++++++++++++++++++
 gnomemusic/widgets/albumwidget.py       |  4 +--
 gnomemusic/widgets/artistalbumwidget.py |  2 +-
 gnomemusic/widgets/songwidget.py        |  6 ++--
 5 files changed, 60 insertions(+), 9 deletions(-)
---
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index af60ce57..d91c5537 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -301,7 +301,6 @@ class MediaPlayer2Service(Server):
             'mpris:trackid': GLib.Variant('o', song_dbus_path),
             'xesam:url': GLib.Variant('s', media.get_url()),
             'mpris:length': GLib.Variant('x', length),
-            'xesam:trackNumber': GLib.Variant('i', media.get_track_number()),
             'xesam:useCount': GLib.Variant('i', media.get_play_count()),
             'xesam:userRating': GLib.Variant('d', user_rating),
             'xesam:title': GLib.Variant('s', utils.get_media_title(media)),
@@ -310,7 +309,7 @@ class MediaPlayer2Service(Server):
             'xesam:albumArtist': GLib.Variant('as', [artist])
         }
 
-        genre = media.get_genre()
+        genre = utils.get_media_genre(media)
         if genre is not None:
             metadata['xesam:genre'] = GLib.Variant('as', [genre])
 
@@ -319,6 +318,10 @@ class MediaPlayer2Service(Server):
             last_played_str = last_played.format("%FT%T%:z")
             metadata['xesam:lastUsed'] = GLib.Variant('s', last_played_str)
 
+        track_nr = utils.get_media_track_nr(media)
+        if track_nr:
+            metadata['xesam:trackNumber'] = GLib.Variant('i', int(track_nr))
+
         # If the media has already been part of an MPRIS playlist, its
         # thumbnail is already set. Otherwise, try to look for it in the
         # cache directory and set the media thumbnail for a future use.
diff --git a/gnomemusic/utils.py b/gnomemusic/utils.py
index bae746e7..56b7e350 100644
--- a/gnomemusic/utils.py
+++ b/gnomemusic/utils.py
@@ -58,6 +58,19 @@ def get_album_title(item):
     return album
 
 
+def get_album_disc_nr(item):
+    """Returns the album song number associated with the media item
+
+    :param Grl.Media item: song
+    :return: The album title
+    :rtype: string
+    """
+    track_number = item.get_album_disc_number()
+    if track_number == 0:
+        track_number = ""
+    return str(track_number)
+
+
 def get_artist_name(item):
     """Returns the preferred artist for a media item.
 
@@ -77,6 +90,43 @@ def get_artist_name(item):
             or _("Unknown Artist"))
 
 
+def get_composer_name(item):
+    """Returns the composer for a media item.
+
+    :param Grl.Media item: song
+    :return: The artist name
+    :rtype: string
+    """
+
+    return (item.get_composer()
+            or None)
+
+
+def get_media_genre(item):
+    """Returns the title of the media item.
+
+    :param item: A Grilo Media object
+    :return: The title
+    :rtype:
+    """
+
+    return (item.get_genre()
+            or None)
+
+
+def get_media_track_nr(item):
+    """Returns the title of the media item.
+
+    :param item: A Grilo Media object
+    :return: The title
+    :rtype:
+    """
+    track_number = item.get_track_number()
+    if track_number == 0:
+        return ""
+    return str(track_number)
+
+
 def get_media_title(item):
     """Returns the title of the media item.
 
diff --git a/gnomemusic/widgets/albumwidget.py b/gnomemusic/widgets/albumwidget.py
index 1eea57f0..38c1f645 100644
--- a/gnomemusic/widgets/albumwidget.py
+++ b/gnomemusic/widgets/albumwidget.py
@@ -142,7 +142,7 @@ class AlbumWidget(Gtk.EventBox):
 
     @log
     def _set_composer_label(self, album):
-        composer = album.get_composer()
+        composer = utils.get_composer_name(album)
         show = False
 
         if composer:
@@ -210,7 +210,7 @@ class AlbumWidget(Gtk.EventBox):
         if remaining == 0:
             discs = {}
             for song in self._songs:
-                disc_nr = song.get_album_disc_number()
+                disc_nr = utils.get_album_disc_nr(song)
                 if disc_nr not in discs.keys():
                     discs[disc_nr] = [song]
                 else:
diff --git a/gnomemusic/widgets/artistalbumwidget.py b/gnomemusic/widgets/artistalbumwidget.py
index 8d8fda88..621aef3d 100644
--- a/gnomemusic/widgets/artistalbumwidget.py
+++ b/gnomemusic/widgets/artistalbumwidget.py
@@ -117,7 +117,7 @@ class ArtistAlbumWidget(Gtk.Box):
 
         discs = {}
         for song in self._songs:
-            disc_nr = song.get_album_disc_number()
+            disc_nr = utils.get_album_disc_nr(song)
             if disc_nr not in discs.keys():
                 discs[disc_nr] = [song]
             else:
diff --git a/gnomemusic/widgets/songwidget.py b/gnomemusic/widgets/songwidget.py
index 78c87181..851d137a 100644
--- a/gnomemusic/widgets/songwidget.py
+++ b/gnomemusic/widgets/songwidget.py
@@ -88,10 +88,8 @@ class SongWidget(Gtk.EventBox):
         self._selection_mode = False
         self._state = SongWidget.State.UNPLAYED
 
-        song_number = media.get_track_number()
-        if song_number == 0:
-            song_number = ""
-        self._number_label.set_text(str(song_number))
+        song_number = utils.get_media_track_nr(media)
+        self._number_label.props.label = song_number
 
         title = utils.get_media_title(media)
         self._title_label.set_max_width_chars(50)


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