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