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