SleepTimerEdit: Add a forgotten onLayourFinished
[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
31                 ConfigListScreen.__init__(self, self.list, session = session)
32                 
33                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
34                 {
35                     "green": self.ok,
36                     "red": self.cancel,
37                     "cancel": self.cancel,
38                     "ok": self.ok,
39                 }, -2)
40
41                 self.onLayoutFinish.append(self.layoutFinished)
42
43         def layoutFinished(self):
44                 self.setTitle(self.setup_title)
45
46         def ok(self):
47                 config.usage.sleep_timer.save()
48                 config.usage.inactivity_timer.save()
49                 if self.getCurrentEntry() == _("Sleeptimer"):
50                         sleepTimer = config.usage.sleep_timer.value
51                         if sleepTimer == "event_shutdown":
52                                 sleepTimer = -self.currentEventTime()
53                         elif sleepTimer == "event_standby":
54                                 sleepTimer = self.currentEventTime()
55                         else:
56                                 sleepTimer = int(sleepTimer)
57                         InfoBar.instance.setSleepTimer(sleepTimer)
58                         self.close(True)
59                 self.close()
60
61         def cancel(self, answer = None):
62                 if answer is None:
63                         if self["config"].isChanged():
64                                 self.session.openWithCallback(self.cancel, MessageBox, _("Really close without saving settings?"))
65                         else:
66                                 self.close()
67                 elif answer:
68                         for x in self["config"].list:
69                                 x[1].cancel()
70                         self.close()
71
72         def getCurrentEntry(self):
73                 return self["config"].getCurrent()[0]
74
75         def getCurrentValue(self):
76                 return str(self["config"].getCurrent()[1].getText())
77
78         def getCurrentDescription(self):
79                 return self["config"].getCurrent() and len(self["config"].getCurrent()) > 2 and self["config"].getCurrent()[2] or ""
80
81         def createSummary(self):
82                 from Screens.Setup import SetupSummary
83                 return SetupSummary
84
85         def currentEventTime(self):
86                 remaining = 0
87                 ref = self.session.nav.getCurrentlyPlayingServiceOrGroup()
88                 if ref:
89                         path = ref.getPath()
90                         if path: # Movie
91                                 service = self.session.nav.getCurrentService()
92                                 seek = service and service.seek()
93                                 if seek:
94                                         length = seek.getLength()
95                                         position = seek.getPlayPosition()
96                                         if length and position:
97                                                 remaining = length[1] - position[1]
98                                                 if remaining > 0:
99                                                         remaining = remaining / 90000
100                         else: # DVB
101                                 epg = eEPGCache.getInstance()
102                                 event = epg.lookupEventTime(ref, -1, 0)
103                                 if event:
104                                         now = int(time())
105                                         start = event.getBeginTime()
106                                         duration = event.getDuration()
107                                         end = start + duration
108                                         remaining = end - now
109                 return remaining + config.recording.margin_after.value * 60