Add config option to startup enigma2 in standby mode
[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                         RecordTimer.RecordTimerEntry.setWasInDeepStandby()
39                 if config.usage.startup_to_standby.value:
40                         Notifications.AddNotification(Screens.Standby.Standby)  
41
42         def wasTimerWakeup(self):
43                 return self.__wasTimerWakeup
44
45         def dispatchEvent(self, i):
46                 for x in self.event:
47                         x(i)
48                 if i == iPlayableService.evEnd:
49                         self.currentlyPlayingServiceReference = None
50                         self.currentlyPlayingServiceOrGroup = None
51                         self.currentlyPlayingService = None
52
53         def dispatchRecordEvent(self, rec_service, event):
54 #               print "record_event", rec_service, event
55                 for x in self.record_event:
56                         x(rec_service, event)
57
58         def playService(self, ref, checkParentalControl=True, forceRestart=False, adjust=True):
59                 oldref = self.currentlyPlayingServiceOrGroup
60                 if ref and oldref and ref == oldref and not forceRestart:
61                         print "ignore request to play already running service(1)"
62                         return 0
63                 print "playing", ref and ref.toString()
64                 if ref is None:
65                         self.stopService()
66                         return 0
67                 from Components.ServiceEventTracker import InfoBarCount
68                 InfoBarInstance = InfoBarCount == 1 and InfoBar.instance
69                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl=False, forceRestart=forceRestart, adjust=adjust)):
70                         if ref.flags & eServiceReference.isGroup:
71                                 oldref = self.currentlyPlayingServiceReference or eServiceReference()
72                                 playref = getBestPlayableServiceReference(ref, oldref)
73                                 print "playref", playref
74                                 if playref and oldref and playref == oldref and not forceRestart:
75                                         print "ignore request to play already running service(2)"
76                                         return 0
77                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
78                                         self.stopService()
79                                         return 0
80                         else:
81                                 playref = ref
82                         if self.pnav:
83                                 self.pnav.stopService()
84                                 self.currentlyPlayingServiceReference = playref
85                                 self.currentlyPlayingServiceOrGroup = ref
86                                 if InfoBarInstance and InfoBarInstance.servicelist.servicelist.setCurrent(ref, adjust):
87                                         self.currentlyPlayingServiceOrGroup = InfoBarInstance.servicelist.servicelist.getCurrent()
88                                 if self.pnav.playService(playref):
89                                         print "Failed to start", playref
90                                         self.currentlyPlayingServiceReference = None
91                                         self.currentlyPlayingServiceOrGroup = None
92                                 return 0
93                 elif oldref and InfoBarInstance and InfoBarInstance.servicelist.servicelist.setCurrent(oldref, adjust):
94                         self.currentlyPlayingServiceOrGroup = InfoBarInstance.servicelist.servicelist.getCurrent()
95                 return 1
96
97         def getCurrentlyPlayingServiceReference(self):
98                 return self.currentlyPlayingServiceReference
99
100         def getCurrentlyPlayingServiceOrGroup(self):
101                 return self.currentlyPlayingServiceOrGroup
102
103         def recordService(self, ref, simulate=False):
104                 service = None
105                 if not simulate: print "recording service: %s" % (str(ref))
106                 if isinstance(ref, ServiceReference.ServiceReference):
107                         ref = ref.ref
108                 if ref:
109                         if ref.flags & eServiceReference.isGroup:
110                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
111                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
112                         if service is None:
113                                 print "record returned non-zero"
114                 return service
115
116         def stopRecordService(self, service):
117                 ret = self.pnav and self.pnav.stopRecordService(service)
118                 return ret
119
120         def getRecordings(self, simulate=False):
121                 return self.pnav and self.pnav.getRecordings(simulate)
122
123         def getCurrentService(self):
124                 if not self.currentlyPlayingService:
125                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
126                 return self.currentlyPlayingService
127
128         def stopService(self):
129                 if self.pnav:
130                         self.pnav.stopService()
131                 self.currentlyPlayingServiceReference = None
132                 self.currentlyPlayingServiceOrGroup = None
133
134         def pause(self, p):
135                 return self.pnav and self.pnav.pause(p)
136
137         def shutdown(self):
138                 self.RecordTimer.shutdown()
139                 self.ServiceHandler = None
140                 self.pnav = None
141
142         def stopUserServices(self):
143                 self.stopService()