add virtual destructor because of iObject
[enigma2.git] / lib / nav / core.cpp
index 9314d2c1756b1b428c28dbc5bc4dad3d37808c5e..c9dcb5022ec6eaa611fb6f1fee961d6f9f5ca17e 100644 (file)
@@ -11,21 +11,31 @@ void eNavigation::serviceEvent(iPlayableService* service, int event)
        switch (event)
        {
        case iPlayableService::evEnd:
+               assert(m_playlist); /* we need to have a playlist */
+               
                        /* at first, kill the running service */
                m_event(this, evStopService);
                m_runningService = 0;
                m_service_event_conn = 0;
-                       /* our running main service stopped. remove it from playlist */
-               if (!m_playlist.empty())
-                       m_playlist.erase(m_playlist.begin());
-               if (!m_playlist.empty())
+                       /* our running main service stopped. identify what to do next. */
+                       
+                       /* unless the playlist current position is invalid (because there was */
+                       /* playlist, for example when the service was engaged with playService */
+               if (m_playlist->m_current != m_playlist->end())
+                       ++m_playlist->m_current;
+                       
+                       /* was the current service the last one? */
+               if (m_playlist->m_current == m_playlist->end())
                {
-                       RESULT res;
-                       res = playService(m_playlist.front());
-                       if (res)
-                               m_event(this, evPlayFailed);
-               } else
                        m_event(this, evPlaylistDone);
+                       break;
+               }
+
+                       /* there is another service in the playlist. play it. */
+               RESULT res;
+               res = playService(*m_playlist->m_current);
+               if (res)
+                       m_event(this, evPlayFailed);
                break;
        case iPlayableService::evStart:
                m_event(this, evNewService);
@@ -49,10 +59,20 @@ RESULT eNavigation::playService(const eServiceReference &service)
 
 RESULT eNavigation::enqueueService(const eServiceReference &service)
 {
-       int doplay = m_playlist.empty();
-       m_playlist.push_back(service);
+       assert(m_playlist);
+               /* check if we need to play after the service was enqueued. */
+       int doplay = m_playlist->m_current == m_playlist->end();
+       
+               /* add the service to the playlist. the playlist's m_current */
+               /* points either to a service before the last or 'doplay' is set. */
+       m_playlist->push_back(service);
+
        if (doplay)
-               return playService(m_playlist.front());
+       {
+               m_playlist->m_current = m_playlist->end();
+               --m_playlist->m_current;
+               return playService(*m_playlist->m_current);
+       }
        return 0;
 }
 
@@ -68,6 +88,14 @@ RESULT eNavigation::getCurrentService(ePtr<iPlayableService> &service)
        return 0;
 }
 
+RESULT eNavigation::getPlaylist(ePtr<ePlaylist> &playlist)
+{
+       if (!m_playlist)
+               return -1;
+       playlist = m_playlist;
+       return 0;
+}
+
 RESULT eNavigation::pause(int dop)
 {
        if (!m_runningService)
@@ -85,6 +113,10 @@ eNavigation::eNavigation(iServiceHandler *serviceHandler): ref(0)
 {
        assert(serviceHandler);
        m_servicehandler = serviceHandler;
+       m_playlist = new ePlaylist;
+
+               /* start with no current selection */
+       m_playlist->m_current = m_playlist->end();
 }
 
 eNavigation::~eNavigation()