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