[gnome-music] player: Use a deque for the shuffle history



commit 067a964df10fa0e335a4e3f79cfcaab3da25ca1a
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Mar 4 21:41:36 2015 +0100

    player: Use a deque for the shuffle history
    
    deque seems better for the task, as we actually want to pop elements as a
    FIFO so we forget about the oldest song and not the most recent in order
    to add the most recent one. This is something managed automatically by
    deque when setting a maxlen.
    
    Also, commit 6f1cb8d448d8 changed player behavior so _get_next_track() is
    called early after currentTrack changes, this means that the current track
    is added very early to the shuffle list, so was always mistakenly popped
    as the "previous" song. Make this behave nicely and either return the
    actual previous song, or "rewind" if we played more than 10s of the song.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=745647

 gnomemusic/player.py |   19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)
---
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index f18393f..2409478 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -37,7 +37,7 @@ GIRepository.Repository.prepend_search_path('libgd')
 from gi.repository import Gtk, Gdk, GLib, Gio, GObject, Gst, GstAudio, GstPbutils
 from gettext import gettext as _
 from random import randint
-from queue import LifoQueue
+from collections import deque
 from gnomemusic.albumArtCache import AlbumArtCache
 from gnomemusic.playlists import Playlists
 playlists = Playlists.get_default()
@@ -71,7 +71,7 @@ class DiscoveryStatus:
 class Player(GObject.GObject):
     nextTrack = None
     timeout = None
-    shuffleHistory = LifoQueue(maxsize=10)
+    shuffleHistory = deque(maxlen=10)
 
     __gsignals__ = {
         'playing-changed': (GObject.SIGNAL_RUN_FIRST, None, ()),
@@ -305,9 +305,7 @@ class Player(GObject.GObject):
         elif self.repeat == RepeatType.SHUFFLE:
             if currentTrack:
                 nextTrack = self._get_random_iter(currentTrack)
-                if self.shuffleHistory.full():
-                    self.shuffleHistory.get_nowait()
-                self.shuffleHistory.put_nowait(currentTrack)
+                self.shuffleHistory.append(currentTrack)
 
         if nextTrack:
             return Gtk.TreeRowReference.new(self.playlist, self.playlist.get_path(nextTrack))
@@ -349,8 +347,15 @@ class Player(GObject.GObject):
                 previousTrack = self.playlist.iter_previous(currentTrack)
         elif self.repeat == RepeatType.SHUFFLE:
             if currentTrack:
-                if not self.shuffleHistory.empty():
-                    previousTrack = self.shuffleHistory.get_nowait()
+                if self.played_seconds < 10 and len(self.shuffleHistory) > 0:
+                    previousTrack = self.shuffleHistory.pop()
+
+                    # Discard the current song, which is already queued
+                    if self.playlist.get_path(previousTrack) == self.playlist.get_path(currentTrack):
+                        previousTrack = None
+
+                if previousTrack is None and len(self.shuffleHistory) > 0:
+                    previousTrack = self.shuffleHistory.pop()
                 else:
                     previousTrack = self._get_random_iter(currentTrack)
 


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