Navigation: delay the 'shutdown after recording' decision with 30s
[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, clearFPWasTimerWakeup
5 from time import time
6 import RecordTimer
7 import SleepTimer
8 import Screens.Standby
9 import NavigationInstance
10 import ServiceReference
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.currentlyPlayingService = None
31                 self.RecordTimer = RecordTimer.RecordTimer()
32                 if getFPWasTimerWakeup():
33                         clearFPWasTimerWakeup()
34                         if getFPWasTimerWakeup(): # sanity check to detect if the FP driver is working correct!
35                                 print "buggy fp driver detected!!! please update drivers.... ignore timer wakeup!"
36                         elif 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                 self.SleepTimer = SleepTimer.SleepTimer()
44
45         def checkShutdownAfterRecording(self):
46                 if len(self.getRecordings()) or abs(self.RecordTimer.getNextRecordingTime() - time()) <= 360:
47                         if not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
48                                 RecordTimer.RecordTimerEntry.TryQuitMainloop(False) # start shutdown handling
49
50         def dispatchEvent(self, i):
51                 for x in self.event:
52                         x(i)
53                 if i == iPlayableService.evEnd:
54                         self.currentlyPlayingServiceReference = 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                                 if self.pnav.playService(playref):
89                                         print "Failed to start", playref
90                                         self.currentlyPlayingServiceReference = None
91                                 return 0
92                 else:
93                         self.stopService()
94                 return 1
95         
96         def getCurrentlyPlayingServiceReference(self):
97                 return self.currentlyPlayingServiceReference
98         
99         def recordService(self, ref, simulate=False):
100                 service = None
101                 if not simulate: print "recording service: %s" % (str(ref))
102                 if isinstance(ref, ServiceReference.ServiceReference):
103                         ref = ref.ref
104                 if ref:
105                         if ref.flags & eServiceReference.isGroup:
106                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
107                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
108                         if service is None:
109                                 print "record returned non-zero"
110                 return service
111
112         def stopRecordService(self, service):
113                 ret = self.pnav and self.pnav.stopRecordService(service)
114                 return ret
115
116         def getRecordings(self, simulate=False):
117                 return self.pnav and self.pnav.getRecordings(simulate)
118
119         def getCurrentService(self):
120                 if not self.currentlyPlayingService:
121                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
122                 return self.currentlyPlayingService
123
124         def stopService(self):
125                 if self.pnav:
126                         self.pnav.stopService()
127
128         def pause(self, p):
129                 return self.pnav and self.pnav.pause(p)
130
131         def shutdown(self):
132                 self.RecordTimer.shutdown()
133                 self.ServiceHandler = None
134                 self.pnav = None
135
136         def stopUserServices(self):
137                 self.stopService()