parental locking fixes
[openblackhole/openblackhole-enigma2.git] / Navigation.py
1 from enigma import eServiceCenter, eServiceReference, pNavigation, getBestPlayableServiceReference
2 from Components.ParentalControl import parentalControl
3 from Tools.BoundFunction import boundFunction
4 import RecordTimer
5 import SleepTimer
6
7 import NavigationInstance
8 import ServiceReference
9
10 from time import time
11
12 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
13 class Navigation:
14         def __init__(self):
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                 self.SleepTimer = SleepTimer.SleepTimer()
33
34         def dispatchEvent(self, i):
35                 for x in self.event:
36                         x(i)
37
38         def dispatchRecordEvent(self, rec_service, event):
39 #               print "record_event", rec_service, event
40                 for x in self.record_event:
41                         x(rec_service, event)
42
43         def playService(self, ref, checkParentalControl = True):
44                 oldref = self.currentlyPlayingServiceReference
45                 if ref and oldref and ref == oldref:
46                         print "ignore request to play already running service"
47                         return 0
48                 print "playing", ref and ref.toString()
49                 self.currentlyPlayingServiceReference = None
50                 self.currentlyPlayingService = None
51                 if ref is None:
52                         self.stopService()
53                         return 0
54                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl = False)):
55                         if ref.flags & eServiceReference.isGroup:
56                                 if not oldref:
57                                         oldref = eServiceReference()
58                                 playref = getBestPlayableServiceReference(ref, oldref)
59                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
60                                         self.stopService()
61                                         return 0
62                         else:
63                                 playref = ref
64                         if self.pnav and not self.pnav.playService(playref):
65                                 self.currentlyPlayingServiceReference = ref
66                                 return 0
67                 else:
68                         self.stopService()
69                 return 1
70         
71         def getCurrentlyPlayingServiceReference(self):
72                 return self.currentlyPlayingServiceReference
73         
74         def recordService(self, ref):
75                 service = None
76                 print "recording service: %s" % (str(ref))
77                 if isinstance(ref, ServiceReference.ServiceReference):
78                         ref = ref.ref
79                 if ref:
80                         if ref.flags & eServiceReference.isGroup:
81                                 ref = getBestPlayableServiceReference(ref, eServiceReference())
82                         service = ref and self.pnav and self.pnav.recordService(ref)
83                         if service is None:
84                                 print "record returned non-zero"
85                 return service
86
87         def stopRecordService(self, service):
88                 ret = self.pnav and self.pnav.stopRecordService(service)
89                 return ret
90
91         def getRecordings(self):
92                 return self.pnav and self.pnav.getRecordings()
93
94         def getCurrentService(self):
95                 if not self.currentlyPlayingService:
96                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
97                 return self.currentlyPlayingService
98
99         def stopService(self):
100                 print "stopService"
101                 if self.pnav:
102                         self.pnav.stopService()
103                 self.currentlyPlayingService = None
104                 self.currentlyPlayingServiceReference = None
105
106         def pause(self, p):
107                 return self.pnav and self.pnav.pause(p)
108
109         def recordWithTimer(self, ref, begin, end, name, description, eit):
110                 if isinstance(ref, eServiceReference):
111                         ref = ServiceReference.ServiceReference(ref)
112                 entry = RecordTimer.RecordTimerEntry(ref, begin, end, name, description, eit)
113                 self.RecordTimer.record(entry)
114                 return entry
115         
116         def shutdown(self):
117                 self.RecordTimer.shutdown()
118                 self.ServiceHandler = None
119                 self.pnav = None
120
121         def stopUserServices(self):
122                 self.stopService()