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