python_service.i: naming change getAITApplication -> getAITApplications
[openblackhole/openblackhole-enigma2.git] / Navigation.py
1 from enigma import eServiceCenter, eServiceReference, eTimer, pNavigation, getBestPlayableServiceReference, iPlayableService, eActionMap
2 from Components.ParentalControl import parentalControl
3 from Components.config import config
4 from Tools.BoundFunction import boundFunction
5 from Tools.StbHardware import setFPWakeuptime, getFPWakeuptime, getFPWasTimerWakeup
6 from Tools import Notifications
7 from time import time, localtime
8 import RecordTimer
9 import Screens.Standby
10 import NavigationInstance
11 import ServiceReference
12 from Screens.InfoBar import InfoBar
13 from sys import maxint
14
15 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
16 class Navigation:
17         def __init__(self):
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.pnav.m_event.get().append(self.dispatchEvent)
29                 self.pnav.m_record_event.get().append(self.dispatchRecordEvent)
30                 self.event = [ ]
31                 self.record_event = [ ]
32                 self.currentlyPlayingServiceReference = None
33                 self.currentlyPlayingServiceOrGroup = None
34                 self.currentlyPlayingService = None
35                 self.RecordTimer = RecordTimer.RecordTimer()
36                 self.__wasTimerWakeup = getFPWasTimerWakeup()
37                 if self.__wasTimerWakeup:
38                         # We need to give the systemclock the chance to sync with the transponder time
39                         self.recordshutdowntimer = eTimer()
40                         self.recordshutdowntimer.callback.append(self.checkShutdownAfterRecording)
41                         self.recordshutdowntimer.startLongTimer(30)
42                         eActionMap.getInstance().bindAction('', -maxint - 1, self.keypress)
43
44         def checkShutdownAfterRecording(self):
45                 if len(self.getRecordings()) or abs(self.RecordTimer.getNextTimerTime() - time()) <= 360:
46                         if Screens.Standby.inStandby: #In case some plugin did put the receiver already in standby
47                                 config.misc.standbyCounter.value = 0
48                         else:
49                                 Notifications.AddNotification(Screens.Standby.Standby, StandbyCounterIncrease=False)
50                         self.keypress() #this ensures to unbind the keypress detection  
51
52         def keypress(self, key=None, flag=1):
53                 if flag:
54                         eActionMap.getInstance().unbindAction('', self.keypress)
55                         self.recordshutdowntimer.stop()
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.currentlyPlayingServiceOrGroup = 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                 oldref = self.currentlyPlayingServiceReference
75                 if ref and oldref and ref == oldref and not forceRestart:
76                         print "ignore request to play already running service(1)"
77                         return 0
78                 print "playing", ref and ref.toString()
79                 if ref is None:
80                         self.stopService()
81                         return 0
82                 InfoBarInstance = InfoBar.instance
83                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl=False, forceRestart=forceRestart)):
84                         if ref.flags & eServiceReference.isGroup:
85                                 if not oldref:
86                                         oldref = eServiceReference()
87                                 playref = getBestPlayableServiceReference(ref, oldref)
88                                 print "playref", playref
89                                 if playref and oldref and playref == oldref and not forceRestart:
90                                         print "ignore request to play already running service(2)"
91                                         return 0
92                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
93                                         self.stopService()
94                                         return 0
95                         else:
96                                 playref = ref
97                         if self.pnav:
98                                 self.pnav.stopService()
99                                 self.currentlyPlayingServiceReference = playref
100                                 self.currentlyPlayingServiceOrGroup = ref
101                                 if InfoBarInstance is not None:
102                                         InfoBarInstance.servicelist.servicelist.setCurrent(ref)
103                                 if self.pnav.playService(playref):
104                                         print "Failed to start", playref
105                                         self.currentlyPlayingServiceReference = None
106                                         self.currentlyPlayingServiceOrGroup = None
107                                 return 0
108                 elif oldref:
109                         InfoBarInstance.servicelist.servicelist.setCurrent(oldref)
110                 return 1
111
112         def getCurrentlyPlayingServiceReference(self):
113                 return self.currentlyPlayingServiceReference
114
115         def getCurrentlyPlayingServiceOrGroup(self):
116                 return self.currentlyPlayingServiceOrGroup
117
118         def recordService(self, ref, simulate=False):
119                 service = None
120                 if not simulate: print "recording service: %s" % (str(ref))
121                 if isinstance(ref, ServiceReference.ServiceReference):
122                         ref = ref.ref
123                 if ref:
124                         if ref.flags & eServiceReference.isGroup:
125                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
126                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
127                         if service is None:
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 self.pnav:
145                         self.pnav.stopService()
146                 self.currentlyPlayingServiceReference = None
147                 self.currentlyPlayingServiceOrGroup = None
148
149         def pause(self, p):
150                 return self.pnav and self.pnav.pause(p)
151
152         def shutdown(self):
153                 self.RecordTimer.shutdown()
154                 self.ServiceHandler = None
155                 self.pnav = None
156
157         def stopUserServices(self):
158                 self.stopService()