[gnome-music/wip/jfelder/mpris-limit-songs: 3/3] mpris: limit the amount of playlist items



commit fa3ad7e71b29e8fd26aad8bf64644609773146e0
Author: Jean Felder <jfelder src gnome org>
Date:   Fri Sep 14 00:16:17 2018 +0200

    mpris: limit the amount of playlist items

 gnomemusic/mpris.py  |  9 ++++++---
 gnomemusic/player.py | 21 +++++++++++++++------
 2 files changed, 21 insertions(+), 9 deletions(-)
---
diff --git a/gnomemusic/mpris.py b/gnomemusic/mpris.py
index 5a33d846..f56984d2 100644
--- a/gnomemusic/mpris.py
+++ b/gnomemusic/mpris.py
@@ -208,6 +208,8 @@ class MediaPlayer2Service(Server):
     MEDIA_PLAYER2_TRACKLIST_IFACE = 'org.mpris.MediaPlayer2.TrackList'
     MEDIA_PLAYER2_PLAYLISTS_IFACE = 'org.mpris.MediaPlayer2.Playlists'
 
+    _nb_songs_max = 10
+
     def __repr__(self):
         return '<MediaPlayer2Service>'
 
@@ -350,7 +352,7 @@ class MediaPlayer2Service(Server):
 
     @log
     def _get_media_from_id(self, track_id):
-        for media in self.player.get_songs():
+        for media in self.player.get_songs(self._nb_songs_max):
             if track_id == self._get_media_id(media):
                 return media
         return None
@@ -359,7 +361,7 @@ class MediaPlayer2Service(Server):
     def _get_track_list(self):
         if self.player.props.playing:
             return [self._get_media_id(song)
-                    for song in self.player.get_songs()]
+                    for song in self.player.get_songs(self._nb_songs_max)]
         else:
             return []
 
@@ -580,7 +582,8 @@ class MediaPlayer2Service(Server):
         pass
 
     def GoTo(self, track_id):
-        for index, song in enumerate(self.player.get_songs()):
+        songs = self.player.get_songs(self._nb_songs_max)
+        for index, song in enumerate(songs):
             if track_id == self._get_media_id(song):
                 self.player_play(index)
                 return
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index a4903458..0e6603ea 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -473,13 +473,20 @@ class PlayerPlaylist(GObject.GObject):
         return self._type
 
     @log
-    def get_songs(self):
-        """Get the current playlist.
+    def get_songs(self, nb_songs, start_at_current_index):
+        """Get some songs from the current playlist.
 
+        :param int nb_songs: number of songs to get
+        :param bool start_at_current_index: start at the current song
         :returns: current playlist
         :rtype: list of Grl.Media
         """
-        songs = [elt[PlayerField.SONG] for elt in self._songs]
+        first_index = 0
+        if start_at_current_index:
+            first_index = self._current_index
+        max_index = min(first_index + nb_songs, len(self._songs))
+        songs = [elt[PlayerField.SONG]
+                 for elt in self._songs[first_index:max_index]]
         return songs
 
 
@@ -857,10 +864,12 @@ class Player(GObject.GObject):
         return self._playlist.props.current_song is not None
 
     @log
-    def get_songs(self):
-        """Get the current playlist.
+    def get_songs(self, nb_songs, start_at_current_index=True):
+        """Get some songs from the current playlist.
 
+        :param int nb_songs: number of songs to get
+        :param bool start_at_current_index: start at the current song
         :returns: current playlist
         :rtype: list of Grl.Media
         """
-        return self._playlist.get_songs()
+        return self._playlist.get_songs(nb_songs, start_at_current_index)


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