Rebrand "receiver is not controlled" to inactivity 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 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                 self.onChangedEntry = [ ]
18
19                 self["key_red"] = StaticText(_("Cancel"))
20                 self["key_green"] = StaticText(_("Save"))
21                 self["description"] = Label("")
22
23                 self.list = []
24                 ConfigListScreen.__init__(self, self.list, session = session)
25                 self.createSetup()
26                 
27                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
28                 {
29                     "green": self.ok,
30                     "red": self.cancel,
31                     "cancel": self.cancel,
32                     "ok": self.ok,
33                 }, -2)
34
35                 self.onLayoutFinish.append(self.layoutFinished)
36
37         def layoutFinished(self):
38                 self.setTitle(self.setup_title)
39
40         def createSetup(self):
41                 self.list = []
42                 self.list.append(getConfigListEntry(_("Sleeptimer"),
43                         config.usage.sleep_timer,
44                         _("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")))
45                 self.list.append(getConfigListEntry(_("Inactivity Sleeptimer"),
46                         config.usage.inactivity_timer,
47                         _("Configure the duration in hours and action, which could be shut down or standby, when the receiver is not controlled.")))
48                 if int(config.usage.inactivity_timer.value):
49                         self.list.append(getConfigListEntry(_("Specify timeframe to ignore inactivity sleeptimer"),
50                                 config.usage.inactivity_timer_blocktime,
51                                 _("When enabled you can specify a timeframe were the inactivity sleeptimer is ignored")))
52                         if config.usage.inactivity_timer_blocktime.value:
53                                 self.list.append(getConfigListEntry(_("Start time to ignore inactivity sleeptimer"),
54                                         config.usage.inactivity_timer_blocktime_begin,
55                                         _("Specify the start time when the inactivity sleeptimer should be ignored")))
56                                 self.list.append(getConfigListEntry(_("End time to ignore inactivity sleeptimer"),
57                                         config.usage.inactivity_timer_blocktime_end,
58                                         _("Specify the end time until the inactivity sleeptimer should be ignored")))
59                 self["config"].list = self.list
60                 self["config"].l.setList(self.list)
61
62         def ok(self):
63                 config.usage.sleep_timer.save()
64                 config.usage.inactivity_timer.save()
65                 config.usage.inactivity_timer_blocktime.save()
66                 config.usage.inactivity_timer_blocktime_begin.save()
67                 config.usage.inactivity_timer_blocktime_end.save()
68                 if self.getCurrentEntry() == _("Sleeptimer"):
69                         sleepTimer = config.usage.sleep_timer.value
70                         if sleepTimer == "event_shutdown":
71                                 sleepTimer = -self.currentEventTime()
72                         elif sleepTimer == "event_standby":
73                                 sleepTimer = self.currentEventTime()
74                         else:
75                                 sleepTimer = int(sleepTimer)
76                         InfoBar.instance.setSleepTimer(sleepTimer)
77                         self.close(True)
78                 self.close()
79
80         def cancel(self, answer = None):
81                 if answer is None:
82                         if self["config"].isChanged():
83                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
84                         else:
85                                 self.close()
86                 elif answer:
87                         for x in self["config"].list:
88                                 x[1].cancel()
89                         self.close()
90
91         def keyLeft(self):
92                 ConfigListScreen.keyLeft(self)
93                 self.createSetup()
94
95         def keyRight(self):
96                 ConfigListScreen.keyRight(self)
97                 self.createSetup()
98
99         def getCurrentEntry(self):
100                 return self["config"].getCurrent()[0]
101
102         def getCurrentValue(self):
103                 return str(self["config"].getCurrent()[1].getText())
104
105         def getCurrentDescription(self):
106                 return self["config"].getCurrent() and len(self["config"].getCurrent()) > 2 and self["config"].getCurrent()[2] or ""
107
108         def createSummary(self):
109                 from Screens.Setup import SetupSummary
110                 return SetupSummary
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