Merge branch 'master' of git.opendreambox.org:/git/enigma2
[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 enigma import eDVBVolumecontrol
7
8 inStandby = None
9
10 class Standby(Screen):
11         def Power(self):
12                 print "leave standby"
13                 #set input to encoder
14                 self.avswitch.setInput("ENCODER")
15                 #restart last played service
16                 #unmute adc
17                 self.leaveMute()
18                 #set brightness of lcd
19                 config.lcd.bright.apply()
20                 #kill me
21                 self.close(True)
22
23         def setMute(self):
24                 if (eDVBVolumecontrol.getInstance().isMuted()):
25                         self.wasMuted = 1
26                         print "mute already active"
27                 else:   
28                         self.wasMuted = 0
29                         eDVBVolumecontrol.getInstance().volumeToggleMute()
30
31         def leaveMute(self):
32                 if self.wasMuted == 0:
33                         eDVBVolumecontrol.getInstance().volumeToggleMute()
34
35         def __init__(self, session):
36                 Screen.__init__(self, session)
37                 self.avswitch = AVSwitch()
38
39                 print "enter standby"
40
41                 self["actions"] = ActionMap( [ "StandbyActions" ],
42                 {
43                         "power": self.Power
44                 }, -1)
45
46                 #mute adc
47                 self.setMute()
48
49                 self.paused_service = None
50                 self.prev_running_service = None
51                 if self.session.current_dialog:
52                         if self.session.current_dialog.ALLOW_SUSPEND == Screen.SUSPEND_STOPS:
53                                 #get currently playing service reference
54                                 self.prev_running_service = self.session.nav.getCurrentlyPlayingServiceReference()
55                                 #stop actual played dvb-service
56                                 self.session.nav.stopService()
57                         elif self.session.current_dialog.ALLOW_SUSPEND == Screen.SUSPEND_PAUSES:
58                                 self.paused_service = self.session.current_dialog
59                                 self.paused_service.pauseService()
60
61                 #set input to vcr scart
62                 if SystemInfo["ScartSwitch"]:
63                         self.avswitch.setInput("SCART")
64                 else:
65                         self.avswitch.setInput("AUX")
66                 #set lcd brightness to standby value
67                 config.lcd.standby.apply()
68                 self.onShow.append(self.__onShow)
69                 self.onHide.append(self.__onHide)
70                 self.onClose.append(self.__onClose)
71
72         def __onClose(self):
73                 if self.prev_running_service:
74                         self.session.nav.playService(self.prev_running_service)
75                 elif self.paused_service:
76                         self.paused_service.unPauseService()
77
78         def createSummary(self):
79                 return StandbySummary
80
81         def __onShow(self):
82                 global inStandby
83                 inStandby = self
84                 self.session.screen["Standby"].boolean = True
85
86         def __onHide(self):
87                 global inStandby
88                 inStandby = None
89                 self.session.screen["Standby"].boolean = False
90
91 class StandbySummary(Screen):
92         skin = """
93         <screen position="0,0" size="132,64">
94                 <widget source="global.CurrentTime" render="Label" position="0,0" size="132,64" font="Regular;40" halign="center">
95                         <convert type="ClockToText" />
96                 </widget>
97                 <widget source="session.RecordState" render="FixedLabel" text=" " position="0,0" size="132,64" zPosition="1" >
98                         <convert type="ConfigEntryTest">config.usage.blinking_display_clock_during_recording,True,CheckSourceBoolean</convert>
99                         <convert type="ConditionalShowHide">Blink</convert>
100                 </widget>
101         </screen>"""
102
103 from enigma import quitMainloop, iRecordableService
104 from Screens.MessageBox import MessageBox
105 from time import time
106 from Components.Task import job_manager
107
108 inTryQuitMainloop = False
109
110 class TryQuitMainloop(MessageBox):
111         def __init__(self, session, retvalue=1, timeout=-1, default_yes = True):
112                 self.retval=retvalue
113                 recordings = session.nav.getRecordings()
114                 jobs = len(job_manager.getPendingJobs())
115                 self.connected = False
116                 reason = ""
117                 next_rec_time = -1
118                 if not recordings:
119                         next_rec_time = session.nav.RecordTimer.getNextRecordingTime()  
120                 if recordings or (next_rec_time > 0 and (next_rec_time - time()) < 360):
121                         reason = _("Recording(s) are in progress or coming up in few seconds!") + '\n'
122                 if jobs:
123                         if jobs == 1:
124                                 job = job_manager.getPendingJobs()[0]
125                                 reason += "%s: %s (%d%%)\n" % (job.getStatustext(), job.name, int(100*job.progress/float(job.end)))
126                         else:
127                                 reason += (_("%d jobs are running in the background!") % jobs) + '\n'
128                 if reason:
129                         if retvalue == 1:
130                                 MessageBox.__init__(self, session, reason+_("Really shutdown now?"), type = MessageBox.TYPE_YESNO, timeout = timeout, default = default_yes)
131                         elif retvalue == 2:
132                                 MessageBox.__init__(self, session, reason+_("Really reboot now?"), type = MessageBox.TYPE_YESNO, timeout = timeout, default = default_yes)
133                         elif retvalue == 4:
134                                 pass
135                         else:
136                                 MessageBox.__init__(self, session, reason+_("Really restart now?"), type = MessageBox.TYPE_YESNO, timeout = timeout, default = default_yes)
137                         self.skinName = "MessageBox"
138                         session.nav.record_event.append(self.getRecordEvent)
139                         self.connected = True
140                         self.onShow.append(self.__onShow)
141                         self.onHide.append(self.__onHide)
142                 else:
143                         self.skin = """<screen position="0,0" size="0,0"/>"""
144                         Screen.__init__(self, session)
145                         self.close(True)
146
147         def getRecordEvent(self, recservice, event):
148                 if event == iRecordableService.evEnd:
149                         recordings = self.session.nav.getRecordings()
150                         if not recordings: # no more recordings exist
151                                 rec_time = self.session.nav.RecordTimer.getNextRecordingTime()
152                                 if rec_time > 0 and (rec_time - time()) < 360:
153                                         self.initTimeout(360) # wait for next starting timer
154                                         self.startTimer()
155                                 else:
156                                         self.close(True) # immediate shutdown
157                 elif event == iRecordableService.evStart:
158                         self.stopTimer()
159
160         def close(self, value):
161                 if self.connected:
162                         self.conntected=False
163                         self.session.nav.record_event.remove(self.getRecordEvent)
164                 if value:
165                         # hack .. we dont like to show any other screens when this screen has closed
166                         self.onClose = [self.__closed]
167                         self.session.dialog_stack = []
168                         self.session.summary_stack = [None]
169                 MessageBox.close(self, True)
170
171         def __closed(self):
172                 quitMainloop(self.retval)
173
174         def __onShow(self):
175                 global inTryQuitMainloop
176                 inTryQuitMainloop = True
177
178         def __onHide(self):
179                 global inTryQuitMainloop
180                 inTryQuitMainloop = False