SleepTimerEdit: save new exclusion goto deepstandby timeout configs
[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                 config.usage.standby_to_shutdown_timer_blocktime.save()
83                 config.usage.standby_to_shutdown_timer_blocktime_begin.save()
84                 config.usage.standby_to_shutdown_timer_blocktime_end.save()
85
86                 if self.getCurrentEntry() == _("Sleeptimer"):
87                         sleepTimer = config.usage.sleep_timer.value
88                         if sleepTimer == "event_shutdown":
89                                 sleepTimer = -self.currentEventTime()
90                         elif sleepTimer == "event_standby":
91                                 sleepTimer = self.currentEventTime()
92                         else:
93                                 sleepTimer = int(sleepTimer)
94                         InfoBar.instance.setSleepTimer(sleepTimer)
95                         self.close(True)
96                 self.close()
97
98         def cancel(self, answer = None):
99                 if answer is None:
100                         if self["config"].isChanged():
101                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
102                         else:
103                                 self.close()
104                 elif answer:
105                         for x in self["config"].list:
106                                 x[1].cancel()
107                         self.close()
108
109         def keyLeft(self):
110                 ConfigListScreen.keyLeft(self)
111                 self.createSetup()
112
113         def keyRight(self):
114                 ConfigListScreen.keyRight(self)
115                 self.createSetup()
116
117         def currentEventTime(self):
118                 remaining = 0
119                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
120                 if ref:
121                         path = ref.getPath()
122                         if path: # Movie
123                                 service = self.session.nav.getCurrentService()
124                                 seek = service and service.seek()
125                                 if seek:
126                                         length = seek.getLength()
127                                         position = seek.getPlayPosition()
128                                         if length and position:
129                                                 remaining = length[1] - position[1]
130                                                 if remaining > 0:
131                                                         remaining = remaining / 90000
132                         else: # DVB
133                                 epg = eEPGCache.getInstance()
134                                 event = epg.lookupEventTime(ref, -1, 0)
135                                 if event:
136                                         now = int(time())
137                                         start = event.getBeginTime()
138                                         duration = event.getDuration()
139                                         end = start + duration
140                                         remaining = end - now
141                 return remaining + config.recording.margin_after.value * 60