4.2.7r1
[enigma2.git] / usr / lib / enigma2 / python / Navigation.py
1 from enigma import eServiceCenter, eServiceReference, pNavigation, getBestPlayableServiceReference, iPlayableService
2 from Components.config import config
3 from Components.ParentalControl import parentalControl
4 from Tools.BoundFunction import boundFunction
5 from Tools.DreamboxHardware import setFPWakeuptime, getFPWakeuptime, getFPWasTimerWakeup, clearFPWasTimerWakeup
6 from Tools import Notifications
7 from time import time
8 import RecordTimer
9 import SleepTimer
10 import Screens.Standby
11 import NavigationInstance
12 import ServiceReference
13
14 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
15 class Navigation:
16         def __init__(self, nextRecordTimerAfterEventActionAuto=False):
17                 from Screens.ChannelSelection import ChannelSelection
18                 if NavigationInstance.instance is not None:
19                         raise NavigationInstance.instance
20                 
21                 NavigationInstance.instance = self
22                 self.ServiceHandler = eServiceCenter.getInstance()
23                 
24                 import Navigation as Nav
25                 Nav.navcore = self
26                 
27                 self.pnav = pNavigation()
28                 self.nav_event_conn = self.pnav.m_event.connect(self.dispatchEvent)
29                 self.nav_record_event_conn = self.pnav.m_record_event.connect(self.dispatchRecordEvent)
30                 self.event = [ ]
31                 self.record_event = [ ]
32                 self.currentlyPlayingServiceReference = None
33                 self.currentlyPlayingService = None
34                 self.RecordTimer = RecordTimer.RecordTimer()
35                 self.__wasTimerWakeup = False
36                 if getFPWasTimerWakeup():
37                         clearFPWasTimerWakeup()
38                         if getFPWasTimerWakeup(): # sanity check to detect if the FP driver is working correct!
39                                 print "buggy fp driver detected!!! please update drivers.... ignore timer wakeup!"
40                         else:
41                                 self.__wasTimerWakeup = True
42                                 if config.misc.prev_wakeup_time.value and config.misc.prev_wakeup_time_type.value == 0:
43                                         if not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
44                                                 Notifications.AddNotificationWithID("Standby", Screens.Standby.Standby, domain="RecordTimer")
45                 self.SleepTimer = SleepTimer.SleepTimer()
46                 self.scheduledServiceReference = None
47                 ChannelSelection.initialized.addNotifier(self.serviceListInitialized, initial_call = True)
48
49         def serviceListInitialized(self, configElement):
50                 self.immediatePlay = configElement.value
51                 if self.immediatePlay and self.scheduledServiceReference is not None:
52                         print "deferred playService ... ChannelSelection is finally initialized now"
53                         ref = self.scheduledServiceReference
54                         self.scheduledServiceReference = None
55                         self.playService(ref[0], ref[1], ref[2])
56
57         def wasTimerWakeup(self):
58                 return self.__wasTimerWakeup
59
60         def dispatchEvent(self, i):
61                 for x in self.event:
62                         x(i)
63                 if i == iPlayableService.evEnd:
64                         self.currentlyPlayingServiceReference = None
65                         self.currentlyPlayingService = None
66
67         def dispatchRecordEvent(self, rec_service, event):
68 #               print "record_event", rec_service, event
69                 for x in self.record_event:
70                         x(rec_service, event)
71
72         def playService(self, ref, checkParentalControl = True, forceRestart = False):
73                 if not self.immediatePlay:
74                         print "delaying playService request until the ChannelSelection is finally initialized"
75                         self.scheduledServiceReference = (ref, checkParentalControl, forceRestart)
76                         return
77                 oldref = self.currentlyPlayingServiceReference
78                 if ref and oldref and ref == oldref and not forceRestart:
79                         print "ignore request to play already running service(1)"
80                         return 0
81                 print "playing", ref and ref.toString()
82                 if ref is None:
83                         self.stopService()
84                         return 0
85                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl = False)):
86                         if ref.flags & eServiceReference.isGroup:
87                                 if not oldref:
88                                         oldref = eServiceReference()
89                                 playref = getBestPlayableServiceReference(ref, oldref)
90                                 print "playref", playref
91                                 if playref and oldref and playref == oldref and not forceRestart:
92                                         print "ignore request to play already running service(2)"
93                                         return 0
94                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
95                                         self.stopService()
96                                         return 0
97                         else:
98                                 playref = ref
99                         if self.pnav and not self.pnav.playService(playref):
100                                 self.currentlyPlayingServiceReference = playref
101                                 return 0
102                 else:
103                         self.stopService()
104                 return 1
105
106         def getCurrentlyPlayingServiceReference(self):
107                 return self.currentlyPlayingServiceReference
108
109         def recordService(self, ref, simulate=False):
110                 service = None
111                 if not simulate:
112                         print "recording service: %s" % (str(ref))
113                 if isinstance(ref, ServiceReference.ServiceReference):
114                         ref = ref.ref
115                 if ref:
116                         if ref.flags & eServiceReference.isGroup:
117                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
118                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
119                         if service is None:
120                                 if simulate:
121                                         print "simulate recording service: %s failed" % (str(ref))
122                                 print "record returned non-zero"
123                 return service
124
125         def stopRecordService(self, service):
126                 if not self.immediatePlay:
127                         self.scheduledServiceReference = None
128                         return
129                 ret = self.pnav and self.pnav.stopRecordService(service)
130                 return ret
131
132         def getRecordings(self, simulate=False):
133                 return self.pnav and self.pnav.getRecordings(simulate)
134
135         def getCurrentService(self):
136                 if not self.currentlyPlayingService:
137                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
138                 return self.currentlyPlayingService
139
140         def stopService(self):
141                 print "stopService"
142                 if self.pnav:
143                         self.pnav.stopService()
144
145         def pause(self, p):
146                 return self.pnav and self.pnav.pause(p)
147
148         def shutdown(self):
149                 self.RecordTimer.shutdown()
150                 self.ServiceHandler = None
151                 self.pnav = None
152
153         def stopUserServices(self):
154                 self.stopService()