remove ugly HACK
[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                 print "playing", ref and ref.toString()
46                 self.currentlyPlayingServiceReference = None
47                 self.currentlyPlayingService = None
48                 if ref is None:
49                         self.stopService()
50                         return 0
51                 if not checkParentalControl or parentalControl.isServicePlayable(ref.toCompareString(), boundFunction(self.playService, checkParentalControl = False)):
52                         if ref.flags & eServiceReference.isGroup:
53                                 if not oldref:
54                                         oldref = eServiceReference()
55                                 playref = getBestPlayableServiceReference(ref, oldref)
56                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref.toCompareString(), boundFunction(self.playService, checkParentalControl = False))):
57                                         self.stopService()
58                                         return 0
59                         else:
60                                 playref = ref
61                         if self.pnav and not self.pnav.playService(playref):
62                                 self.currentlyPlayingServiceReference = ref
63                                 return 0
64                 else:
65                         self.stopService()
66                 return 1
67         
68         def getCurrentlyPlayingServiceReference(self):
69                 return self.currentlyPlayingServiceReference
70         
71         def recordService(self, ref):
72                 service = None
73                 print "recording service: %s" % (str(ref))
74                 if isinstance(ref, ServiceReference.ServiceReference):
75                         ref = ref.ref
76                 if ref:
77                         if ref.flags & eServiceReference.isGroup:
78                                 ref = getBestPlayableServiceReference(ref, eServiceReference())
79                         service = ref and self.pnav and self.pnav.recordService(ref)
80                         if service is None:
81                                 print "record returned non-zero"
82                 return service
83
84         def stopRecordService(self, service):
85                 ret = self.pnav and self.pnav.stopRecordService(service)
86                 return ret
87
88         def getRecordings(self):
89                 return self.pnav and self.pnav.getRecordings()
90
91         def getCurrentService(self):
92                 if not self.currentlyPlayingService:
93                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
94                 return self.currentlyPlayingService
95
96         def stopService(self):
97                 print "stopService"
98                 if self.pnav:
99                         self.pnav.stopService()
100                 self.currentlyPlayingService = None
101                 self.currentlyPlayingServiceReference = None
102
103         def pause(self, p):
104                 return self.pnav and self.pnav.pause(p)
105
106         def recordWithTimer(self, ref, begin, end, name, description, eit):
107                 if isinstance(ref, eServiceReference):
108                         ref = ServiceReference.ServiceReference(ref)
109                 entry = RecordTimer.RecordTimerEntry(ref, begin, end, name, description, eit)
110                 self.RecordTimer.record(entry)
111                 return entry
112         
113         def shutdown(self):
114                 self.RecordTimer.shutdown()
115                 self.ServiceHandler = None
116                 self.pnav = None
117
118         def stopUserServices(self):
119                 self.stopService()