Navigation: Correct channel number when userbouquet was searched
[openblackhole/openblackhole-enigma2.git] / Navigation.py
1 from enigma import eServiceCenter, eServiceReference, eTimer, pNavigation, getBestPlayableServiceReference, iPlayableService, eActionMap
2 from Components.ParentalControl import parentalControl
3 from Components.config import config
4 from Tools.BoundFunction import boundFunction
5 from Tools.StbHardware import setFPWakeuptime, getFPWakeuptime, getFPWasTimerWakeup
6 from Tools import Notifications
7 from time import time, localtime
8 import RecordTimer
9 import Screens.Standby
10 import NavigationInstance
11 import ServiceReference
12 from Screens.InfoBar import InfoBar
13 from sys import maxint
14
15 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
16 class Navigation:
17         def __init__(self):
18                 if NavigationInstance.instance is not None:
19                         raise NavigationInstance.instance
20
21                 NavigationInstance.instance = self
22                 self.ServiceHandler = eServiceCenter.getInstance()
23
24                 import Navigation as Nav
25                 Nav.navcore = self
26
27                 self.pnav = pNavigation()
28                 self.pnav.m_event.get().append(self.dispatchEvent)
29                 self.pnav.m_record_event.get().append(self.dispatchRecordEvent)
30                 self.event = [ ]
31                 self.record_event = [ ]
32                 self.currentlyPlayingServiceReference = None
33                 self.currentlyPlayingServiceOrGroup = None
34                 self.currentlyPlayingService = None
35                 self.RecordTimer = RecordTimer.RecordTimer()
36                 self.__wasTimerWakeup = getFPWasTimerWakeup()
37                 if self.__wasTimerWakeup:
38                         RecordTimer.RecordTimerEntry.setWasInDeepStandby()
39
40         def wasTimerWakeup(self):
41                 return self.__wasTimerWakeup
42
43         def dispatchEvent(self, i):
44                 for x in self.event:
45                         x(i)
46                 if i == iPlayableService.evEnd:
47                         self.currentlyPlayingServiceReference = None
48                         self.currentlyPlayingServiceOrGroup = None
49                         self.currentlyPlayingService = None
50
51         def dispatchRecordEvent(self, rec_service, event):
52 #               print "record_event", rec_service, event
53                 for x in self.record_event:
54                         x(rec_service, event)
55
56         def playService(self, ref, checkParentalControl=True, forceRestart=False):
57                 oldref = self.currentlyPlayingServiceReference
58                 if ref and oldref and ref == oldref and not forceRestart:
59                         print "ignore request to play already running service(1)"
60                         return 0
61                 print "playing", ref and ref.toString()
62                 if ref is None:
63                         self.stopService()
64                         return 0
65                 InfoBarInstance = InfoBar.instance
66                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl=False, forceRestart=forceRestart)):
67                         if ref.flags & eServiceReference.isGroup:
68                                 if not oldref:
69                                         oldref = eServiceReference()
70                                 playref = getBestPlayableServiceReference(ref, oldref)
71                                 print "playref", playref
72                                 if playref and oldref and playref == oldref and not forceRestart:
73                                         print "ignore request to play already running service(2)"
74                                         return 0
75                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
76                                         self.stopService()
77                                         return 0
78                         else:
79                                 playref = ref
80                         if self.pnav:
81                                 self.pnav.stopService()
82                                 self.currentlyPlayingServiceReference = playref
83                                 self.currentlyPlayingServiceOrGroup = ref
84                                 if InfoBarInstance is not None:
85                                         if InfoBarInstance.servicelist.servicelist.setCurrent(ref):
86                                                 self.currentlyPlayingServiceOrGroup = InfoBarInstance.servicelist.servicelist.getCurrent()
87                                 if self.pnav.playService(playref):
88                                         print "Failed to start", playref
89                                         self.currentlyPlayingServiceReference = None
90                                         self.currentlyPlayingServiceOrGroup = None
91                                 return 0
92                 elif oldref:
93                         InfoBarInstance.servicelist.servicelist.setCurrent(oldref)
94                 return 1
95
96         def getCurrentlyPlayingServiceReference(self):
97                 return self.currentlyPlayingServiceReference
98
99         def getCurrentlyPlayingServiceOrGroup(self):
100                 return self.currentlyPlayingServiceOrGroup
101
102         def recordService(self, ref, simulate=False):
103                 service = None
104                 if not simulate: print "recording service: %s" % (str(ref))
105                 if isinstance(ref, ServiceReference.ServiceReference):
106                         ref = ref.ref
107                 if ref:
108                         if ref.flags & eServiceReference.isGroup:
109                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
110                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
111                         if service is None:
112                                 print "record returned non-zero"
113                 return service
114
115         def stopRecordService(self, service):
116                 ret = self.pnav and self.pnav.stopRecordService(service)
117                 return ret
118
119         def getRecordings(self, simulate=False):
120                 return self.pnav and self.pnav.getRecordings(simulate)
121
122         def getCurrentService(self):
123                 if not self.currentlyPlayingService:
124                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
125                 return self.currentlyPlayingService
126
127         def stopService(self):
128                 if self.pnav:
129                         self.pnav.stopService()
130                 self.currentlyPlayingServiceReference = None
131                 self.currentlyPlayingServiceOrGroup = None
132
133         def pause(self, p):
134                 return self.pnav and self.pnav.pause(p)
135
136         def shutdown(self):
137                 self.RecordTimer.shutdown()
138                 self.ServiceHandler = None
139                 self.pnav = None
140
141         def stopUserServices(self):
142                 self.stopService()