SleepTimerEdit: indicate if sleeptimer is active or not
[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 enigma import eEPGCache
10 from time import time
11
12 class SleepTimerEdit(ConfigListScreen, Screen):
13         def __init__(self, session):
14                 Screen.__init__(self, session)
15                 self.skinName = ["SleepTimerSetup", "Setup"]
16                 self.setup_title = _("SleepTimer Configuration")
17
18                 self["key_red"] = StaticText(_("Cancel"))
19                 self["key_green"] = StaticText(_("Save"))
20                 self["description"] = Label("")
21
22                 self.list = []
23                 ConfigListScreen.__init__(self, self.list, session = session)
24                 self.createSetup()
25
26                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
27                 {
28                     "green": self.ok,
29                     "red": self.cancel,
30                     "cancel": self.cancel,
31                     "ok": self.ok,
32                 }, -2)
33
34                 self.onLayoutFinish.append(self.layoutFinished)
35
36         def layoutFinished(self):
37                 self.setTitle(self.setup_title)
38
39         def createSetup(self):
40                 self.list = []
41                 if InfoBar.instance and InfoBar.instance.sleepTimer.isActive():
42                         statusSleeptimerText = _("(activated +%d min)") % InfoBar.instance.sleepTimerState()
43                 else:
44                         statusSleeptimerText = _("(not activated)")
45                 self.list.append(getConfigListEntry(_("Sleeptimer") + " " + statusSleeptimerText,
46                         config.usage.sleep_timer,
47                         _("Configure the duration in minutes for the sleeptimer. Select this entry and click OK or green to start/stop the sleeptimer")))
48                 self.list.append(getConfigListEntry(_("Inactivity Sleeptimer"),
49                         config.usage.inactivity_timer,
50                         _("Configure the duration in hours the receiver should go to standby when the receiver is not controlled.")))
51                 if int(config.usage.inactivity_timer.value):
52                         self.list.append(getConfigListEntry(_("Specify timeframe to ignore inactivity sleeptimer"),
53                                 config.usage.inactivity_timer_blocktime,
54                                 _("When enabled you can specify a timeframe were the inactivity sleeptimer is ignored. Not the detection is disabled during this timeframe but the inactivity timeout is disabled")))
55                         if config.usage.inactivity_timer_blocktime.value:
56                                 self.list.append(getConfigListEntry(_("Start time to ignore inactivity sleeptimer"),
57                                         config.usage.inactivity_timer_blocktime_begin,
58                                         _("Specify the start time when the inactivity sleeptimer should be ignored")))
59                                 self.list.append(getConfigListEntry(_("End time to ignore inactivity sleeptimer"),
60                                         config.usage.inactivity_timer_blocktime_end,
61                                         _("Specify the end time until the inactivity sleeptimer should be ignored")))
62                 self.list.append(getConfigListEntry(_("Shutdown when in Standby"),
63                         config.usage.standby_to_shutdown_timer,
64                         _("Configure the duration when the receiver should go to shut down in case the receiver is in standby mode.")))
65                 if int(config.usage.standby_to_shutdown_timer.value):
66                         self.list.append(getConfigListEntry(_("Specify timeframe to ignore the shutdown in standby"),
67                                 config.usage.standby_to_shutdown_timer_blocktime,
68                                 _("When enabled you can specify a timeframe to ignore the shutdown timer when the receiver is in standby mode")))
69                         if config.usage.standby_to_shutdown_timer_blocktime.value:
70                                 self.list.append(getConfigListEntry(_("Start time to ignore shutdown in standby"),
71                                         config.usage.standby_to_shutdown_timer_blocktime_begin,
72                                         _("Specify the start time to ignore the shutdown timer when the receiver is in standby mode")))
73                                 self.list.append(getConfigListEntry(_("End time to ignore shutdown in standby"),
74                                         config.usage.standby_to_shutdown_timer_blocktime_end,
75                                         _("Specify the end time to ignore the shutdown timer when the receiver is in standby mode")))
76                 self["config"].list = self.list
77                 self["config"].l.setList(self.list)
78
79         def ok(self):
80                 if self["config"].isChanged():
81                         for x in self["config"].list:
82                                 x[1].save()
83                 if self.getCurrentEntry().startswith(_("Sleeptimer")):
84                         sleepTimer = config.usage.sleep_timer.value
85                         if sleepTimer == "event_standby":
86                                 sleepTimer = self.currentEventTime()
87                         else:
88                                 sleepTimer = int(sleepTimer)
89                         InfoBar.instance.setSleepTimer(sleepTimer)
90                         self.close(True)
91                 self.close()
92
93         def cancel(self, answer = None):
94                 if answer is None:
95                         if self["config"].isChanged():
96                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
97                         else:
98                                 self.close()
99                 elif answer:
100                         for x in self["config"].list:
101                                 x[1].cancel()
102                         self.close()
103
104         def keyLeft(self):
105                 ConfigListScreen.keyLeft(self)
106                 self.createSetup()
107
108         def keyRight(self):
109                 ConfigListScreen.keyRight(self)
110                 self.createSetup()
111
112         def currentEventTime(self):
113                 remaining = 0
114                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
115                 if ref:
116                         path = ref.getPath()
117                         if path: # Movie
118                                 service = self.session.nav.getCurrentService()
119                                 seek = service and service.seek()
120                                 if seek:
121                                         length = seek.getLength()
122                                         position = seek.getPlayPosition()
123                                         if length and position:
124                                                 remaining = length[1] - position[1]
125                                                 if remaining > 0:
126                                                         remaining = remaining / 90000
127                         else: # DVB
128                                 epg = eEPGCache.getInstance()
129                                 event = epg.lookupEventTime(ref, -1, 0)
130                                 if event:
131                                         now = int(time())
132                                         start = event.getBeginTime()
133                                         duration = event.getDuration()
134                                         end = start + duration
135                                         remaining = end - now
136                 return remaining + config.recording.margin_after.value * 60