Rename _current_song_id to _current_song_pos. Fix crashes when sending 'play' or 'stop' with empty playlist. Support 'play' when no song is selected but playlist is added.

This commit is contained in:
Stein Magnus Jodal 2009-12-26 14:47:31 +01:00
parent 1be0884526
commit 886376b5f8

View File

@ -65,24 +65,24 @@ class SpotifyBackend(BaseBackend):
@property @property
def _current_track(self): def _current_track(self):
return self._current_playlist[self._current_song_id] if self._current_song_pos is not None:
return self._current_playlist[self._current_song_pos]
@property @property
def _current_song_id(self): def _current_song_pos(self):
if not hasattr(self, '_x_current_song_id'): if not hasattr(self, '_x_current_song_pos'):
self._x_current_song_id = 0 self._x_current_song_pos = None
return self._x_current_song_id if self._current_playlist is None or len(self._current_playlist) == 0:
self._x_current_song_pos = None
elif self._x_current_song_pos < 0:
self._x_current_song_pos = 0
elif self._x_current_song_pos >= len(self._current_playlist):
self._x_current_song_pos = len(self._current_playlist) - 1
return self._x_current_song_pos
@_current_song_id.setter @_current_song_pos.setter
def _current_song_id(self, songid): def _current_song_pos(self, songid):
if (self._current_playlist is None self._x_current_song_pos = songid
or len(self._current_playlist) == 0
or songid < 0):
self._x_current_song_id = 0
elif songid >= len(self._current_playlist):
self._x_current_song_id = len(self._current_playlist) - 1
else:
self._x_current_song_id = songid
def _format_playlist(self, playlist, pos_range=None): def _format_playlist(self, playlist, pos_range=None):
if pos_range is None: if pos_range is None:
@ -113,7 +113,7 @@ class SpotifyBackend(BaseBackend):
# Control methods # Control methods
def next(self): def next(self):
self._current_song_id += 1 self._current_song_pos += 1
self.play() self.play()
def pause(self): def pause(self):
@ -123,23 +123,22 @@ class SpotifyBackend(BaseBackend):
def play(self): def play(self):
if self.state == self.PAUSE: if self.state == self.PAUSE:
return self.resume() return self.resume()
super(SpotifyBackend, self).play() if self._current_track is not None:
self.spotify.play(self._current_track) super(SpotifyBackend, self).play()
self.spotify.play(self._current_track)
def play_pos(self, songpos): def play_pos(self, songpos):
super(SpotifyBackend, self).play_pos(songpos) super(SpotifyBackend, self).play_pos(songpos)
self._current_song_id = songpos self._current_song_pos = songpos
track = self._current_playlist[songid] self.spotify.play(self._current_track)
self.spotify.play(track)
def play_id(self, songid): def play_id(self, songid):
super(SpotifyBackend, self).play_id(songid) super(SpotifyBackend, self).play_id(songid)
self._current_song_id = songid self._current_song_pos = songid # XXX
track = self._current_playlist[songid] self.spotify.play(self._current_track)
self.spotify.play(track)
def previous(self): def previous(self):
self._current_song_id -= 1 self._current_song_pos -= 1
self.play() self.play()
def resume(self): def resume(self):
@ -147,20 +146,16 @@ class SpotifyBackend(BaseBackend):
self.spotify.resume() self.spotify.resume()
def stop(self): def stop(self):
super(SpotifyBackend, self).stop() if self.state != self.STOP:
self.spotify.stop() super(SpotifyBackend, self).stop()
self.spotify.stop()
# Unsorted # Unsorted
def current_song(self): def current_song(self):
try: if self.state is not self.STOP and self._current_track is not None:
if self.state is not self.STOP: return self._format_track(self._current_track,
track = self._current_playlist[self._current_song_id] self._current_song_pos)
return self._format_track(track, self._current_song_id)
else:
return None
except IndexError:
return None
def playlist_load(self, name): def playlist_load(self, name):
playlists = filter(lambda p: decode(p.name) == name, self._playlists) playlists = filter(lambda p: decode(p.name) == name, self._playlists)
@ -204,10 +199,13 @@ class SpotifyBackend(BaseBackend):
return len(self._current_playlist) return len(self._current_playlist)
def status_song_id(self): def status_song_id(self):
return self._current_song_id return self._current_song_pos # XXX
def status_time_total(self): def status_time_total(self):
return self._current_track.length // 1000 if self._current_track is not None:
return self._current_track.length // 1000
else:
return 0
def url_handlers(self): def url_handlers(self):
return [u'spotify:', u'http://open.spotify.com/'] return [u'spotify:', u'http://open.spotify.com/']