Streamline behavior for recordings coming from deep standby
[openblackhole/openblackhole-enigma2.git] / Navigation.py
1 from enigma import eServiceCenter, eServiceReference, eTimer, pNavigation, getBestPlayableServiceReference, iPlayableService
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
7 import RecordTimer
8 import Screens.Standby
9 import NavigationInstance
10 import ServiceReference
11 from Screens.InfoBar import InfoBar
12
13 # TODO: remove pNavgation, eNavigation and rewrite this stuff in python.
14 class Navigation:
15         def __init__(self):
16                 if NavigationInstance.instance is not None:
17                         raise NavigationInstance.instance
18
19                 NavigationInstance.instance = self
20                 self.ServiceHandler = eServiceCenter.getInstance()
21
22                 import Navigation as Nav
23                 Nav.navcore = self
24
25                 self.pnav = pNavigation()
26                 self.pnav.m_event.get().append(self.dispatchEvent)
27                 self.pnav.m_record_event.get().append(self.dispatchRecordEvent)
28                 self.event = [ ]
29                 self.record_event = [ ]
30                 self.currentlyPlayingServiceReference = None
31                 self.currentlyPlayingServiceOrGroup = None
32                 self.currentlyPlayingService = None
33                 self.RecordTimer = RecordTimer.RecordTimer()
34                 self.__wasTimerWakeup = getFPWasTimerWakeup()
35                 if self.__wasTimerWakeup:
36                         Notifications.AddNotification(Screens.Standby.Standby)
37
38         def wasTimerWakeup(self):
39                 return self.__wasTimerWakeup
40
41         def dispatchEvent(self, i):
42                 for x in self.event:
43                         x(i)
44                 if i == iPlayableService.evEnd:
45                         self.currentlyPlayingServiceReference = None
46                         self.currentlyPlayingServiceOrGroup = None
47                         self.currentlyPlayingService = None
48
49         def dispatchRecordEvent(self, rec_service, event):
50 #               print "record_event", rec_service, event
51                 for x in self.record_event:
52                         x(rec_service, event)
53
54         def playService(self, ref, checkParentalControl=True, forceRestart=False):
55                 oldref = self.currentlyPlayingServiceReference
56                 if ref and oldref and ref == oldref and not forceRestart:
57                         print "ignore request to play already running service(1)"
58                         return 0
59                 print "playing", ref and ref.toString()
60                 if ref is None:
61                         self.stopService()
62                         return 0
63                 InfoBarInstance = InfoBar.instance
64                 if not checkParentalControl or parentalControl.isServicePlayable(ref, boundFunction(self.playService, checkParentalControl=False, forceRestart=forceRestart)):
65                         if ref.flags & eServiceReference.isGroup:
66                                 if not oldref:
67                                         oldref = eServiceReference()
68                                 playref = getBestPlayableServiceReference(ref, oldref)
69                                 print "playref", playref
70                                 if playref and oldref and playref == oldref and not forceRestart:
71                                         print "ignore request to play already running service(2)"
72                                         return 0
73                                 if not playref or (checkParentalControl and not parentalControl.isServicePlayable(playref, boundFunction(self.playService, checkParentalControl = False))):
74                                         self.stopService()
75                                         return 0
76                         else:
77                                 playref = ref
78                         if self.pnav:
79                                 self.pnav.stopService()
80                                 self.currentlyPlayingServiceReference = playref
81                                 self.currentlyPlayingServiceOrGroup = ref
82                                 if InfoBarInstance is not None:
83                                         InfoBarInstance.servicelist.servicelist.setCurrent(ref)
84                                 if self.pnav.playService(playref):
85                                         print "Failed to start", playref
86                                         self.currentlyPlayingServiceReference = None
87                                         self.currentlyPlayingServiceOrGroup = None
88                                 return 0
89                 elif oldref:
90                         InfoBarInstance.servicelist.servicelist.setCurrent(oldref)
91                 return 1
92
93         def getCurrentlyPlayingServiceReference(self):
94                 return self.currentlyPlayingServiceReference
95
96         def getCurrentlyPlayingServiceOrGroup(self):
97                 return self.currentlyPlayingServiceOrGroup
98
99         def recordService(self, ref, simulate=False):
100                 service = None
101                 if not simulate: print "recording service: %s" % (str(ref))
102                 if isinstance(ref, ServiceReference.ServiceReference):
103                         ref = ref.ref
104                 if ref:
105                         if ref.flags & eServiceReference.isGroup:
106                                 ref = getBestPlayableServiceReference(ref, eServiceReference(), simulate)
107                         service = ref and self.pnav and self.pnav.recordService(ref, simulate)
108                         if service is None:
109                                 print "record returned non-zero"
110                 return service
111
112         def stopRecordService(self, service):
113                 ret = self.pnav and self.pnav.stopRecordService(service)
114                 return ret
115
116         def getRecordings(self, simulate=False):
117                 return self.pnav and self.pnav.getRecordings(simulate)
118
119         def getCurrentService(self):
120                 if not self.currentlyPlayingService:
121                         self.currentlyPlayingService = self.pnav and self.pnav.getCurrentService()
122                 return self.currentlyPlayingService
123
124         def stopService(self):
125                 if self.pnav:
126                         self.pnav.stopService()
127                 self.currentlyPlayingServiceReference = None
128                 self.currentlyPlayingServiceOrGroup = None
129
130         def pause(self, p):
131                 return self.pnav and self.pnav.pause(p)
132
133         def shutdown(self):
134                 self.RecordTimer.shutdown()
135                 self.ServiceHandler = None
136                 self.pnav = None
137
138         def stopUserServices(self):
139                 self.stopService()