062a7d00046a48d7736eb0fd9dae146bf55fcc73
[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 (m_runningService && service != m_runningService)
8         {
9                 eDebug("nav: event %d for other service", event);
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
63         ePtr<iPlayableService> tmp = m_runningService;
64         m_runningService=0;
65         tmp->stop();
66
67         /* send stop event */
68         m_event(iPlayableService::evEnd);
69
70                 /* kill service. */
71         m_service_event_conn = 0;
72         return 0;
73 }
74
75 RESULT eNavigation::recordService(const eServiceReference &ref, ePtr<iRecordableService> &service)
76 {
77         assert(m_servicehandler);
78         RESULT res = m_servicehandler->record(ref, service);
79         eDebug("record: %d", res);
80         if (res)
81                 service = 0;
82         else
83         {
84                 ePtr<eConnection> conn;
85                 service->connectEvent(slot(*this, &eNavigation::recordEvent), conn);
86                 m_recordings[service]=conn;
87         }
88         return res;
89 }
90
91 RESULT eNavigation::stopRecordService(ePtr<iRecordableService> &service)
92 {
93         service->stop();
94         std::map<ePtr<iRecordableService>, ePtr<eConnection> >::iterator it =
95                 m_recordings.find(service);
96         if (it != m_recordings.end())
97         {
98                 m_recordings.erase(it);
99                 /* send stop event */
100                 m_record_event(service, iRecordableService::evEnd);
101                 return 0;
102         }
103
104         eDebug("try to stop non running recording!!");  // this should not happen
105         return -1;
106 }
107
108 PyObject *eNavigation::getRecordings(void)
109 {
110         ePyObject result = PyList_New(m_recordings.size());
111         int pos=0;
112         for (std::map<ePtr<iRecordableService>, ePtr<eConnection> >::iterator it(m_recordings.begin()); it != m_recordings.end(); ++it)
113                 PyList_SET_ITEM(result, pos++, NEW_iRecordableServicePtr(it->first)); 
114         return result;
115 }
116
117 RESULT eNavigation::pause(int dop)
118 {
119         if (!m_runningService)
120                 return -1;
121         ePtr<iPauseableService> p;
122         if (m_runningService->pause(p))
123                 return -2;
124         if (dop)
125                 return p->pause();
126         else
127                 return p->unpause();
128 }
129
130 eNavigation::eNavigation(iServiceHandler *serviceHandler)
131 {
132         assert(serviceHandler);
133         m_servicehandler = serviceHandler;
134 }
135
136 eNavigation::~eNavigation()
137 {
138         stopService();
139 }
140
141 DEFINE_REF(eNavigation);