[gnome-music] player: Store current track as row reference



commit e1a311f7e4dad7da0b1448932e6c8f9db39961c7
Author: Arnel A. Borja <arnelborja src gnome org>
Date:   Sun Sep 1 22:49:48 2013 +0800

    player: Store current track as row reference
    
    Ensures that currentTrack iter won't get invalidated if a value changes
    in its row in the model.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=705855

 gnomemusic/player.py  |   32 ++++++++++++++++++++++----------
 gnomemusic/widgets.py |    3 ++-
 2 files changed, 24 insertions(+), 11 deletions(-)
---
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index c8834d6..fd512ef 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -167,7 +167,11 @@ class Player(GObject.GObject):
             self.progressScale.set_value(0)
             self.progressScale.set_sensitive(False)
             if self.playlist is not None:
-                self.currentTrack = self.playlist.get_iter_first()
+                currentTrack = self.playlist.get_path(self.playlist.get_iter_first())
+                if currentTrack:
+                    self.currentTrack = Gtk.TreeRowReference.new(self.playlist, currentTrack)
+                else:
+                    self.currentTrack = None
                 self.load(self.get_current_media())
         else:
             #Stop playback
@@ -182,7 +186,7 @@ class Player(GObject.GObject):
         self.play()
 
     def _get_next_track(self):
-        currentTrack = self.currentTrack
+        currentTrack = self.playlist.get_iter(self.currentTrack.get_path())
         nextTrack = None
         if self.repeat == RepeatType.SONG:
             nextTrack = currentTrack
@@ -198,7 +202,10 @@ class Player(GObject.GObject):
             for i in range(1, randint(1, rows)):
                 nextTrack = self.playlist.iter_next(nextTrack)
 
-        return nextTrack
+        if nextTrack:
+            return Gtk.TreeRowReference.new(self.playlist, self.playlist.get_path(nextTrack))
+        else:
+            return None
 
     def _get_iter_last(self):
         iter = self.playlist.get_iter_first()
@@ -211,7 +218,7 @@ class Player(GObject.GObject):
         return last
 
     def _get_previous_track(self):
-        currentTrack = self.currentTrack
+        currentTrack = self.playlist.get_iter(self.currentTrack.get_path())
         previousTrack = None
 
         if self.repeat == RepeatType.SONG:
@@ -228,7 +235,10 @@ class Player(GObject.GObject):
             for i in range(1, randint(1, rows)):
                 previousTrack = self.playlist.iter_next(previousTrack)
 
-        return previousTrack
+        if previousTrack:
+            return Gtk.TreeRowReference.new(self.playlist, self.playlist.get_path(previousTrack))
+        else:
+            return None
 
     def has_next(self):
         if not self.currentTrack:
@@ -236,7 +246,7 @@ class Player(GObject.GObject):
         elif self.repeat in [RepeatType.ALL, RepeatType.SONG, RepeatType.SHUFFLE]:
             return True
         else:
-            tmp = self.currentTrack.copy()
+            tmp = self.playlist.get_iter(self.currentTrack.get_path())
             return self.playlist.iter_next(tmp) is not None
 
     def has_previous(self):
@@ -245,7 +255,7 @@ class Player(GObject.GObject):
         elif self.repeat in [RepeatType.ALL, RepeatType.SONG, RepeatType.SHUFFLE]:
             return True
         else:
-            tmp = self.currentTrack.copy()
+            tmp = self.playlist.get_iter(self.currentTrack.get_path())
             return self.playlist.iter_previous(tmp) is not None
 
     def _get_playing(self):
@@ -306,7 +316,8 @@ class Player(GObject.GObject):
         if url != self.player.get_value('current-uri', 0):
             self.player.set_property('uri', url)
 
-        self.emit('playlist-item-changed', self.playlist, self.currentTrack)
+        currentTrack = self.playlist.get_iter(self.currentTrack.get_path())
+        self.emit('playlist-item-changed', self.playlist, currentTrack)
         self.emit('current-changed')
 
     def _on_cache_lookup(self, pixbuf, path, data=None):
@@ -384,7 +395,7 @@ class Player(GObject.GObject):
         self.playlist = model
         self.playlistType = type
         self.playlistId = id
-        self.currentTrack = iter
+        self.currentTrack = Gtk.TreeRowReference.new(model, model.get_path(iter))
         self.playlistField = field
         self.emit('current-changed')
 
@@ -571,7 +582,8 @@ class Player(GObject.GObject):
     def get_current_media(self):
         if not self.currentTrack:
             return None
-        return self.playlist.get_value(self.currentTrack, self.playlistField)
+        currentTrack = self.playlist.get_iter(self.currentTrack.get_path())
+        return self.playlist.get_value(currentTrack, self.playlistField)
 
 
 class SelectionToolbar():
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index d8ab888..6980d6e 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -221,8 +221,9 @@ class AlbumWidget(Gtk.EventBox):
         cached_playlist = self.player.running_playlist('Album', album)
         if cached_playlist:
             self.model = cached_playlist
+            currentTrack = self.player.playlist.get_iter(self.player.currentTrack.get_path())
             self.update_model(self.player, cached_playlist,
-                              self.player.currentTrack)
+                              currentTrack)
         else:
             self.duration = 0
             self._create_model()


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