45b4aa6fe99d251ab65927b2a2626b78bfd5dc7e
[openblackhole/openblackhole-enigma2.git] / lib / nav / core.cpp
1 #include <lib/nav/core.h>
2 #include <lib/base/eerror.h>
3 #include <lib/python/python.h>
4
5 void eNavigation::serviceEvent(iPlayableService* service, int event)
6 {
7         if (service != m_runningService)
8         {
9                 eDebug("nav: event for other service");
10                 return;
11         }
12         m_event(event);
13 }
14
15 void eNavigation::recordEvent(iRecordableService* service, int event)
16 {
17         if (m_recordings.find(service) == m_recordings.end())
18         {
19                 eDebug("nav: event for non registered recording service");
20                 return;
21         }
22         m_record_event(service, event);
23 }
24
25 RESULT eNavigation::playService(const eServiceReference &service)
26 {
27         stopService();
28         
29         assert(m_servicehandler);
30         RESULT res = m_servicehandler->play(service, m_runningService);
31         if (m_runningService)
32         {
33                 m_runningService->connectEvent(slot(*this, &eNavigation::serviceEvent), m_service_event_conn);
34                 res = m_runningService->start();
35         }
36         return res;
37 }
38
39 RESULT eNavigation::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
40 {
41         connection = new eConnection(this, m_event.connect(event));
42         return 0;
43 }
44
45 RESULT eNavigation::connectRecordEvent(const Slot2<void,ePtr<iRecordableService>,int> &event, ePtr<eConnection> &connection)
46 {
47         connection = new eConnection(this, m_record_event.connect(event));
48         return 0;
49 }
50
51 RESULT eNavigation::getCurrentService(ePtr<iPlayableService> &service)
52 {
53         service = m_runningService;
54         return 0;
55 }
56
57 RESULT eNavigation::stopService(void)
58 {
59                 /* check if there is a running service... */
60         if (!m_runningService)
61                 return 1;
62                         /* send stop event */
63         m_event(iPlayableService::evEnd);
64
65         ePtr<iPlayableService> tmp = m_runningService;
66         m_runningService=0;
67         tmp->stop();
68
69                 /* kill service. */
70         m_service_event_conn = 0;
71         return 0;
72 }
73
74 RESULT eNavigation::recordService(const eServiceReference &ref, ePtr<iRecordableService> &service)
75 {
76         assert(m_servicehandler);
77         RESULT res = m_servicehandler->record(ref, service);
78         eDebug("record: %d", res);
79         if (res)
80                 service = 0;
81         else
82         {
83                 ePtr<eConnection> conn;
84                 service->connectEvent(slot(*this, &eNavigation::recordEvent), conn);
85                 m_recordings[service]=conn;
86         }
87         return res;
88 }
89
90 RESULT eNavigation::stopRecordService(ePtr<iRecordableService> &service)
91 {
92         service->stop();
93         std::map<ePtr<iRecordableService>, ePtr<eConnection> >::iterator it =
94                 m_recordings.find(service);
95         if (it != m_recordings.end())
96         {
97                 m_recordings.erase(it);
98                 return 0;
99         }
100         eDebug("try to stop non running recording!!");  // this should not happen
101         return -1;
102 }
103
104 PyObject *eNavigation::getRecordings(void)
105 {
106         ePyObject result = PyList_New(m_recordings.size());
107         int pos=0;
108         for (std::map<ePtr<iRecordableService>, ePtr<eConnection> >::iterator it(m_recordings.begin()); it != m_recordings.end(); ++it)
109                 PyList_SET_ITEM(result, pos++, NEW_iRecordableServicePtr(it->first)); 
110         return result;
111 }
112
113 RESULT eNavigation::pause(int dop)
114 {
115         if (!m_runningService)
116                 return -1;
117         ePtr<iPauseableService> p;
118         if (m_runningService->pause(p))
119                 return -2;
120         if (dop)
121                 return p->pause();
122         else
123                 return p->unpause();
124 }
125
126 eNavigation::eNavigation(iServiceHandler *serviceHandler)
127 {
128         assert(serviceHandler);
129         m_servicehandler = serviceHandler;
130 }
131
132 eNavigation::~eNavigation()
133 {
134         stopService();
135 }
136
137 DEFINE_REF(eNavigation);