e96760dce466c40ba92a2fed3bdf27ce18b8d364
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / Standby.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.config import config
4 from Components.AVSwitch import AVSwitch
5 from Components.Harddisk import internalHDDNotSleeping
6 from Components.SystemInfo import SystemInfo
7 from Tools import Notifications
8 from GlobalActions import globalActionMap
9 import RecordTimer
10 from enigma import eDVBVolumecontrol, eTimer, eDVBLocalTimeHandler, eServiceReference
11 from time import time, localtime
12
13 inStandby = None
14
15 class Standby(Screen):
16         def Power(self):
17                 print "leave standby"
18                 #set input to encoder
19                 self.avswitch.setInput("ENCODER")
20                 #restart last played service
21                 #unmute adc
22                 self.leaveMute()
23                 #kill me
24                 self.close(True)
25
26         def setMute(self):
27                 if (eDVBVolumecontrol.getInstance().isMuted()):
28                         self.wasMuted = 1
29                         print "mute already active"
30                 else:
31                         self.wasMuted = 0
32                         eDVBVolumecontrol.getInstance().volumeToggleMute()
33
34         def leaveMute(self):
35                 if self.wasMuted == 0:
36                         eDVBVolumecontrol.getInstance().volumeToggleMute()
37
38         def __init__(self, session, StandbyCounterIncrease=True):
39                 Screen.__init__(self, session)
40                 self.avswitch = AVSwitch()
41
42                 print "enter standby"
43
44                 self["actions"] = ActionMap( [ "StandbyActions" ],
45                 {
46                         "power": self.Power,
47                         "discrete_on": self.Power
48                 }, -1)
49
50                 globalActionMap.setEnabled(False)
51
52                 from Screens.InfoBar import InfoBar
53                 self.infoBarInstance = InfoBar.instance
54                 self.StandbyCounterIncrease = StandbyCounterIncrease
55                 self.standbyTimeoutTimer = eTimer()
56                 self.standbyTimeoutTimer.callback.append(self.standbyTimeout)
57                 self.standbyStopServiceTimer = eTimer()
58                 self.standbyStopServiceTimer.callback.append(self.stopService)
59                 self.timeHandler = None
60
61                 #mute adc
62                 self.setMute()
63
64                 self.paused_service = None
65                 self.prev_running_service = None
66
67                 self.prev_running_service = self.session.nav.getCurrentlyPlayingServiceOrGroup()
68                 service = self.prev_running_service and self.prev_running_service.toString()
69                 if service:
70                         if service.rsplit(":", 1)[1].startswith("/"):
71                                 self.paused_service = True
72                                 self.infoBarInstance.pauseService()
73                         else:
74                                 self.timeHandler =  eDVBLocalTimeHandler.getInstance()
75                                 if self.timeHandler.ready():
76                                         if self.session.nav.getCurrentlyPlayingServiceOrGroup():
77                                                 self.stopService()
78                                         else:
79                                                 self.standbyStopServiceTimer.startLongTimer(5)
80                                         self.timeHandler = None
81                                 else:
82                                         self.timeHandler.m_timeUpdated.get().append(self.stopService)
83
84                 if self.session.pipshown:
85                         self.infoBarInstance and hasattr(self.infoBarInstance, "showPiP") and self.infoBarInstance.showPiP()
86
87                 #set input to vcr scart
88                 if SystemInfo["ScartSwitch"]:
89                         self.avswitch.setInput("SCART")
90                 else:
91                         self.avswitch.setInput("AUX")
92
93                 gotoShutdownTime = int(config.usage.standby_to_shutdown_timer.value)
94                 if gotoShutdownTime:
95                         self.standbyTimeoutTimer.startLongTimer(gotoShutdownTime)
96
97                 self.onFirstExecBegin.append(self.__onFirstExecBegin)
98                 self.onClose.append(self.__onClose)
99
100         def __onClose(self):
101                 global inStandby
102                 inStandby = None
103                 self.standbyTimeoutTimer.stop()
104                 self.standbyStopServiceTimer.stop()
105                 self.timeHandler and self.timeHandler.m_timeUpdated.get().remove(self.stopService)
106                 if self.paused_service:
107                         self.infoBarInstance.unPauseService()
108                 elif self.prev_running_service:
109                         service = self.prev_running_service.toString()
110                         if config.servicelist.startupservice_onstandby.value:
111                                 self.session.nav.playService(eServiceReference(config.servicelist.startupservice.value))
112                                 from Screens.InfoBar import InfoBar
113                                 InfoBar.instance and InfoBar.instance.servicelist.correctChannelNumber()
114                         else:
115                                 self.session.nav.playService(self.prev_running_service)
116                 self.session.screen["Standby"].boolean = False
117                 globalActionMap.setEnabled(True)
118                 if RecordTimer.RecordTimerEntry.receiveRecordEvents:
119                         RecordTimer.RecordTimerEntry.stopTryQuitMainloop()
120
121         def __onFirstExecBegin(self):
122                 global inStandby
123                 inStandby = self
124                 self.session.screen["Standby"].boolean = True
125                 if self.StandbyCounterIncrease:
126                         config.misc.standbyCounter.value += 1
127
128         def stopService(self):
129                 self.session.nav.stopService()
130
131         def createSummary(self):
132                 return StandbySummary
133
134         def standbyTimeout(self):
135                 if config.usage.standby_to_shutdown_timer_blocktime.value:
136                         curtime = localtime(time())
137                         if curtime.tm_year > 1970: #check if the current time is valid
138                                 curtime = (curtime.tm_hour, curtime.tm_min, curtime.tm_sec)
139                                 begintime = tuple(config.usage.standby_to_shutdown_timer_blocktime_begin.value)
140                                 endtime = tuple(config.usage.standby_to_shutdown_timer_blocktime_end.value)
141                                 if begintime <= endtime and (curtime >= begintime and curtime < endtime) or begintime > endtime and (curtime >= begintime or curtime < endtime):
142                                         duration = (endtime[0]*3600 + endtime[1]*60) - (curtime[0]*3600 + curtime[1]*60 + curtime[2])
143                                         if duration:
144                                                 if duration < 0:
145                                                         duration += 24*3600
146                                                 self.standbyTimeoutTimer.startLongTimer(duration)
147                                                 return
148                 if self.session.screen["TunerInfo"].tuner_use_mask or internalHDDNotSleeping():
149                         self.standbyTimeoutTimer.startLongTimer(600)
150                 else:
151                         from RecordTimer import RecordTimerEntry
152                         RecordTimerEntry.TryQuitMainloop()
153
154 class StandbySummary(Screen):
155         skin = """
156         <screen position="0,0" size="132,64">
157                 <widget source="global.CurrentTime" render="Label" position="0,0" size="132,64" font="Regular;40" halign="center">
158                         <convert type="ClockToText" />
159                 </widget>
160                 <widget source="session.RecordState" render="FixedLabel" text=" " position="0,0" size="132,64" zPosition="1" >
161                         <convert type="ConfigEntryTest">config.usage.blinking_display_clock_during_recording,True,CheckSourceBoolean</convert>
162                         <convert type="ConditionalShowHide">Blink</convert>
163                 </widget>
164         </screen>"""
165
166 from enigma import quitMainloop, iRecordableService
167 from Screens.MessageBox import MessageBox
168 from time import time
169 from Components.Task import job_manager
170
171 class QuitMainloopScreen(Screen):
172
173         def __init__(self, session, retvalue=1):
174                 self.skin = """<screen name="QuitMainloopScreen" position="fill" flags="wfNoBorder">
175                                 <ePixmap pixmap="skin_default/icons/input_info.png" position="c-27,c-60" size="53,53" alphatest="on" />
176                                 <widget name="text" position="center,c+5" size="720,100" font="Regular;22" halign="center" />
177                         </screen>"""
178                 Screen.__init__(self, session)
179                 from Components.Label import Label
180                 text = { 1: _("Your receiver is shutting down"),
181                         2: _("Your receiver is rebooting"),
182                         3: _("The user interface of your receiver is restarting"),
183                         4: _("Your frontprocessor will be upgraded\nPlease wait until your receiver reboots\nThis may take a few minutes"),
184                         5: _("The user interface of your receiver is restarting\ndue to an error in mytest.py"),
185                         42: _("Unattended upgrade in progress\nPlease wait until your receiver reboots\nThis may take a few minutes") }.get(retvalue)
186                 self["text"] = Label(text)
187
188 inTryQuitMainloop = False
189
190 class TryQuitMainloop(MessageBox):
191         def __init__(self, session, retvalue=1, timeout=-1, default_yes = False):
192                 self.retval = retvalue
193                 recordings = session.nav.getRecordings()
194                 jobs = len(job_manager.getPendingJobs())
195                 self.connected = False
196                 reason = ""
197                 next_rec_time = -1
198                 if not recordings:
199                         next_rec_time = session.nav.RecordTimer.getNextRecordingTime()
200                 if recordings or (next_rec_time > 0 and (next_rec_time - time()) < 360):
201                         reason = _("Recording(s) are in progress or coming up in few seconds!") + '\n'
202                 if jobs:
203                         if jobs == 1:
204                                 job = job_manager.getPendingJobs()[0]
205                                 reason += "%s: %s (%d%%)\n" % (job.getStatustext(), job.name, int(100*job.progress/float(job.end)))
206                         else:
207                                 reason += (ngettext("%d job is running in the background!", "%d jobs are running in the background!", jobs) % jobs) + '\n'
208                 if reason:
209                         text = { 1: _("Really shutdown now?"),
210                                 2: _("Really reboot now?"),
211                                 3: _("Really restart now?"),
212                                 4: _("Really upgrade the frontprocessor and reboot now?"),
213                                 42: _("Really upgrade your settop box and reboot now?") }.get(retvalue)
214                         if text:
215                                 MessageBox.__init__(self, session, reason+text, type = MessageBox.TYPE_YESNO, timeout = timeout, default = default_yes)
216                                 self.skinName = "MessageBoxSimple"
217                                 session.nav.record_event.append(self.getRecordEvent)
218                                 self.connected = True
219                                 self.onShow.append(self.__onShow)
220                                 self.onHide.append(self.__onHide)
221                                 return
222                 self.skin = """<screen position="0,0" size="0,0"/>"""
223                 Screen.__init__(self, session)
224                 self.close(True)
225
226         def getRecordEvent(self, recservice, event):
227                 if event == iRecordableService.evEnd:
228                         recordings = self.session.nav.getRecordings()
229                         if not recordings: # no more recordings exist
230                                 rec_time = self.session.nav.RecordTimer.getNextRecordingTime()
231                                 if rec_time > 0 and (rec_time - time()) < 360:
232                                         self.initTimeout(360) # wait for next starting timer
233                                         self.startTimer()
234                                 else:
235                                         self.close(True) # immediate shutdown
236                 elif event == iRecordableService.evStart:
237                         self.stopTimer()
238
239         def close(self, value):
240                 if self.connected:
241                         self.conntected=False
242                         self.session.nav.record_event.remove(self.getRecordEvent)
243                 if value:
244                         self.hide()
245                         if self.retval == 1:
246                                 config.misc.DeepStandby.value = True
247                         elif not inStandby:
248                                 config.misc.RestartUI.value = True
249                                 config.misc.RestartUI.save()
250                         self.session.nav.stopService()
251                         self.quitScreen = self.session.instantiateDialog(QuitMainloopScreen,retvalue=self.retval)
252                         self.quitScreen.show()
253                         quitMainloop(self.retval)
254                 else:
255                         MessageBox.close(self, True)
256
257         def __onShow(self):
258                 global inTryQuitMainloop
259                 inTryQuitMainloop = True
260
261         def __onHide(self):
262                 global inTryQuitMainloop
263                 inTryQuitMainloop = False