Navigation.py: Introduce getCurrentlyPlayingServiceOrGroup
[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 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.currentlyPlayingServiceOrGroup = None
32                 self.currentlyPlayingService = None
33                 self.RecordTimer = RecordTimer.RecordTimer()
34                 if getFPWasTimerWakeup():
35                         if nextRecordTimerAfterEventActionAuto:
36                                 # We need to give the systemclock the chance to sync with the transponder time,
37                                 # before we will make the decision about whether or not we need to shutdown
38                                 # after the upcoming recording has completed
39                                 self.recordshutdowntimer = eTimer()
40                                 self.recordshutdowntimer.callback.append(self.checkShutdownAfterRecording)
41                                 self.recordshutdowntimer.start(30000, True)
42                 self.SleepTimer = SleepTimer.SleepTimer()
43
44         def checkShutdownAfterRecording(self):
45                 if len(self.getRecordings()) or abs(self.RecordTimer.getNextRecordingTime() - time()) <= 360:
46                         if not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
47                                 RecordTimer.RecordTimerEntry.TryQuitMainloop(False) # start shutdown handling
48
49         def dispatchEvent(self, i):
50                 for x in self.event:
51                         x(i)
52                 if i == iPlayableService.evEnd:
53                         self.currentlyPlayingServiceReference = None
54                         self.currentlyPlayingServiceOrGroup = None
55                         self.currentlyPlayingService = None
56
57         def dispatchRecordEvent(self, rec_service, event):
58 #               print "record_event", rec_service, event
59                 for x in self.record_event:
60                         x(rec_service, event)
61
62         def playService(self, ref, checkParentalControl = True, forceRestart = False):
63                 oldref = self.currentlyPlayingServiceReference
64                 if ref and oldref and ref == oldref and not forceRestart:
65                         print "ignore request to play already running service(1)"
66                         return 0
67                 print "playing", ref and ref.toString()
68                 if ref is None:
69                         self.stopService()
70                         return 0
71                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl = False)):
72                         if ref.flags & eServiceReference.isGroup:
73                                 if not oldref:
74                                         oldref = eServiceReference()
75                                 playref = getBestPlayableServiceReference(ref, oldref)
76                                 print "playref", playref
77                                 if playref and oldref and playref == oldref and not forceRestart:
78                                         print "ignore request to play already running service(2)"
79                                         return 0
80                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
81                                         self.stopService()
82                                         return 0
83                         else:
84                                 playref = ref
85                         if self.pnav:
86                                 self.pnav.stopService()
87                                 self.currentlyPlayingServiceReference = playref
88                                 self.currentlyPlayingServiceOrGroup = ref
89                                 InfoBarInstance = InfoBar.instance
90                                 if InfoBarInstance is not None:
91                                         InfoBarInstance.servicelist.servicelist.setCurrent(ref)
92                                 if self.pnav.playService(playref):
93                                         print "Failed to start", playref
94                                         self.currentlyPlayingServiceReference = None
95                                         self.currentlyPlayingServiceOrGroup = None
96                                 return 0
97                 else:
98                         self.stopService()
99                 return 1
100
101         def getCurrentlyPlayingServiceReference(self):
102                 return self.currentlyPlayingServiceReference
103
104         def getCurrentlyPlayingServiceOrGroup(self):
105                 return self.currentlyPlayingServiceOrGroup
106
107         def recordService(self, ref, simulate=False):
108                 service = None
109                 if not simulate: print "recording service: %s" % (str(ref))
110                 if isinstance(ref, ServiceReference.ServiceReference):
111                         ref = ref.ref
112                 if ref:
113                         if ref.flags & eServiceReference.isGroup:
114                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
115                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
116                         if service is None:
117                                 print "record returned non-zero"
118                 return service
119
120         def stopRecordService(self, service):
121                 ret = self.pnav and self.pnav.stopRecordService(service)
122                 return ret
123
124         def getRecordings(self, simulate=False):
125                 return self.pnav and self.pnav.getRecordings(simulate)
126
127         def getCurrentService(self):
128                 if not self.currentlyPlayingService:
129                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
130                 return self.currentlyPlayingService
131
132         def stopService(self):
133                 if self.pnav:
134                         self.pnav.stopService()
135                 self.currentlyPlayingServiceReference = None
136                 self.currentlyPlayingServiceOrGroup = None
137
138         def pause(self, p):
139                 return self.pnav and self.pnav.pause(p)
140
141         def shutdown(self):
142                 self.RecordTimer.shutdown()
143                 self.ServiceHandler = None
144                 self.pnav = None
145
146         def stopUserServices(self):
147                 self.stopService()