now nav.getCurrentlyPlayingServiceReference() and nav.getCurrentService()
[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 import RecordTimer
5 import SleepTimer
6
7 import NavigationInstance
8 import ServiceReference
9
10 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
11 class Navigation:
12         def __init__(self):
13                 if NavigationInstance.instance is not None:
14                         raise NavigationInstance.instance
15                 
16                 NavigationInstance.instance = self
17                 self.ServiceHandler = eServiceCenter.getInstance()
18                 
19                 import Navigation as Nav
20                 Nav.navcore = self
21                 
22                 self.pnav = pNavigation()
23                 self.pnav.m_event.get().append(self.dispatchEvent)
24                 self.pnav.m_record_event.get().append(self.dispatchRecordEvent)
25                 self.event = [ ]
26                 self.record_event = [ ]
27                 self.currentlyPlayingServiceReference = None
28                 self.currentlyPlayingService = None
29                 self.RecordTimer = RecordTimer.RecordTimer()
30                 self.SleepTimer = SleepTimer.SleepTimer()
31
32         def dispatchEvent(self, i):
33                 for x in self.event:
34                         x(i)
35                 if i == iPlayableService.evEnd:
36                         self.currentlyPlayingServiceReference = None
37                         self.currentlyPlayingService = None
38
39         def dispatchRecordEvent(self, rec_service, event):
40 #               print "record_event", rec_service, event
41                 for x in self.record_event:
42                         x(rec_service, event)
43
44         def playService(self, ref, checkParentalControl = True):
45                 oldref = self.currentlyPlayingServiceReference
46                 if ref and oldref and ref == oldref:
47                         print "ignore request to play already running service"
48                         return 0
49                 print "playing", ref and ref.toString()
50                 if ref is None:
51                         self.stopService()
52                         return 0
53                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl = False)):
54                         if ref.flags & eServiceReference.isGroup:
55                                 if not oldref:
56                                         oldref = eServiceReference()
57                                 playref = getBestPlayableServiceReference(ref, oldref)
58                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
59                                         self.stopService()
60                                         return 0
61                         else:
62                                 playref = ref
63                         if self.pnav and not self.pnav.playService(playref):
64                                 self.currentlyPlayingServiceReference = playref
65                                 return 0
66                 else:
67                         self.stopService()
68                 return 1
69         
70         def getCurrentlyPlayingServiceReference(self):
71                 return self.currentlyPlayingServiceReference
72         
73         def recordService(self, ref):
74                 service = None
75                 print "recording service: %s" % (str(ref))
76                 if isinstance(ref, ServiceReference.ServiceReference):
77                         ref = ref.ref
78                 if ref:
79                         if ref.flags & eServiceReference.isGroup:
80                                 ref = getBestPlayableServiceReference(ref, eServiceReference())
81                         service = ref and self.pnav and self.pnav.recordService(ref)
82                         if service is None:
83                                 print "record returned non-zero"
84                 return service
85
86         def stopRecordService(self, service):
87                 ret = self.pnav and self.pnav.stopRecordService(service)
88                 return ret
89
90         def getRecordings(self):
91                 return self.pnav and self.pnav.getRecordings()
92
93         def getCurrentService(self):
94                 if not self.currentlyPlayingService:
95                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
96                 return self.currentlyPlayingService
97
98         def stopService(self):
99                 print "stopService"
100                 if self.pnav:
101                         self.pnav.stopService()
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()