[gnome-music] Always check iter for validity before using it



commit d62b6622f8cf0299ff14deaa18bce841698b5d76
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Thu Mar 5 13:09:05 2015 +0100

    Always check iter for validity before using it

 gnomemusic/player.py |   25 +++++++++++++++----------
 gnomemusic/view.py   |    2 +-
 2 files changed, 16 insertions(+), 11 deletions(-)
---
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index 2409478..314481f 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -217,8 +217,9 @@ class Player(GObject.GObject):
     def _onBusError(self, bus, message):
         media = self.get_current_media()
         if media is not None:
-            currentTrack = self.playlist.get_iter(self.currentTrack.get_path())
-            self.playlist.set_value(currentTrack, self.discovery_status_field, DiscoveryStatus.FAILED)
+            if self.currentTrack and self.currentTrack.valid():
+                currentTrack = self.playlist.get_iter(self.currentTrack.get_path())
+                self.playlist.set_value(currentTrack, self.discovery_status_field, DiscoveryStatus.FAILED)
             uri = media.get_url()
         else:
             uri = 'none'
@@ -261,8 +262,9 @@ class Player(GObject.GObject):
     @log
     def _on_glib_idle(self):
         self.currentTrack = self.nextTrack
-        self.currentTrackUri = self.playlist.get_value(
-            self.playlist.get_iter(self.currentTrack.get_path()), 5).get_url()
+        if self.currentTrack and self.currentTrack.valid():
+            self.currentTrackUri = self.playlist.get_value(
+                self.playlist.get_iter(self.currentTrack.get_path()), 5).get_url()
         self.play()
 
     @log
@@ -271,6 +273,8 @@ class Player(GObject.GObject):
 
     @log
     def _get_random_iter(self, currentTrack):
+        if not currentTrack or not self.playlist.iter_is_valid(currentTrack):
+            return None
         currentPath = int(self.playlist.get_path(currentTrack).to_string())
         rows = self.playlist.iter_n_children(None)
         if rows == 1:
@@ -480,9 +484,10 @@ class Player(GObject.GObject):
         if url != self.player.get_value('current-uri', 0):
             self.player.set_property('uri', url)
 
-        currentTrack = self.playlist.get_iter(self.currentTrack.get_path())
-        self.emit('playlist-item-changed', self.playlist, currentTrack)
-        self.emit('current-changed')
+        if self.currentTrack and self.currentTrack.valid():
+            currentTrack = self.playlist.get_iter(self.currentTrack.get_path())
+            self.emit('playlist-item-changed', self.playlist, currentTrack)
+            self.emit('current-changed')
 
         self._validate_next_track()
 
@@ -576,7 +581,7 @@ class Player(GObject.GObject):
 
         self.stop()
         self.currentTrack = self.nextTrack
-        if self.currentTrack:
+        if self.currentTrack and self.currentTrack.valid():
             self.currentTrackUri = self.playlist.get_value(
                 self.playlist.get_iter(self.currentTrack.get_path()), 5).get_url()
             self.play()
@@ -598,7 +603,7 @@ class Player(GObject.GObject):
         self.stop()
 
         self.currentTrack = self._get_previous_track()
-        if self.currentTrack:
+        if self.currentTrack and self.currentTrack.valid():
             self.currentTrackUri = self.playlist.get_value(
                 self.playlist.get_iter(self.currentTrack.get_path()), 5).get_url()
             self.play()
@@ -625,7 +630,7 @@ class Player(GObject.GObject):
         self.playlistType = type
         self.playlistId = id
         self.currentTrack = Gtk.TreeRowReference.new(model, model.get_path(iter))
-        if self.currentTrack:
+        if self.currentTrack and self.currentTrack.valid():
             self.currentTrackUri = self.playlist.get_value(
                 self.playlist.get_iter(self.currentTrack.get_path()), 5).get_url()
         self.playlistField = field
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index 5750975..9d188b3 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -1240,7 +1240,7 @@ class Playlist(ViewContainer):
                 # Is the removed track now being played?
                 if self.current_playlist and \
                    playlist.get_id() == self.current_playlist.get_id():
-                    if self.player.currentTrack is not None:
+                    if self.player.currentTrack is not None and self.player.currentTrack.valid():
                         currentTrackpath = self.player.currentTrack.get_path().to_string()
                         if row.path is not None and row.path.to_string() == currentTrackpath:
                             update_playing_track = True


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