New background, code cleanup
[jamaendo] / jamaui / player.py
index d67f9fc..13af7a6 100644 (file)
@@ -26,6 +26,8 @@ import util
 import dbus
 import dbus.service
 
+import jamaendo
+
 log = logging.getLogger(__name__)
 
 class _Player(object):
@@ -216,11 +218,16 @@ class GStreamer(_Player):
 
         if t == gst.MESSAGE_EOS:
             self.eos_callback()
-
+            log.info("End of stream")
+        elif t == gst.MESSAGE_STATE_CHANGED:
+            old, new, pending = message.parse_state_changed()
+            log.info("State changed: %s -> %s -> %s", old, new, pending)
         elif t == gst.MESSAGE_ERROR:
             err, debug = message.parse_error()
             log.critical( 'Error: %s %s', err, debug )
             self.stop()
+        else:
+            log.info("? %s", message.type)
 
     def set_eos_callback(self, cb):
         self.eos_callback = cb
@@ -311,33 +318,21 @@ if util.platform == 'maemo':
 PlayerBackend = GStreamer
 
 class Playlist(object):
-    class Entry(object):
-        def __init__(self, data):
-            if isinstance(data, dict):
-                self.id = data['id']
-                self.name = data['name']
-                self.image = data['image']
-                self.numalbum = int(data['numalbum'])
-                self.url = data['mp3']
-                self.type = 'mp3'
-            elif isinstance(data, basestring): # assume URI
-                self.id = 0
-                self.name = ''
-                self.image = None
-                self.numalbum = 0
-                self.url = data
-                self.type = 'mp3'
-        def __str__(self):
-            return "{%s}" % (", ".join([str(self.name), str(self.numalbum), str(self.url)]))
-
     def __init__(self, items = []):
         if items is None:
             items = []
-        self.items = [Playlist.Entry(item) for item in items]
+        for item in items:
+            assert(isinstance(item, jamaendo.Track))
+        self.items = items
         self._current = -1
 
     def add(self, item):
-        self.items.append(Playlist.Entry(item))
+        if isinstance(item, list):
+            for i in item:
+                assert(isinstance(i, jamaendo.Track))
+            self.items.extend(item)
+        else:
+            self.items.append(item)
 
     def next(self):
         if self.has_next():
@@ -345,9 +340,18 @@ class Playlist(object):
             return self.items[self._current]
         return None
 
+    def prev(self):
+        if self.has_prev():
+            self._current = self._current - 1
+            return self.items[self._current]
+        return None
+
     def has_next(self):
         return self._current < (len(self.items)-1)
 
+    def has_prev(self):
+        return self._current > 0
+
     def current(self):
         if self._current >= 0:
             return self.items[self._current]
@@ -356,23 +360,29 @@ class Playlist(object):
     def current_index(self):
         return self._current
 
-    def __len__(self):
+    def size(self):
+        print type(self)
         return len(self.items)
 
+    def __repr__(self):
+        return "Playlist(%s)"%(", ".join([str(item) for item in self.items]))
+
 class Player(Playlist):
     def __init__(self):
         self.backend = PlayerBackend()
         self.backend.set_eos_callback(self._on_eos)
-        self.playlist = None
+        self.playlist = Playlist()
 
     def play(self, playlist = None):
         if playlist:
             self.playlist = playlist
-        if self.playlist is not None:
+        elif self.playlist is None:
+            self.playlist = Playlist()
+        if self.playlist.size():
             if self.playlist.has_next():
                 entry = self.playlist.next()
                 log.debug("playing %s", entry)
-                self.backend.play_url(entry.type, entry.url)
+                self.backend.play_url('mp3', entry.mp3_url())
 
     def pause(self):
         self.backend.pause()
@@ -391,8 +401,13 @@ class Player(Playlist):
             self.stop()
 
     def prev(self):
-        pass
+        if self.playlist.has_prev():
+            entry = self.playlist.prev()
+            log.debug("playing %s", entry)
+            self.backend.play_url('mp3', entry.mp3_url())
 
     def _on_eos(self):
         log.debug("EOS!")
         self.next()
+
+the_player = Player() # the player instance