Simplify 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                 sleepTimer = config.usage.sleep_timer.value
47                 message = None
48                 if sleepTimer == "event_shutdown":
49                         sleepTimer = -self.currentEventTime()
50                 elif sleepTimer == "event_standby":
51                         sleepTimer = self.currentEventTime()
52                 else:
53                         sleepTimer = int(sleepTimer)
54                 if sleepTimer:
55                         if sleepTimer < 0:
56                                 message = _("And will shutdown your receiver over ")
57                         else:
58                                 message = _("And will put your receiver in standby over ")
59                         m = abs(sleepTimer / 60)
60                         message = _("The sleep timer has been activated.") + "\n" + message + ngettext("%d minute", "%d minutes", m) % m
61                         InfoBar.instance.setSleepTimer(sleepTimer)
62                 else:
63                         message = _("The sleep timer has been disabled.")
64                         InfoBar.instance.setSleepTimer(0)
65                 AddPopup(message, type = MessageBox.TYPE_INFO, timeout = 5)
66                 config.usage.sleep_timer.save()
67                 config.usage.inactivity_timer.save()
68                 self.close(True)
69
70         def cancel(self):
71                 self.close()
72
73         def getCurrentEntry(self):
74                 return self["config"].getCurrent()[0]
75
76         def getCurrentValue(self):
77                 return str(self["config"].getCurrent()[1].getText())
78
79         def getCurrentDescription(self):
80                 return self["config"].getCurrent() and len(self["config"].getCurrent()) > 2 and self["config"].getCurrent()[2] or ""
81
82         def createSummary(self):
83                 from Screens.Setup import SetupSummary
84                 return SetupSummary
85
86         def currentEventTime(self):
87                 remaining = 0
88                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
89                 if ref:
90                         path = ref.getPath()
91                         if path: # Movie
92                                 service = self.session.nav.getCurrentService()
93                                 seek = service and service.seek()
94                                 if seek:
95                                         length = seek.getLength()
96                                         position = seek.getPlayPosition()
97                                         if length and position:
98                                                 remaining = length[1] - position[1]
99                                                 if remaining > 0:
100                                                         remaining = remaining / 90000
101                         else: # DVB
102                                 epg = eEPGCache.getInstance()
103                                 event = epg.lookupEventTime(ref, -1, 0)
104                                 if event:
105                                         now = int(time())
106                                         start = event.getBeginTime()
107                                         duration = event.getDuration()
108                                         end = start + duration
109                                         remaining = end - now
110                 return remaining + config.recording.margin_after.value * 60