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