Add timeframe option were inactivity timer is ignored
[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                 self.list.append(getConfigListEntry(_("Sleeptimer"),
25                         config.usage.sleep_timer,
26                         _("Configure the duration in minutes and action (shut down or standby) for the sleeptimer. Select this entry and click OK or green to start/stop the sleeptimer")))
27                 self.list.append(getConfigListEntry(_("Action when receiver is not controlled"),
28                         config.usage.inactivity_timer,
29                         _("Configure the duration in hours and action (shut down or standby) when the receiver is not controlled.")))
30                 self.list.append(getConfigListEntry(_("Specify timeframe ignoring action when receiver is not controlled"),
31                         config.usage.inactivity_timer_blocktime,
32                         _("When enabled you can specify a start and end time were the configured duration in hours and action when receiver is not controlled are ignored")))
33                 self.list.append(getConfigListEntry(_("Timeframe start"),
34                         config.usage.inactivity_timer_blocktime_begin,
35                         _("Specify the start time were the configured duration in hours and action when receiver is not controlled are ignored")))
36                 self.list.append(getConfigListEntry(_("Timeframe end"),
37                         config.usage.inactivity_timer_blocktime_end,
38                         _("Specify the end time were the configured duration in hours and action when receiver is not controlled are ignored")))
39
40                 ConfigListScreen.__init__(self, self.list, session = session)
41                 
42                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
43                 {
44                     "green": self.ok,
45                     "red": self.cancel,
46                     "cancel": self.cancel,
47                     "ok": self.ok,
48                 }, -2)
49
50                 self.onLayoutFinish.append(self.layoutFinished)
51
52         def layoutFinished(self):
53                 self.setTitle(self.setup_title)
54
55         def ok(self):
56                 config.usage.sleep_timer.save()
57                 config.usage.inactivity_timer.save()
58                 config.usage.inactivity_timer_blocktime.save()
59                 config.usage.inactivity_timer_blocktime_begin.save()
60                 config.usage.inactivity_timer_blocktime_end.save()
61                 if self.getCurrentEntry() == _("Sleeptimer"):
62                         sleepTimer = config.usage.sleep_timer.value
63                         if sleepTimer == "event_shutdown":
64                                 sleepTimer = -self.currentEventTime()
65                         elif sleepTimer == "event_standby":
66                                 sleepTimer = self.currentEventTime()
67                         else:
68                                 sleepTimer = int(sleepTimer)
69                         InfoBar.instance.setSleepTimer(sleepTimer)
70                         self.close(True)
71                 self.close()
72
73         def cancel(self, answer = None):
74                 if answer is None:
75                         if self["config"].isChanged():
76                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
77                         else:
78                                 self.close()
79                 elif answer:
80                         for x in self["config"].list:
81                                 x[1].cancel()
82                         self.close()
83
84         def getCurrentEntry(self):
85                 return self["config"].getCurrent()[0]
86
87         def getCurrentValue(self):
88                 return str(self["config"].getCurrent()[1].getText())
89
90         def getCurrentDescription(self):
91                 return self["config"].getCurrent() and len(self["config"].getCurrent()) > 2 and self["config"].getCurrent()[2] or ""
92
93         def createSummary(self):
94                 from Screens.Setup import SetupSummary
95                 return SetupSummary
96
97         def currentEventTime(self):
98                 remaining = 0
99                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
100                 if ref:
101                         path = ref.getPath()
102                         if path: # Movie
103                                 service = self.session.nav.getCurrentService()
104                                 seek = service and service.seek()
105                                 if seek:
106                                         length = seek.getLength()
107                                         position = seek.getPlayPosition()
108                                         if length and position:
109                                                 remaining = length[1] - position[1]
110                                                 if remaining > 0:
111                                                         remaining = remaining / 90000
112                         else: # DVB
113                                 epg = eEPGCache.getInstance()
114                                 event = epg.lookupEventTime(ref, -1, 0)
115                                 if event:
116                                         now = int(time())
117                                         start = event.getBeginTime()
118                                         duration = event.getDuration()
119                                         end = start + duration
120                                         remaining = end - now
121                 return remaining + config.recording.margin_after.value * 60