Only start/stop sleeptimer when the focus is on the sleeptimer
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / SleepTimerEdit.py
1 from Screens.InfoBar import InfoBar
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import ActionMap
5 from Components.ConfigList import ConfigListScreen
6 from Components.Label import Label
7 from Components.Sources.StaticText import StaticText
8 from Components.config import config, getConfigListEntry
9 from Tools.Notifications import AddPopup
10 from enigma import eEPGCache
11 from time import time
12
13 class SleepTimerEdit(ConfigListScreen, Screen):
14         def __init__(self, session):
15                 Screen.__init__(self, session)
16                 self.skinName = ["SleepTimerSetup", "Setup" ]
17                 self.setup_title = _("SleepTimer Configuration")
18                 self.onChangedEntry = [ ]
19
20                 self["key_red"] = StaticText(_("Cancel"))
21                 self["key_green"] = StaticText(_("Save"))
22                 self["description"] = Label("")
23
24                 self.list = []
25                 self.list.append(getConfigListEntry(_("Sleeptimer"),
26                         config.usage.sleep_timer,
27                         _("Configure the duration in minutes and action (shut down or standby) for the sleeptimer.")))
28                 self.list.append(getConfigListEntry(_("Action when receiver is not controlled"),
29                         config.usage.inactivity_timer,
30                         _("Configure the duration in hours and action (shut down or standby) when the receiver is not controlled.")))
31
32                 ConfigListScreen.__init__(self, self.list, session = session)
33                 
34                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
35                 {
36                     "green": self.ok,
37                     "red": self.cancel,
38                     "cancel": self.cancel,
39                     "ok": self.ok,
40                 }, -2)
41
42         def layoutFinished(self):
43                 self.setTitle(self.setup_title)
44
45         def ok(self):
46                 config.usage.sleep_timer.save()
47                 config.usage.inactivity_timer.save()
48                 if self.getCurrentEntry() == _("Sleeptimer"):
49                         sleepTimer = config.usage.sleep_timer.value
50                         message = None
51                         if sleepTimer == "event_shutdown":
52                                 sleepTimer = -self.currentEventTime()
53                         elif sleepTimer == "event_standby":
54                                 sleepTimer = self.currentEventTime()
55                         else:
56                                 sleepTimer = int(sleepTimer)
57                         if sleepTimer:
58                                 if sleepTimer < 0:
59                                         message = _("And will shutdown your receiver over ")
60                                 else:
61                                         message = _("And will put your receiver in standby over ")
62                                 m = abs(sleepTimer / 60)
63                                 message = _("The sleep timer has been activated.") + "\n" + message + ngettext("%d minute", "%d minutes", m) % m
64                                 InfoBar.instance.setSleepTimer(sleepTimer)
65                         else:
66                                 message = _("The sleep timer has been disabled.")
67                                 InfoBar.instance.setSleepTimer(0)
68                         AddPopup(message, type = MessageBox.TYPE_INFO, timeout = 5)
69                         self.close(True)
70                 self.close()
71
72         def cancel(self):
73                 self.close()
74
75         def getCurrentEntry(self):
76                 return self["config"].getCurrent()[0]
77
78         def getCurrentValue(self):
79                 return str(self["config"].getCurrent()[1].getText())
80
81         def getCurrentDescription(self):
82                 return self["config"].getCurrent() and len(self["config"].getCurrent()) > 2 and self["config"].getCurrent()[2] or ""
83
84         def createSummary(self):
85                 from Screens.Setup import SetupSummary
86                 return SetupSummary
87
88         def currentEventTime(self):
89                 remaining = 0
90                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
91                 if ref:
92                         path = ref.getPath()
93                         if path: # Movie
94                                 service = self.session.nav.getCurrentService()
95                                 seek = service and service.seek()
96                                 if seek:
97                                         length = seek.getLength()
98                                         position = seek.getPlayPosition()
99                                         if length and position:
100                                                 remaining = length[1] - position[1]
101                                                 if remaining > 0:
102                                                         remaining = remaining / 90000
103                         else: # DVB
104                                 epg = eEPGCache.getInstance()
105                                 event = epg.lookupEventTime(ref, -1, 0)
106                                 if event:
107                                         now = int(time())
108                                         start = event.getBeginTime()
109                                         duration = event.getDuration()
110                                         end = start + duration
111                                         remaining = end - now
112                 return remaining + config.recording.margin_after.value * 60