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