Navigation: When the reference is already playing it should be successfull
[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 or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
82                                         self.stopService()
83                                         return 0
84                         else:
85                                 playref = ref
86                         if self.pnav:
87                                 self.pnav.stopService()
88                                 self.currentlyPlayingServiceReference = playref
89                                 self.currentlyPlayingServiceOrGroup = ref
90                                 if InfoBarInstance and InfoBarInstance.servicelist.servicelist.setCurrent(ref, adjust):
91                                         self.currentlyPlayingServiceOrGroup = InfoBarInstance.servicelist.servicelist.getCurrent()
92                                 if self.pnav.playService(playref):
93                                         print "Failed to start", playref
94                                         self.currentlyPlayingServiceReference = None
95                                         self.currentlyPlayingServiceOrGroup = None
96                                 return 0
97                 elif oldref and InfoBarInstance and InfoBarInstance.servicelist.servicelist.setCurrent(oldref, adjust):
98                         self.currentlyPlayingServiceOrGroup = InfoBarInstance.servicelist.servicelist.getCurrent()
99                 return 1
100
101         def getCurrentlyPlayingServiceReference(self):
102                 return self.currentlyPlayingServiceReference
103
104         def getCurrentlyPlayingServiceOrGroup(self):
105                 return self.currentlyPlayingServiceOrGroup
106
107         def recordService(self, ref, simulate=False):
108                 service = None
109                 if not simulate: print "recording service: %s" % (str(ref))
110                 if isinstance(ref, ServiceReference.ServiceReference):
111                         ref = ref.ref
112                 if ref:
113                         if ref.flags & eServiceReference.isGroup:
114                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
115                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
116                         if service is None:
117                                 print "record returned non-zero"
118                 return service
119
120         def stopRecordService(self, service):
121                 ret = self.pnav and self.pnav.stopRecordService(service)
122                 return ret
123
124         def getRecordings(self, simulate=False):
125                 return self.pnav and self.pnav.getRecordings(simulate)
126
127         def getCurrentService(self):
128                 if not self.currentlyPlayingService:
129                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
130                 return self.currentlyPlayingService
131
132         def stopService(self):
133                 if self.pnav:
134                         self.pnav.stopService()
135                 self.currentlyPlayingServiceReference = None
136                 self.currentlyPlayingServiceOrGroup = None
137
138         def pause(self, p):
139                 return self.pnav and self.pnav.pause(p)
140
141         def shutdown(self):
142                 self.RecordTimer.shutdown()
143                 self.ServiceHandler = None
144                 self.pnav = None
145
146         def stopUserServices(self):
147                 self.stopService()