Remember playback resume points in RAM for filetypes without cue support
[openblackhole/openblackhole-enigma2.git] / Navigation.py
1 from enigma import eServiceCenter, eServiceReference, 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 and (len(self.getRecordings()) or abs(self.RecordTimer.getNextRecordingTime() - time()) <= 360):
37                                 if not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
38                                         RecordTimer.RecordTimerEntry.TryQuitMainloop(False) # start shutdown handling
39                 self.SleepTimer = SleepTimer.SleepTimer()
40
41         def dispatchEvent(self, i):
42                 for x in self.event:
43                         x(i)
44                 if i == iPlayableService.evEnd:
45                         self.currentlyPlayingServiceReference = None
46                         self.currentlyPlayingService = None
47
48         def dispatchRecordEvent(self, rec_service, event):
49 #               print "record_event", rec_service, event
50                 for x in self.record_event:
51                         x(rec_service, event)
52
53         def playService(self, ref, checkParentalControl = True, forceRestart = False):
54                 oldref = self.currentlyPlayingServiceReference
55                 if ref and oldref and ref == oldref and not forceRestart:
56                         print "ignore request to play already running service(1)"
57                         return 0
58                 print "playing", ref and ref.toString()
59                 if ref is None:
60                         self.stopService()
61                         return 0
62                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl = False)):
63                         if ref.flags & eServiceReference.isGroup:
64                                 if not oldref:
65                                         oldref = eServiceReference()
66                                 playref = getBestPlayableServiceReference(ref, oldref)
67                                 print "playref", playref
68                                 if playref and oldref and playref == oldref and not forceRestart:
69                                         print "ignore request to play already running service(2)"
70                                         return 0
71                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
72                                         self.stopService()
73                                         return 0
74                         else:
75                                 playref = ref
76                         if self.pnav:
77                                 self.pnav.stopService()
78                                 self.currentlyPlayingServiceReference = playref
79                                 if self.pnav.playService(playref):
80                                         print "Failed to start", playref
81                                         self.currentlyPlayingServiceReference = None
82                                 return 0
83                 else:
84                         self.stopService()
85                 return 1
86         
87         def getCurrentlyPlayingServiceReference(self):
88                 return self.currentlyPlayingServiceReference
89         
90         def recordService(self, ref, simulate=False):
91                 service = None
92                 if not simulate: print "recording service: %s" % (str(ref))
93                 if isinstance(ref, ServiceReference.ServiceReference):
94                         ref = ref.ref
95                 if ref:
96                         if ref.flags & eServiceReference.isGroup:
97                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
98                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
99                         if service is None:
100                                 print "record returned non-zero"
101                 return service
102
103         def stopRecordService(self, service):
104                 ret = self.pnav and self.pnav.stopRecordService(service)
105                 return ret
106
107         def getRecordings(self, simulate=False):
108                 return self.pnav and self.pnav.getRecordings(simulate)
109
110         def getCurrentService(self):
111                 if not self.currentlyPlayingService:
112                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
113                 return self.currentlyPlayingService
114
115         def stopService(self):
116                 if self.pnav:
117                         self.pnav.stopService()
118
119         def pause(self, p):
120                 return self.pnav and self.pnav.pause(p)
121
122         def shutdown(self):
123                 self.RecordTimer.shutdown()
124                 self.ServiceHandler = None
125                 self.pnav = None
126
127         def stopUserServices(self):
128                 self.stopService()