Add blocktime for going to deep standy after a while in standby
[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                 self.list.append(getConfigListEntry(_("Sleeptimer"),
42                         config.usage.sleep_timer,
43                         _("Configure the duration in minutes and action, which could be shut down or standby, for the sleeptimer. Select this entry and click OK or green to start/stop the sleeptimer")))
44                 self.list.append(getConfigListEntry(_("Inactivity Sleeptimer"),
45                         config.usage.inactivity_timer,
46                         _("Configure the duration in hours and action, which could be shut down or standby, when the receiver is not controlled.")))
47                 if int(config.usage.inactivity_timer.value):
48                         self.list.append(getConfigListEntry(_("Specify timeframe to ignore inactivity sleeptimer"),
49                                 config.usage.inactivity_timer_blocktime,
50                                 _("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")))
51                         if config.usage.inactivity_timer_blocktime.value:
52                                 self.list.append(getConfigListEntry(_("Start time to ignore inactivity sleeptimer"),
53                                         config.usage.inactivity_timer_blocktime_begin,
54                                         _("Specify the start time when the inactivity sleeptimer should be ignored")))
55                                 self.list.append(getConfigListEntry(_("End time to ignore inactivity sleeptimer"),
56                                         config.usage.inactivity_timer_blocktime_end,
57                                         _("Specify the end time until the inactivity sleeptimer should be ignored")))
58                 self.list.append(getConfigListEntry(_("Shutdown when in Standby"),
59                         config.usage.standby_to_shutdown_timer,
60                         _("Configure the duration when the receiver should go to shut down in case the receiver is in standby mode.")))
61                 if int(config.usage.standby_to_shutdown_timer.value):
62                         self.list.append(getConfigListEntry(_("Specify timeframe to ignore the shutdown in standby"),
63                                 config.usage.standby_to_shutdown_timer_blocktime,
64                                 _("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")))
65                         if config.usage.inactivity_timer_blocktime.value:
66                                 self.list.append(getConfigListEntry(_("Start time to ignore shutdown in standby"),
67                                         config.usage.standby_to_shutdown_timer_blocktime_begin,
68                                         _("Specify the start time to ignore the shutdown timer when the receiver is in standby mode")))
69                                 self.list.append(getConfigListEntry(_("End time to ignore shutdown in standby"),
70                                         config.usage.standby_to_shutdown_timer_blocktime_end,
71                                         _("Specify the end time to ignore the shutdown timer when the receiver is in standby mode")))
72                 self["config"].list = self.list
73                 self["config"].l.setList(self.list)
74
75         def ok(self):
76                 config.usage.sleep_timer.save()
77                 config.usage.inactivity_timer.save()
78                 config.usage.inactivity_timer_blocktime.save()
79                 config.usage.inactivity_timer_blocktime_begin.save()
80                 config.usage.inactivity_timer_blocktime_end.save()
81                 config.usage.standby_to_shutdown_timer.save()
82                 if self.getCurrentEntry() == _("Sleeptimer"):
83                         sleepTimer = config.usage.sleep_timer.value
84                         if sleepTimer == "event_shutdown":
85                                 sleepTimer = -self.currentEventTime()
86                         elif sleepTimer == "event_standby":
87                                 sleepTimer = self.currentEventTime()
88                         else:
89                                 sleepTimer = int(sleepTimer)
90                         InfoBar.instance.setSleepTimer(sleepTimer)
91                         self.close(True)
92                 self.close()
93
94         def cancel(self, answer = None):
95                 if answer is None:
96                         if self["config"].isChanged():
97                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
98                         else:
99                                 self.close()
100                 elif answer:
101                         for x in self["config"].list:
102                                 x[1].cancel()
103                         self.close()
104
105         def keyLeft(self):
106                 ConfigListScreen.keyLeft(self)
107                 self.createSetup()
108
109         def keyRight(self):
110                 ConfigListScreen.keyRight(self)
111                 self.createSetup()
112
113         def currentEventTime(self):
114                 remaining = 0
115                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
116                 if ref:
117                         path = ref.getPath()
118                         if path: # Movie
119                                 service = self.session.nav.getCurrentService()
120                                 seek = service and service.seek()
121                                 if seek:
122                                         length = seek.getLength()
123                                         position = seek.getPlayPosition()
124                                         if length and position:
125                                                 remaining = length[1] - position[1]
126                                                 if remaining > 0:
127                                                         remaining = remaining / 90000
128                         else: # DVB
129                                 epg = eEPGCache.getInstance()
130                                 event = epg.lookupEventTime(ref, -1, 0)
131                                 if event:
132                                         now = int(time())
133                                         start = event.getBeginTime()
134                                         duration = event.getDuration()
135                                         end = start + duration
136                                         remaining = end - now
137                 return remaining + config.recording.margin_after.value * 60