#ifdeffing the sdl-stuff
[enigma2.git] / lib / nav / core.cpp
1 #include <lib/nav/core.h>
2 #include <lib/base/eerror.h>
3
4 void eNavigation::serviceEvent(iPlayableService* service, int event)
5 {
6         if (service != m_runningService)
7         {
8                 eDebug("nav: event for other service");
9                 return;
10         }
11
12         switch (event)
13         {
14         case iPlayableService::evEnd:
15                 assert(m_playlist); /* we need to have a playlist */
16                 
17                 /* at first, kill the running service */
18                 stopService();
19                 
20                 /* our running main service stopped. identify what to do next. */
21                         
22                         /* unless the playlist current position is invalid (because there was */
23                         /* playlist, for example when the service was engaged with playService */
24                 if (m_playlist->m_current != m_playlist->end())
25                         ++m_playlist->m_current;
26                         
27                         /* was the current service the last one? */
28                 if (m_playlist->m_current == m_playlist->end())
29                 {
30                         m_event(this, evPlaylistDone);
31                         break;
32                 }
33
34                         /* there is another service in the playlist. play it. */
35                 RESULT res;
36                 res = playService(*m_playlist->m_current);
37                 if (res)
38                         m_event(this, evPlayFailed);
39                 break;
40         case iPlayableService::evStart:
41                 m_event(this, evNewService);
42                 break;
43         case iPlayableService::evUpdatedEventInfo:
44                 m_event(this, evUpdatedEventInfo);
45                 break;
46         default:
47                 break;
48         }
49 }
50
51 RESULT eNavigation::playService(const eServiceReference &service)
52 {
53         stopService();
54         
55         assert(m_servicehandler);
56         RESULT res = m_servicehandler->play(service, m_runningService);
57         if (m_runningService)
58         {
59                 m_runningService->connectEvent(slot(*this, &eNavigation::serviceEvent), m_service_event_conn);
60                 res = m_runningService->start();
61         }
62         return res;
63 }
64
65 RESULT eNavigation::enqueueService(const eServiceReference &service)
66 {
67         assert(m_playlist);
68                 /* check if we need to play after the service was enqueued. */
69         int doplay = m_playlist->m_current == m_playlist->end();
70         
71                 /* add the service to the playlist. the playlist's m_current */
72                 /* points either to a service before the last or 'doplay' is set. */
73         m_playlist->push_back(service);
74
75         if (doplay)
76         {
77                 m_playlist->m_current = m_playlist->end();
78                 --m_playlist->m_current;
79                 return playService(*m_playlist->m_current);
80         }
81         return 0;
82 }
83
84 RESULT eNavigation::connectEvent(const Slot2<void,eNavigation*,int> &event, ePtr<eConnection> &connection)
85 {
86         connection = new eConnection(this, m_event.connect(event));
87         return 0;
88 }
89
90 RESULT eNavigation::getCurrentService(ePtr<iPlayableService> &service)
91 {
92         service = m_runningService;
93         return 0;
94 }
95
96 RESULT eNavigation::getPlaylist(ePtr<ePlaylist> &playlist)
97 {
98         if (!m_playlist)
99                 return -1;
100         playlist = m_playlist;
101         return 0;
102 }
103
104 RESULT eNavigation::stopService(void)
105 {
106                 /* check if there is a running service... */
107         if (!m_runningService)
108                 return 1;
109                         /* send stop event */
110         m_event(this, evStopService);
111
112                 /* kill service. */
113         m_runningService = 0;
114         m_service_event_conn = 0;
115         return 0;
116 }
117
118 RESULT eNavigation::recordService(const eServiceReference &ref, ePtr<iRecordableService> &service)
119 {
120         assert(m_servicehandler);
121         RESULT res = m_servicehandler->record(ref, service);
122         eDebug("record: %d", res);
123         if (res)
124                 service = 0;
125         return res;
126 }
127
128 RESULT eNavigation::pause(int dop)
129 {
130         if (!m_runningService)
131                 return -1;
132         ePtr<iPauseableService> p;
133         if (m_runningService->pause(p))
134                 return -2;
135         if (dop)
136                 return p->pause();
137         else
138                 return p->unpause();
139 }
140
141 eNavigation::eNavigation(iServiceHandler *serviceHandler)
142 {
143         assert(serviceHandler);
144         m_servicehandler = serviceHandler;
145         m_playlist = new ePlaylist;
146
147                 /* start with no current selection */
148         m_playlist->m_current = m_playlist->end();
149 }
150
151 eNavigation::~eNavigation()
152 {
153 }
154
155 DEFINE_REF(eNavigation);