[gnome-music/wip/jfelder/mpris-cleanup: 6/29] mpris: Make the trackid truly unique



commit 2b0456abc04181c09edb490c6d366563c1854574
Author: Jean Felder <jfelder src gnome org>
Date:   Wed Sep 5 01:22:52 2018 +0200

    mpris: Make the trackid truly unique
    
    Use the song position as a suffix to trackid to handle duplicate
    songs.
    
    Related: #100

 gnomemusic/mpris.py  | 31 +++++++++++++++++++------------
 gnomemusic/player.py | 16 +++++++++++++---
 2 files changed, 32 insertions(+), 15 deletions(-)
---
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index 715124f6..fcff5922 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -295,8 +295,8 @@ class MPRIS(DBusInterface):
             return 'Playlist'
 
     @log
-    def _get_metadata(self, media=None):
-        song_dbus_path = self._get_song_dbus_path(media)
+    def _get_metadata(self, media=None, index=None):
+        song_dbus_path = self._get_song_dbus_path(media, index)
         if not self.player.props.current_song:
             return {
                 'mpris:trackid': GLib.Variant('o', song_dbus_path)
@@ -337,20 +337,29 @@ class MPRIS(DBusInterface):
         return metadata
 
     @log
-    def _get_song_dbus_path(self, media):
+    def _get_song_dbus_path(self, media=None, index=None):
         """Convert a Grilo media to a D-Bus path
 
         The hex encoding is used to remove any possible invalid character.
+        Use player index to make the path truly unique in case the same song
+        is present multiple times in a playlist.
+        If media is None, it means that the current song path is requested.
 
         :param Grl.Media media: The media object
+        :param int index: The media position in the current playlist
         :return: a D-Bus id to uniquely identify the song
         :rtype: str
         """
-        if not media:
+        if not self.player.props.current_song:
             return "/org/mpris/MediaPlayer2/TrackList/NoTrack"
 
+        if not media:
+            media = self.player.props.current_song
+            index = self.player.get_current_index()
+
         id_hex = media.get_id().encode('ascii').hex()
-        path = "/org/gnome/GnomeMusic/TrackList/{}".format(id_hex)
+        path = "/org/gnome/GnomeMusic/TrackList/{}_{}".format(
+            id_hex, index)
         return path
 
     @log
@@ -358,9 +367,9 @@ class MPRIS(DBusInterface):
         previous_path_list = self._path_list
         self._path_list = []
         self._metadata_list = []
-        for song in self.player.get_mpris_playlist():
-            path = self._get_song_dbus_path(song)
-            metadata = self._get_metadata(song)
+        for index, song in self.player.get_mpris_playlist():
+            path = self._get_song_dbus_path(song, index)
+            metadata = self._get_metadata(song, index)
             self._path_list.append(path)
             self._metadata_list.append(metadata)
 
@@ -368,8 +377,7 @@ class MPRIS(DBusInterface):
         if (not previous_path_list
                 or previous_path_list[0] != self._path_list[0]
                 or previous_path_list[-1] != self._path_list[-1]):
-            current_song_path = self._get_song_dbus_path(
-                self.player.props.current_song)
+            current_song_path = self._get_song_dbus_path()
             self.TrackListReplaced(self._path_list, current_song_path)
             self.PropertiesChanged(
                 MPRIS.MEDIA_PLAYER2_TRACKLIST_IFACE,
@@ -620,8 +628,7 @@ class MPRIS(DBusInterface):
         pass
 
     def GoTo(self, path):
-        current_song_path = self._get_song_dbus_path(
-            self.player.props.current_song)
+        current_song_path = self._get_song_dbus_path()
         current_song_index = self._path_list.index(current_song_path)
         goto_index = self._path_list.index(path)
         self.player.play(goto_index - current_song_index)
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index a2e7c89c..5ada066b 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -502,7 +502,7 @@ class PlayerPlaylist(GObject.GObject):
         This method is used by mpris to expose a TrackList.
 
         :returns: current playlist
-        :rtype: list of Grl.Media
+        :rtype: list of index and Grl.Media
         """
         if not self.props.current_song:
             return []
@@ -540,7 +540,8 @@ class PlayerPlaylist(GObject.GObject):
                 range(nb_songs - offset_inf, nb_songs), indexes,
                 range(offset_sup))
 
-        songs = [self._songs[index][PlayerField.SONG] for index in indexes]
+        songs = [[index, self._songs[index][PlayerField.SONG]]
+                 for index in indexes]
         return songs
 
 
@@ -861,6 +862,15 @@ class Player(GObject.GObject):
         """
         return self._playlist.props.playlist_id
 
+    @log
+    def get_current_index(self):
+        """Get current song index.
+
+        :returns: position of the current song int the playlist.
+        :rtype: int
+        """
+        return self._playlist.get_current_index()
+
     @log
     def get_position(self):
         """Get player position.
@@ -906,6 +916,6 @@ class Player(GObject.GObject):
         This method is used by mpris to expose a TrackList.
 
         :returns: current playlist
-        :rtype: list of Grl.Media
+        :rtype: list of index and Grl.Media
         """
         return self._playlist.get_mpris_playlist()


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