enigma2 20130801 (master) -> 20130815 (master)
[enigma2.git] / usr / lib / enigma2 / python / 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 Tools import Notifications
6 from time import time
7 import RecordTimer
8 import SleepTimer
9 import Screens.Standby
10 import NavigationInstance
11 import ServiceReference
12
13 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
14 class Navigation:
15         def __init__(self, nextRecordTimerAfterEventActionAuto=False):
16                 if NavigationInstance.instance is not None:
17                         raise NavigationInstance.instance
18                 
19                 NavigationInstance.instance = self
20                 self.ServiceHandler = eServiceCenter.getInstance()
21                 
22                 import Navigation as Nav
23                 Nav.navcore = self
24                 
25                 self.pnav = pNavigation()
26                 self.pnav.m_event.get().append(self.dispatchEvent)
27                 self.pnav.m_record_event.get().append(self.dispatchRecordEvent)
28                 self.event = [ ]
29                 self.record_event = [ ]
30                 self.currentlyPlayingServiceReference = None
31                 self.currentlyPlayingService = None
32                 self.RecordTimer = RecordTimer.RecordTimer()
33                 self.__wasTimerWakeup = False
34                 if getFPWasTimerWakeup():
35                         clearFPWasTimerWakeup()
36                         if getFPWasTimerWakeup(): # sanity check to detect if the FP driver is working correct!
37                                 print "buggy fp driver detected!!! please update drivers.... ignore timer wakeup!"
38                         else:
39                                 self.__wasTimerWakeup = True
40                                 if nextRecordTimerAfterEventActionAuto and (len(self.getRecordings()) or abs(self.RecordTimer.getNextRecordingTime() - time()) <= 360):
41                                         if not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
42                                                 Notifications.AddNotification(Screens.Standby.Standby, domain="RecordTimer")
43                 self.SleepTimer = SleepTimer.SleepTimer()
44
45         def wasTimerWakeup(self):
46                 return self.__wasTimerWakeup
47
48         def dispatchEvent(self, i):
49                 for x in self.event:
50                         x(i)
51                 if i == iPlayableService.evEnd:
52                         self.currentlyPlayingServiceReference = None
53                         self.currentlyPlayingService = None
54
55         def dispatchRecordEvent(self, rec_service, event):
56 #               print "record_event", rec_service, event
57                 for x in self.record_event:
58                         x(rec_service, event)
59
60         def playService(self, ref, checkParentalControl = True, forceRestart = False):
61                 oldref = self.currentlyPlayingServiceReference
62                 if ref and oldref and ref == oldref and not forceRestart:
63                         print "ignore request to play already running service(1)"
64                         return 0
65                 print "playing", ref and ref.toString()
66                 if ref is None:
67                         self.stopService()
68                         return 0
69                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl = False)):
70                         if ref.flags & eServiceReference.isGroup:
71                                 if not oldref:
72                                         oldref = eServiceReference()
73                                 playref = getBestPlayableServiceReference(ref, oldref)
74                                 print "playref", playref
75                                 if playref and oldref and playref == oldref and not forceRestart:
76                                         print "ignore request to play already running service(2)"
77                                         return 0
78                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
79                                         self.stopService()
80                                         return 0
81                         else:
82                                 playref = ref
83                         if self.pnav and not self.pnav.playService(playref):
84                                 self.currentlyPlayingServiceReference = playref
85                                 return 0
86                 else:
87                         self.stopService()
88                 return 1
89         
90         def getCurrentlyPlayingServiceReference(self):
91                 return self.currentlyPlayingServiceReference
92         
93         def recordService(self, ref, simulate=False):
94                 service = None
95                 print "recording service: %s" % (str(ref))
96                 if isinstance(ref, ServiceReference.ServiceReference):
97                         ref = ref.ref
98                 if ref:
99                         if ref.flags & eServiceReference.isGroup:
100                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
101                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
102                         if service is None:
103                                 print "record returned non-zero"
104                 return service
105
106         def stopRecordService(self, service):
107                 ret = self.pnav and self.pnav.stopRecordService(service)
108                 return ret
109
110         def getRecordings(self, simulate=False):
111                 return self.pnav and self.pnav.getRecordings(simulate)
112
113         def getCurrentService(self):
114                 if not self.currentlyPlayingService:
115                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
116                 return self.currentlyPlayingService
117
118         def stopService(self):
119                 print "stopService"
120                 if self.pnav:
121                         self.pnav.stopService()
122
123         def pause(self, p):
124                 return self.pnav and self.pnav.pause(p)
125
126         def shutdown(self):
127                 self.RecordTimer.shutdown()
128                 self.ServiceHandler = None
129                 self.pnav = None
130
131         def stopUserServices(self):
132                 self.stopService()