4.5.0r6
[enigma2.git] / usr / lib / enigma2 / python / Navigation.py
1 from __future__ import print_function
2 from enigma import eServiceCenter, eServiceReference, pNavigation, getBestPlayableServiceReference, iPlayableService
3 from Components.config import config
4 from Components.ParentalControl import parentalControl
5 from Tools.BoundFunction import boundFunction
6 from Tools.DreamboxHardware import getFPWasTimerWakeup, clearFPWasTimerWakeup
7 from Tools import Notifications
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.immediatePlay = False
35                 self.RecordTimer = RecordTimer.RecordTimer()
36                 self.__wasTimerWakeup = False
37                 if getFPWasTimerWakeup():
38                         clearFPWasTimerWakeup()
39                         if getFPWasTimerWakeup(): # sanity check to detect if the FP driver is working correct!
40                                 print("buggy fp driver detected!!! please update drivers.... ignore timer wakeup!")
41                         else:
42                                 self.__wasTimerWakeup = True
43                                 if config.misc.prev_wakeup_time.value and config.misc.prev_wakeup_time_type.value == 0:
44                                         if not Screens.Standby.inTryQuitMainloop: # not a shutdown messagebox is open
45                                                 Notifications.AddNotificationWithID("Standby", Screens.Standby.Standby, domain="RecordTimer")
46                 self.SleepTimer = SleepTimer.SleepTimer()
47                 self.scheduledServiceReference = None
48                 ChannelSelection.initialized.addNotifier(self.serviceListInitialized, initial_call = True)
49
50         def serviceListInitialized(self, configElement):
51                 self.immediatePlay = configElement.value
52                 if self.immediatePlay and self.scheduledServiceReference is not None:
53                         print("deferred playService ... ChannelSelection is finally initialized now")
54                         ref = self.scheduledServiceReference
55                         self.scheduledServiceReference = None
56                         self.playService(ref[0], ref[1], ref[2])
57
58         def wasTimerWakeup(self):
59                 return self.__wasTimerWakeup
60
61         def dispatchEvent(self, i):
62                 for x in self.event:
63                         x(i)
64                 if i == iPlayableService.evEnd:
65                         self.currentlyPlayingServiceReference = None
66                         self.currentlyPlayingService = None
67
68         def dispatchRecordEvent(self, rec_service, event):
69 #               print "record_event", rec_service, event
70                 for x in self.record_event:
71                         x(rec_service, event)
72
73         def playService(self, ref, checkParentalControl = True, forceRestart = False):
74                 if not self.immediatePlay:
75                         print("delaying playService request until the ChannelSelection is finally initialized")
76                         self.scheduledServiceReference = (ref, checkParentalControl, forceRestart)
77                         return
78                 oldref = self.currentlyPlayingServiceReference
79                 if ref and oldref and ref == oldref and not forceRestart:
80                         print("ignore request to play already running service(1)")
81                         return 0
82                 print("playing", ref and ref.toString())
83                 if ref is None:
84                         self.stopService()
85                         return 0
86                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl = False)):
87                         if ref.flags & eServiceReference.isGroup:
88                                 if not oldref:
89                                         oldref = eServiceReference()
90                                 playref = getBestPlayableServiceReference(ref, oldref)
91                                 print("playref", playref)
92                                 if playref and oldref and playref == oldref and not forceRestart:
93                                         print("ignore request to play already running service(2)")
94                                         return 0
95                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
96                                         self.stopService()
97                                         return 0
98                         else:
99                                 playref = ref
100                         if self.pnav and not self.pnav.playService(playref):
101                                 self.currentlyPlayingServiceReference = self.pnav.getCurrentServiceReference();
102                                 return 0
103                 else:
104                         self.stopService()
105                 return 1
106
107         # You'll break ServiceEventTracker if you ever change this, keep your hand's of it!
108         # It HAS to be set after evStart
109         def getCurrentlyPlayingServiceReference(self):
110                 return self.currentlyPlayingServiceReference
111
112         def getCurrentServiceReference(self):
113                 return self.pnav.getCurrentServiceReference()
114
115         def recordService(self, ref, simulate=False):
116                 service = None
117                 if not simulate:
118                         print("recording service: %s" % (str(ref)))
119                 if isinstance(ref, ServiceReference.ServiceReference):
120                         ref = ref.ref
121                 if ref:
122                         if ref.flags & eServiceReference.isGroup:
123                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
124                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
125                         if service is None:
126                                 if simulate:
127                                         print("simulate recording service: %s failed" % (str(ref)))
128                                 print("record returned non-zero")
129                 return service
130
131         def stopRecordService(self, service):
132                 ret = self.pnav and self.pnav.stopRecordService(service)
133                 return ret
134
135         def getRecordings(self, simulate=False):
136                 return self.pnav and self.pnav.getRecordings(simulate)
137
138         def getCurrentService(self):
139                 if not self.currentlyPlayingService:
140                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
141                 return self.currentlyPlayingService
142
143         def stopService(self):
144                 if not self.immediatePlay:
145                         self.scheduledServiceReference = None
146                         return
147                 print("stopService")
148                 if self.pnav:
149                         self.pnav.stopService()
150
151         def pause(self, p):
152                 return self.pnav and self.pnav.pause(p)
153
154         def shutdown(self):
155                 self.RecordTimer.shutdown()
156                 self.ServiceHandler = None
157                 self.pnav = None
158
159         def stopUserServices(self):
160                 self.stopService()