Re-allow go back to standby for zap timers
[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 Tools.BoundFunction import boundFunction
4 from Tools.StbHardware import setFPWakeuptime, getFPWakeuptime, getFPWasTimerWakeup
5 from Tools import Notifications
6 from time import time, localtime
7 import RecordTimer
8 import Screens.Standby
9 import NavigationInstance
10 import ServiceReference
11 from Screens.InfoBar import InfoBar
12 from sys import maxint
13
14 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
15 class Navigation:
16         def __init__(self):
17                 if NavigationInstance.instance is not None:
18                         raise NavigationInstance.instance
19
20                 NavigationInstance.instance = self
21                 self.ServiceHandler = eServiceCenter.getInstance()
22
23                 import Navigation as Nav
24                 Nav.navcore = self
25
26                 self.pnav = pNavigation()
27                 self.pnav.m_event.get().append(self.dispatchEvent)
28                 self.pnav.m_record_event.get().append(self.dispatchRecordEvent)
29                 self.event = [ ]
30                 self.record_event = [ ]
31                 self.currentlyPlayingServiceReference = None
32                 self.currentlyPlayingServiceOrGroup = None
33                 self.currentlyPlayingService = None
34                 self.RecordTimer = RecordTimer.RecordTimer()
35                 self.__wasTimerWakeup = getFPWasTimerWakeup()
36                 if self.__wasTimerWakeup:
37                         # We need to give the systemclock the chance to sync with the transponder time
38                         self.recordshutdowntimer = eTimer()
39                         self.recordshutdowntimer.callback.append(self.checkShutdownAfterRecording)
40                         self.recordshutdowntimer.startLongTimer(30)
41                         eActionMap.getInstance().bindAction('', -maxint - 1, self.keypress)
42
43         def checkShutdownAfterRecording(self):
44                 if len(self.getRecordings()) or abs(self.RecordTimer.getNextTimerTime() - time()) <= 360:
45                         if Screens.Standby.inStandby: #In case some plugin did put the receiver already in standby
46                                 config.misc.standbyCounter.value = 0
47                         else:
48                                 Notifications.AddNotification(Screens.Standby.Standby, StandbyCounterIncrease=False)
49                         self.keypress() #this ensures to unbind the keypress detection  
50
51         def keypress(self, key=None, flag=1):
52                 if flag:
53                         eActionMap.getInstance().unbindAction('', self.keypress)
54                         self.recordshutdowntimer.stop()
55
56         def wasTimerWakeup(self):
57                 return self.__wasTimerWakeup
58
59         def dispatchEvent(self, i):
60                 for x in self.event:
61                         x(i)
62                 if i == iPlayableService.evEnd:
63                         self.currentlyPlayingServiceReference = None
64                         self.currentlyPlayingServiceOrGroup = 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                 oldref = self.currentlyPlayingServiceReference
74                 if ref and oldref and ref == oldref and not forceRestart:
75                         print "ignore request to play already running service(1)"
76                         return 0
77                 print "playing", ref and ref.toString()
78                 if ref is None:
79                         self.stopService()
80                         return 0
81                 InfoBarInstance = InfoBar.instance
82                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl=False, forceRestart=forceRestart)):
83                         if ref.flags & eServiceReference.isGroup:
84                                 if not oldref:
85                                         oldref = eServiceReference()
86                                 playref = getBestPlayableServiceReference(ref, oldref)
87                                 print "playref", playref
88                                 if playref and oldref and playref == oldref and not forceRestart:
89                                         print "ignore request to play already running service(2)"
90                                         return 0
91                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
92                                         self.stopService()
93                                         return 0
94                         else:
95                                 playref = ref
96                         if self.pnav:
97                                 self.pnav.stopService()
98                                 self.currentlyPlayingServiceReference = playref
99                                 self.currentlyPlayingServiceOrGroup = ref
100                                 if InfoBarInstance is not None:
101                                         InfoBarInstance.servicelist.servicelist.setCurrent(ref)
102                                 if self.pnav.playService(playref):
103                                         print "Failed to start", playref
104                                         self.currentlyPlayingServiceReference = None
105                                         self.currentlyPlayingServiceOrGroup = None
106                                 return 0
107                 elif oldref:
108                         InfoBarInstance.servicelist.servicelist.setCurrent(oldref)
109                 return 1
110
111         def getCurrentlyPlayingServiceReference(self):
112                 return self.currentlyPlayingServiceReference
113
114         def getCurrentlyPlayingServiceOrGroup(self):
115                 return self.currentlyPlayingServiceOrGroup
116
117         def recordService(self, ref, simulate=False):
118                 service = None
119                 if not simulate: print "recording service: %s" % (str(ref))
120                 if isinstance(ref, ServiceReference.ServiceReference):
121                         ref = ref.ref
122                 if ref:
123                         if ref.flags & eServiceReference.isGroup:
124                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
125                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
126                         if service is None:
127                                 print "record returned non-zero"
128                 return service
129
130         def stopRecordService(self, service):
131                 ret = self.pnav and self.pnav.stopRecordService(service)
132                 return ret
133
134         def getRecordings(self, simulate=False):
135                 return self.pnav and self.pnav.getRecordings(simulate)
136
137         def getCurrentService(self):
138                 if not self.currentlyPlayingService:
139                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
140                 return self.currentlyPlayingService
141
142         def stopService(self):
143                 if self.pnav:
144                         self.pnav.stopService()
145                 self.currentlyPlayingServiceReference = None
146                 self.currentlyPlayingServiceOrGroup = None
147
148         def pause(self, p):
149                 return self.pnav and self.pnav.pause(p)
150
151         def shutdown(self):
152                 self.RecordTimer.shutdown()
153                 self.ServiceHandler = None
154                 self.pnav = None
155
156         def stopUserServices(self):
157                 self.stopService()