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